commit 6ac5f01cc94b2a6f7b6406ddd151e7b4d8fb1d7d
Author:     Michael Forney <mfor...@mforney.org>
AuthorDate: Wed Dec 14 19:40:05 2016 -0800
Commit:     Anselm R Garbe <ans...@garbe.us>
CommitDate: Mon Jul 3 21:03:09 2017 +0200

    mkdir -p: Fail if argument exists, but is not a directory
    
    If it is a directory, we can just return straightaway.

diff --git a/libutil/mkdirp.c b/libutil/mkdirp.c
index 7796e24..2ef94a3 100644
--- a/libutil/mkdirp.c
+++ b/libutil/mkdirp.c
@@ -10,6 +10,15 @@ int
 mkdirp(const char *path)
 {
        char tmp[PATH_MAX], *p;
+       struct stat st;
+
+       if (stat(path, &st) == 0) {
+               if (S_ISDIR(st.st_mode))
+                       return 0;
+               errno = ENOTDIR;
+               weprintf("%s:", path);
+               return -1;
+       }
 
        estrlcpy(tmp, path, sizeof(tmp));
        for (p = tmp + (tmp[0] == '/'); *p; p++) {

Reply via email to