use new ensc_wrappers/ headers
[util-vserver.git] / util-vserver / src / vunify.c
index 7630329..769214a 100644 (file)
@@ -21,8 +21,6 @@
 #endif
 
 #include "vunify.h"
-#include "wrappers-dirent.h"
-#include "vunify-operations.h"
 #include "util.h"
 
 #include <lib/vserver.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <stdbool.h>
+#include <errno.h>
+#include <wait.h>
+#include <fcntl.h>
 
-int    wrapper_exit_code = 1;
-
+#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>
 
-static struct WalkdownInfo     global_info;
-static struct Operations       operations;
+int    wrapper_exit_code = 1;
 
 
 #define CMD_HELP               0x8000
@@ -53,16 +57,9 @@ CMDLINE_OPTIONS[] = {
   { 0,0,0,0 }
 };
 
-struct Arguments {
-    enum {mdMANUALLY, mdVSERVER}       mode;
-    bool                               do_revert;
-    bool                               do_dry_run;
-    unsigned int                       verbosity;
-    bool                               local_fs;
-    bool                               do_renew;
-};
-
-struct Arguments const *               global_args;
+static struct WalkdownInfo             global_info;
+static struct SkipReason               skip_reason;
+static struct Arguments const *                global_args;
 
 static void
 showHelp(int fd, char const *cmd, int res)
@@ -81,7 +78,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"
@@ -102,40 +99,46 @@ showVersion()
   exit(0);
 }
 
+#include "vunify-compare.hc"
 
 // 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,
           PathInfo const * const  basename,
           PathInfo const * const  path,
