]> mj.ucw.cz Git - libucw.git/blob - lib/buckettool.c
Due to a bug, the "fsck" mode was unable to fix broken trailers.
[libucw.git] / lib / buckettool.c
1 /*
2  *      Sherlock Library -- Bucket Manipulation Tool
3  *
4  *      (c) 2001 Martin Mares <mj@ucw.cz>
5  */
6
7 #include "lib/lib.h"
8 #include "lib/bucket.h"
9 #include "lib/fastbuf.h"
10 #include "lib/lfs.h"
11 #include "lib/conf.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <getopt.h>
16 #include <fcntl.h>
17 #include <unistd.h>
18
19 static int verbose;
20
21 static void
22 help(void)
23 {
24   fprintf(stderr, "\
25 Usage: buckettool [<options>] <command>\n\
26 \n\
27 Options:\n"
28 CF_USAGE
29 "\nCommands:\n\
30 -l\t\tlist all buckets\n\
31 -L\t\tlist all buckets including deleted ones\n\
32 -d <obj>\tdelete bucket\n\
33 -x <obj>\textract bucket\n\
34 -i\t\tinsert buckets separated by blank lines\n\
35 -c\t\tconcatenate and dump all buckets\n\
36 -f\t\taudit bucket file structure\n\
37 -F\t\taudit and fix bucket file structure\n\
38 -s\t\tshake down bucket file (without updating other structures!!!)\n\
39 -v\t\tbe verbose\n\
40 ");
41   exit(1);
42 }
43
44 static oid_t
45 parse_id(char *c)
46 {
47   char *e;
48   oid_t o = strtoul(c, &e, 16);
49   if (e && *e)
50     die("Invalid object ID: %s", c);
51   return o;
52 }
53
54 static void
55 list(int full)
56 {
57   struct obuck_header h;
58
59   obuck_init(0);
60   if (obuck_find_first(&h, full))
61     do
62       {
63         if (h.oid == OBUCK_OID_DELETED)
64           printf("DELETED  %6d\n", h.length);
65         else
66           printf("%08x %6d %6d\n", h.oid, h.length, h.orig_length);
67       }
68     while (obuck_find_next(&h, full));
69   obuck_cleanup();
70 }
71
72 static void
73 delete(char *id)
74 {
75   oid_t oid = parse_id(id);
76   obuck_init(1);
77   obuck_delete(oid);
78   obuck_cleanup();
79 }
80
81 static void
82 extract(char *id)
83 {
84   struct fastbuf *b;
85   byte buf[1024];
86   int l;
87   struct obuck_header h;
88
89   h.oid = parse_id(id);
90   obuck_init(0);
91   obuck_find_by_oid(&h);
92   b = obuck_fetch();
93   while ((l = bread(b, buf, sizeof(buf))))
94     fwrite(buf, 1, l, stdout);
95   obuck_fetch_end(b);
96   obuck_cleanup();
97 }
98
99 static void
100 insert(void)
101 {
102   struct fastbuf *b, *in;
103   byte buf[4096];
104   struct obuck_header h;
105   byte *e;
106
107   in = bfdopen(0, 4096);
108   obuck_init(1);
109   do
110     {
111       b = obuck_create();
112       while ((e = bgets(in, buf, sizeof(buf))) && buf[0])
113         {
114           *e++ = '\n';
115           bwrite(b, buf, e-buf);
116         }
117       obuck_create_end(b, &h);
118       printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
119     }
120   while (e);
121   obuck_cleanup();
122   /* bclose(in) not done, we don't want fd 0 closed */
123 }
124
125 static void
126 cat(void)
127 {
128   struct obuck_header h;
129   struct fastbuf *b;
130   byte buf[1024];
131   int l, lf;
132
133   obuck_init(0);
134   if (obuck_find_first(&h, 0))
135     do
136       {
137         printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length);
138         b = obuck_fetch();
139         lf = 1;
140         while ((l = bread(b, buf, sizeof(buf))))
141           {
142             fwrite(buf, 1, l, stdout);
143             lf = (buf[l-1] == '\n');
144           }
145         obuck_fetch_end(b);
146         if (!lf)
147           printf("\n# <missing EOL>\n");
148       }
149     while (obuck_find_next(&h, 0));
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 int
266 main(int argc, char **argv)
267 {
268   int i, op;
269   char *arg = NULL;
270
271   log_init(NULL);
272   op = 0;
273   while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfFsv", CF_NO_LONG_OPTS, NULL)) != -1)
274     if (i == '?' || op)
275       help();
276     else if (i == 'v')
277       verbose++;
278     else
279       {
280         op = i;
281         arg = optarg;
282       }
283   if (optind < argc)
284     help();
285
286   switch (op)
287     {
288     case 'l':
289       list(0);
290       break;
291     case 'L':
292       list(1);
293       break;
294     case 'd':
295       delete(arg);
296       break;
297     case 'x':
298       extract(arg);
299       break;
300     case 'i':
301       insert();
302       break;
303     case 'c':
304       cat();
305       break;
306     case 'f':
307       fsck(0);
308       break;
309     case 'F':
310       fsck(1);
311       break;
312     case 's':
313       shake();
314       break;
315     default:
316       help();
317     }
318
319   return 0;
320 }