2 * Sherlock Library -- Bucket Manipulation Tool
4 * (c) 2001 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/pools.h"
17 #include "lib/object.h"
18 #include "lib/buck2obj.h"
19 #include "lib/obj2buck.h"
20 #include "lib/lizard.h"
21 #include "charset/unistream.h"
30 static struct mempool *pool;
31 static struct buck2obj_buf *buck_buf;
37 Usage: buckettool [<options>] <command>\n\
42 -l\t\tlist all buckets\n\
43 -L\t\tlist all buckets including deleted ones\n\
44 -d <obj>\tdelete bucket\n\
45 -x <obj>\textract bucket\n\
46 -i[<type>]\tinsert buckets separated by blank lines\n\
47 -c\t\tconcatenate and dump all buckets\n\
48 -f\t\taudit bucket file structure\n\
49 -F\t\taudit and fix bucket file structure\n\
50 -q\t\tquick check of bucket file consistency\n\
51 -r\t\tdo not parse V33 buckets, but print the raw content\n\
52 -s\t\tshake down bucket file (without updating other structures!!!)\n\
62 oid_t o = strtoul(c, &e, 16);
64 die("Invalid object ID: %s", c);
71 struct obuck_header h;
74 if (obuck_find_first(&h, full))
77 if (h.oid == OBUCK_OID_DELETED)
78 printf("DELETED %6d\n", h.length);
80 printf("%08x %6d %08x\n", h.oid, h.length, h.type);
82 while (obuck_find_next(&h, full));
89 oid_t oid = parse_id(id);
96 dump_oattrs(struct fastbuf *out, struct oattr *a)
98 for (; 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);
144 #define GBUF_TYPE byte
145 #define GBUF_PREFIX(x) bb_##x
146 #include "lib/gbuf.h"
151 struct fastbuf *b, *in;
153 struct obuck_header h;
156 bb_t lizard_buf, compressed_buf;
158 bb_init(&lizard_buf);
159 bb_init(&compressed_buf);
161 type = BUCKET_TYPE_PLAIN;
162 else if (sscanf(arg, "%x", &type) != 1)
163 die("Type `%s' is not a hexadecimal number");
166 in = bfdopen_shared(0, 4096);
170 uns lizard_filled = 0;
173 while ((e = bgets(in, buf, sizeof(buf))))
177 if (in_body || type < BUCKET_TYPE_V30)
182 b = obuck_create(type);
188 else if (type <= BUCKET_TYPE_V33 || !in_body)
190 bput_attr(b, buf[0], buf+1, e-buf-1);
194 ASSERT(BUCKET_TYPE_V33_LIZARD);
195 uns want_len = lizard_filled + (e-buf) + 6 + LIZARD_NEEDS_CHARS; // +6 is the maximum UTF-8 length
196 bb_grow(&lizard_buf, want_len);
197 byte *ptr = lizard_buf.ptr + lizard_filled;
198 ptr = put_attr(ptr, buf[0], buf+1, e-buf-1);
199 lizard_filled = ptr - lizard_buf.ptr;
202 if (in_body && type == BUCKET_TYPE_V33_LIZARD)
204 bputl(b, lizard_filled
205 #if 0 //TEST error resilience: write wrong length
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);
264 struct obuck_header h, nh;
270 int fatal_errors = 0;
272 fd = sh_open(obuck_name, O_RDWR);
274 die("Unable to open the bucket file %s: %m", obuck_name);
277 oid = pos >> OBUCK_SHIFT;
278 i = sh_pread(fd, &h, sizeof(h), pos);
282 printf("%08x incomplete header\n", oid);
283 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
284 printf("%08x incomplete file\n", oid);
285 else if (h.magic != OBUCK_MAGIC)
286 printf("%08x invalid header magic\n", oid);
287 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
288 printf("%08x invalid header backlink\n", oid);
291 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
292 if (sh_pread(fd, &chk, 4, end-4) != 4)
293 printf("%08x missing trailer\n", oid);
294 else if (chk != OBUCK_TRAILER)
295 printf("%08x mismatched trailer\n", oid);
307 if (pos - end > 0x10000000)
309 printf("*** skipped for too long, giving up\n");
314 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
316 printf("*** unable to find next header\n");
319 printf("*** truncating file\n");
320 sh_ftruncate(fd, pos);
323 printf("*** would truncate the file here\n");
327 while (nh.magic != OBUCK_MAGIC ||
328 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
329 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
332 h.magic = OBUCK_MAGIC;
333 h.oid = OBUCK_OID_DELETED;
334 h.length = end - pos - sizeof(h) - 4;
335 sh_pwrite(fd, &h, sizeof(h), pos);
337 sh_pwrite(fd, &chk, 4, end-4);
338 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
341 printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
346 if (!fix && errors || fatal_errors)
351 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
355 printf("%08x -> ", old->oid);
356 if (new == OBUCK_OID_DELETED)
359 printf("%08x\n", new);
368 obuck_shakedown(shake_kibitz);
380 main(int argc, char **argv)
388 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqrsv", CF_NO_LONG_OPTS, NULL)) != -1)
405 pool = mp_new(1<<14);
406 buck_buf = buck2obj_alloc();
445 buck2obj_free(buck_buf);