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;
123 struct obuck_header h;
125 h.oid = parse_id(id);
127 obuck_find_by_oid(&h);
128 out = bfdopen_shared(1, 65536);
130 bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type);
132 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
133 bbcopy_slow(b, out, ~0U);
135 dump_parsed_bucket(out, &h, b);
144 struct fastbuf *b, *in;
146 struct obuck_header h;
149 bb_t lizard_buf, compressed_buf;
151 bb_init(&lizard_buf);
152 bb_init(&compressed_buf);
154 type = BUCKET_TYPE_PLAIN;
155 else if (sscanf(arg, "%x", &type) != 1)
156 die("Type `%s' is not a hexadecimal number");
158 type += BUCKET_TYPE_PLAIN;
159 put_attr_set_type(type);
161 in = bfdopen_shared(0, 4096);
165 uns lizard_filled = 0;
168 while ((e = bgets(in, buf, sizeof(buf))))
172 if (in_body || type < BUCKET_TYPE_V30)
177 b = obuck_create(type);
183 else if (type <= BUCKET_TYPE_V33 || !in_body)
185 bput_attr(b, buf[0], buf+1, e-buf-1);
189 ASSERT(BUCKET_TYPE_V33_LIZARD);
190 uns want_len = lizard_filled + (e-buf) + 6 + LIZARD_NEEDS_CHARS; // +6 is the maximum UTF-8 length
191 bb_grow(&lizard_buf, want_len);
192 byte *ptr = lizard_buf.ptr + lizard_filled;
193 ptr = put_attr(ptr, buf[0], buf+1, e-buf-1);
194 lizard_filled = ptr - lizard_buf.ptr;
197 if (in_body && type == BUCKET_TYPE_V33_LIZARD)
199 bputl(b, lizard_filled
200 #if 0 //TEST error resilience: write wrong length
204 bputl(b, adler32(lizard_buf.ptr, lizard_filled)
205 #if 0 //TEST error resilience: write wrong checksum
209 uns want_len = lizard_filled * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
210 bb_grow(&compressed_buf, want_len);
211 want_len = lizard_compress(lizard_buf.ptr, lizard_filled, compressed_buf.ptr);
212 #if 0 //TEST error resilience: tamper the compressed data by removing EOF
213 compressed_buf[want_len-1] = 1;
215 bwrite(b, compressed_buf.ptr, want_len);
219 obuck_create_end(b, &h);
220 printf("%08x %d %08x\n", h.oid, h.length, h.type);
224 bb_done(&lizard_buf);
225 bb_done(&compressed_buf);
233 struct obuck_header h;
234 struct fastbuf *b, *out;
238 out = bfdopen_shared(1, 65536);
239 while (b = obuck_slurp_pool(&h))
241 bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type);
242 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
245 while ((l = bread(b, buf, sizeof(buf))))
248 lf = (buf[l-1] == '\n');
251 bprintf(out, "\n# <missing EOL>\n");
254 dump_parsed_bucket(out, &h, b);
265 struct obuck_header h, nh;
271 int fatal_errors = 0;
273 fd = sh_open(obuck_name, O_RDWR);
275 die("Unable to open the bucket file %s: %m", obuck_name);
278 oid = pos >> OBUCK_SHIFT;
279 i = sh_pread(fd, &h, sizeof(h), pos);
283 printf("%08x incomplete header\n", oid);
284 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
285 printf("%08x incomplete file\n", oid);
286 else if (h.magic != OBUCK_MAGIC)
287 printf("%08x invalid header magic\n", oid);
288 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
289 printf("%08x invalid header backlink\n", oid);
292 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
293 if (sh_pread(fd, &chk, 4, end-4) != 4)
294 printf("%08x missing trailer\n", oid);
295 else if (chk != OBUCK_TRAILER)
296 printf("%08x mismatched trailer\n", oid);
308 if (pos - end > 0x10000000)
310 printf("*** skipped for too long, giving up\n");
315 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
317 printf("*** unable to find next header\n");
320 printf("*** truncating file\n");
321 sh_ftruncate(fd, pos);
324 printf("*** would truncate the file here\n");
328 while (nh.magic != OBUCK_MAGIC ||
329 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
330 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
333 h.magic = OBUCK_MAGIC;
334 h.oid = OBUCK_OID_DELETED;
335 h.length = end - pos - sizeof(h) - 4;
336 sh_pwrite(fd, &h, sizeof(h), pos);
338 sh_pwrite(fd, &chk, 4, end-4);
339 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
342 printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
347 if (!fix && errors || fatal_errors)
352 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
356 printf("%08x -> ", old->oid);
357 if (new == OBUCK_OID_DELETED)
360 printf("%08x\n", new);
369 obuck_shakedown(shake_kibitz);
381 main(int argc, char **argv)
389 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqrsv", CF_NO_LONG_OPTS, NULL)) != -1)
406 pool = mp_new(1<<14);
407 buck_buf = buck2obj_alloc();
446 buck2obj_free(buck_buf);