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