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