]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
libmount; add recursive mkdir
authorKarel Zak <kzak@redhat.com>
Wed, 9 Jan 2013 17:05:08 +0000 (18:05 +0100)
committerKarel Zak <kzak@redhat.com>
Wed, 9 Jan 2013 17:05:08 +0000 (18:05 +0100)
Signed-off-by: Karel Zak <kzak@redhat.com>
libmount/src/mountP.h
libmount/src/utils.c

index dca70accbaccb427f4cb38e0cf7ec959f21837e4..5f2f780dc12ab7e04744842b3e993b3228d68334 100644 (file)
@@ -131,6 +131,9 @@ extern int endswith(const char *s, const char *sx)
                        __attribute__((nonnull));
 extern int startswith(const char *s, const char *sx)
                        __attribute__((nonnull));
+
+extern int mkdir_p(const char *path, mode_t mode);
+
 extern int mnt_is_readonly(const char *path)
                        __attribute__((nonnull));
 
index bfd2fff2a9a12ead9eadbe7291d8fd6fe4ff018f..fbfa89a4c6d6c03e0b5ef5eba74d77a91e0efb76 100644 (file)
@@ -978,6 +978,43 @@ char *mnt_get_kernel_cmdline_option(const char *name)
        return res;
 }
 
+int mkdir_p(const char *path, mode_t mode)
+{
+       char *p, *dir;
+       int rc = 0;
+
+       if (!path || !*path)
+               return -EINVAL;
+
+       dir = p = strdup(path);
+       if (!dir)
+               return -ENOMEM;
+
+       if (*p == '/')
+               p++;
+
+       while (p && *p) {
+               char *e = strchr(p, '/');
+               if (e)
+                       *e = '\0';
+               if (*p) {
+                       rc = mkdir(dir, mode);
+                       if (rc && errno != EEXIST)
+                               break;
+                       rc = 0;
+               }
+               if (!e)
+                       break;
+               *e = '/';
+               p = e + 1;
+       }
+
+       DBG(UTILS, mnt_debug("%s mkdir %s", path, rc ? "FAILED" : "SUCCESS"));
+
+       free(dir);
+       return rc;
+}
+
 #ifdef TEST_PROGRAM
 int test_match_fstype(struct libmnt_test *ts, int argc, char *argv[])
 {
@@ -1089,6 +1126,18 @@ int test_kernel_cmdline(struct libmnt_test *ts, int argc, char *argv[])
        return 0;
 }
 
+int test_mkdir(struct libmnt_test *ts, int argc, char *argv[])
+{
+       int rc;
+
+       rc = mkdir_p(argv[1], S_IRWXU |
+                        S_IRGRP | S_IXGRP |
+                        S_IROTH | S_IXOTH);
+       if (rc)
+               printf("mkdir %s failed\n", argv[1]);
+       return rc;
+}
+
 
 int main(int argc, char *argv[])
 {
@@ -1102,6 +1151,8 @@ int main(int argc, char *argv[])
        { "--fs-root",       test_fsroot,          "<path>" },
        { "--cd-parent",     test_chdir,           "<path>" },
        { "--kernel-cmdline",test_kernel_cmdline,  "<option> | <option>=" },
+       { "--mkdir",         test_mkdir,           "<path>" },
+
        { NULL }
        };