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\t\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 -s\t\tshake down bucket file (without updating other structures!!!)\n\
51 oid_t o = strtoul(c, &e, 16);
53 die("Invalid object ID: %s", c);
60 struct obuck_header h;
63 if (obuck_find_first(&h, full))
66 if (h.oid == OBUCK_OID_DELETED)
67 printf("DELETED %6d\n", h.length);
69 printf("%08x %6d %6d\n", h.oid, h.length, h.orig_length);
71 while (obuck_find_next(&h, full));
78 oid_t oid = parse_id(id);
90 struct obuck_header h;
94 obuck_find_by_oid(&h);
96 while ((l = bread(b, buf, sizeof(buf))))
97 fwrite(buf, 1, l, stdout);
105 struct fastbuf *b, *in;
107 struct obuck_header h;
110 in = bfdopen(0, 4096);
115 while ((e = bgets(in, buf, sizeof(buf))) && buf[0])
118 bwrite(b, buf, e-buf);
120 obuck_create_end(b, &h);
121 printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
125 /* bclose(in) not done, we don't want fd 0 closed */
131 struct obuck_header h;
137 if (obuck_find_first(&h, 0))
140 printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length);
143 while ((l = bread(b, buf, sizeof(buf))))
145 fwrite(buf, 1, l, stdout);
146 lf = (buf[l-1] == '\n');
150 printf("\n# <missing EOL>\n");
152 while (obuck_find_next(&h, 0));
160 struct obuck_header h, nh;
166 int fatal_errors = 0;
168 fd = sh_open(obuck_name, O_RDWR);
170 die("Unable to open the bucket file %s: %m", obuck_name);
173 oid = pos >> OBUCK_SHIFT;
174 i = sh_pread(fd, &h, sizeof(h), pos);
178 printf("%08x incomplete header\n", oid);
179 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
180 printf("%08x incomplete file\n", oid);
181 else if (h.magic != OBUCK_MAGIC)
182 printf("%08x invalid header magic\n", oid);
183 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
184 printf("%08x invalid header backlink\n", oid);
187 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
188 if (sh_pread(fd, &chk, 4, end-4) != 4)
189 printf("%08x missing trailer\n", oid);
190 else if (chk != OBUCK_TRAILER)
191 printf("%08x mismatched trailer\n", oid);
203 if (pos - end > 0x10000000)
205 printf("*** skipped for too long, giving up\n");
210 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
212 printf("*** unable to find next header\n");
215 printf("*** truncating file\n");
216 sh_ftruncate(fd, pos);
219 printf("*** would truncate the file here\n");
223 while (nh.magic != OBUCK_MAGIC ||
224 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
225 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
228 h.magic = OBUCK_MAGIC;
229 h.oid = OBUCK_OID_DELETED;
230 h.length = h.orig_length = end - pos - sizeof(h) - 4;
231 sh_pwrite(fd, &h, sizeof(h), pos);
233 sh_pwrite(fd, &chk, 4, end-4);
234 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
237 printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
242 if (!fix && errors || fatal_errors)
247 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
251 printf("%08x -> ", old->oid);
252 if (new == OBUCK_OID_DELETED)
255 printf("%08x\n", new);
264 obuck_shakedown(shake_kibitz);
269 main(int argc, char **argv)
276 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfFsv", CF_NO_LONG_OPTS, NULL)) != -1)