summaryrefslogtreecommitdiff
path: root/libmount/src
diff options
context:
space:
mode:
authorKarel Zak <kzak@redhat.com>2013-01-09 18:05:08 +0100
committerKarel Zak <kzak@redhat.com>2013-01-09 18:05:08 +0100
commitfd73f46830fb14ef1da158b47241ee7673b905cd (patch)
tree1098d58a9ea80bdc07e80a87b91d73256bf27652 /libmount/src
parentf3242e065af2436ebf84c11e49101cce32a3a18b (diff)
downloadutil-linux-fd73f46830fb14ef1da158b47241ee7673b905cd.tar.gz
libmount; add recursive mkdir
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'libmount/src')
-rw-r--r--libmount/src/mountP.h3
-rw-r--r--libmount/src/utils.c51
2 files changed, 54 insertions, 0 deletions
diff --git a/libmount/src/mountP.h b/libmount/src/mountP.h
index dca70acc..5f2f780d 100644
--- a/libmount/src/mountP.h
+++ b/libmount/src/mountP.h
@@ -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));
diff --git a/libmount/src/utils.c b/libmount/src/utils.c
index bfd2fff2..fbfa89a4 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 }
};