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