s!/etc/slackware-release!/etc/slackware-version! (reported by bubulak)
[util-vserver.git] / util-vserver / src / vhashify.c
index 08d86a7..5e37887 100644 (file)
@@ -28,8 +28,9 @@
 #include "lib_internal/unify.h"
 #include "ensc_vector/vector.h"
 
-#include <setjmp.h>
 #include <beecrypt/beecrypt.h>
+
+#include <setjmp.h>
 #include <unistd.h>
 #include <getopt.h>
 #include <string.h>
 #define ENSC_WRAPPERS_UNISTD    1
 #define ENSC_WRAPPERS_FCNTL     1
 #define ENSC_WRAPPERS_DIRENT    1
+#define ENSC_WRAPPERS_IO       1
 #include <wrappers.h>
 
 
-#define HASH_BLOCKSIZE         0x10000000
+#define HASH_BLOCKSIZE         0x10000000u
 #define HASH_MINSIZE           0x10
+#define HASH_MAXBITS           256             // we have to take care about
+                                               // max filename-length...
 
+#if HASH_MINSIZE<=0
+#  error HASH_MINSIZE must be not '0'
+#endif
 
 
 #define CMD_HELP               0x8000
@@ -62,6 +69,7 @@
 #define CMD_INSECURE           0x1001
 #define CMD_SLEDGE             0x1002
 #define CMD_MANUALLY           0x1003
