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