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