]> mj.ucw.cz Git - libucw.git/blob - lib/buckettool.c
`buckettool -c' (cat) now separates buckets by an empty line.
[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       bputc(out, '\n');
258     }
259   bclose(out);
260   obuck_cleanup();
261 }
262
263 static void
264 fsck(int fix)
265 {
266   int fd, i;
267   struct obuck_header h, nh;
268   sh_off_t pos = 0;
269   sh_off_t end;
270   oid_t oid;
271   u32 chk;
272   int errors = 0;
273   int fatal_errors = 0;
274
275   fd = sh_open(obuck_name, O_RDWR);
276   if (fd < 0)
277     die("Unable to open the bucket file %s: %m", obuck_name);
278   for(;;)
279     {
280       oid = pos >> OBUCK_SHIFT;
281       i = sh_pread(fd, &h, sizeof(h), pos);
282       if (!i)
283         break;
284       if (i != sizeof(h))
285         printf("%08x  incomplete header\n", oid);
286       else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
287         printf("%08x  incomplete file\n", oid);
288       else if (h.magic != OBUCK_MAGIC)
289         printf("%08x  invalid header magic\n", oid);
290       else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
291         printf("%08x  invalid header backlink\n", oid);
292       else
293         {
294           end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
295           if (sh_pread(fd, &chk, 4, end-4) != 4)
296             printf("%08x  missing trailer\n", oid);
297           else if (chk != OBUCK_TRAILER)
298             printf("%08x  mismatched trailer\n", oid);
299           else
300             {
301               /* OK */
302               pos = end;
303               continue;
304             }
305         }
306       errors++;
307       end = pos;
308       do
309         {
310           if (pos - end > 0x10000000)
311             {
312               printf("*** skipped for too long, giving up\n");
313               fatal_errors++;
314               goto finish;
315             }
316           end += OBUCK_ALIGN;
317           if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
318             {
319               printf("*** unable to find next header\n");
320               if (fix)
321                 {
322                   printf("*** truncating file\n");
323                   sh_ftruncate(fd, pos);
324                 }
325               else
326                 printf("*** would truncate the file here\n");
327               goto finish;
328             }
329         }
330       while (nh.magic != OBUCK_MAGIC ||
331              (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
332       printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
333       if (fix)
334         {
335           h.magic = OBUCK_MAGIC;
336           h.oid = OBUCK_OID_DELETED;
337           h.length = end - pos - sizeof(h) - 4;
338           sh_pwrite(fd, &h, sizeof(h), pos);
339           chk = OBUCK_TRAILER;
340           sh_pwrite(fd, &chk, 4, end-4);
341           printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
342         }
343       else
344         printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
345       pos = end;
346     }
347  finish:
348   close(fd);
349   if (!fix && errors || fatal_errors)
350     exit(1);
351 }
352
353 static int
354 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
355 {
356   if (verbose)
357     {
358       printf("%08x -> ", old->oid);
359       if (new == OBUCK_OID_DELETED)
360         puts("DELETED");
361       else
362         printf("%08x\n", new);
363     }
364   return 1;
365 }
366
367 static void
368 shake(void)
369 {
370   obuck_init(1);
371   obuck_shakedown(shake_kibitz);
372   obuck_cleanup();
373 }
374
375 static void
376 quickcheck(void)
377 {
378   obuck_init(1);
379   obuck_cleanup();
380 }
381
382 int
383 main(int argc, char **argv)
384 {
385   int i, op;
386   char *arg = NULL;
387   uns raw = 0;
388
389   log_init(NULL);
390   op = 0;
391   while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqrsv", CF_NO_LONG_OPTS, NULL)) != -1)
392     if (i == '?' || op)
393       help();
394     else if (i == 'v')
395       verbose++;
396     else if (i == 'r')
397       raw++;
398     else
399       {
400         op = i;
401         arg = optarg;
402       }
403   if (optind < argc)
404     help();
405
406   if (!raw)
407   {
408     pool = mp_new(1<<14);
409     buck_buf = buck2obj_alloc();
410   }
411   switch (op)
412     {
413     case 'l':
414       list(0);
415       break;
416     case 'L':
417       list(1);
418       break;
419     case 'd':
420       delete(arg);
421       break;
422     case 'x':
423       extract(arg);
424       break;
425     case 'i':
426       insert(arg);
427       break;
428     case 'c':
429       cat();
430       break;
431     case 'f':
432       fsck(0);
433       break;
434     case 'F':
435       fsck(1);
436       break;
437     case 'q':
438       quickcheck();
439       break;
440     case 's':
441       shake();
442       break;
443     default:
444       help();
445     }
446   if (buck_buf)
447   {
448     buck2obj_free(buck_buf);
449     mp_delete(pool);
450   }
451
452   return 0;
453 }