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