]> mj.ucw.cz Git - libucw.git/blob - lib/buckettool.c
Introduced obuck_slurp_pool() to make reading of the whole bucket
[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\t\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 %6d\n", h.oid, h.length, h.orig_length);
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   obuck_fetch_end(b);
100   obuck_cleanup();
101 }
102
103 static void
104 insert(void)
105 {
106   struct fastbuf *b, *in;
107   byte buf[4096];
108   struct obuck_header h;
109   byte *e;
110
111   in = bfdopen_shared(0, 4096);
112   obuck_init(1);
113   do
114     {
115       b = obuck_create();
116       while ((e = bgets(in, buf, sizeof(buf))) && buf[0])
117         {
118           *e++ = '\n';
119           bwrite(b, buf, e-buf);
120         }
121       obuck_create_end(b, &h);
122       printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
123     }
124   while (e);
125   obuck_cleanup();
126   bclose(in);
127 }
128
129 static void
130 cat(void)
131 {
132   struct obuck_header h;
133   struct fastbuf *b;
134   byte buf[1024];
135   int l, lf;
136
137   obuck_init(0);
138   while (b = obuck_slurp_pool(&h))
139     {
140       printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length);
141       lf = 1;
142       while ((l = bread(b, buf, sizeof(buf))))
143         {
144           fwrite(buf, 1, l, stdout);
145           lf = (buf[l-1] == '\n');
146         }
147       if (!lf)
148         printf("\n# <missing EOL>\n");
149     }
150   obuck_cleanup();
151 }
152
153 static void
154 fsck(int fix)
155 {
156   int fd, i;
157   struct obuck_header h, nh;
158   sh_off_t pos = 0;
159   sh_off_t end;
160   oid_t oid;
161   u32 chk;
162   int errors = 0;
163   int fatal_errors = 0;
164
165   fd = sh_open(obuck_name, O_RDWR);
166   if (fd < 0)
167     die("Unable to open the bucket file %s: %m", obuck_name);
168   for(;;)
169     {
170       oid = pos >> OBUCK_SHIFT;
171       i = sh_pread(fd, &h, sizeof(h), pos);
172       if (!i)
173         break;
174       if (i != sizeof(h))
175         printf("%08x  incomplete header\n", oid);
176       else if (h.magic == OBUCK_INCOMPLETE_MAGIC)
177         printf("%08x  incomplete file\n", oid);
178       else if (h.magic != OBUCK_MAGIC)
179         printf("%08x  invalid header magic\n", oid);
180       else if (h.oid != oid && h.oid != OBUCK_OID_DELETED)
181         printf("%08x  invalid header backlink\n", oid);
182       else
183         {
184           end = (pos + sizeof(h) + h.length + 4 + OBUCK_ALIGN - 1) & ~(sh_off_t)(OBUCK_ALIGN - 1);
185           if (sh_pread(fd, &chk, 4, end-4) != 4)
186             printf("%08x  missing trailer\n", oid);
187           else if (chk != OBUCK_TRAILER)
188             printf("%08x  mismatched trailer\n", oid);
189           else
190             {
191               /* OK */
192               pos = end;
193               continue;
194             }
195         }
196       errors++;
197       end = pos;
198       do
199         {
200           if (pos - end > 0x10000000)
201             {
202               printf("*** skipped for too long, giving up\n");
203               fatal_errors++;
204               goto finish;
205             }
206           end += OBUCK_ALIGN;
207           if (sh_pread(fd, &nh, sizeof(nh), end) != sizeof(nh))
208             {
209               printf("*** unable to find next header\n");
210               if (fix)
211                 {
212                   printf("*** truncating file\n");
213                   sh_ftruncate(fd, pos);
214                 }
215               else
216                 printf("*** would truncate the file here\n");
217               goto finish;
218             }
219         }
220       while (nh.magic != OBUCK_MAGIC ||
221              (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
222       printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
223       if (fix)
224         {
225           h.magic = OBUCK_MAGIC;
226           h.oid = OBUCK_OID_DELETED;
227           h.length = h.orig_length = end - pos - sizeof(h) - 4;
228           sh_pwrite(fd, &h, sizeof(h), pos);
229           chk = OBUCK_TRAILER;
230           sh_pwrite(fd, &chk, 4, end-4);
231           printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
232         }
233       else
234         printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos));
235       pos = end;
236     }
237  finish:
238   close(fd);
239   if (!fix && errors || fatal_errors)
240     exit(1);
241 }
242
243 static int
244 shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED)
245 {
246   if (verbose)
247     {
248       printf("%08x -> ", old->oid);
249       if (new == OBUCK_OID_DELETED)
250         puts("DELETED");
251       else
252         printf("%08x\n", new);
253     }
254   return 1;
255 }
256
257 static void
258 shake(void)
259 {
260   obuck_init(1);
261   obuck_shakedown(shake_kibitz);
262   obuck_cleanup();
263 }
264
265 static void
266 quickcheck(void)
267 {
268   obuck_init(1);
269   obuck_cleanup();
270 }
271
272 int
273 main(int argc, char **argv)
274 {
275   int i, op;
276   char *arg = NULL;
277
278   log_init(NULL);
279   op = 0;
280   while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfFqsv", CF_NO_LONG_OPTS, NULL)) != -1)
281     if (i == '?' || op)
282       help();
283     else if (i == 'v')
284       verbose++;
285     else
286       {
287         op = i;
288         arg = optarg;
289       }
290   if (optind < argc)
291     help();
292
293   switch (op)
294     {
295     case 'l':
296       list(0);
297       break;
298     case 'L':
299       list(1);
300       break;
301     case 'd':
302       delete(arg);
303       break;
304     case 'x':
305       extract(arg);
306       break;
307     case 'i':
308       insert();
309       break;
310     case 'c':
311       cat();
312       break;
313     case 'f':
314       fsck(0);
315       break;
316     case 'F':
317       fsck(1);
318       break;
319     case 'q':
320       quickcheck();
321       break;
322     case 's':
323       shake();
324       break;
325     default:
326       help();
327     }
328
329   return 0;
330 }