use $(LIBENSCVECTOR) instead of libensc_vector.a
[util-vserver.git] / util-vserver / src / vunify.c
index cf01597..0c98af4 100644 (file)
 #endif
 
 #include "vunify.h"
-#include "wrappers-dirent.h"
 #include "util.h"
 
+#include "lib_internal/unify.h"
+#include "lib_internal/matchlist.h"
+#include "lib_internal/util-dotfile.h"
+#include "lib_internal/util-safechdir.h"
 #include <lib/vserver.h>
 
 #include <getopt.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <stdbool.h>
+#include <errno.h>
+#include <wait.h>
+#include <fcntl.h>
+#include <assert.h>
+
+#define ENSC_WRAPPERS_IO       1
+#define ENSC_WRAPPERS_FCNTL    1
+#define ENSC_WRAPPERS_DIRENT   1
+#define ENSC_WRAPPERS_UNISTD   1
+#define ENSC_WRAPPERS_STDLIB   1
+#include <wrappers.h>
 
 int    wrapper_exit_code = 1;
 
@@ -52,6 +66,14 @@ static struct WalkdownInfo           global_info;
 static struct SkipReason               skip_reason;
 static struct Arguments const *                global_args;
 
+int Global_getVerbosity() {
+  return global_args->verbosity;
+}
+
+bool Global_doRenew() {
+  return global_args->do_renew;
+}
+
 static void
 showHelp(int fd, char const *cmd, int res)
 {
@@ -69,7 +91,7 @@ showHelp(int fd, char const *cmd, int res)
            "                       done (in combination with '-v')\n"
            "  -v              ...  verbose mode\n"
            "  -x              ...  do not cross filesystems; this is valid in manual\n"
-           "                       mode only and will be ignored for vserver unification\n"
+           "                       mode only and will be ignored for vserver unification\n\n"
            "Please report bugs to " PACKAGE_BUGREPORT "\n");
 #if 0      
            "  -C              ...  use cached excludelists; usually they will be\n"
@@ -90,8 +112,6 @@ showVersion()
   exit(0);
 }
 
