summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2012-10-02 14:42:10 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2012-10-02 14:56:26 +0200
commit5b585b5380e8e9b7975905989042743911d699e2 (patch)
treecfdf65c07c08dd519ebaf68549dc179566544d37
parent27407a01c6c115ed09ad938ab95dcb56ab963ba9 (diff)
shared: fail mkdir_p if the target exists and is not a directory
This makes mkdir_p actually behave like mkdir -p.
-rw-r--r--src/shared/mkdir.c10
1 files changed, 9 insertions, 1 deletions
diff --git a/src/shared/mkdir.c b/src/shared/mkdir.c
index d654b632eb..0e51b64f69 100644
--- a/src/shared/mkdir.c
+++ b/src/shared/mkdir.c
@@ -115,6 +115,13 @@ int mkdir_parents_label(const char *path, mode_t mode) {
return makedir_parents(path, mode, true);
}
+static int is_dir(const char* path) {
+ struct stat st;
+ if (stat(path, &st) < 0)
+ return -errno;
+ return S_ISDIR(st.st_mode);
+}
+
static int makedir_p(const char *path, mode_t mode, bool apply) {
int r;
@@ -124,7 +131,8 @@ static int makedir_p(const char *path, mode_t mode, bool apply) {
if (r < 0)
return r;
- if (label_mkdir(path, mode, apply) < 0 && errno != EEXIST)
+ r = label_mkdir(path, mode, apply);
+ if (r < 0 && (errno != EEXIST || is_dir(path) <= 0))
return -errno;
return 0;