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