X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=sidebyside;f=lib%2Fbuckettool.c;h=179e3d98ea67e10844be6dfe4b33a8f94ed7d844;hb=b42162f5526360acc6930e3d2e296af1fef08e63;hp=eaf376c76da98c22a95bcbee56541add479dfd2e;hpb=31d51567ca4e34c814c5c45b3058bbac6d778ab0;p=libucw.git diff --git a/lib/buckettool.c b/lib/buckettool.c index eaf376c7..179e3d98 100644 --- a/lib/buckettool.c +++ b/lib/buckettool.c @@ -2,6 +2,9 @@ * 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" @@ -31,10 +34,11 @@ CF_USAGE -L\t\tlist all buckets including deleted ones\n\ -d \tdelete bucket\n\ -x \textract bucket\n\ --i\t\tinsert buckets separated by blank lines\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\ "); @@ -63,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(); @@ -92,61 +96,70 @@ extract(char *id) b = obuck_fetch(); while ((l = bread(b, buf, sizeof(buf)))) fwrite(buf, 1, l, stdout); - obuck_fetch_end(b); + bclose(b); obuck_cleanup(); } static void -insert(void) +insert(byte *arg) { 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(0, 4096); + in = bfdopen_shared(0, 4096); obuck_init(1); do { - b = obuck_create(); + b = NULL; while ((e = bgets(in, buf, sizeof(buf))) && buf[0]) { *e++ = '\n'; + if (!b) + b = obuck_create(type); bwrite(b, buf, e-buf); } - obuck_create_end(b, &h); - printf("%08x %d %d\n", h.oid, h.length, h.orig_length); + if (b) + { + obuck_create_end(b, &h); + printf("%08x %d %08x\n", h.oid, h.length, h.type); + } } while (e); obuck_cleanup(); - /* bclose(in) not done, we don't want fd 0 closed */ + bclose(in); } static void cat(void) { struct obuck_header h; - struct fastbuf *b; + struct fastbuf *b, *out; byte buf[1024]; 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)); + out = bfdopen_shared(1, 65536); + while (b = obuck_slurp_pool(&h)) + { + bprintf(out, "### %08x %6d %08x\n", h.oid, h.length, h.type); + lf = 1; + while ((l = bread(b, buf, sizeof(buf)))) + { + bwrite(out, buf, l); + lf = (buf[l-1] == '\n'); + } + if (!lf) + bprintf(out, "\n# \n"); + } + bclose(out); obuck_cleanup(); } @@ -186,9 +199,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; @@ -221,12 +237,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: @@ -257,6 +275,13 @@ shake(void) obuck_cleanup(); } +static void +quickcheck(void) +{ + obuck_init(1); + obuck_cleanup(); +} + int main(int argc, char **argv) { @@ -265,7 +290,7 @@ main(int argc, char **argv) log_init(NULL); op = 0; - while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfFsv", CF_NO_LONG_OPTS, NULL)) != -1) + 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') @@ -293,7 +318,7 @@ main(int argc, char **argv) extract(arg); break; case 'i': - insert(); + insert(arg); break; case 'c': cat(); @@ -304,6 +329,9 @@ main(int argc, char **argv) case 'F': fsck(1); break; + case 'q': + quickcheck(); + break; case 's': shake(); break;