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 [<options>] <command>\n\
30 -l\t\tlist all buckets\n\
31 -L\t\tlist all buckets including deleted ones\n\
32 -d <obj>\tdelete bucket\n\
33 -x <obj>\textract bucket\n\
34 -i\t\tinsert buckets separated by blank lines\n\
35 -c\t\tconcatenate and dump all buckets\n\
36 -f\t\taudit bucket file structure\n\
37 -F\t\taudit and fix bucket file structure\n\
38 -s\t\tshake down bucket file (without updating other structures!!!)\n\
48 oid_t o = strtoul(c, &e, 16);
50 die("Invalid object ID: %s", c);
57 struct obuck_header h;
60 if (obuck_find_first(&h, full))
63 if (h.oid == OBUCK_OID_DELETED)
64 printf("DELETED %6d\n", h.length);
66 printf("%08x %6d %6d\n", h.oid, h.length, h.orig_length);
68 while (obuck_find_next(&h, full));
75 oid_t oid = parse_id(id);
87 struct obuck_header h;
91 obuck_find_by_oid(&h);
93 while ((l = bread(b, buf, sizeof(buf))))
94 fwrite(buf, 1, l, stdout);
102 struct fastbuf *b, *in;
104 struct obuck_header h;
107 in = bfdopen(0, 4096);
112 while ((e = bgets(in, buf, sizeof(buf))) && buf[0])
115 bwrite(b, buf, e-buf);
117 obuck_create_end(b, &h);
118 printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
122 /* bclose(in) not done, we don't want fd 0 closed */
128 struct obuck_header h;
134 if (obuck_find_first(&h, 0))
137 printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length);
140 while ((l = bread(b, buf, sizeof(buf))))
142 fwrite(buf, 1, l, stdout);
143 lf = (buf[l-1] == '\n');
147 printf("\n# <missing EOL>\n");
149 while (obuck_find_next(&h, 0));
157 struct obuck_header h, nh;
163 int fatal_errors = 0;
165 fd = sh_open(obuck_name, O_RDWR);
167 die("Unable to open the bucket file %s: %m", obuck_name);
170 oid = pos >> OBUCK_SHIFT;
171 i = sh_pread(fd, &h, sizeof(h), pos);
175 printf("%08x incomplete header\n", oid);
176 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
177 printf("%08x incomplete file\n", oid);
178 else if (h.magic != OBUCK_MAGIC)
179 printf("%08x invalid header magic\n", oid);
180 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
181 printf("%08x invalid header backlink\n", oid);
184 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
185 if (sh_pread(fd, &chk, 4, end-4) != 4)
186 printf("%08x missing trailer\n", oid);
187 else if (chk != OBUCK_TRAILER)
188 printf("%08x mismatched trailer\n", oid);
197 if (pos - end > 0x10000000)
199 printf("*** skipped for too long, giving up\n");
204 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
206 printf("*** unable to find next header\n");
209 printf("*** truncating file\n");
210 sh_ftruncate(fd, pos);
213 printf("*** would truncate the file here\n");
217 while (nh.magic != OBUCK_MAGIC ||
218 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
219 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
222 h.magic = OBUCK_MAGIC;
223 h.oid = OBUCK_OID_DELETED;
224 h.length = h.orig_length = end - pos - sizeof(h) - 4;
225 sh_pwrite(fd, &h, sizeof(h), pos);
227 sh_pwrite(fd, &chk, 4, end-4);
228 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
234 if (!fix && errors || fatal_errors)
239 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
243 printf("%08x -> ", old->oid);
244 if (new == OBUCK_OID_DELETED)
247 printf("%08x\n", new);
256 obuck_shakedown(shake_kibitz);
261 main(int argc, char **argv)
268 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfFsv", CF_NO_LONG_OPTS, NULL)) != -1)