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