2 * Sherlock Library -- Bucket Manipulation Tool
4 * (c) 2001 Martin Mares <mj@ucw.cz>
8 #include "lib/bucket.h"
9 #include "lib/fastbuf.h"
25 Usage: buckettool <command>\n\
28 -l\t\tlist all buckets\n\
29 -L\t\tlist all buckets including deleted ones\n\
30 -d <obj>\tdelete bucket\n\
31 -x <obj>\textract bucket\n\
32 -i\t\tinsert bucket\n\
33 -c\t\tconcatenate and dump all buckets\n\
34 -f\t\taudit bucket file structure\n\
35 -F\t\taudit and fix bucket file structure\n\
36 -s\t\tshake down bucket file (without updating other structures!!!)\n\
46 oid_t o = strtoul(c, &e, 16);
48 die("Invalid object ID: %s", c);
55 struct obuck_header h;
58 if (obuck_find_first(&h, full))
61 if (h.oid == OBUCK_OID_DELETED)
62 printf("DELETED %6d\n", h.length);
64 printf("%08x %6d %6d\n", h.oid, h.length, h.orig_length);
66 while (obuck_find_next(&h, full));
73 oid_t oid = parse_id(id);
85 struct obuck_header h;
89 obuck_find_by_oid(&h);
91 while ((l = bread(b, buf, sizeof(buf))))
92 fwrite(buf, 1, l, stdout);
103 struct obuck_header h;
107 while ((l = fread(buf, 1, sizeof(buf), stdin)))
109 obuck_create_end(b, &h);
111 printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
117 struct obuck_header h;
123 if (obuck_find_first(&h, 0))
126 printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length);
129 while ((l = bread(b, buf, sizeof(buf))))
131 fwrite(buf, 1, l, stdout);
132 lf = (buf[l-1] == '\n');
136 printf("\n# <missing EOL>\n");
138 while (obuck_find_next(&h, 0));
146 struct obuck_header h, nh;
152 int fatal_errors = 0;
154 fd = sh_open(obuck_name, O_RDWR);
156 die("Unable to open the bucket file %s: %m", obuck_name);
159 oid = pos >> OBUCK_SHIFT;
160 i = sh_pread(fd, &h, sizeof(h), pos);
164 printf("%08x incomplete header\n", oid);
165 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
166 printf("%08x incomplete file\n", oid);
167 else if (h.magic != OBUCK_MAGIC)
168 printf("%08x invalid header magic\n", oid);
169 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
170 printf("%08x invalid header backlink\n", oid);
173 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
174 if (sh_pread(fd, &chk, 4, end-4) != 4)
175 printf("%08x missing trailer\n", oid);
176 else if (chk != OBUCK_TRAILER)
177 printf("%08x mismatched trailer\n", oid);
186 if (pos - end > 0x10000000)
188 printf("*** skipped for too long, giving up\n");
193 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
195 printf("*** unable to find next header\n");
198 printf("*** truncating file\n");
199 sh_ftruncate(fd, pos);
202 printf("*** would truncate the file here\n");
206 while (nh.magic != OBUCK_MAGIC ||
207 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
208 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
211 h.magic = OBUCK_MAGIC;
212 h.oid = OBUCK_OID_DELETED;
213 h.length = h.orig_length = end - pos - sizeof(h) - 4;
214 sh_pwrite(fd, &h, sizeof(h), pos);
216 sh_pwrite(fd, &chk, 4, end-4);
217 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
223 if (!fix && errors || fatal_errors)
228 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
232 printf("%08x -> ", old->oid);
233 if (new == OBUCK_OID_DELETED)
236 printf("%08x\n", new);
245 obuck_shakedown(shake_kibitz);
250 main(int argc, char **argv)
257 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfFsv", CF_NO_LONG_OPTS, NULL)) != -1)