]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/findlib/find_one.c
Add my_postgresql_max_length which caculates the maximum width for all values in...
[bacula/bacula] / bacula / src / findlib / find_one.c
index 39a58ccff104951d696fe11f51e3f8a3ef980e02..ae7c984746d0bf0838c4f95dc3323f8a3530bfd5 100755 (executable)
 #include "bacula.h"
 #include "find.h"
 
-
-extern size_t name_max;              /* filename max length */
-extern size_t path_max;              /* path name max length */
-
-#ifndef HAVE_READDIR_R
-int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
-#endif
-
+extern int32_t name_max;             /* filename max length */
+extern int32_t path_max;             /* path name max length */
 
 /*
  * Structure for keeping track of hard linked files, we   
@@ -53,16 +47,13 @@ struct f_link {
     char name[1];                    /* The name */
 };
 
-
-#if HAVE_UTIME_H
-# include <utime.h>
-#else
-struct utimbuf {
-    long actime;
-    long modtime;
-};
-#endif
-
+static void free_dir_ff_pkt(FF_PKT *dir_ff_pkt)
+{
+   free(dir_ff_pkt->fname);
+   free(dir_ff_pkt->link);
+   free_pool_memory(dir_ff_pkt->sys_fname);
+   free(dir_ff_pkt);
+}
 
 /*
  * Find a single file.                       
@@ -88,11 +79,7 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
        return handle_file(ff_pkt, pkt);
    }
 
-   Dmsg1(60, "File ----: %s\n", fname);
-#ifdef DEBUG
-   if (S_ISLNK(ff_pkt->statp.st_mode))
-      Dmsg1(60, "Link-------------: %s \n", fname);
-#endif
+   Dmsg1(300, "File ----: %s\n", fname);
 
    /* Save current times of this directory in case we need to
     * reset them because the user doesn't want them changed.
@@ -107,21 +94,18 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
     * or Incremental.
     */
    if (ff_pkt->incremental && !S_ISDIR(ff_pkt->statp.st_mode)) {
-      Dmsg1(100, "Non-directory incremental: %s\n", ff_pkt->fname);
+      Dmsg1(300, "Non-directory incremental: %s\n", ff_pkt->fname);
       /* Not a directory */
       if (ff_pkt->statp.st_mtime < ff_pkt->save_time
          && (ff_pkt->mtime_only || 
              ff_pkt->statp.st_ctime < ff_pkt->save_time)) {
         /* Incremental option, file not changed */
         ff_pkt->type = FT_NOCHG;
-         Dmsg1(100, "File not changed: %s\n", ff_pkt->fname);
-         Dmsg4(200, "save_time=%d mtime=%d mtime_only=%d st_ctime=%d\n",
-           ff_pkt->save_time, ff_pkt->statp.st_mtime, 
-           ff_pkt->mtime_only, ff_pkt->statp.st_ctime);
         return handle_file(ff_pkt, pkt);
       }
    }
 
