2 * Sherlock Library -- Bucket Manipulation Tool
4 * (c) 2001 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
11 #include "lib/bucket.h"
12 #include "lib/fastbuf.h"
28 Usage: buckettool [<options>] <command>\n\
33 -l\t\tlist all buckets\n\
34 -L\t\tlist all buckets including deleted ones\n\
35 -d <obj>\tdelete bucket\n\
36 -x <obj>\textract bucket\n\
37 -i[<type>]\tinsert buckets separated by blank lines\n\
38 -c\t\tconcatenate and dump all buckets\n\
39 -f\t\taudit bucket file structure\n\
40 -F\t\taudit and fix bucket file structure\n\
41 -q\t\tquick check of bucket file consistency\n\
42 -s\t\tshake down bucket file (without updating other structures!!!)\n\
52 oid_t o = strtoul(c, &e, 16);
54 die("Invalid object ID: %s", c);
61 struct obuck_header h;
64 if (obuck_find_first(&h, full))
67 if (h.oid == OBUCK_OID_DELETED)
68 printf("DELETED %6d\n", h.length);
70 printf("%08x %6d %08x\n", h.oid, h.length, h.type);
72 while (obuck_find_next(&h, full));
79 oid_t oid = parse_id(id);
91 struct obuck_header h;
95 obuck_find_by_oid(&h);
97 while ((l = bread(b, buf, sizeof(buf))))
98 fwrite(buf, 1, l, stdout);
106 struct fastbuf *b, *in;
108 struct obuck_header h;
113 type = BUCKET_TYPE_PLAIN;
114 else if (sscanf(arg, "%x", &type) != 1)
115 die("Type `%s' is not a hexadecimal number");
117 in = bfdopen_shared(0, 4096);
122 while ((e = bgets(in, buf, sizeof(buf))) && buf[0])
126 b = obuck_create(type);
127 bwrite(b, buf, e-buf);
131 obuck_create_end(b, &h);
132 printf("%08x %d %08x\n", h.oid, h.length, h.type);
143 struct obuck_header h;
149 while (b = obuck_slurp_pool(&h))
151 printf("### %08x %6d %08x\n", h.oid, h.length, h.type);
153 while ((l = bread(b, buf, sizeof(buf))))
155 fwrite(buf, 1, l, stdout);
156 lf = (buf[l-1] == '\n');
159 printf("\n# <missing EOL>\n");
168 struct obuck_header h, nh;
174 int fatal_errors = 0;
176 fd = sh_open(obuck_name, O_RDWR);
178 die("Unable to open the bucket file %s: %m", obuck_name);
181 oid = pos >> OBUCK_SHIFT;
182 i = sh_pread(fd, &h, sizeof(h), pos);
186 printf("%08x incomplete header\n", oid);
187 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
188 printf("%08x incomplete file\n", oid);
189 else if (h.magic != OBUCK_MAGIC)
190 printf("%08x invalid header magic\n", oid);
191 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
192 printf("%08x invalid header backlink\n", oid);
195 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
196 if (sh_pread(fd, &chk, 4, end-4) != 4)
197 printf("%08x missing trailer\n", oid);
198 else if (chk != OBUCK_TRAILER)
199 printf("%08x mismatched trailer\n", oid);
211 if (pos - end > 0x10000000)
213 printf("*** skipped for too long, giving up\n");
218 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
220 printf("*** unable to find next header\n");
223 printf("*** truncating file\n");
224 sh_ftruncate(fd, pos);
227 printf("*** would truncate the file here\n");
231 while (nh.magic != OBUCK_MAGIC ||
232 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
233 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
236 h.magic = OBUCK_MAGIC;
237 h.oid = OBUCK_OID_DELETED;
238 h.length = end - pos - sizeof(h) - 4;
239 sh_pwrite(fd, &h, sizeof(h), pos);
241 sh_pwrite(fd, &chk, 4, end-4);
242 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
245 printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
250 if (!fix && errors || fatal_errors)
255 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
259 printf("%08x -> ", old->oid);
260 if (new == OBUCK_OID_DELETED)
263 printf("%08x\n", new);
272 obuck_shakedown(shake_kibitz);
284 main(int argc, char **argv)
291 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqsv", CF_NO_LONG_OPTS, NULL)) != -1)