]> mj.ucw.cz Git - libucw.git/blob - lib/buckettool.c
- lizard_alloc() turns on the wrapper for SIGSEGV and lizard_free() restores
[libucw.git] / lib / buckettool.c
1 /*
2  *      Sherlock Library -- Bucket Manipulation Tool
3  *
4  *      (c) 2001 Martin Mares <mj@ucw.cz>
5  *
6  *      This software may be freely distributed and used according to the terms
7  *      of the GNU Lesser General Public License.
8  */
9
10 #include "lib/lib.h"
11 #include "lib/bucket.h"
12 #include "lib/fastbuf.h"
13 #include "lib/lfs.h"
14 #include "lib/conf.h"
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <getopt.h>
19 #include <fcntl.h>
20 #include <unistd.h>
21
22 static int verbose;
23
24 static void
25 help(void)
26 {
27   fprintf(stderr, "\
28 Usage: buckettool [<options>] <command>\n\
29 \n\
30 Options:\n"
31 CF_USAGE
32 "\nCommands:\n\
33 -l\t\tlist all buckets\n\
34 -L\t\tlist all buckets including deleted ones\n\
35 -d <obj>\tdelete bucket\n\
36 -x <obj>\textract bucket\n\
37 -i[<type>]\tinsert buckets separated by blank lines\n\
38 -c\t\tconcatenate and dump all buckets\n\
39 -f\t\taudit bucket file structure\n\
40 -F\t\taudit and fix bucket file structure\n\
41 -q\t\tquick check of bucket file consistency\n\
42 -s\t\tshake down bucket file (without updating other structures!!!)\n\
43 -v\t\tbe verbose\n\
44 ");
45   exit(1);
46 }
47
48 static oid_t
49 parse_id(char *c)
50 {
51   char *e;
52   oid_t o = strtoul(c, &e, 16);
53   if (e && *e)
54     die("Invalid object ID: %s", c);
55   return o;
56 }
57
58 static void
59 list(int full)
60 {
61   struct obuck_header h;
62
63   obuck_init(0);
64   if (obuck_find_first(&h, full))
65     do
66       {
67         if (h.oid == OBUCK_OID_DELETED)
68           printf("DELETED  %6d\n", h.length);
69         else
70           printf("%08x %6d %08x\n", h.oid, h.length, h.type);
71       }
72     while (obuck_find_next(&h, full));
73   obuck_cleanup();
74 }
75
76 static void
77 delete(char *id)
78 {
79   oid_t oid = parse_id(id);
80   obuck_init(1);
81   obuck_delete(oid);
82   obuck_cleanup();
83 }
84
85 static void
86 extract(char *id)
87 {
88   struct fastbuf *b;
89   byte buf[1024];
90   int l;
91   struct obuck_header h;
92
93   h.oid = parse_id(id);
94   obuck_init(0);
95   obuck_find_by_oid(&h);
96   b = obuck_fetch();
97   while ((l = bread(b, buf, sizeof(buf))))
98     fwrite(buf, 1, l, stdout);
99   bclose(b);
100   obuck_cleanup();
101 }
102
103 static void
104 insert(byte *arg)
105 {
106   struct fastbuf *b, *in;
107   byte buf[4096];
108   struct obuck_header h;
109   byte *e;
110   u32 type;
111
112   if (!arg)
113     type = BUCKET_TYPE_PLAIN;
114   else if (sscanf(arg, "%x", &type) != 1)
115     die("Type `%s' is not a hexadecimal number");
116
117   in = bfdopen_shared(0, 4096);
118   obuck_init(1);
119   do
120     {
121       b = NULL;
122       while ((e = bgets(in, buf, sizeof(buf))) && buf[0])
123         {
124           *e++ = '\n';
125           if (!b)
126             b = obuck_create(type);
127           bwrite(b, buf, e-buf);
128         }
129       if (b)
130         {
131           obuck_create_end(b, &h);
132           printf("%08x %d %08x\n", h.oid, h.length, h.type);
133         }
134     }
135   while (e);
136   obuck_cleanup();
137   bclose(in);
138 }
139
140 static void
141 cat(void)
142 {
143   struct obuck_header h;
144   struct fastbuf *b, *out;
145   byte buf[1024];
146   int l, lf;
147
148   obuck_init(0);
149   out = bfdopen_shared(1, 65536);
150   while (b = obuck_slurp_pool(&h))
151     {
152       bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type);
153       lf = 1;
154       while ((l = bread(b, buf, sizeof(buf))))
155         {
156           bwrite(out, buf, l);
157           lf = (buf[l-1] == '\n');
158         }
159       if (!lf)
160         bprintf(out, "\n# <missing EOL>\n");
161     }
162   bclose(out);
163   obuck_cleanup();
164 }
165
166 static void
167 fsck(int fix)
168 {
169   int fd, i;
170   struct obuck_header h, nh;
171   sh_off_t pos = 0;
172   sh_off_t end;
173   oid_t oid;
174   u32 chk;
175   int errors = 0;
176   int fatal_errors = 0;
177
178   fd = sh_open(obuck_name, O_RDWR);
179   if (fd < 0)
180     die("Unable to open the bucket file %s: %m", obuck_name);
181   for(;;)
182     {
183       oid = pos >> OBUCK_SHIFT;
184       i = sh_pread(fd, &h, sizeof(h), pos);
185       if (!i)
186         break;
187       if (i != sizeof(h))
188         printf("%08x  incomplete header\n", oid);
189       else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
190         printf("%08x  incomplete file\n", oid);
191       else if (h.magic != OBUCK_MAGIC)
192         printf("%08x  invalid header magic\n", oid);
193       else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
194         printf("%08x  invalid header backlink\n", oid);
195       else
196         {
197           end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
198           if (sh_pread(fd, &chk, 4, end-4) != 4)
199             printf("%08x  missing trailer\n", oid);
200           else if (chk != OBUCK_TRAILER)
201             printf("%08x  mismatched trailer\n", oid);
202           else
203             {
204               /* OK */
205               pos = end;
206               continue;
207             }
208         }
209       errors++;
210       end = pos;
211       do
212         {
213           if (pos - end > 0x10000000)
214             {
215               printf("*** skipped for too long, giving up\n");
216               fatal_errors++;
217               goto finish;
218             }
219           end += OBUCK_ALIGN;
220           if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
221             {
222               printf("*** unable to find next header\n");
223               if (fix)
224                 {
225                   printf("*** truncating file\n");
226                   sh_ftruncate(fd, pos);
227                 }
228               else
229                 printf("*** would truncate the file here\n");
230               goto finish;
231             }
232         }
233       while (nh.magic != OBUCK_MAGIC ||
234              (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
235       printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
236       if (fix)
237         {
238           h.magic = OBUCK_MAGIC;
239           h.oid = OBUCK_OID_DELETED;
240           h.length = end - pos - sizeof(h) - 4;
241           sh_pwrite(fd, &h, sizeof(h), pos);
242           chk = OBUCK_TRAILER;
243           sh_pwrite(fd, &chk, 4, end-4);
244           printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
245         }
246       else
247         printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
248       pos = end;
249     }
250  finish:
251   close(fd);
252   if (!fix && errors || fatal_errors)
253     exit(1);
254 }
255
256 static int
257 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
258 {
259   if (verbose)
260     {
261       printf("%08x -> ", old->oid);
262       if (new == OBUCK_OID_DELETED)
263         puts("DELETED");
264       else
265         printf("%08x\n", new);
266     }
267   return 1;
268 }
269
270 static void
271 shake(void)
272 {
273   obuck_init(1);
274   obuck_shakedown(shake_kibitz);
275   obuck_cleanup();
276 }
277
278 static void
279 quickcheck(void)
280 {
281   obuck_init(1);
282   obuck_cleanup();
283 }
284
285 int
286 main(int argc, char **argv)
287 {
288   int i, op;
289   char *arg = NULL;
290
291   log_init(NULL);
292   op = 0;
293   while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqsv", CF_NO_LONG_OPTS, NULL)) != -1)
294     if (i == '?' || op)
295       help();
296     else if (i == 'v')
297       verbose++;
298     else
299       {
300         op = i;
301         arg = optarg;
302       }
303   if (optind < argc)
304     help();
305
306   switch (op)
307     {
308     case 'l':
309       list(0);
310       break;
311     case 'L':
312       list(1);
313       break;
314     case 'd':
315       delete(arg);
316       break;
317     case 'x':
318       extract(arg);
319       break;
320     case 'i':
321       insert(arg);
322       break;
323     case 'c':
324       cat();
325       break;
326     case 'f':
327       fsck(0);
328       break;
329     case 'F':
330       fsck(1);
331       break;
332     case 'q':
333       quickcheck();
334       break;
335     case 's':
336       shake();
337       break;
338     default:
339       help();
340     }
341
342   return 0;
343 }