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 *a)
97 for (; a; a = a->same)
98 bprintf(out, "%c%s\n", a->attr, a->val);
102 dump_parsed_bucket(struct fastbuf *out, struct obuck_header *h, struct fastbuf *b)
104 struct odes *o_hdr, *o_body;
106 o_hdr = obj_new(pool);
107 o_body = obj_new(pool);
108 if (buck2obj_parse(buck_buf, h->type, h->length, b, o_hdr, NULL, o_body) < 0)
109 bprintf(out, ".Cannot parse bucket %x of type %x and length %d: %m\n", h->oid, h->type, h->length);
112 dump_oattrs(out, o_hdr->attrs);
114 dump_oattrs(out, o_body->attrs);
121 struct fastbuf *b, *out;
124 struct obuck_header h;
126 h.oid = parse_id(id);
128 obuck_find_by_oid(&h);
129 out = bfdopen_shared(1, 65536);
131 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
133 while ((l = bread(b, buf, sizeof(buf))))
137 dump_parsed_bucket(out, &h, b);
146 struct fastbuf *b, *in;
148 struct obuck_header h;
151 bb_t lizard_buf, compressed_buf;
153 bb_init(&lizard_buf);
154 bb_init(&compressed_buf);
156 type = BUCKET_TYPE_PLAIN;
157 else if (sscanf(arg, "%x", &type) != 1)
158 die("Type `%s' is not a hexadecimal number");
160 type += BUCKET_TYPE_PLAIN;
163 in = bfdopen_shared(0, 4096);
167 uns lizard_filled = 0;
170 while ((e = bgets(in, buf, sizeof(buf))))
174 if (in_body || type < BUCKET_TYPE_V30)
179 b = obuck_create(type);
185 else if (type <= BUCKET_TYPE_V33 || !in_body)
187 bput_attr(b, buf[0], buf+1, e-buf-1);
191 ASSERT(BUCKET_TYPE_V33_LIZARD);
192 uns want_len = lizard_filled + (e-buf) + 6 + LIZARD_NEEDS_CHARS; // +6 is the maximum UTF-8 length
193 bb_grow(&lizard_buf, want_len);
194 byte *ptr = lizard_buf.ptr + lizard_filled;
195 ptr = put_attr(ptr, buf[0], buf+1, e-buf-1);
196 lizard_filled = ptr - lizard_buf.ptr;
199 if (in_body && type == BUCKET_TYPE_V33_LIZARD)
201 bputl(b, lizard_filled
202 #if 0 //TEST error resilience: write wrong length
206 uns want_len = lizard_filled * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
207 bb_grow(&compressed_buf, want_len);
208 want_len = lizard_compress(lizard_buf.ptr, lizard_filled, compressed_buf.ptr);
209 #if 0 //TEST error resilience: tamper the compressed data by removing EOF
210 compressed_buf[want_len-1] = 1;
212 bwrite(b, compressed_buf.ptr, want_len);
216 obuck_create_end(b, &h);
217 printf("%08x %d %08x\n", h.oid, h.length, h.type);
221 bb_done(&lizard_buf);
222 bb_done(&compressed_buf);
230 struct obuck_header h;
231 struct fastbuf *b, *out;
235 out = bfdopen_shared(1, 65536);
236 while (b = obuck_slurp_pool(&h))
238 bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type);
239 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
242 while ((l = bread(b, buf, sizeof(buf))))
245 lf = (buf[l-1] == '\n');
248 bprintf(out, "\n# <missing EOL>\n");
251 dump_parsed_bucket(out, &h, b);
261 struct obuck_header h, nh;
267 int fatal_errors = 0;
269 fd = sh_open(obuck_name, O_RDWR);
271 die("Unable to open the bucket file %s: %m", obuck_name);
274 oid = pos >> OBUCK_SHIFT;
275 i = sh_pread(fd, &h, sizeof(h), pos);
279 printf("%08x incomplete header\n", oid);
280 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
281 printf("%08x incomplete file\n", oid);
282 else if (h.magic != OBUCK_MAGIC)
283 printf("%08x invalid header magic\n", oid);
284 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
285 printf("%08x invalid header backlink\n", oid);
288 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
289 if (sh_pread(fd, &chk, 4, end-4) != 4)
290 printf("%08x missing trailer\n", oid);
291 else if (chk != OBUCK_TRAILER)
292 printf("%08x mismatched trailer\n", oid);
304 if (pos - end > 0x10000000)
306 printf("*** skipped for too long, giving up\n");
311 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
313 printf("*** unable to find next header\n");
316 printf("*** truncating file\n");
317 sh_ftruncate(fd, pos);
320 printf("*** would truncate the file here\n");
324 while (nh.magic != OBUCK_MAGIC ||
325 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
326 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
329 h.magic = OBUCK_MAGIC;
330 h.oid = OBUCK_OID_DELETED;
331 h.length = end - pos - sizeof(h) - 4;
332 sh_pwrite(fd, &h, sizeof(h), pos);
334 sh_pwrite(fd, &chk, 4, end-4);
335 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
338 printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
343 if (!fix && errors || fatal_errors)
348 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
352 printf("%08x -> ", old->oid);
353 if (new == OBUCK_OID_DELETED)
356 printf("%08x\n", new);
365 obuck_shakedown(shake_kibitz);
377 main(int argc, char **argv)
385 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqrsv", CF_NO_LONG_OPTS, NULL)) != -1)
402 pool = mp_new(1<<14);
403 buck_buf = buck2obj_alloc();
442 buck2obj_free(buck_buf);