]> mj.ucw.cz Git - libucw.git/blobdiff - lib/buckettool.c
Introduced SKIP_TAGGED_CHAR.
[libucw.git] / lib / buckettool.c
index c93aaf51d2f3c75c053b65f613f2a44dce2ad2f7..eaf376c76da98c22a95bcbee56541add479dfd2e 100644 (file)
@@ -8,6 +8,7 @@
 #include "lib/bucket.h"
 #include "lib/fastbuf.h"
 #include "lib/lfs.h"
+#include "lib/conf.h"
 
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
 #include <unistd.h>
 
+static int verbose;
+
 static void
 help(void)
 {
   fprintf(stderr, "\
-Usage: buckettool <commands>\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\
+-s\t\tshake down bucket file (without updating other structures!!!)\n\
+-v\t\tbe verbose\n\
 ");
   exit(1);
 }
@@ -92,18 +99,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(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) not done, we don't want fd 0 closed */
 }
 
 static void
@@ -143,8 +159,10 @@ fsck(int fix)
   sh_off_t end;
   oid_t oid;
   u32 chk;
+  int errors = 0;
+  int fatal_errors = 0;
 
-  fd = open(obuck_name, O_RDWR);
+  fd = sh_open(obuck_name, O_RDWR);
   if (fd < 0)
     die("Unable to open the bucket file %s: %m", obuck_name);
   for(;;)
@@ -172,12 +190,14 @@ fsck(int fix)
          pos = end;
          continue;
        }
+      errors++;
       end = pos;
       do
        {
          if (pos - end > 0x10000000)
            {
              printf("*** skipped for too long, giving up\n");
+             fatal_errors++;
              goto finish;
            }
          end += OBUCK_ALIGN;
@@ -187,7 +207,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");
@@ -196,7 +216,7 @@ fsck(int fix)
        }
       while (nh.magic != OBUCK_MAGIC ||
             (nh.oid != (oid_t)(end >> OBUCK_SHIFT) && nh.oid != OBUCK_OID_DELETED));
-      printf("*** match at oid %08x\n", end >> OBUCK_SHIFT);
+      printf("*** match at oid %08x\n", (uns)(end >> OBUCK_SHIFT));
       if (fix)
        {
          h.magic = OBUCK_MAGIC;
@@ -205,60 +225,91 @@ fsck(int fix)
          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", end - pos);
+         printf("*** replaced the invalid chunk by a DELETED bucket of size %d\n", (uns)(end - pos));
        }
       pos = end;
     }
  finish:
   close(fd);
+  if (!fix && errors || fatal_errors)
+    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();
 }
 
 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:icfFsv", 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();
+      break;
+    case 'c':
+      cat();
+      break;
+    case 'f':
+      fsck(0);
+      break;
+    case 'F':
+      fsck(1);
+      break;
+    case 's':
+      shake();
+      break;
+    default:
+      help();
+    }
+
   return 0;
 }