X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fbuckettool.c;h=c911e5ce694937f5e111e56354bd4314a0b5bf1e;hb=a0de81cbe1f6282fdc13f6e61d07ebf0ae1c0f4c;hp=f540f24ca31b19f540bcebe70523734f219d8044;hpb=8d62665f2a8d2a102f75017897cbc4f72c69d8fc;p=libucw.git diff --git a/lib/buckettool.c b/lib/buckettool.c index f540f24c..c911e5ce 100644 --- a/lib/buckettool.c +++ b/lib/buckettool.c @@ -2,12 +2,16 @@ * Sherlock Library -- Bucket Manipulation Tool * * (c) 2001 Martin Mares + * + * This software may be freely distributed and used according to the terms + * of the GNU Lesser General Public License. */ #include "lib/lib.h" #include "lib/bucket.h" #include "lib/fastbuf.h" #include "lib/lfs.h" +#include "lib/conf.h" #include #include @@ -15,21 +19,28 @@ #include #include +static int verbose; + static void help(void) { fprintf(stderr, "\ -Usage: buckettool \n\ +Usage: buckettool [] \n\ \n\ -Commands:\n\ +Options:\n" +CF_USAGE +"\nCommands:\n\ -l\t\tlist all buckets\n\ -L\t\tlist all buckets including deleted ones\n\ -d \tdelete bucket\n\ -x \textract bucket\n\ --i\t\tinsert bucket\n\ +-i[]\tinsert buckets separated by blank lines\n\ -c\t\tconcatenate and dump all buckets\n\ -f\t\taudit bucket file structure\n\ -F\t\taudit and fix bucket file structure\n\ +-q\t\tquick check of bucket file consistency\n\ +-s\t\tshake down bucket file (without updating other structures!!!)\n\ +-v\t\tbe verbose\n\ "); exit(1); } @@ -56,7 +67,7 @@ list(int full) if (h.oid == OBUCK_OID_DELETED) printf("DELETED %6d\n", h.length); else - printf("%08x %6d %6d\n", h.oid, h.length, h.orig_length); + printf("%08x %6d %08x\n", h.oid, h.length, h.type); } while (obuck_find_next(&h, full)); obuck_cleanup(); @@ -90,20 +101,35 @@ extract(char *id) } static void -insert(void) +insert(byte *arg) { - struct fastbuf *b; - byte buf[1024]; - int l; + struct fastbuf *b, *in; + byte buf[4096]; struct obuck_header h; + byte *e; + u32 type; + + if (!arg) + type = BUCKET_TYPE_PLAIN; + else if (sscanf(arg, "%x", &type) != 1) + die("Type `%s' is not a hexadecimal number"); + in = bfdopen_shared(0, 4096); obuck_init(1); - b = obuck_create(); - while ((l = fread(buf, 1, sizeof(buf), stdin))) - bwrite(b, buf, l); - obuck_create_end(b, &h); + do + { + b = obuck_create(type); + while ((e = bgets(in, buf, sizeof(buf))) && buf[0]) + { + *e++ = '\n'; + bwrite(b, buf, e-buf); + } + obuck_create_end(b, &h); + printf("%08x %d %08x\n", h.oid, h.length, h.type); + } + while (e); obuck_cleanup(); - printf("%08x %d %d\n", h.oid, h.length, h.orig_length); + bclose(in); } static void @@ -115,22 +141,18 @@ cat(void) int l, lf; obuck_init(0); - if (obuck_find_first(&h, 0)) - do - { - printf("### %08x %6d %6d\n", h.oid, h.length, h.orig_length); - b = obuck_fetch(); - lf = 1; - while ((l = bread(b, buf, sizeof(buf)))) - { - fwrite(buf, 1, l, stdout); - lf = (buf[l-1] == '\n'); - } - obuck_fetch_end(b); - if (!lf) - printf("\n# \n"); - } - while (obuck_find_next(&h, 0)); + while (b = obuck_slurp_pool(&h)) + { + printf("### %08x %6d %08x\n", h.oid, h.length, h.type); + lf = 1; + while ((l = bread(b, buf, sizeof(buf)))) + { + fwrite(buf, 1, l, stdout); + lf = (buf[l-1] == '\n'); + } + if (!lf) + printf("\n# \n"); + } obuck_cleanup(); } @@ -170,9 +192,12 @@ fsck(int fix) printf("%08x missing trailer\n", oid); else if (chk != OBUCK_TRAILER) printf("%08x mismatched trailer\n", oid); - /* OK */ - pos = end; - continue; + else + { + /* OK */ + pos = end; + continue; + } } errors++; end = pos; @@ -191,7 +216,7 @@ fsck(int fix) if (fix) { printf("*** truncating file\n"); - ftruncate(fd, pos); + sh_ftruncate(fd, pos); } else printf("*** would truncate the file here\n"); @@ -205,12 +230,14 @@ fsck(int fix) { h.magic = OBUCK_MAGIC; h.oid = OBUCK_OID_DELETED; - h.length = h.orig_length = end - pos - sizeof(h) - 4; + h.length = end - pos - sizeof(h) - 4; sh_pwrite(fd, &h, sizeof(h), pos); chk = OBUCK_TRAILER; sh_pwrite(fd, &chk, 4, end-4); printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos)); } + else + printf("*** would mark %d bytes as DELETED\n", (uns)(end - pos)); pos = end; } finish: @@ -219,52 +246,91 @@ fsck(int fix) exit(1); } +static int +shake_kibitz(struct obuck_header *old, oid_t new, byte *buck UNUSED) +{ + if (verbose) + { + printf("%08x -> ", old->oid); + if (new == OBUCK_OID_DELETED) + puts("DELETED"); + else + printf("%08x\n", new); + } + return 1; +} + +static void +shake(void) +{ + obuck_init(1); + obuck_shakedown(shake_kibitz); + obuck_cleanup(); +} + +static void +quickcheck(void) +{ + obuck_init(1); + obuck_cleanup(); +} + int main(int argc, char **argv) { int i, op; + char *arg = NULL; + log_init(NULL); op = 0; - while ((i = getopt(argc, argv, "lLd:x:icfF")) != -1) - switch (i) + while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:i::cfFqsv", CF_NO_LONG_OPTS, NULL)) != -1) + if (i == '?' || op) + help(); + else if (i == 'v') + verbose++; + else { - case 'l': - list(0); - op++; - break; - case 'L': - list(1); - op++; - break; - case 'd': - delete(optarg); - op++; - break; - case 'x': - extract(optarg); - op++; - break; - case 'i': - insert(); - op++; - break; - case 'c': - cat(); - op++; - break; - case 'f': - fsck(0); - op++; - break; - case 'F': - fsck(1); - op++; - break; - default: - help(); + op = i; + arg = optarg; } - if (optind < argc || !op) + if (optind < argc) help(); + switch (op) + { + case 'l': + list(0); + break; + case 'L': + list(1); + break; + case 'd': + delete(arg); + break; + case 'x': + extract(arg); + break; + case 'i': + insert(arg); + break; + case 'c': + cat(); + break; + case 'f': + fsck(0); + break; + case 'F': + fsck(1); + break; + case 'q': + quickcheck(); + break; + case 's': + shake(); + break; + default: + help(); + } + return 0; }