]> mj.ucw.cz Git - libucw.git/blobdiff - lib/buckettool.c
When writing, the data needn't start at the beginning of the buffer.
[libucw.git] / lib / buckettool.c
index c46cf425023a9e724352306459ff5381dc41aa22..ae55b3ca4d4f92c784c25bae194ac3c0fae308fd 100644 (file)
@@ -2,6 +2,9 @@
  *     Sherlock Library -- Bucket Manipulation Tool
  *
  *     (c) 2001 Martin Mares <mj@ucw.cz>
+ *
+ *     This software may be freely distributed and used according to the terms
+ *     of the GNU Lesser General Public License.
  */
 
 #include "lib/lib.h"
 #include <fcntl.h>
 #include <unistd.h>
 
+static int verbose;
+
 static void
 help(void)
 {
   fprintf(stderr, "\
-Usage: buckettool <command>\n\
+Usage: buckettool [<options>] <command>\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 <obj>\tdelete bucket\n\
 -x <obj>\textract bucket\n\
--i\t\tinsert bucket\n\
+-i\t\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);
 }
@@ -93,18 +103,27 @@ extract(char *id)
 static void
 insert(void)
 {
-  struct fastbuf *b;
-  byte buf[1024];
-  int l;
+  struct fastbuf *b, *in;
+  byte buf[4096];
   struct obuck_header h;
+  byte *e;
 
+  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();
+      while ((e = bgets(in, buf, sizeof(buf))) && buf[0])
+       {
+         *e++ = '\n';
+         bwrite(b, buf, e-buf);
+       }
+      obuck_create_end(b, &h);
+      printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
+    }
+  while (e);
   obuck_cleanup();
-  printf("%08x %d %d\n", h.oid, h.length, h.orig_length);
+  bclose(in);
 }
 
 static void
@@ -171,9 +190,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;
@@ -192,7 +214,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");
@@ -212,6 +234,8 @@ fsck(int fix)
          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:
@@ -220,6 +244,35 @@ 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)
 {
@@ -228,9 +281,11 @@ main(int argc, char **argv)
 
   log_init(NULL);
   op = 0;
-  while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfF", CF_NO_LONG_OPTS, NULL)) != -1)
+  while ((i = cf_getopt(argc, argv, CF_SHORT_OPTS "lLd:x:icfFqsv", CF_NO_LONG_OPTS, NULL)) != -1)
     if (i == '?' || op)
       help();
+    else if (i == 'v')
+      verbose++;
     else
       {
        op = i;
@@ -265,6 +320,12 @@ main(int argc, char **argv)
     case 'F':
       fsck(1);
       break;
+    case 'q':
+      quickcheck();
+      break;
+    case 's':
+      shake();
+      break;
     default:
       help();
     }