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.
* This function passes the fixed mode value 0777 to mkdir(3) (which consults
* the file creation mask and restricts this value).
*
* 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)
{
*/
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 -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);