]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - fd.c
fd.c: Improve error checking of para_mkdir().
[paraslash.git] / fd.c
diff --git a/fd.c b/fd.c
index 917ed186a00182568c3aa6618ef54d771e59ea17..f30bd8d69d0e17a54673fb367b0f82ff1d62b09f 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -447,17 +447,25 @@ close_cwd:
  * This function passes the fixed mode value 0777 to mkdir(3) (which consults
  * the file creation mask and restricts this value).
  *
- * \return Zero if the directory already existed, one if the directory has been
- * created, negative error code if the mkdir(3) call failed for any reason
- * other than EEXIST.
+ * \return Zero if the path already existed as a directory or as a symbolic
+ * link which leads to a directory, one if the path did not exist and the
+ * directory has been created successfully, negative error code else.
  */
 int para_mkdir(const char *path)
 {
-       if (mkdir(path, 0777) == 0)
-               return 1;
-       if (errno == EEXIST)
+       /*
+        * We call opendir(3) rather than relying on stat(2) because this way
+        * we don't need extra code to get the symlink case right.
+        */
+       DIR *dir = opendir(path);
+
+       if (dir) {
+               closedir(dir);
                return 0;
-       return -ERRNO_TO_PARA_ERROR(errno);
+       }
+       if (errno != ENOENT)
+               return -ERRNO_TO_PARA_ERROR(errno);
+       return mkdir(path, 0777) == 0? 1 : -ERRNO_TO_PARA_ERROR(errno);
 }
 
 /**