]> mj.ucw.cz Git - libucw.git/blob - lib/buckettool.c
- obj_read_bucket() takes a new parameter: struct mempool *pool
[libucw.git] / lib / buckettool.c
1 /*
2  *      Sherlock Library -- Bucket Manipulation Tool
3  *
4  *      (c) 2001 Martin Mares <mj@ucw.cz>
5  *      (c) 2004 Robert Spalek <robert@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "lib/lib.h"
12 #include "lib/bucket.h"
13 #include "lib/fastbuf.h"
14 #include "lib/lfs.h"
15 #include "lib/conf.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"
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <getopt.h>
26 #include <fcntl.h>
27 #include <unistd.h>
28
29 static int verbose;
30 static struct mempool *pool;
31 static struct buck2obj_buf *buck_buf;
32
33 static void
34 help(void)
35 {
36   fprintf(stderr, "\
37 Usage: buckettool [<options>] <command>\n\
38 \n\
39 Options:\n"
40 CF_USAGE
41 "\nCommands:\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\
52 -v\t\tbe verbose\n\
53 -r\t\tdo not parse V33 buckets, but print the raw content\n\
54 ");
55   exit(1);
56 }
57
58 static oid_t
59 parse_id(char *c)
60 {
61   char *e;
62   oid_t o = strtoul(c, &e, 16);
63   if (e && *e)
64     die("Invalid object ID: %s", c);
65   return o;
66 }
67
68 static void
69 list(int full)
70 {
71   struct obuck_header h;
72
73   obuck_init(0);
74   if (obuck_find_first(&h, full))
75     do
76       {
77         if (h.oid == OBUCK_OID_DELETED)
78           printf("DELETED  %6d\n", h.length);
79         else
80           printf("%08x %6d %08x\n", h.oid, h.length, h.type);
81       }
82     while (obuck_find_next(&h, full));
83   obuck_cleanup();
84 }
85
86 static void
87 delete(char *id)
88 {
89   oid_t oid = parse_id(id);
90   obuck_init(1);
91   obuck_delete(oid);
92   obuck_cleanup();
93 }
94
95 static inline void
96 dump_oattr(struct fastbuf *out, struct oattr *oa)
97 {
98   for (struct oattr *a = oa; a; a = a->same)
99     bprintf(out, "%c%s\n", a->attr, a->val);
100 }
101
102 static void
103 dump_parsed_bucket(struct fastbuf *out, struct obuck_header *h, struct fastbuf *b)
104 {
105   struct odes *o = obj_read_bucket(buck_buf, pool, h->type, h->length, b, NULL);
106   if (!o)
107     bprintf(out, "Cannot parse bucket %x of type %x and length %d: %m\n", h->oid, h->type, h->length);
108   else
109   {
110     if (h->type < BUCKET_TYPE_V30)
111     {
112       for (struct oattr *oa = o->attrs; oa; oa = oa->next)
113         dump_oattr(out, oa);
114     }
115     else
116     {
117 #define IS_HEADER(x) (x=='O' || x=='U')
118       for (struct oattr *oa = o->attrs; oa; oa = oa->next)
119         if (IS_HEADER(oa->attr))
120           dump_oattr(out, oa);
121       bputc(out, '\n');
122       for (struct oattr *oa = o->attrs; oa; oa = oa->next)
123         if (!IS_HEADER(oa->attr))
124           dump_oattr(out, oa);
125     }
126   }
127 }
128
129 static void
130 extract(char *id)
131 {
132   struct fastbuf *b, *out;
133   byte buf[1024];
134   int l;
135   struct obuck_header h;
136
137   h.oid = parse_id(id);
138   obuck_init(0);
139   obuck_find_by_oid(&h);
140   out = bfdopen_shared(1, 65536);
141   b = obuck_fetch();
142   if (h.type < BUCKET_TYPE_V33 || !buck_buf)
143   {
144     while ((l = bread(b, buf, sizeof(buf))))
145       bwrite(out, buf, l);
146   }
147   else
148     dump_parsed_bucket(out, &h, b);
149   bclose(b);
150   bclose(out);
151   obuck_cleanup();
152 }
153
154 #define GBUF_TYPE       byte
155 #define GBUF_PREFIX(x)  bb_##x
156 #include "lib/gbuf.h"
157
158 static void
159 insert(byte *arg)
160 {
161   struct fastbuf *b, *in;
162   byte buf[4096];
163   struct obuck_header h;
164   byte *e;
165   u32 type;
166   bb_t lizard_buf, compressed_buf;
167
168   bb_init(&lizard_buf);
169   bb_init(&compressed_buf);
170   if (!arg)
171     type = BUCKET_TYPE_PLAIN;
172   else if (sscanf(arg, "%x", &type) != 1)
173     die("Type `%s' is not a hexadecimal number");
174
175   in = bfdopen_shared(0, 4096);
176   obuck_init(1);
177   do
178     {
179       uns lizard_filled = 0;
180       uns in_body = 0;
181       b = NULL;
182       while ((e = bgets(in, buf, sizeof(buf))))
183         {
184           if (!buf[0])
185           {
186             if (in_body || type < BUCKET_TYPE_V30)
187               break;
188             in_body = 1;
189           }
190           if (!b)
191             b = obuck_create(type);
192           if (type < BUCKET_TYPE_V33)
193           {
194             *e++ = '\n';
195             bwrite(b, buf, e-buf);
196           }
197           else if (in_body == 1)
198           {
199             bputc(b, 0);
200             in_body = 2;
201           }
202           else if (type == BUCKET_TYPE_V33 || !in_body)
203           {
204             bwrite_v33(b, buf[0], buf+1, e-buf-1);
205           }
206           else
207           {
208             uns want_len = lizard_filled + (e-buf) + 6 + LIZARD_NEEDS_CHARS;    // +6 is the maximum UTF-8 length
209             bb_grow(&lizard_buf, want_len);
210             byte *ptr = lizard_buf.ptr + lizard_filled;
211             WRITE_V33(ptr, buf[0], buf+1, e-buf-1);
212             lizard_filled = ptr - lizard_buf.ptr;
213           }
214         }
215       if (in_body && type == BUCKET_TYPE_V33_LIZARD)
216       {
217         bputl(b, lizard_filled
218 #if 0   //TEST error resilience: write wrong length
219             +1
220 #endif
221             );
222         uns want_len = lizard_filled * LIZARD_MAX_MULTIPLY + LIZARD_MAX_ADD;
223         bb_grow(&compressed_buf, want_len);
224         want_len = lizard_compress(lizard_buf.ptr, lizard_filled, compressed_buf.ptr);
225 #if 0   //TEST error resilience: tamper the compressed data by removing EOF
226         compressed_buf[want_len-1] = 1;
227 #endif
228         bwrite(b, compressed_buf.ptr, want_len);
229       }
230       if (b)
231         {
232           obuck_create_end(b, &h);
233           printf("%08x %d %08x\n", h.oid, h.length, h.type);
234         }
235     }
236   while (e);
237   bb_done(&lizard_buf);
238   bb_done(&compressed_buf);
239   obuck_cleanup();
240   bclose(in);
241 }
242
243 static void
244 cat(void)
245 {
246   struct obuck_header h;
247   struct fastbuf *b, *out;
248   byte buf[1024];
249
250   obuck_init(0);
251   out = bfdopen_shared(1, 65536);
252   while (b = obuck_slurp_pool(&h))
253     {
254       bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type);
255       if (h.type < BUCKET_TYPE_V33 || !buck_buf)
256       {
257         int lf = 1, l;
258         while ((l = bread(b, buf, sizeof(buf))))
259         {
260           bwrite(out, buf, l);
261           lf = (buf[l-1] == '\n');
262         }
263         if (!lf)
264           bprintf(out, "\n# <missing EOL>\n");
265       }
266       else
267         dump_parsed_bucket(out, &h, b);
268     }
269   bclose(out);
270   obuck_cleanup();
271 }
272
273 static void
274 fsck(int fix)
275 {
276   int fd, i;
277   struct obuck_header h, nh;
278   sh_off_t pos = 0;
279   sh_off_t end;
280   oid_t oid;
281   u32 chk;
282   int errors = 0;
283   int fatal_errors = 0;
284
285   fd = sh_open(obuck_name, O_RDWR);
286   if (fd < 0)
287     die("Unable to open the bucket file %s: %m", obuck_name);
288   for(;;)
289     {
290       oid = pos >> OBUCK_SHIFT;
291       i = sh_pread(fd, &h, sizeof(h), pos);
292       if (!i)
293         break;
294       if (i != sizeof(h))
295         printf("%08x  incomplete header\n", oid);
296       else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
297         printf("%08x  incomplete file\n", oid);
298       else if (h.magic != OBUCK_MAGIC)
299         printf("%08x  invalid header magic\n", oid);
300       else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
301         printf("%08x  invalid header backlink\n", oid);
302       else
303         {
304           end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
305           if (sh_pread(fd, &chk, 4, end-4) != 4)
306             printf("%08x  missing trailer\n", oid);
307           else if (chk != OBUCK_TRAILER)
308             printf("%08x  mismatched trailer\n", oid);
309           else
310             {
311               /* OK */
312               pos = end;
313               continue;
314             }
315         }
316       errors++;
317       end = pos;
318       do
319         {
320           if (pos - end > 0x10000000)
321             {
322               printf("*** skipped for too long, giving up\n");
323               fatal_errors++;
324               goto finish;
325             }
326           end += OBUCK_ALIGN;
327           if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
328             {
329               printf("*** unable to find next header\n");
330               if (fix)
331                 {
332                   printf("*** truncating file\n");
333                   sh_ftruncate(fd, pos);
334                 }
335               else
336                 printf("*** would truncate the file here\n");
337               goto finish;
338             }
339         }
340       while (nh.magic != OBUCK_MAGIC ||
341              (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
342       printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
343       if (fix)
344         {
345           h.magic = OBUCK_MAGIC;
346           h.oid = OBUCK_OID_DELETED;
347           h.length = end - pos - sizeof(h) - 4;
348           sh_pwrite(fd, &h, sizeof(h), pos);
349           chk = OBUCK_TRAILER;
350           sh_pwrite(fd, &chk, 4, end-4);
351           printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
352         }
353       else
354         printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
355       pos = end;
356     }
357  finish:
358   close(fd);
359   if (!fix && errors || fatal_errors)
360     exit(1);
361 }
362
363 static int
364 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
365 {
366   if (verbose)
367     {
368       printf("%08x -> ", old->oid);
369       if (new == OBUCK_OID_DELETED)
370         puts("DELETED");
371       else
372         printf("%08x\n", new);
373     }
374   return 1;
375 }
376
377 static void
378 shake(void)
379 {
380   obuck_init(1);
381   obuck_shakedown(shake_kibitz);
382   obuck_cleanup();
383 }
384
385 static void
386 quickcheck(void)
387 {
388   obuck_init(1);
389   obuck_cleanup();
390 }
391
392 int
393 main(int argc, char **argv)
394 {
395   int i, op;
396   char *arg = NULL;
397   uns raw = 0;
398
399   log_init(NULL);
400   op = 0;
401   while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqsvr", CF_NO_LONG_OPTS, NULL)) != -1)
402     if (i == '?' || op)
403       help();
404     else if (i == 'v')
405       verbose++;
406     else if (i == 'r')
407       raw++;
408     else
409       {
410         op = i;
411         arg = optarg;
412       }
413   if (optind < argc)
414     help();
415
416   if (!raw)
417   {
418     pool = mp_new(1<<14);
419     buck_buf = buck2obj_alloc();
420   }
421   switch (op)
422     {
423     case 'l':
424       list(0);
425       break;
426     case 'L':
427       list(1);
428       break;
429     case 'd':
430       delete(arg);
431       break;
432     case 'x':
433       extract(arg);
434       break;
435     case 'i':
436       insert(arg);
437       break;
438     case 'c':
439       cat();
440       break;
441     case 'f':
442       fsck(0);
443       break;
444     case 'F':
445       fsck(1);
446       break;
447     case 'q':
448       quickcheck();
449       break;
450     case 's':
451       shake();
452       break;
453     default:
454       help();
455     }
456   if (buck_buf)
457   {
458     buck2obj_free(buck_buf);
459     mp_delete(pool);
460   }
461
462   return 0;
463 }