-          struct stat const ** const result, struct stat * const result_buf)
+          struct stat const ** const dst_fstat, struct stat * const dst_fstat_buf,
+          struct stat * const src_fstat)
 {
   assert(basename->d[0] != '/');
 
-  if (*result==0) {
+  if (*dst_fstat==0) {
     // local file does not exist... strange
     // TODO: message
-    if (lstat(basename->d, result_buf)==-1) return false;
-    *result = result_buf;
+    skip_reason.r = rsFSTAT;
+    if (lstat(basename->d, dst_fstat_buf)==-1) return false;
+    *dst_fstat = dst_fstat_buf;
   }
 
-  assert(*result!=0);
+  assert(*dst_fstat!=0);
   
   {
-    struct stat                src_fstat;
     PathInfo           src_path = mlist->root;
     char               src_path_buf[ENSC_PI_APPSZ(src_path, *path)];
 
     PathInfo_append(&src_path, path, src_path_buf);
 
     // source file does not exist
-    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
-    if (S_ISDIR((*result)->st_mode) && S_ISDIR(src_fstat.st_mode)) return true;
+    // these are directories; this succeeds everytime
+    if (S_ISDIR((*dst_fstat)->st_mode) && S_ISDIR(src_fstat->st_mode)) return true;
 
     // both files are different, so return false
-    if (!(operations.compare)(*result, &src_fstat)) 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;
   }
 
   // these are the same files
@@ -144,28 +147,48 @@ checkFstat(struct MatchList const * const mlist,
 
 static struct MatchList const *
 checkDirEntry(PathInfo const *path,
-             PathInfo const *d_path, bool *is_dir, struct stat *f_stat)
+             PathInfo const *d_path, bool *is_dir,
+             struct stat *src_stat, struct stat *dst_stat)
 {
   struct WalkdownInfo const * const    info     = &global_info;
   struct MatchList const *             mlist;
-  struct stat const *                  cache_stat = 0;
+  struct stat const *                  cache_stat;
 
   // Check if it is in the exclude/include list of the destination vserver and
   // 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;
 
   // Now, go through the reference vservers and do the lightweigt list-check
   // first and compare then the fstat's.
   for (mlist=info->src_lists.v; mlist<info->src_lists.v+info->src_lists.l; ++mlist) {
+    cache_stat = 0;
+    skip_reason.r      = rsEXCL_SRC;
+    skip_reason.d.list = mlist;
     if (MatchList_compare(mlist, path->d) &&
-       checkFstat(mlist, d_path, path, &cache_stat, f_stat)) {
+       checkFstat(mlist, d_path, path, &cache_stat, dst_stat, src_stat)) {
 
       // Failed the check or is it a symlink which can not be handled
-      if (cache_stat==0 || S_ISLNK(f_stat->st_mode)) return 0;
+      if (cache_stat==0) return 0;
+
+      skip_reason.r = rsSYMLINK;
+      if (S_ISLNK(dst_stat->st_mode)) return 0;
+
+      skip_reason.r = rsSPECIAL;
+      if (!S_ISREG(dst_stat->st_mode) &&
+         !S_ISDIR(dst_stat->st_mode)) return 0;
       
-      *is_dir = S_ISDIR(f_stat->st_mode);
+      *is_dir = S_ISDIR(dst_stat->st_mode);
       return mlist;
     }
+    else if (cache_stat!=0 && !global_args->do_revert &&
+            skip_reason.r == rsDIFFERENT &&
+            compareDeUnify(cache_stat, src_stat)) {
+      skip_reason.r      = rsUNIFIED;
+      skip_reason.d.list = mlist;
+      return 0;
+    }
   }
 
   // No luck...
@@ -212,9 +235,12 @@ EsafeChdir(char const *path, struct stat const *exp_stat)
   FatalErrnoError(safeChdir(path, exp_stat)==-1, "safeChdir()");
 }
 
+#include "vunify-doit.hc"
+
 static bool
-doit(struct MatchList const *mlist, PathInfo const *src_path,
-     char const *dst_path)
+doit(struct MatchList const *mlist,
+     PathInfo const *src_path, struct stat const *src_stat,
+     char const *dst_path,     struct stat const *dst_stat)
 {
   PathInfo     path = mlist->root;
   char         path_buf[ENSC_PI_APPSZ(path, *src_path)];
@@ -238,9 +264,47 @@ doit(struct MatchList const *mlist, PathInfo const *src_path,
   }
   
   PathInfo_append(&path, src_path, path_buf);
-  return (operations.doit)(path.d, dst_path);
+  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)));
+}
+
+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()
+{
+  WRITE_MSG(1, " (");
+  switch (skip_reason.r) {
+    case rsDOTFILE     :  WRITE_MSG(1, "dotfile"); break;
+    case rsEXCL_DST    :
+    case rsEXCL_SRC    :
+      WRITE_MSG(1, "excluded by ");
+      printListId(skip_reason.d.list);
+      break;
+    case rsFSTAT       :  WRITE_MSG(1, "fstat error"); break;
+    case rsNOEXISTS    :  WRITE_MSG(1, "does not exists 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;
+    case rsDIFFERENT   :  WRITE_MSG(1, "different"); break;
+    default            :  assert(false); abort();
+  }
+  WRITE_MSG(1, ")");
+}
 
 static void
 visitDirEntry(struct dirent const *ent)
@@ -256,14 +320,17 @@ visitDirEntry(struct dirent const *ent)
   };
   char                         path_buf[ENSC_PI_APPSZ(path, d_path)];
   bool                         is_dotfile;
+  struct stat                  src_stat;
 
   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);
+  skip_reason.r = rsDOTFILE;
+
   if (is_dotfile ||
-      (match=checkDirEntry(&path, &d_path, &is_dir, &f_stat))==0) {
+      (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 &&
@@ -272,7 +339,9 @@ visitDirEntry(struct dirent const *ent)
         (global_args->verbosity>4 && is_link)) ) {
       WRITE_MSG(1, "  skipping '");
       write(1, path.d, path.l);
-      WRITE_MSG(1, "'\n");
+      WRITE_MSG(1, "'");
+      if (global_args->verbosity>2) printSkipReason();
+      WRITE_MSG(1, "\n");
     }
     return;
   }