+/* ***FIXME*** implement this */
 #if xxxxxxx
    /* See if we are trying to dump the archive.  */
    if (ar_dev && ff_pkt->statp.st_dev == ar_dev && ff_pkt->statp.st_ino == ar_ino) {
@@ -188,11 +172,11 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
       return rtn_stat;
 
 
-   } else if (S_ISLNK(ff_pkt->statp.st_mode)) {
+   } else if (S_ISLNK(ff_pkt->statp.st_mode)) {  /* soft link */
       int size;
-      char *buffer = (char *)alloca(path_max + name_max + 2);
+      char *buffer = (char *)alloca(path_max + name_max + 102);
 
-      size = readlink(fname, buffer, path_max + name_max + 1);
+      size = readlink(fname, buffer, path_max + name_max + 101);
       if (size < 0) {
         /* Could not follow link */                             
         ff_pkt->type = FT_NOFOLLOW;
@@ -204,8 +188,8 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
         return rtn_stat;
       }
       buffer[size] = 0;
-      ff_pkt->link = buffer;
-      ff_pkt->type = FT_LNK;          /* got a real link */
+      ff_pkt->link = buffer;         /* point to link */
+      ff_pkt->type = FT_LNK;         /* got a real link */
       rtn_stat = handle_file(ff_pkt, pkt);
       if (ff_pkt->linked) {
         ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
@@ -217,22 +201,29 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
       struct dirent *entry, *result;
       char *link;
       int link_len;
-      int len;
+      int len;  
       int status;
       dev_t our_device = ff_pkt->statp.st_dev;
 
-      if (access(fname, R_OK) == -1 && geteuid() != 0) {
-        /* Could not access() directory */
-        ff_pkt->type = FT_NOACCESS;
-        ff_pkt->ff_errno = errno;
-        rtn_stat = handle_file(ff_pkt, pkt);
-        if (ff_pkt->linked) {
-           ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
+      /*  
+       * If we are using Win32 (non-portable) backup API, don't check
+       *  access as everything is more complicated, and
+       *  in principle, we should be able to access everything.
+       */
+      if (!have_win32_api() || (ff_pkt->flags & FO_PORTABLE)) {
+        if (access(fname, R_OK) == -1 && geteuid() != 0) {
+           /* Could not access() directory */
+           ff_pkt->type = FT_NOACCESS;
+           ff_pkt->ff_errno = errno;
+           rtn_stat = handle_file(ff_pkt, pkt);
+           if (ff_pkt->linked) {
+              ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
+           }
+           return rtn_stat;
         }
-        return rtn_stat;
       }
 
-      /* Build a canonical directory name with a trailing slash. */
+      /* Build a canonical directory name with a trailing slash in link var */
       len = strlen(fname);
       link_len = len + 200;
       link = (char *)bmalloc(link_len + 2);
@@ -252,11 +243,26 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
       } else {
         ff_pkt->type = FT_DIR;
       }
-      handle_file(ff_pkt, pkt);       /* handle directory entry */
-      if (ff_pkt->linked) {
-        ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
-      }
 
+      /*
+       * Create a temporary ff packet for this directory
+       *   entry, and defer handling the directory until
+       *   we have recursed into it.  This saves the
+       *   directory after all files have been processed, and
+       *   during the restore, the directory permissions will
+       *   be reset after all the files have been restored.
+       */
+      Dmsg1(300, "Create temp ff packet for dir: %s\n", ff_pkt->fname);
+      FF_PKT *dir_ff_pkt = (FF_PKT *)bmalloc(sizeof(FF_PKT));
+      memcpy(dir_ff_pkt, ff_pkt, sizeof(FF_PKT));
+      dir_ff_pkt->fname = bstrdup(ff_pkt->fname);
+      dir_ff_pkt->link = bstrdup(ff_pkt->link);
+      dir_ff_pkt->sys_fname = get_pool_memory(PM_FNAME);
+      dir_ff_pkt->included_files_list = NULL;
+      dir_ff_pkt->excluded_files_list = NULL;
+      dir_ff_pkt->excluded_paths_list = NULL;
+      dir_ff_pkt->linklist = NULL;
+       
       ff_pkt->link = ff_pkt->fname;     /* reset "link" */
 
       /* 
@@ -264,13 +270,14 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
        * user has turned it off for this directory.
        */
       if (ff_pkt->flags & FO_NO_RECURSION) {
-        free(link);
         /* No recursion into this directory */
         ff_pkt->type = FT_NORECURSE;
         rtn_stat = handle_file(ff_pkt, pkt);
         if (ff_pkt->linked) {
            ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
         }
+        free(link);
+        free_dir_ff_pkt(dir_ff_pkt);
         return rtn_stat;
       }
 
@@ -280,47 +287,49 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
        */
       if (!top_level && !(ff_pkt->flags & FO_MULTIFS) &&
           parent_device != ff_pkt->statp.st_dev) {
-        free(link);
         /* returning here means we do not handle this directory */
         ff_pkt->type = FT_NOFSCHG;
         rtn_stat = handle_file(ff_pkt, pkt);
         if (ff_pkt->linked) {
            ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
         }
+        free(link);
+        free_dir_ff_pkt(dir_ff_pkt);
         return rtn_stat;
       }
       /* 
-       * Now process the files in this directory.
+       * Decend into or "recurse" into the directory to read
+       *   all the files in it.
        */
       errno = 0;
       if ((directory = opendir(fname)) == NULL) {
-        free(link);
         ff_pkt->type = FT_NOOPEN;
         ff_pkt->ff_errno = errno;
         rtn_stat = handle_file(ff_pkt, pkt);
         if (ff_pkt->linked) {
            ff_pkt->linked->FileIndex = ff_pkt->FileIndex;
         }
+        free(link);
+        free_dir_ff_pkt(dir_ff_pkt);
         return rtn_stat;
       }
 
       /*
-       * This would possibly run faster if we chdir to the directory
-       * before traversing it.
+       * Process all files in this directory entry (recursing).
+       *    This would possibly run faster if we chdir to the directory
+       *    before traversing it.
        */
       rtn_stat = 1;
       entry = (struct dirent *)malloc(sizeof(struct dirent) + name_max + 100);
-      for ( ; !job_cancelled(jcr); ) {
+      for ( ; !job_canceled(jcr); ) {
         char *p, *q;
         int i;
 
         status  = readdir_r(directory, entry, &result);
-         Dmsg3(200, "readdir stat=%d result=%x name=%s\n", status, result,
-           entry->d_name);
         if (status != 0 || result == NULL) {
            break;
         }
-        ASSERT(name_max+1 > sizeof(struct dirent) + (int)NAMELEN(entry));
+        ASSERT(name_max+1 > (int)sizeof(struct dirent) + (int)NAMELEN(entry));
         p = entry->d_name;
          /* Skip `.', `..', and excluded file names.  */
          if (p[0] == '\0' || (p[0] == '.' && (p[1] == '\0' ||
@@ -348,6 +357,19 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
       free(link);
       free(entry);
 
+      /*
+       * Now that we have recursed through all the files in the
+       *  directory, we "save" the directory so that after all
+       *  the files are restored, this entry will serve to reset
+       *  the directory modes and dates.  Temp directory values
+       *  were used without this record.
+       */
+      handle_file(dir_ff_pkt, pkt);      /* handle directory entry */
+      if (ff_pkt->linked) {
+        ff_pkt->linked->FileIndex = dir_ff_pkt->FileIndex;
+      }
+      free_dir_ff_pkt(dir_ff_pkt);
+
       if (ff_pkt->atime_preserve) {
         utime(fname, &restore_times);
       }
@@ -359,7 +381,18 @@ find_one_file(JCR *jcr, FF_PKT *ff_pkt, int handle_file(FF_PKT *ff, void *hpkt),
     *  a block device, we do a raw backup of it or if it is
     *  a fifo, we simply read it.
     */
+#ifdef HAVE_FREEBSD_OS
+   /*
+    * On FreeBSD, all block devices are character devices, so
+    *  to be able to read a raw disk, we need the check for
+    *  a character device.
+    * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/ad0s3
+    * crw-r-----  1 root  operator  - 116, 0x00040002 Jun  9 19:32 /dev/rad0s3
+    */
+   if (top_level && (S_ISBLK(ff_pkt->statp.st_mode) || S_ISCHR(ff_pkt->statp.st_mode))) {
+#else
    if (top_level && S_ISBLK(ff_pkt->statp.st_mode)) {
+#endif
       ff_pkt->type = FT_RAW;         /* raw partition */
    } else if (top_level && S_ISFIFO(ff_pkt->statp.st_mode) &&
              ff_pkt->flags & FO_READFIFO) {