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