@@ -283,7 +352,7 @@ visitDirEntry(struct dirent const *ent)
       updateSkipDepth(&path, false);
     }
   }
-  else if (!doit(match, &path, dirname)) {
+  else if (!doit(match, &path, &src_stat, dirname, &f_stat)) {
       // TODO: message
   }
 }
@@ -322,92 +391,7 @@ visitDir(char const *name, struct stat const *expected_stat)
   global_info.state = old_state;
 }
 
-#include "vunify-init.ic"
-
-static void
-initModeManually(struct Arguments const UNUSED *args, int argc, char *argv[])
-{
-  int          i, count=argc/2;
-  
-  if (argc%2) {
-    WRITE_MSG(2, "Odd number of (path,excludelist) arguments\n");
-    exit(1);
-  }
-
-  if (count<2) {
-    WRITE_MSG(2, "No reference path(s) given\n");
-    exit(1);
-  }
-
-  initMatchList(&global_info.dst_list, 0, argv[0], argv[1]);
-
-  --count;
-  global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
-  global_info.src_lists.l = count;
-
-  for (i=0; i<count; ++i)
-    initMatchList(global_info.src_lists.v+i, 0, argv[2 + i*2], argv[3 + i*2]);
-}
-
-static int
-selectRefserver(struct dirent const *ent)
-{
-  return strncmp(ent->d_name, "refserver.", 10)==0;
-}
-
-static void
-initModeVserver(struct Arguments const UNUSED *args, int argc, char *argv[])
-{
-  char const   *appdir;
-  int          cur_dir = Eopen(".", O_RDONLY, 0);
-  struct dirent        **entries;
-  int          count, i;
-  
-  if (argc!=1) {
-    WRITE_MSG(2, "More than one vserver is not supported\n");
-    exit(1);
-  }
-
-  if (!initMatchListByVserver(&global_info.dst_list, argv[0], &appdir)) {
-    WRITE_MSG(2, "unification not configured for this vserver\n");
-    exit(1);
-  }
-
-  Echdir(appdir);
-  count = scandir(".", &entries, selectRefserver, alphasort);
-  if (count==-1) {
-    perror("scandir()");
-    exit(1);
-  }
-
-  if (count==0) {
-    WRITE_MSG(2, "no reference vserver configured\n");
-    exit(1);
-  }
-
-  global_info.src_lists.v = Emalloc(sizeof(struct MatchList) * count);
-  global_info.src_lists.l = count;
-  for (i=0; i<count; ++i) {
-    char const                 *tmp   = entries[i]->d_name;
-    size_t             l      = strlen(tmp);
-    char               vname[sizeof("./") + l];
-
-    memcpy(vname,   "./", 2);
-    memcpy(vname+2, tmp,  l+1);
-    
-    if (!initMatchListByVserver(global_info.src_lists.v+i, vname, 0)) {
-      WRITE_MSG(2, "unification for reference vserver not configured\n");
-      exit(1);
-    }
-
-    free(entries[i]);
-  }
-  free(entries);
-  free(const_cast(char *)(appdir));
-
-  Efchdir(cur_dir);
-  Eclose(cur_dir);
-}
+#include "vunify-init.hc"
 
 int main(int argc, char *argv[])
 {
@@ -449,13 +433,9 @@ int main(int argc, char *argv[])
     return EXIT_FAILURE;
   }
 
-  Operations_init(&operations,
-                 args.do_revert ? opDEUNIFY : opUNIFY,
-                 args.do_dry_run);
-
   switch (args.mode) {
     case mdMANUALLY    :  initModeManually(&args, argc-optind, argv+optind); break;
-    case mdVSERVER     :  initModeVserver(&args,  argc-optind, argv+optind); break;
+    case mdVSERVER     :  initModeVserver (&args, argc-optind, argv+optind); break;
     default            :  assert(false); return EXIT_FAILURE;
   }
     
@@ -463,6 +443,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");
   Echdir(global_info.dst_list.root.d);
   visitDir("/", 0);