]> git.ipfire.org Git - thirdparty/dracut.git/commitdiff
dracut_mkdir(): create parent directories as needed.
authorLucas C. Villa Real <lucasvr@gmail.com>
Thu, 20 Feb 2020 19:37:32 +0000 (16:37 -0300)
committerHarald Hoyer <harald@hoyer.xyz>
Mon, 2 Mar 2020 13:48:11 +0000 (14:48 +0100)
install/dracut-install.c

index 5d48e9d56f27205dab61e4bbaa1f2bd7eabf28f3..3d64ed7a0872c10f619d3de470a91679e7c8c8d5 100644 (file)
@@ -687,29 +687,41 @@ static bool check_hashmap(Hashmap *hm, const char *item)
 
 static int dracut_mkdir(const char *src) {
         _cleanup_free_ char *parent = NULL;
 
 static int dracut_mkdir(const char *src) {
         _cleanup_free_ char *parent = NULL;
+        char *path;
         struct stat sb;
 
         parent = strdup(src);
         if (!parent)
                 return 1;
 
         struct stat sb;
 
         parent = strdup(src);
         if (!parent)
                 return 1;
 
-        parent[dir_len(parent)] = '\0';
+        path = parent[0] == '/' ? parent+1 : parent;
+        while (path) {
+                path = strstr(path, "/");
+                if (path)
+                        *path = '\0';
 
 
-        if (stat(parent, &sb) == 0) {
-                if (!S_ISDIR(sb.st_mode)) {
-                        log_error("%s exists but is not a directory!", parent);
+                if (stat(parent, &sb) == 0) {
+                        if (!S_ISDIR(sb.st_mode)) {
+                                log_error("%s exists but is not a directory!", parent);
+                                return 1;
+                        }
+                } else if (errno != ENOENT) {
+                        log_error("ERROR: stat '%s': %s", parent, strerror(errno));
                         return 1;
                         return 1;
+                } else {
+                        if (mkdir(parent, 0755) < 0) {
+                                log_error("ERROR: mkdir '%s': %s", parent, strerror(errno));
+                                return 1;
+                        }
                 }
 
                 }
 
-                return mkdir(src, 0755);
-        }
-
-        if (errno != ENOENT) {
-                log_error("ERROR: stat '%s': %m", src);
-                return 1;
+                if (path) {
+                        *path = '/';
+                        path++;
+                }
         }
 
         }
 
-        return dracut_mkdir(parent);
+        return 0;
 }
 
 static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir, bool resolvedeps, bool hashdst)
 }
 
 static int dracut_install(const char *orig_src, const char *orig_dst, bool isdir, bool resolvedeps, bool hashdst)