]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
fd.c: Improve error checking of para_mkdir().
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 1 Nov 2022 20:55:33 +0000 (21:55 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Sun, 7 May 2023 18:45:22 +0000 (20:45 +0200)
The old code returned success in case the pathname existed but was
no directory, so try to improve on this a bit. However, don't be
over-zealous as any pathname based approach won't be bullet-proof
because the file identified by the pathname may change at any time.

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);
 }
 
 /**