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