2 * Sherlock Library -- Bucket Manipulation Tool
4 * (c) 2001 Martin Mares <mj@ucw.cz>
8 #include "lib/bucket.h"
9 #include "lib/fastbuf.h"
22 Usage: buckettool <commands>\n\
25 -l\t\tlist all buckets\n\
26 -L\t\tlist all buckets including deleted ones\n\
27 -d <obj>\tdelete bucket\n\
28 -x <obj>\textract bucket\n\
29 -i\t\tinsert bucket\n\
30 -c\t\tconcatenate and dump all buckets\n\
31 -f\t\taudit bucket file structure\n\
32 -F\t\taudit and fix bucket file structure\n\
41 oid_t o = strtoul(c, &e, 16);
43 die("Invalid object ID: %s", c);
50 struct obuck_header h;
53 if (obuck_find_first(&h, full))
56 if (h.oid == OBUCK_OID_DELETED)
57 printf("DELETED %6d\n", h.length);
59 printf("%08x %6d %6d\n", h.oid, h.length, h.orig_length);
61 while (obuck_find_next(&h, full));
68 oid_t oid = parse_id(id);
80 struct obuck_header h;
84 obuck_find_by_oid(&h);
86 while ((l = bread(b, buf, sizeof(buf))))
87 fwrite(buf, 1, l, stdout);
98 struct obuck_header h;
102 while ((l = fread(buf, 1, sizeof(buf), stdin)))
104 obuck_create_end(b, &h);
106 printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
112 struct obuck_header h;
118 if (obuck_find_first(&h, 0))
121 printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length);
124 while ((l = bread(b, buf, sizeof(buf))))
126 fwrite(buf, 1, l, stdout);
127 lf = (buf[l-1] == '\n');
131 printf("\n# <missing EOL>\n");
133 while (obuck_find_next(&h, 0));
141 struct obuck_header h, nh;
147 int fatal_errors = 0;
149 fd = open(obuck_name, O_RDWR);
151 die("Unable to open the bucket file %s: %m", obuck_name);
154 oid = pos >> OBUCK_SHIFT;
155 i = sh_pread(fd, &h, sizeof(h), pos);
159 printf("%08x incomplete header\n", oid);
160 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
161 printf("%08x incomplete file\n", oid);
162 else if (h.magic != OBUCK_MAGIC)
163 printf("%08x invalid header magic\n", oid);
164 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
165 printf("%08x invalid header backlink\n", oid);
168 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
169 if (sh_pread(fd, &chk, 4, end-4) != 4)
170 printf("%08x missing trailer\n", oid);
171 else if (chk != OBUCK_TRAILER)
172 printf("%08x mismatched trailer\n", oid);
181 if (pos - end > 0x10000000)
183 printf("*** skipped for too long, giving up\n");
188 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
190 printf("*** unable to find next header\n");
193 printf("*** truncating file\n");
197 printf("*** would truncate the file here\n");
201 while (nh.magic != OBUCK_MAGIC ||
202 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
203 printf("*** match at oid %08x\n", end >> OBUCK_SHIFT);
206 h.magic = OBUCK_MAGIC;
207 h.oid = OBUCK_OID_DELETED;
208 h.length = h.orig_length = end - pos - sizeof(h) - 4;
209 sh_pwrite(fd, &h, sizeof(h), pos);
211 sh_pwrite(fd, &chk, 4, end-4);
212 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", end - pos);
218 if (!fix && errors || fatal_errors)
223 main(int argc, char **argv)
228 while ((i = getopt(argc, argv, "lLd:x:icfF")) != -1)
266 if (optind < argc || !op)