+#define CMD_REFRESH            0x1004
 
 struct option const
 CMDLINE_OPTIONS[] = {
@@ -71,16 +79,29 @@ CMDLINE_OPTIONS[] = {
   { "insecure",     no_argument,               0, CMD_INSECURE },
   { "sledgehammer", no_argument,       0, CMD_SLEDGE },
   { "manually",     no_argument,       0, CMD_MANUALLY },
+  { "refresh",      no_argument,        0, CMD_REFRESH },
+  { "dry-run",      no_argument,       0, 'n' },
+  { "verbose",      no_argument,       0, 'v' },
   { 0,0,0,0 }
 };
 
-  // SHA1, grouped by 4 digits + hash-collision counter + 2* '/' + NULL
-typedef char                   HashPath[160/4 + (160/4/4) + sizeof(unsigned int)*2 + 3];
+  // hash digest grouped by 2 digits + hash-collision counter + 2* '/' + NULL
+typedef char                   HashPath[HASH_MAXBITS/4 + (HASH_MAXBITS/4/2) +
+                                        sizeof(unsigned int)*2 + 3];
+
+struct HashDirConfiguration
+{
+    hashFunction const                         *method;
+    enum { hshALL=0, hshSTART = 1, hshMIDDLE=2,
+          hshEND = 4, hshINVALID = -1 }        blocks;
+    size_t                                     blocksize;
+};
 
 struct WalkdownInfo
 {
     PathInfo                   state;
     struct MatchList           dst_list;
+    struct HashDirConfiguration        hash_conf;
     HashDirCollection          hash_dirs;
     size_t                     hash_dirs_max_size;
 
@@ -89,9 +110,14 @@ struct WalkdownInfo
 
 int                            wrapper_exit_code = 1;
 struct Arguments const         *global_args;
-struct WalkdownInfo            global_info;
 static struct SkipReason       skip_reason;
 
+struct WalkdownInfo            global_info = {
+  .hash_conf = { .method     = 0,
+                .blocks     = hshALL,
+                .blocksize  = 0x10000 }
+};
+
 #include "vhashify-init.hc"
 
 int Global_getVerbosity() {
@@ -102,19 +128,24 @@ int Global_doRenew() {
   return true;
 }
 
+int Global_isVserverRunning() {
+    // TODO
+  return global_args->insecure<2;
+}
+
 static void
 showHelp(char const *cmd)
 {
   WRITE_MSG(1, "Usage:\n  ");
   WRITE_STR(1, cmd);
   WRITE_MSG(1,
-           " [-Rnv] <vserver>\n    or\n  ");
+           " [-nv] [--refresh] <vserver>\n    or\n  ");
   WRITE_STR(1, cmd);
   WRITE_MSG(1,
-           " --manually [-Rnvx] [--] <hashdir> <path> <excludelist>\n\n"
+           " --manually [-nv] [--] <hashdir> <path> <excludelist>\n\n"
            "  --manually      ...  hashify generic paths; excludelists must be generated\n"
            "                       manually\n"
-           "  -R              ...  revert operation; dehashify files\n"
+           "  --refresh       ...  hashify already hashified files also\n"
            "  -n              ...  do not modify anything; just show what there will be\n"
            "                       done (in combination with '-v')\n"
            "  -v              ...  verbose mode\n"
@@ -186,22 +217,30 @@ checkFstat(PathInfo const * const basename,
   skip_reason.r = rsTOOSMALL;
   if (st->st_size < HASH_MINSIZE) return false;
   
-  skip_reason.r = rsUNIFIED;
-  if ((!global_args->do_revert && !(st->st_nlink==1 || Unify_isIUnlinkable(basename->d))) ||
-      ( global_args->do_revert &&                      Unify_isIUnlinkable(basename->d)))
-    return false;
+  switch (Unify_isIUnlinkable(basename->d)) {
+    case unifyUNSUPPORTED      :  skip_reason.r = rsUNSUPPORTED; return false;
+    case unifyBUSY             :
+       // do an implicit refresh on busy files when there are no active links
+      if (st->st_nlink>1 && !global_args->do_refresh) {
+         // TODO: message
+       skip_reason.r = rsUNIFIED;
+       return false;
+      }
+      break;
+    default                    :  break;
+  }
 
   return true;
 }
 
-static jmp_buf                 bus_error_restore;
+static sigjmp_buf              bus_error_restore;
 static volatile sig_atomic_t   bus_error;
 
 static void
 handlerSIGBUS(int UNUSED num)
 {
   bus_error = 1;
-  longjmp(bus_error_restore, 1);
+  siglongjmp(bus_error_restore, 1);
 }
 
 static bool
@@ -220,15 +259,17 @@ convertDigest(HashPath d_path)
   for (size_t in=0;
        out+1<sizeof(HashPath)-(sizeof(unsigned int)*2 + 2) && in<d_size;
        ++in) {
-    if (in%2 == 0 && in>0) d_path[out++]='/';
-    d_path[out++] = HEX_DIGIT[digest[in] >>    4];
-    d_path[out++] = HEX_DIGIT[digest[in] &  0x0f];
+    if ((in+254)%(in<=2 ? 1 : 256) == 0 && in>0)
+      d_path[out++]='/';
+    d_path[out++]  = HEX_DIGIT[digest[in] >>    4];
+    d_path[out++]  = HEX_DIGIT[digest[in] &  0x0f];
   }
   d_path[out++] = '\0';
   
   return true;
 }
 
+#ifndef ENSC_TESTSUITE
 static bool
 addStatHash(hashFunctionContext *h_ctx, struct stat const * const st)
 {
@@ -251,18 +292,27 @@ addStatHash(hashFunctionContext *h_ctx, struct stat const * const st)
     SET_ATTR(mtime)
   };
 
+#undef SET_ATTR
+#undef DECL_ATTR
+
+  
   return hashFunctionContextUpdate(h_ctx, (void *)&tmp, sizeof tmp)!=-1;
 }
-
+#else
+static bool
+addStatHash(hashFunctionContext UNUSED *h_ctx, struct stat const UNUSED * const st)
+{
+  return true;
+}
+#endif
+  
 static bool
 calculateHashFromFD(int fd, HashPath d_path, struct stat const * const st)
 {
   hashFunctionContext * const  h_ctx    = &global_info.hash_context;
-  bool                         res      = false;
-  loff_t                       offset   = 0;
-  void                         *buf     = 0;
-  off_t                                size     = st->st_size;
-  loff_t                       cur_size = 0;
+  void const * volatile                buf      = 0;
+  loff_t volatile              buf_size = 0;
+  bool   volatile              res      = false;
 
 
   if (hashFunctionContextReset(h_ctx)==-1 ||
@@ -270,36 +320,37 @@ calculateHashFromFD(int fd, HashPath d_path, struct stat const * const st)
     return false;
 
   bus_error = 0;
-  if (setjmp(bus_error_restore)!=0) goto out;
+  if (sigsetjmp(bus_error_restore,1)==0) {
+    loff_t                     offset   = 0;
+    off_t                      size     = st->st_size;
 
-  while (offset < size) {
-    size_t     real_size = size-offset;
-    cur_size = real_size;
-      //cur_size = (real_size + PAGESIZE-1)/PAGESIZE * PAGESIZE;
-    if (cur_size>HASH_BLOCKSIZE) cur_size = HASH_BLOCKSIZE;
+    while (offset < size) {
+      buf_size = size-offset;
+      if (buf_size>HASH_BLOCKSIZE) buf_size = HASH_BLOCKSIZE;
 
-    buf     = mmap(0, cur_size, PROT_READ, MAP_SHARED, fd, offset);
-    offset += real_size;
+      if ((buf=mmap(0, buf_size, PROT_READ, MAP_SHARED, fd, offset))==0) {
+       perror("mmap(<hash>)");
+       goto out;
+      }
 
-    madvise(buf, cur_size, MADV_SEQUENTIAL);   // ignore error...
+      offset += buf_size;
+      madvise(const_cast(void *)(buf), buf_size, MADV_SEQUENTIAL);     // ignore error...
 
-    if (buf==0) goto out;
-    if (hashFunctionContextUpdate(h_ctx, buf, real_size)==-1) goto out;
+      if (hashFunctionContextUpdate(h_ctx, buf, buf_size)==-1) goto out;
 
-    munmap(buf, cur_size);
-    buf = 0;
-  }
+      munmap(const_cast(void *)(buf), buf_size);
+      buf = 0;
+    }
 
-  if (!convertDigest(d_path)) goto out;
-    
-  res = true;
+    res = convertDigest(d_path);
+  }
 
   out:
-  if (buf!=0) munmap(buf, cur_size);
+  if (buf!=0) munmap(const_cast(void *)(buf), buf_size);
   return res;
 }
 
-bool
+static bool
 calculateHash(PathInfo const *filename, HashPath d_path, struct stat const * const st)
 {
   int          fd  = open(filename->d, O_NOFOLLOW|O_NONBLOCK|O_RDONLY|O_NOCTTY);
@@ -338,38 +389,72 @@ calculateHash(PathInfo const *filename, HashPath d_path, struct stat const * con
   return res;
 }
 
+static enum { mkdirFAIL, mkdirSUCCESS, mkdirSKIP }
+mkdirSingle(char const *path, char *end_ptr, int good_err)
+{
+  *end_ptr = '\0';
+  if (mkdir(path, 0700)!=-1 || errno==EEXIST) {
+    *end_ptr = '/';
+    return mkdirSUCCESS;
+  }
+  else if (errno==good_err) {
+    *end_ptr = '/';
+    return mkdirSKIP;
+  }
+  else {
+    int                old_errno = errno;
+    WRITE_MSG(2, "mkdir('");
+    WRITE_STR(2, path);
+    errno = old_errno;
+    perror("')");
+    return mkdirFAIL;
+  }
+}
+
+static char *
+rstrchr(char *str, char c)
+{
+  while (*str!=c) --str;
+  return str;
+}
+
 static bool
 mkdirRecursive(char const *path)
 {
-  struct stat          st;
-
-  if (path[0]!='/')       return false; // only absolute paths
-  if (lstat(path,&st)!=-1) return true;
+  if (path[0]!='/')      return false; // only absolute paths
 
   char                 buf[strlen(path)+1];
-  char *               ptr = buf+1;
-  
+  char *               ptr = buf + sizeof(buf) - 2;
+
   strcpy(buf, path);
 
-  while ((ptr = strchr(ptr, '/'))!=0) {
-    *ptr = '\0';
-    if (mkdir(buf, 0700)==-1 && errno!=EEXIST) {
-      int              old_errno = errno;
-      WRITE_MSG(2, "mkdir('");
-      WRITE_STR(2, buf);
-      errno = old_errno;
-      perror("')");
-      return false;
+  while (ptr>buf && (ptr = rstrchr(ptr, '/'))!=0) {
+    switch (mkdirSingle(buf, ptr, ENOENT)) {
+      case mkdirSUCCESS                :  break;
+      case mkdirSKIP           :  --ptr; continue;
+      case mkdirFAIL           :  return false;
+    }
+
+    break;     // implied by mkdirSUCCESS
+  }
+
+  assert(ptr!=0);
+  ++ptr;
+
+  while ((ptr=strchr(ptr, '/'))!=0) {
+    switch (mkdirSingle(buf, ptr, 0)) {
+      case mkdirSKIP           :
+      case mkdirFAIL           :  return false;
+      case mkdirSUCCESS                :  ++ptr; continue;
     }
-    *ptr = '/';
-    ++ptr;
   }
 
   return true;
 }
 
 static bool
-resolveCollisions(char *result, PathInfo const *root, HashPath d_path, struct stat *st)
+resolveCollisions(char *result, PathInfo const *root, HashPath d_path,
+                 struct stat *st, struct stat *hash_st)
 {
   strcpy(result, root->d);     // 'root' ends on '/' already (see initHashList())
   strcat(result, d_path);
@@ -379,21 +464,16 @@ resolveCollisions(char *result, PathInfo const *root, HashPath d_path, struct st
   char                 buf[sizeof(int)*2 + 1];
   size_t               len;
 
-  *ptr++             = '/';
-  *ptr               = '\0';
-  ptr[sizeof(int)*2] = '\0';
-
-  if (!mkdirRecursive(result))
-    return false;
+  *ptr                 = '-';
+  ptr[sizeof(int)*2+1] = '\0';
 
   for (;; ++idx) {
     len = utilvserver_fmt_xuint(buf, idx);
-    memset(ptr, '0', sizeof(int)*2 - len);
-    memcpy(ptr + sizeof(int)*2 - len, buf, len);
+    memset(ptr+1, '0', sizeof(int)*2 - len);
+    memcpy(ptr+1 + sizeof(int)*2 - len, buf, len);
 
-    struct stat                new_st;
-    if (lstat(result, &new_st)==-1) {
-      if (errno!=ENOENT) {
+    if (lstat(result, hash_st)==-1) {
+      if (global_args->dry_run && errno!=ENOENT) {
        int             old_errno = errno;
        WRITE_MSG(2, "lstat('");
        WRITE_STR(2, buf);
@@ -402,23 +482,37 @@ resolveCollisions(char *result, PathInfo const *root, HashPath d_path, struct st
        return false;
       }
     }
-    else if (!Unify_isUnifyable(st, &new_st))
-      continue;                // continue with next number
+    else if (Unify_isUnified(st, hash_st)) {
+      skip_reason.r = rsUNIFIED;
+      return false;
+    }
+    else if (!Unify_isUnifyable(st, hash_st))
+      continue;                // continue with next number*****
     else
       break;           // ok, we finish here
 
-    int                fd = open(result, O_NOFOLLOW|O_EXCL|O_CREAT|O_WRONLY, 0200);
+    if (!global_args->dry_run) {
+      *ptr = '\0';
+      if (!mkdirRecursive(result))
+       return false;
+      *ptr = '-';
 
-    if (fd==-1) {
-      int              old_errno = errno;
-      WRITE_MSG(2, "open('");
-      WRITE_STR(2, buf);
-      errno = old_errno;
-      perror("')");
-      return false;
+      int              fd = open(result, O_NOFOLLOW|O_EXCL|O_CREAT|O_WRONLY, 0200);
+
+      if (fd==-1) {
+       int             old_errno = errno;
+       WRITE_MSG(2, "open('");
+       WRITE_STR(2, buf);
+       errno = old_errno;
+       perror("')");
+       return false;
+      }
+
+      close(fd);
     }
 
-    close(fd);
+      // HACK: avoid an additional lstat on the resulting hash-file
+    hash_st->st_size = 0;
     break;
   }
 
@@ -427,7 +521,9 @@ resolveCollisions(char *result, PathInfo const *root, HashPath d_path, struct st
 
 static char const *
 checkDirEntry(PathInfo const *path, PathInfo const *basename,
-             bool *is_dir, struct stat *st, char *result_buf)
+             bool *is_dir,
+             struct stat *st, struct stat *hash_st,
+             char *result_buf)
 {
     //printf("checkDirEntry(%s, %s, %u)\n", path->d, d_path, is_dir);
 
@@ -445,9 +541,11 @@ checkDirEntry(PathInfo const *path, PathInfo const *basename,
     *is_dir = S_ISDIR(st->st_mode);
 
     if (!*is_dir &&
-       !((hash_root_path = HashDirInfo_findDevice(&info->hash_dirs, st->st_dev))!=0 &&
-         calculateHash(basename, d_path, st) &&
-         resolveCollisions(result_buf, hash_root_path, d_path, st)))
+       !((skip_reason.r = rsWRONGDEV,
+          (hash_root_path = HashDirInfo_findDevice(&info->hash_dirs, st->st_dev))!=0) &&
+         (skip_reason.r = rsGENERAL,
+          calculateHash(basename, d_path, st)) &&
+         resolveCollisions(result_buf, hash_root_path, d_path, st, hash_st)))
       return 0;
 
     return result_buf;
@@ -456,6 +554,70 @@ checkDirEntry(PathInfo const *path, PathInfo const *basename,
   return 0;
 }
 
+static void
+printSkipReason()
+{
+  WRITE_MSG(1, " (");
+  switch (skip_reason.r) {
+    case rsDOTFILE     :  WRITE_MSG(1, "dotfile"); break;
+    case rsEXCL                :  WRITE_MSG(1, "excluded"); break;
+    case rsTOOSMALL    :  WRITE_MSG(1, "too small"); break;
+    case rsUNSUPPORTED :  WRITE_MSG(1, "operation not supported"); break;
+    case rsFSTAT       :  WRITE_MSG(1, "fstat error"); break;
+    case rsSYMLINK     :  WRITE_MSG(1, "symlink"); break;
+    case rsUNIFIED     :  WRITE_MSG(1, "already unified"); break;
+    case rsSPECIAL     :  WRITE_MSG(1, "non regular file"); break;
+    case rsWRONGDEV    :  WRITE_MSG(1, "no matching device"); break;
+    case rsGENERAL     :  WRITE_MSG(1, "general error"); break;
+    default            :  assert(false); abort();
+  }
+  WRITE_MSG(1, ")");
+}
+
+static bool
+doit(char const *src, char const *dst,
+     struct stat const *src_st, struct stat const *dst_st,
+     PathInfo const *path)
+{
+  if (global_args->dry_run || Global_getVerbosity()>=2) {
+    WRITE_MSG(1, "unifying   '");
+    Vwrite(1, path->d, path->l);
+    WRITE_MSG(1, "'");
+    
+    if (Global_getVerbosity()>=4) {
+      WRITE_MSG(1, " (to '");
+      WRITE_STR(1, dst);
+      WRITE_MSG(1, "')");
+    }
+
+    WRITE_MSG(1, "\n");
+  }
+
+    // abort here in dry-run mode
+  if (global_args->dry_run) return true;
+
+  if (dst_st->st_size==0) {
+      // file was not unified yet
+    
+    if (Global_isVserverRunning()) {
+      (void)unlink(dst);
+      if (Unify_copy (src, src_st, dst) &&
+         // the mixed 'dst' and 'src_st' params are intentionally...
+         Unify_unify(dst, src_st, src, false))
+       return true;
+    }
+    else if (Unify_unify(src, src_st, dst, true))
+      return true;
+
+    (void)unlink(dst); // cleanup in error-case
+  }
+    // there exists already a reference-file
+  else if (Unify_unify(dst, dst_st, src, false))
+    return true;
+
+  return false;
+}
+
 static uint64_t
 visitDirEntry(struct dirent const *ent)
 {
@@ -474,7 +636,8 @@ visitDirEntry(struct dirent const *ent)
 
   bool                         is_dotfile    = isDotfile(dirname);
   bool                         is_dir;
-  struct stat                  src_stat;
+  struct stat                  src_stat = { .st_mode=0 };
+  struct stat                  hash_stat;
   char                         tmpbuf[global_info.hash_dirs_max_size +
                                       sizeof(HashPath) + 2];
   
@@ -482,7 +645,22 @@ visitDirEntry(struct dirent const *ent)
 
   if (is_dotfile ||
       (match=checkDirEntry(&path, &tmp_path,
-                          &is_dir, &src_stat, tmpbuf))==0) {
+                          &is_dir, &src_stat, &hash_stat,
+                          tmpbuf))==0) {
+
+    bool       is_link = !is_dotfile && S_ISLNK(src_stat.st_mode);
+
+    if (Global_getVerbosity()>=1 &&
+       (Global_getVerbosity()>=3 || skip_reason.r!=rsUNIFIED) &&
+       ((!is_dotfile && !is_link) ||
+        (Global_getVerbosity()>=6 && is_dotfile) ||
+        (Global_getVerbosity()>=6 && is_link)) ) {
+      WRITE_MSG(1, "  skipping '");
+      Vwrite(1, path.d, path.l);
+      WRITE_MSG(1, "'");
+      if (Global_getVerbosity()>=2) printSkipReason();
+      WRITE_MSG(1, "\n");
+    }
 
     return 0;
   }
@@ -490,8 +668,10 @@ visitDirEntry(struct dirent const *ent)
   if (is_dir) {
     res = visitDir(dirname, &src_stat);
   }
+  else if (doit(dirname, match, &src_stat, &hash_stat, &path))
+    res = 1;
   else {
-    printf("%s <- %s\n", match, path.d);
+      // TODO: message
     res = 0;
   }
 
@@ -506,18 +686,15 @@ int main(int argc, char *argv[])
     .hash_dir           =  0,
     .verbosity         =  0,
     .insecure           =  0,
+    .dry_run            =  false,
+    .do_refresh         =  false,
   };
 
   Vector_init(&global_info.hash_dirs, sizeof(struct HashDirInfo));
 
-  if (hashFunctionContextInit(&global_info.hash_context,
-                             hashFunctionDefault())==-1)
-    return EXIT_FAILURE;
-
-
   global_args = &args;
   while (1) {
-    int                c = getopt_long(argc, argv, "",
+    int                c = getopt_long(argc, argv, "+nv",
                                CMDLINE_OPTIONS, 0);
     if (c==-1) break;
 
@@ -526,8 +703,10 @@ int main(int argc, char *argv[])
       case CMD_VERSION         :  showVersion();
       case CMD_DESTINATION     :  args.hash_dir    = optarg; break;
       case CMD_MANUALLY                :  args.mode        = mdMANUALLY; break;
-      case CMD_INSECURE                :  args.insecure    = 1; break;
-      case CMD_SLEDGE          :  args.insecure    = 2; break;
+      case CMD_INSECURE                :  args.insecure    = 1;    break;
+      case CMD_SLEDGE          :  args.insecure    = 2;    break;
+      case CMD_REFRESH         :  args.do_refresh  = true; break;
+      case 'n'                 :  args.dry_run     = true; break;
       case 'v'                 :  ++args.verbosity; break;
       default          :
        WRITE_MSG(2, "Try '");
@@ -554,11 +733,25 @@ int main(int argc, char *argv[])
     default            :  assert(false); return EXIT_FAILURE;
   };
 
+  if (hashFunctionContextInit(&global_info.hash_context,
+                             global_info.hash_conf.method)==-1) {
+    WRITE_MSG(2, "Failed to initialize hash-context\n");
+    return EXIT_FAILURE;
+  }
+
   if (Global_getVerbosity()>=1)
     WRITE_MSG(1, "Starting to traverse directories...\n");
 
   signal(SIGBUS, handlerSIGBUS);
   
   Echdir(global_info.dst_list.root.d);
-  visitDir("/", 0);  
+  visitDir("/", 0);
+
+#ifndef NDEBUG
+  MatchList_destroy(&global_info.dst_list);
+  freeHashList(&global_info.hash_dirs);
+  hashFunctionContextFree(&global_info.hash_context);
+#endif
+
+  return EXIT_SUCCESS;
 }