-#include "vunify-compare.ic"
-
 // Returns 'false' iff one of the files is not existing, or of the files are different/not unifyable
 static bool
 checkFstat(struct MatchList const * const mlist,
@@ -111,26 +131,25 @@ checkFstat(struct MatchList const * const mlist,
   }
 
   assert(*dst_fstat!=0);
+
   
-  {
-    PathInfo           src_path = mlist->root;
-    char               src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
+  PathInfo     src_path = mlist->root;
+  char         src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
 
-    PathInfo_append(&src_path, path, src_path_buf);
+  PathInfo_append(&src_path, path, src_path_buf);
 
     // source file does not exist
-    skip_reason.r = rsNOEXISTS;
-    if (lstat(src_path.d, src_fstat)==-1) return false;
+  skip_reason.r = rsNOEXISTS;
+  if (lstat(src_path.d, src_fstat)==-1) return false;
 
     // these are directories; this succeeds everytime
-    if (S_ISDIR((*dst_fstat)->st_mode) && S_ISDIR(src_fstat->st_mode)) return true;
+  if (S_ISDIR((*dst_fstat)->st_mode) && S_ISDIR(src_fstat->st_mode)) return true;
 
     // both files are different, so return false
-    skip_reason.r = rsDIFFERENT;
-    if ((!global_args->do_revert && !compareUnify  (*dst_fstat, src_fstat)) ||
-       ( global_args->do_revert && !compareDeUnify(*dst_fstat, src_fstat)))
-      return false;
-  }
+  skip_reason.r = rsDIFFERENT;
+  if ((!global_args->do_revert && !Unify_isUnifyable(*dst_fstat, src_fstat)) ||
+      ( global_args->do_revert && !Unify_isUnified  (*dst_fstat, src_fstat)))
+    return false;
 
   // these are the same files
   return true;
@@ -149,7 +168,7 @@ checkDirEntry(PathInfo const *path,
   // abort when it is not matching an allowed entry
   skip_reason.r      = rsEXCL_DST;
   skip_reason.d.list = &info->dst_list;
-  if (!MatchList_compare(&info->dst_list, path->d)) return 0;
+  if (MatchList_compare(&info->dst_list, path->d)!=stINCLUDE) return 0;
 
   // Now, go through the reference vservers and do the lightweigt list-check
   // first and compare then the fstat's.
@@ -157,7 +176,7 @@ checkDirEntry(PathInfo const *path,
     cache_stat = 0;
     skip_reason.r      = rsEXCL_SRC;
     skip_reason.d.list = mlist;
-    if (MatchList_compare(mlist, path->d) &&
+    if (MatchList_compare(mlist, path->d)==stINCLUDE &&
        checkFstat(mlist, d_path, path, &cache_stat, dst_stat, src_stat)) {
 
       // Failed the check or is it a symlink which can not be handled
@@ -175,7 +194,7 @@ checkDirEntry(PathInfo const *path,
     }
     else if (cache_stat!=0 && !global_args->do_revert &&
             skip_reason.r == rsDIFFERENT &&
-            compareDeUnify(cache_stat, src_stat)) {
+            Unify_isUnified(cache_stat, src_stat)) {
       skip_reason.r      = rsUNIFIED;
       skip_reason.d.list = mlist;
       return 0;
@@ -220,35 +239,27 @@ updateSkipDepth(PathInfo const *path, bool walk_down)
   return result;
 }
 
-inline static void
-EsafeChdir(char const *path, struct stat const *exp_stat)
-{
-  FatalErrnoError(safeChdir(path, exp_stat)==-1, "safeChdir()");
-}
-
-#include "vunify-doit.ic"
-
 static bool
 doit(struct MatchList const *mlist,
      PathInfo const *src_path, struct stat const *src_stat,
-     char const *dst_path,     struct stat const *dst_stat)
+     char const *dst_path,     struct stat const UNUSED *dst_stat)
 {
   PathInfo     path = mlist->root;
   char         path_buf[ENSC_PI_APPSZ(path, *src_path)];
 
-  if (global_args->do_dry_run || global_args->verbosity>0) {
+  if (global_args->do_dry_run || Global_getVerbosity()>=2) {
     if (global_args->do_revert) WRITE_MSG(1, "deunifying '");
     else                        WRITE_MSG(1, "unifying   '");
 
-    write(1, src_path->d, src_path->l);
+    Vwrite(1, src_path->d, src_path->l);
     WRITE_MSG(1, "'");
 
-    if (global_args->verbosity>2) {
+    if (Global_getVerbosity()>=4) {
       WRITE_MSG(1, " (from ");
-      if (global_args->verbosity==2 && mlist->id.d)
-       write(1, mlist->id.d, mlist->id.l);
+      if (Global_getVerbosity()==4 && mlist->id.d)
+       Vwrite(1, mlist->id.d, mlist->id.l);
       else
-       write(1, mlist->root.d, mlist->root.l);
+       Vwrite(1, mlist->root.d, mlist->root.l);
       WRITE_MSG(1, ")");
     }
     WRITE_MSG(1, "\n");
@@ -256,24 +267,10 @@ doit(struct MatchList const *mlist,
   
   PathInfo_append(&path, src_path, path_buf);
   return (global_args->do_dry_run ||
-         (!global_args->do_revert && doitUnify(  path.d, src_stat, dst_path, dst_stat)) ||
-         ( global_args->do_revert && doitDeUnify(path.d, src_stat, dst_path, dst_stat)));
+         (!global_args->do_revert && Unify_unify  (path.d, src_stat, dst_path, false)) ||
+         ( global_args->do_revert && Unify_deUnify(dst_path)));
 }
 
-static void
-printListId(struct MatchList const *l)
-{
-  if (l->id.l>0) {
-    WRITE_MSG(1, "'");
-    write(1, l->id.d, l->id.l);
-    WRITE_MSG(1, "'");
-  }
-  else if (l->root.l>0) {
-    write(1, l->root.d, l->root.l);
-  }
-  else
-    WRITE_MSG(1, "???");
-}
 
 static void
 printSkipReason()
@@ -284,10 +281,10 @@ printSkipReason()
     case rsEXCL_DST    :
     case rsEXCL_SRC    :
       WRITE_MSG(1, "excluded by ");
-      printListId(skip_reason.d.list);
+      MatchList_printId(skip_reason.d.list, 1);
       break;
     case rsFSTAT       :  WRITE_MSG(1, "fstat error"); break;
-    case rsNOEXISTS    :  WRITE_MSG(1, "does not exists in refserver(s)"); break;
+    case rsNOEXISTS    :  WRITE_MSG(1, "does not exist in refserver(s)"); break;
     case rsSYMLINK     :  WRITE_MSG(1, "symlink"); break;
     case rsSPECIAL     :  WRITE_MSG(1, "non regular file"); break;
     case rsUNIFIED     :  WRITE_MSG(1, "already unified"); break;
@@ -297,92 +294,66 @@ printSkipReason()
   WRITE_MSG(1, ")");
 }
 
-static void
+#include "vserver-visitdir.hc"
+
+static uint64_t
 visitDirEntry(struct dirent const *ent)
 {
   bool                         is_dir;
   struct MatchList const *     match;
-  struct stat                  f_stat;
+  struct stat                  f_stat = { .st_dev = 0 };
   char const *                 dirname  = ent->d_name;
   PathInfo                     path     = global_info.state;
   PathInfo                     d_path = {
     .d = dirname,
-    .l = strlen(ent->d_name)
+    .l = strlen(dirname)
   };
   char                         path_buf[ENSC_PI_APPSZ(path, d_path)];
   bool                         is_dotfile;
   struct stat                  src_stat;
+  uint64_t                     res = 1;
 
   PathInfo_append(&path, &d_path, path_buf);
 
-  is_dotfile = (dirname[0]=='.' &&
-               (dirname[1]=='\0' || (dirname[1]=='.' && dirname[2]=='\0')));
-  memset(&f_stat, 0, sizeof f_stat);
+  is_dotfile    = isDotfile(dirname);
   skip_reason.r = rsDOTFILE;
 
   if (is_dotfile ||
       (match=checkDirEntry(&path, &d_path, &is_dir, &src_stat, &f_stat))==0) {
     bool       is_link = is_dotfile ? false : S_ISLNK(f_stat.st_mode);
     
-    if (global_args->verbosity>1 &&
+    if (Global_getVerbosity()>=1 &&
+       (Global_getVerbosity()>=3 || skip_reason.r!=rsUNIFIED) &&
        ((!is_dotfile && !is_link) ||
-        (global_args->verbosity>4 && is_dotfile) ||
-        (global_args->verbosity>4 && is_link)) ) {
+        (Global_getVerbosity()>=6 && is_dotfile) ||
+        (Global_getVerbosity()>=6 && is_link)) ) {
       WRITE_MSG(1, "  skipping '");
-      write(1, path.d, path.l);
+      Vwrite(1, path.d, path.l);
       WRITE_MSG(1, "'");
-      if (global_args->verbosity>2) printSkipReason();
+      if (Global_getVerbosity()>=2) printSkipReason();
       WRITE_MSG(1, "\n");
     }
-    return;
+    return 0;
   }
 
   if (is_dir) {
     if (updateSkipDepth(&path, true)) {
-      visitDir(dirname, &f_stat);
+      res = visitDir(dirname, &f_stat);
       updateSkipDepth(&path, false);
     }
+    else
+      res = 0;
   }
   else if (!doit(match, &path, &src_stat, dirname, &f_stat)) {
       // TODO: message
   }
-}
-
-static void
-visitDir(char const *name, struct stat const *expected_stat)
-{
-  int          fd = Eopen(".", O_RDONLY, 0);
-  PathInfo     old_state = global_info.state;
-  PathInfo     rhs_path = {
-    .d = name,
-    .l = strlen(name)
-  };
-  char         new_path[ENSC_PI_APPSZ(global_info.state, rhs_path)];
-  DIR *                dir;
-
-  PathInfo_append(&global_info.state, &rhs_path, new_path);
-
-  if (expected_stat!=0)
-    EsafeChdir(name, expected_stat);
-  
-  dir = Eopendir(".");
-
-  for (;;) {
-    struct dirent              *ent = Ereaddir(dir);
-    if (ent==0) break;
-
-    visitDirEntry(ent);
-  }
-
-  Eclosedir(dir);
-
-  Efchdir(fd);
-  Eclose(fd);
+  else
+    res = 0;
 
-  global_info.state = old_state;
+  return res;
 }
 
-#include "vunify-init.ic"
+#include "vunify-init.hc"
 
 int main(int argc, char *argv[])
 {
@@ -434,7 +405,7 @@ int main(int argc, char *argv[])
   global_info.state.l = 0;
 
 
-  if (global_args->verbosity>3) WRITE_MSG(1, "Starting to traverse directories...\n");
+  if (Global_getVerbosity()>=1) WRITE_MSG(1, "Starting to traverse directories...\n");
   Echdir(global_info.dst_list.root.d);
   visitDir("/", 0);