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