From: Andre Noll Date: Tue, 1 Nov 2022 20:55:33 +0000 (+0100) Subject: fd.c: Improve error checking of para_mkdir(). X-Git-Tag: v0.7.3~13^2~1 X-Git-Url: http://git.tuebingen.mpg.de/versions/paraslash-0.3.1.tar.bz2.asc?a=commitdiff_plain;h=07a2d4510f0adc6eff211400f16a0760409bc507;p=paraslash.git fd.c: Improve error checking of para_mkdir(). 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. --- diff --git a/fd.c b/fd.c index 917ed186..f30bd8d6 100644 --- 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); } /**