]> git.sur5r.net Git - bacula/bacula/blobdiff - bacula/src/findlib/makepath.c
- Add Rudolf Cejka's new rc-chio-changer.
[bacula/bacula] / bacula / src / findlib / makepath.c
index 53fa1cce300774115f98eedca1731c4f9be4faae..452c45bcfd7d3b7f68e1bec0b140b8d935a2554d 100644 (file)
@@ -18,7 +18,7 @@
 
 /* Written by David MacKenzie <djm@gnu.ai.mit.edu> and Jim Meyering.  */
 
-/* 
+/*
  *   Modified by Kern Sibbald for use in Bacula, December 2000
  *
  *   Version $Id$
@@ -73,8 +73,9 @@ cleanup(struct saved_cwd *cwd)
    if (cwd->do_chdir) {
       int _fail = restore_cwd(cwd, NULL, NULL);
       free_cwd(cwd);
-      if (_fail)
+      if (_fail) {
         return 1;
+      }
    }
    return 0;
 }
@@ -89,13 +90,15 @@ cleanup(struct saved_cwd *cwd)
    (indicating success) and will set *CREATED_DIR_P to zero.  */
 
 static int
-make_dir(void *jcr, const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
+make_dir(JCR *jcr, const char *dir, const char *dirpath, mode_t mode, int *created_dir_p)
 {
   int fail = 0;
   int created_dir;
+  int save_errno;
 
   Dmsg2(300, "make_dir mode=%o dir=%s\n", mode, dir);
   created_dir = (mkdir(dir, mode) == 0);
+  save_errno = errno;
 
   if (!created_dir) {
       struct stat stats;
@@ -103,17 +106,19 @@ make_dir(void *jcr, const char *dir, const char *dirpath, mode_t mode, int *crea
       /* The mkdir and stat calls below may appear to be reversed.
         They are not.  It is important to call mkdir first and then to
         call stat (to distinguish the three cases) only if mkdir fails.
-         The alternative to this approach is to `stat' each directory,
-         then to call mkdir if it doesn't exist.  But if some other process
+        The alternative to this approach is to `stat' each directory,
+        then to call mkdir if it doesn't exist.  But if some other process
         were to create the directory between the stat & mkdir, the mkdir
         would fail with EEXIST.  */
 
       if (stat(dir, &stats)) {
-          Jmsg(jcr, M_ERROR, 0, "Cannot create directory %s: %s\n", 
-                 dirpath, strerror(errno));
+         berrno be;
+         be.set_errno(save_errno);
+         Jmsg(jcr, M_ERROR, 0, _("Cannot create directory %s: ERR=%s\n"),
+                 dirpath, be.strerror());
          fail = 1;
       } else if (!S_ISDIR(stats.st_mode)) {
-          Jmsg(jcr, M_ERROR, 0, "%s exists but is not a directory\n", quote(dirpath));
+         Jmsg(jcr, M_ERROR, 0, _("%s exists but is not a directory\n"), quote(dirpath));
          fail = 1;
       } else {
          /* DIR (aka DIRPATH) already exists and is a directory. */
@@ -127,12 +132,23 @@ make_dir(void *jcr, const char *dir, const char *dirpath, mode_t mode, int *crea
   return fail;
 }
 
+/* return non-zero if path is absolute or zero if relative. */
+int
+isAbsolute(const char *path)
+{
+#if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
+    return path[1] == ':' || *path == '/' || *path == '\\';     /* drivespec:/blah is absolute */
+#else
+    return *path == '/';
+#endif
+}
+
 /* Ensure that the directory ARGPATH exists.
    Remove any trailing slashes from ARGPATH before calling this function.
 
    Create any leading directories that don't already exist, with
    permissions PARENT_MODE.
-    
+
    If the last element of ARGPATH does not exist, create it as
    a new directory with permissions MODE.
 
@@ -151,7 +167,7 @@ make_dir(void *jcr, const char *dir, const char *dirpath, mode_t mode, int *crea
 
 int
 make_path(
-          void *jcr,
+          JCR *jcr,
           const char *argpath,
           int mode,
           int parent_mode,
@@ -177,7 +193,7 @@ make_path(
       char *dirpath;
 
       /* Temporarily relax umask in case it's overly restrictive.  */
-      mode_t oldmask = umask (0);
+      mode_t oldmask = umask(0);
 
       /* Make a copy of ARGPATH that we can scribble NULs on.  */
       dirpath = (char *)alloca(strlen(argpath) + 1);
@@ -188,25 +204,29 @@ make_path(
         or should have set[ug]id or sticky bits set and we are setting
         their owners, we need to fix their permissions after making them.  */
       if (((parent_mode & WX_USR) != WX_USR)
-         || ((owner != (uid_t) -1 || group != (gid_t) -1)
-             && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0))
-       {
-         tmp_mode = S_IRWXU;
-         re_protect = 1;
-       }
-      else {
-         tmp_mode = parent_mode;
-         re_protect = 0;
+         || ((owner != (uid_t)-1 || group != (gid_t)-1)
+             && (parent_mode & (S_ISUID | S_ISGID | S_ISVTX)) != 0)) {
+        tmp_mode = S_IRWXU;
+        re_protect = 1;
+      } else {
+        tmp_mode = parent_mode;
+        re_protect = 0;
       }
 
+#if defined(HAVE_CYGWIN)
+      /* Because of silly Win32 security, we allow everything */
+      tmp_mode = S_IRWXUGO;
+      re_protect = 0;
+#endif
+
       /* If we can record the current working directory, we may be able
         to do the chdir optimization.  */
       cwd.do_chdir = !save_cwd(&cwd);
 
       /* If we've saved the cwd and DIRPATH is an absolute pathname,
-         we must chdir to `/' in order to enable the chdir optimization.
-         So if chdir ("/") fails, turn off the optimization.  */
-      if (cwd.do_chdir && *dirpath == '/' && chdir ("/") < 0) {
+        we must chdir to `/' in order to enable the chdir optimization.
+        So if chdir ("/") fails, turn off the optimization.  */
+      if (cwd.do_chdir && isAbsolute(dirpath) && (chdir("/") < 0)) {
         cwd.do_chdir = 0;
       }
 
@@ -223,18 +243,18 @@ make_path(
          /* slash points to the leftmost unprocessed component of dirpath.  */
          basename_dir = slash;
 
-          slash = strchr (slash, '/');
+         slash = strchr (slash, '/');
          if (slash == NULL) {
             break;
          }
 
-          /* If we're *not* doing chdir before each mkdir, then we have to refer
+         /* If we're *not* doing chdir before each mkdir, then we have to refer
             to the target using the full (multi-component) directory name.  */
          if (!cwd.do_chdir) {
             basename_dir = dirpath;
          }
 
-          *slash = '\0';
+         *slash = '\0';
          fail = make_dir(jcr, basename_dir, dirpath, tmp_mode, &newly_created_dir);
          if (fail) {
              umask(oldmask);
@@ -243,32 +263,28 @@ make_path(
          }
 
          if (newly_created_dir) {
-              Dmsg0(300, "newly_created_dir\n");
-             if (verbose_fmt_string) {
-                Jmsg(jcr, M_ERROR, 0, verbose_fmt_string, quote(dirpath));
-             }
+             Dmsg0(300, "newly_created_dir\n");
 
-             if ((owner != (uid_t) -1 || group != (gid_t) -1)
-                 && chown (basename_dir, owner, group)
+             if ((owner != (uid_t)-1 || group != (gid_t)-1)
+                 && chown(basename_dir, owner, group)
 #if defined(AFS) && defined (EPERM)
                  && errno != EPERM
 #endif
                  ) {
-                 Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n",
-                     quote (dirpath), strerror(errno));
-                umask(oldmask);
-                cleanup(&cwd);
-                return 1;
+                /* Note, if we are restoring as NON-root, this may not be fatal */
+                berrno be;
+                Jmsg(jcr, M_ERROR, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
+                     quote(dirpath), be.strerror());
              }
-              Dmsg0(300, "Chown done.\n");
+             Dmsg0(300, "Chown done.\n");
 
              if (re_protect) {
                 struct ptr_list *pnew = (struct ptr_list *)
-                   alloca (sizeof (struct ptr_list));
+                   alloca(sizeof (struct ptr_list));
                 pnew->dirname_end = slash;
                 pnew->next = leading_dirs;
                 leading_dirs = pnew;
-                 Dmsg0(300, "re_protect\n");
+                Dmsg0(300, "re_protect\n");
              }
          }
 
@@ -277,18 +293,19 @@ make_path(
             creating an entry in that directory.  This avoids making
             stat and mkdir process O(n^2) file name components.  */
          if (cwd.do_chdir && chdir(basename_dir) < 0) {
-              Jmsg(jcr, M_ERROR, 0, "Cannot chdir to directory, %s: %s\n",
-                    quote (dirpath), strerror(errno));
+             berrno be;
+             Jmsg(jcr, M_ERROR, 0, _("Cannot chdir to directory, %s: ERR=%s\n"),
+                    quote(dirpath), be.strerror());
              umask(oldmask);
              cleanup(&cwd);
              return 1;
          }
 
-          *slash++ = '/';
+         *slash++ = '/';
 
-          /* Avoid unnecessary calls to `stat' when given
+         /* Avoid unnecessary calls to `stat' when given
             pathnames containing multiple adjacent slashes.  */
-          while (*slash == '/')
+         while (*slash == '/')
             slash++;
       } /* end while (1) */
 
@@ -307,52 +324,49 @@ make_path(
       }
 
       /* Done creating directories.  Restore original umask.  */
-      umask (oldmask);
+      umask(oldmask);
 
-      if (verbose_fmt_string != NULL) {
-        Jmsg(jcr, M_ERROR, 0, verbose_fmt_string, dirpath);
-      }
-
-      if (owner != (uid_t) -1 || group != (gid_t) -1) {
+      if (owner != (uid_t)-1 || group != (gid_t)-1) {
          if (chown(basename_dir, owner, group)
 #ifdef AFS
              && errno != EPERM
 #endif
              )
            {
-              Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n",
-                    quote (dirpath), strerror(errno));
-             retval = 1;
+             berrno be;
+             Jmsg(jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
+                    quote(dirpath), be.strerror());
            }
       }
 
       /* The above chown may have turned off some permission bits in MODE.
         Another reason we may have to use chmod here is that mkdir(2) is
         required to honor only the file permission bits.  In particular,
-         it need not honor the `special' bits, so if MODE includes any
+        it need not honor the `special' bits, so if MODE includes any
         special bits, set them here.  */
       if (mode & ~S_IRWXUGO) {
-         Dmsg1(300, "Final chmod mode=%o\n", mode);
+        Dmsg1(300, "Final chmod mode=%o\n", mode);
       }
       if ((mode & ~S_IRWXUGO) && chmod(basename_dir, mode)) {
-          Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n", 
-            quote(dirpath), strerror(errno));
-         retval = 1;
+         berrno be;
+         Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
+            quote(dirpath), be.strerror());
       }
 
-     if (cleanup(&cwd))
+     if (cleanup(&cwd)) {
        return 1;
+     }
 
       /* If the mode for leading directories didn't include owner "wx"
         privileges, we have to reset their protections to the correct
         value.  */
       for (p = leading_dirs; p != NULL; p = p->next) {
-          *(p->dirname_end) = '\0';
-          Dmsg2(300, "Reset parent mode=%o dir=%s\n", parent_mode, dirpath);
+         *(p->dirname_end) = '\0';
+         Dmsg2(300, "Reset parent mode=%o dir=%s\n", parent_mode, dirpath);
          if (chmod(dirpath, parent_mode)) {
-              Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n",
-                    quote (dirpath), strerror(errno));
-             retval = 1;
+             berrno be;
+             Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
+                    quote(dirpath), be.strerror());
          }
       }
   } else {
@@ -361,35 +375,34 @@ make_path(
       const char *dirpath = argpath;
 
       if (!S_ISDIR(stats.st_mode)) {
-          Jmsg(jcr, M_ERROR, 0, "%s exists but is not a directory\n", quote(dirpath));
+         Jmsg(jcr, M_ERROR, 0, _("%s exists but is not a directory\n"), quote(dirpath));
          return 1;
       }
 
       if (!preserve_existing) {
-          Dmsg0(300, "Do not preserve existing.\n");
+         Dmsg0(300, "Do not preserve existing.\n");
          /* chown must precede chmod because on some systems,
             chown clears the set[ug]id bits for non-superusers,
             resulting in incorrect permissions.
             On System V, users can give away files with chown and then not
-             be able to chmod them.  So don't give files away.  */
+            be able to chmod them.  So don't give files away.  */
 
-         if ((owner != (uid_t) -1 || group != (gid_t) -1)
+         if ((owner != (uid_t)-1 || group != (gid_t)-1)
              && chown(dirpath, owner, group)
 #ifdef AFS
              && errno != EPERM
 #endif
-             )
-           {
-              Jmsg(jcr, M_ERROR, 0, "Cannot change owner and/or group of %s: %s\n",
-                    quote(dirpath), strerror(errno));
-             retval = 1;
+             ) {
+             berrno be;
+             Jmsg(jcr, M_WARNING, 0, _("Cannot change owner and/or group of %s: ERR=%s\n"),
+                    quote(dirpath), be.strerror());
            }
          if (chmod(dirpath, mode)) {
-              Jmsg(jcr, M_ERROR, 0, "Cannot change permissions of %s: %s\n",
-                                quote(dirpath), strerror(errno));
-             retval = 1;
+             berrno be;
+             Jmsg(jcr, M_WARNING, 0, _("Cannot change permissions of %s: ERR=%s\n"),
+                                quote(dirpath), be.strerror());
          }
-          Dmsg2(300, "pathexists chmod mode=%o dir=%s\n", mode, dirpath);
+         Dmsg2(300, "pathexists chmod mode=%o dir=%s\n", mode, dirpath);
       }
   }
   return retval;