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