From: Lucas C. Villa Real Date: Thu, 20 Feb 2020 19:37:32 +0000 (-0300) Subject: dracut_mkdir(): create parent directories as needed. X-Git-Tag: 050~13 X-Git-Url: http://git.ipfire.org/?p=thirdparty%2Fdracut.git;a=commitdiff_plain;h=3d2a6d29d4cec422e2d00c67b12ec5adae1ada86 dracut_mkdir(): create parent directories as needed. --- diff --git a/install/dracut-install.c b/install/dracut-install.c index 5d48e9d56..3d64ed7a0 100644 --- a/install/dracut-install.c +++ b/install/dracut-install.c @@ -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; + char *path; 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; + } 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)