2 * Sherlock Library -- Bucket Manipulation Tool
4 * (c) 2001 Martin Mares <mj@ucw.cz>
8 #include "lib/bucket.h"
9 #include "lib/fastbuf.h"
23 Usage: buckettool <command>\n\
26 -l\t\tlist all buckets\n\
27 -L\t\tlist all buckets including deleted ones\n\
28 -d <obj>\tdelete bucket\n\
29 -x <obj>\textract bucket\n\
30 -i\t\tinsert bucket\n\
31 -c\t\tconcatenate and dump all buckets\n\
32 -f\t\taudit bucket file structure\n\
33 -F\t\taudit and fix bucket file structure\n\
42 oid_t o = strtoul(c, &e, 16);
44 die("Invalid object ID: %s", c);
51 struct obuck_header h;
54 if (obuck_find_first(&h, full))
57 if (h.oid == OBUCK_OID_DELETED)
58 printf("DELETED %6d\n", h.length);
60 printf("%08x %6d %6d\n", h.oid, h.length, h.orig_length);
62 while (obuck_find_next(&h, full));
69 oid_t oid = parse_id(id);
81 struct obuck_header h;
85 obuck_find_by_oid(&h);
87 while ((l = bread(b, buf, sizeof(buf))))
88 fwrite(buf, 1, l, stdout);
99 struct obuck_header h;
103 while ((l = fread(buf, 1, sizeof(buf), stdin)))
105 obuck_create_end(b, &h);
107 printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
113 struct obuck_header h;
119 if (obuck_find_first(&h, 0))
122 printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length);
125 while ((l = bread(b, buf, sizeof(buf))))
127 fwrite(buf, 1, l, stdout);
128 lf = (buf[l-1] == '\n');
132 printf("\n# <missing EOL>\n");
134 while (obuck_find_next(&h, 0));
142 struct obuck_header h, nh;
148 int fatal_errors = 0;
150 fd = sh_open(obuck_name, O_RDWR);
152 die("Unable to open the bucket file %s: %m", obuck_name);
155 oid = pos >> OBUCK_SHIFT;
156 i = sh_pread(fd, &h, sizeof(h), pos);
160 printf("%08x incomplete header\n", oid);
161 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
162 printf("%08x incomplete file\n", oid);
163 else if (h.magic != OBUCK_MAGIC)
164 printf("%08x invalid header magic\n", oid);
165 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
166 printf("%08x invalid header backlink\n", oid);
169 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
170 if (sh_pread(fd, &chk, 4, end-4) != 4)
171 printf("%08x missing trailer\n", oid);
172 else if (chk != OBUCK_TRAILER)
173 printf("%08x mismatched trailer\n", oid);
182 if (pos - end > 0x10000000)
184 printf("*** skipped for too long, giving up\n");
189 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
191 printf("*** unable to find next header\n");
194 printf("*** truncating file\n");
195 sh_ftruncate(fd, pos);
198 printf("*** would truncate the file here\n");
202 while (nh.magic != OBUCK_MAGIC ||
203 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
204 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
207 h.magic = OBUCK_MAGIC;
208 h.oid = OBUCK_OID_DELETED;
209 h.length = h.orig_length = end - pos - sizeof(h) - 4;
210 sh_pwrite(fd, &h, sizeof(h), pos);
212 sh_pwrite(fd, &chk, 4, end-4);
213 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
219 if (!fix && errors || fatal_errors)
224 main(int argc, char **argv)
231 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfF", CF_NO_LONG_OPTS, NULL)) != -1)