diff options
author | Karel Zak | 2013-01-09 18:05:08 +0100 |
---|---|---|
committer | Karel Zak | 2013-01-09 18:05:08 +0100 |
commit | fd73f46830fb14ef1da158b47241ee7673b905cd (patch) | |
tree | 1098d58a9ea80bdc07e80a87b91d73256bf27652 /libmount/src/utils.c | |
parent | mount: document x-* options (diff) | |
download | kernel-qcow2-util-linux-fd73f46830fb14ef1da158b47241ee7673b905cd.tar.gz kernel-qcow2-util-linux-fd73f46830fb14ef1da158b47241ee7673b905cd.tar.xz kernel-qcow2-util-linux-fd73f46830fb14ef1da158b47241ee7673b905cd.zip |
libmount; add recursive mkdir
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/src/utils.c')
-rw-r--r-- | libmount/src/utils.c | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/libmount/src/utils.c b/libmount/src/utils.c index bfd2fff2a..fbfa89a4c 100644 --- a/libmount/src/utils.c +++ b/libmount/src/utils.c @@ -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 } }; |