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