2 * Sherlock Library -- Bucket Manipulation Tool
4 * (c) 2001--2004 Martin Mares <mj@ucw.cz>
5 * (c) 2004 Robert Spalek <robert@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
12 #include "lib/bucket.h"
13 #include "lib/fastbuf.h"
16 #include "lib/mempool.h"
17 #include "lib/object.h"
18 #include "lib/lizard.h"
20 #include "lib/ff-utf8.h"
29 static struct mempool *pool;
30 static struct buck2obj_buf *buck_buf;
36 Usage: buckettool [<options>] <command>\n\
41 -l\t\tlist all buckets\n\
42 -L\t\tlist all buckets including deleted ones\n\
43 -d <obj>\tdelete bucket\n\
44 -x <obj>\textract bucket\n\
45 -i[<type>]\tinsert buckets separated by blank lines\n\
46 -c\t\tconcatenate and dump all buckets\n\
47 -f\t\taudit bucket file structure\n\
48 -F\t\taudit and fix bucket file structure\n\
49 -q\t\tquick check of bucket file consistency\n\
50 -r\t\tdo not parse V33 buckets, but print the raw content\n\
51 -s\t\tshake down bucket file (without updating other structures!!!)\n\
61 oid_t o = strtoul(c, &e, 16);
63 die("Invalid object ID: %s", c);
70 struct obuck_header h;
73 if (obuck_find_first(&h, full))
76 if (h.oid == OBUCK_OID_DELETED)
77 printf("DELETED %6d\n", h.length);
79 printf("%08x %6d %08x\n", h.oid, h.length, h.type);
81 while (obuck_find_next(&h, full));
88 oid_t oid = parse_id(id);
95 dump_oattrs(struct fastbuf *out, struct oattr *oa)
97 for (; oa; oa = oa->next)
98 for (struct oattr *a=oa; a; a = a->same)
99 bprintf(out, "%c%s\n", a->attr, a->val);
103 dump_parsed_bucket(struct fastbuf *out, struct obuck_header *h, struct fastbuf *b)
105 struct odes *o_hdr, *o_body;
107 o_hdr = obj_new(pool);
108 o_body = obj_new(pool);
109 if (buck2obj_parse(buck_buf, h->type, h->length, b, o_hdr, NULL, o_body) < 0)
110 bprintf(out, ".Cannot parse bucket %x of type %x and length %d: %m\n", h->oid, h->type, h->length);
113 dump_oattrs(out, o_hdr->attrs);
115 dump_oattrs(out, o_body->attrs);
122 struct fastbuf *b, *out;
125 struct obuck_header h;
127 h.oid = parse_id(id);
129 obuck_find_by_oid(&h);
130 out = bfdopen_shared(1, 65536);
132 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
134 while ((l = bread(b, buf, sizeof(buf))))
138 dump_parsed_bucket(out, &h, b);
147 struct fastbuf *b, *in;
149 struct obuck_header h;
152 bb_t lizard_buf, compressed_buf;
154 bb_init(&lizard_buf);
155 bb_init(&compressed_buf);
157 type = BUCKET_TYPE_PLAIN;
158 else if (sscanf(arg, "%x", &type) != 1)
159 die("Type `%s' is not a hexadecimal number");
161 type += BUCKET_TYPE_PLAIN;
164 in = bfdopen_shared(0, 4096);
168 uns lizard_filled = 0;
171 while ((e = bgets(in, buf, sizeof(buf))))
175 if (in_body || type < BUCKET_TYPE_V30)
180 b = obuck_create(type);
186 else if (type <= BUCKET_TYPE_V33 || !in_body)
188 bput_attr(b, buf[0], buf+1, e-buf-1);
192 ASSERT(BUCKET_TYPE_V33_LIZARD);
193 uns want_len = lizard_filled + (e-buf) + 6 + LIZARD_NEEDS_CHARS; // +6 is the maximum UTF-8 length
194 bb_grow(&lizard_buf, want_len);
195 byte *ptr = lizard_buf.ptr + lizard_filled;
196 ptr = put_attr(ptr, buf[0], buf+1, e-buf-1);
197 lizard_filled = ptr - lizard_buf.ptr;
200 if (in_body && type == BUCKET_TYPE_V33_LIZARD)
202 bputl(b, lizard_filled
203 #if 0 //TEST error resilience: write wrong length
207 bputl(b, adler32(lizard_buf.ptr, lizard_filled)
208 #if 0 //TEST error resilience: write wrong checksum
212 uns want_len = lizard_filled * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
213 bb_grow(&compressed_buf, want_len);
214 want_len = lizard_compress(lizard_buf.ptr, lizard_filled, compressed_buf.ptr);
215 #if 0 //TEST error resilience: tamper the compressed data by removing EOF
216 compressed_buf[want_len-1] = 1;
218 bwrite(b, compressed_buf.ptr, want_len);
222 obuck_create_end(b, &h);
223 printf("%08x %d %08x\n", h.oid, h.length, h.type);
227 bb_done(&lizard_buf);
228 bb_done(&compressed_buf);
236 struct obuck_header h;
237 struct fastbuf *b, *out;
241 out = bfdopen_shared(1, 65536);
242 while (b = obuck_slurp_pool(&h))
244 bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type);
245 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
248 while ((l = bread(b, buf, sizeof(buf))))
251 lf = (buf[l-1] == '\n');
254 bprintf(out, "\n# <missing EOL>\n");
257 dump_parsed_bucket(out, &h, b);
268 struct obuck_header h, nh;
274 int fatal_errors = 0;
276 fd = sh_open(obuck_name, O_RDWR);
278 die("Unable to open the bucket file %s: %m", obuck_name);
281 oid = pos >> OBUCK_SHIFT;
282 i = sh_pread(fd, &h, sizeof(h), pos);
286 printf("%08x incomplete header\n", oid);
287 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
288 printf("%08x incomplete file\n", oid);
289 else if (h.magic != OBUCK_MAGIC)
290 printf("%08x invalid header magic\n", oid);
291 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
292 printf("%08x invalid header backlink\n", oid);
295 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
296 if (sh_pread(fd, &chk, 4, end-4) != 4)
297 printf("%08x missing trailer\n", oid);
298 else if (chk != OBUCK_TRAILER)
299 printf("%08x mismatched trailer\n", oid);
311 if (pos - end > 0x10000000)
313 printf("*** skipped for too long, giving up\n");
318 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
320 printf("*** unable to find next header\n");
323 printf("*** truncating file\n");
324 sh_ftruncate(fd, pos);
327 printf("*** would truncate the file here\n");
331 while (nh.magic != OBUCK_MAGIC ||
332 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
333 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
336 h.magic = OBUCK_MAGIC;
337 h.oid = OBUCK_OID_DELETED;
338 h.length = end - pos - sizeof(h) - 4;
339 sh_pwrite(fd, &h, sizeof(h), pos);
341 sh_pwrite(fd, &chk, 4, end-4);
342 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
345 printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
350 if (!fix && errors || fatal_errors)
355 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
359 printf("%08x -> ", old->oid);
360 if (new == OBUCK_OID_DELETED)
363 printf("%08x\n", new);
372 obuck_shakedown(shake_kibitz);
384 main(int argc, char **argv)
392 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqrsv", CF_NO_LONG_OPTS, NULL)) != -1)
409 pool = mp_new(1<<14);
410 buck_buf = buck2obj_alloc();
449 buck2obj_free(buck_buf);