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