Code

Don't free dirname()'s return value.
[sysdb.git] / src / utils / os.c
index b1e143a6a651d140e0ee10ce36fdff7f05da421b..08cb188d172355126d16f80ed4ae096aad47ff06 100644 (file)
 #include <libgen.h>
 
 /*
- * private helper functions
+ * public API
  */
 
-/*
- * Recursively create the directory 'pathname' using the specified 'mode'. If
- * 'enforce_mode' is true, the mode will be enforced even if the directory
- * exists already.
- */
-static int
-mkdir_rec(const char *pathname, mode_t mode, _Bool enforce_mode)
+int
+sdb_mkdir_all(const char *pathname, mode_t mode)
 {
        struct stat st;
+       char *pathname_copy;
        char *base_dir;
 
        int status = 0;
@@ -70,9 +66,6 @@ mkdir_rec(const char *pathname, mode_t mode, _Bool enforce_mode)
                        errno = ENOTDIR;
                        return -1;
                }
-
-               if ((st.st_mode != mode) && enforce_mode)
-                       return chmod(pathname, mode);
                return 0;
        }
 
@@ -80,28 +73,17 @@ mkdir_rec(const char *pathname, mode_t mode, _Bool enforce_mode)
                /* pathname exists but we cannot access it */
                return -1;
 
-       base_dir = strdup(pathname);
-       if (! base_dir)
+       pathname_copy = strdup(pathname);
+       if (! pathname_copy)
                return -1;
-       base_dir = dirname(base_dir);
+       base_dir = dirname(pathname_copy);
 
-       /* don't enforce the mode on parent directories */
-       status = mkdir_rec(base_dir, mode, 0);
+       status = sdb_mkdir_all(base_dir, mode);
        if (! status)
                status = mkdir(pathname, mode);
 
-       free(base_dir);
+       free(pathname_copy);
        return status;
-} /* mkdir_rec */
-
-/*
- * public API
- */
-
-int
-sdb_mkdir_all(const char *pathname, mode_t mode)
-{
-       return mkdir_rec(pathname, mode, 1);
 } /* sdb_mkdir_all */
 
 /* vim: set tw=78 sw=4 ts=4 noexpandtab : */