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