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 -s\t\tshake down bucket file (without updating other structures!!!)\n\
53 -r\t\tdo not parse V33 buckets, but print the raw content\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_oattr(struct fastbuf *out, struct oattr *oa)
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 = obj_read_bucket(buck_buf, pool, h->type, h->length, b, NULL);
107 bprintf(out, "Cannot parse bucket %x of type %x and length %d: %m\n", h->oid, h->type, h->length);
110 #define IS_HEADER(x) (x=='O' || x=='U')
111 for (struct oattr *oa = o->attrs; oa; oa = oa->next)
112 if (IS_HEADER(oa->attr))
115 for (struct oattr *oa = o->attrs; oa; oa = oa->next)
116 if (!IS_HEADER(oa->attr))
124 struct fastbuf *b, *out;
127 struct obuck_header h;
129 h.oid = parse_id(id);
131 obuck_find_by_oid(&h);
132 out = bfdopen_shared(1, 65536);
134 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
136 while ((l = bread(b, buf, sizeof(buf))))
140 dump_parsed_bucket(out, &h, b);
146 #define GBUF_TYPE byte
147 #define GBUF_PREFIX(x) bb_##x
148 #include "lib/gbuf.h"
153 struct fastbuf *b, *in;
155 struct obuck_header h;
158 bb_t lizard_buf, compressed_buf;
160 bb_init(&lizard_buf);
161 bb_init(&compressed_buf);
163 type = BUCKET_TYPE_PLAIN;
164 else if (sscanf(arg, "%x", &type) != 1)
165 die("Type `%s' is not a hexadecimal number");
167 in = bfdopen_shared(0, 4096);
171 uns lizard_filled = 0;
174 while ((e = bgets(in, buf, sizeof(buf))))
178 if (in_body || type < BUCKET_TYPE_V30)
183 b = obuck_create(type);
184 if (type < BUCKET_TYPE_V33)
187 bwrite(b, buf, e-buf);
189 else if (in_body == 1)
194 else if (type == BUCKET_TYPE_V33 || !in_body)
196 bwrite_v33(b, buf[0], buf+1, e-buf-1);
200 uns want_len = lizard_filled + (e-buf) + 6 + LIZARD_NEEDS_CHARS; // +6 is the maximum UTF-8 length
201 bb_grow(&lizard_buf, want_len);
202 byte *ptr = lizard_buf.ptr + lizard_filled;
203 WRITE_V33(ptr, buf[0], buf+1, e-buf-1);
204 lizard_filled = ptr - lizard_buf.ptr;
207 if (in_body && type == BUCKET_TYPE_V33_LIZARD)
209 bputl(b, lizard_filled
210 #if 0 //TEST error resilience: write wrong length
214 uns want_len = lizard_filled * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
215 bb_grow(&compressed_buf, want_len);
216 want_len = lizard_compress(lizard_buf.ptr, lizard_filled, compressed_buf.ptr);
217 #if 0 //TEST error resilience: tamper the compressed data by removing EOF
218 compressed_buf[want_len-1] = 1;
220 bwrite(b, compressed_buf.ptr, want_len);
224 obuck_create_end(b, &h);
225 printf("%08x %d %08x\n", h.oid, h.length, h.type);
229 bb_done(&lizard_buf);
230 bb_done(&compressed_buf);
238 struct obuck_header h;
239 struct fastbuf *b, *out;
243 out = bfdopen_shared(1, 65536);
244 while (b = obuck_slurp_pool(&h))
246 bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type);
247 if (h.type < BUCKET_TYPE_V33 || !buck_buf)
250 while ((l = bread(b, buf, sizeof(buf))))
253 lf = (buf[l-1] == '\n');
256 bprintf(out, "\n# <missing EOL>\n");
259 dump_parsed_bucket(out, &h, b);
269 struct obuck_header h, nh;
275 int fatal_errors = 0;
277 fd = sh_open(obuck_name, O_RDWR);
279 die("Unable to open the bucket file %s: %m", obuck_name);
282 oid = pos >> OBUCK_SHIFT;
283 i = sh_pread(fd, &h, sizeof(h), pos);
287 printf("%08x incomplete header\n", oid);
288 else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
289 printf("%08x incomplete file\n", oid);
290 else if (h.magic != OBUCK_MAGIC)
291 printf("%08x invalid header magic\n", oid);
292 else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
293 printf("%08x invalid header backlink\n", oid);
296 end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
297 if (sh_pread(fd, &chk, 4, end-4) != 4)
298 printf("%08x missing trailer\n", oid);
299 else if (chk != OBUCK_TRAILER)
300 printf("%08x mismatched trailer\n", oid);
312 if (pos - end > 0x10000000)
314 printf("*** skipped for too long, giving up\n");
319 if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
321 printf("*** unable to find next header\n");
324 printf("*** truncating file\n");
325 sh_ftruncate(fd, pos);
328 printf("*** would truncate the file here\n");
332 while (nh.magic != OBUCK_MAGIC ||
333 (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
334 printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
337 h.magic = OBUCK_MAGIC;
338 h.oid = OBUCK_OID_DELETED;
339 h.length = end - pos - sizeof(h) - 4;
340 sh_pwrite(fd, &h, sizeof(h), pos);
342 sh_pwrite(fd, &chk, 4, end-4);
343 printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
346 printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
351 if (!fix && errors || fatal_errors)
356 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
360 printf("%08x -> ", old->oid);
361 if (new == OBUCK_OID_DELETED)
364 printf("%08x\n", new);
373 obuck_shakedown(shake_kibitz);
385 main(int argc, char **argv)
393 while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqsvr", CF_NO_LONG_OPTS, NULL)) != -1)
410 pool = mp_new(1<<14);
411 buck_buf = buck2obj_alloc();
450 buck2obj_free(buck_buf);