util: Allow symlinks in directory_create_or_exist
authorChristof Schmitt <cs@samba.org>
Fri, 14 Aug 2020 16:36:26 +0000 (09:36 -0700)
committerVolker Lendecke <vl@samba.org>
Sun, 16 Aug 2020 05:45:35 +0000 (05:45 +0000)
Commit 9f60a77e0b updated the check to avoid having files or other
objects instead of a directory. This missed the valid case that there
might be a symlink to a directory. Updated the check accordingly to
allow symlinks to directories.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=14166

Signed-off-by: Christof Schmitt <cs@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
lib/util/util.c

index 1f37688cbad85f9db04592d3ac1577a346271c20..d7331f743f31fc9f105367d11857f01c583e2f81 100644 (file)
@@ -191,6 +191,7 @@ _PUBLIC_ bool directory_exist(const char *dname)
 
 /**
  * Try to create the specified directory if it didn't exist.
+ * A symlink to a directory is also accepted as a valid existing directory.
  *
  * @retval true if the directory already existed
  * or was successfully created.
@@ -224,9 +225,22 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname,
                        return false;
                }
 
-               if (!S_ISDIR(sbuf.st_mode)) {
-                       return false;
+               if (S_ISDIR(sbuf.st_mode)) {
+                       return true;
                }
+
+               if (S_ISLNK(sbuf.st_mode)) {
+                       ret = stat(dname, &sbuf);
+                       if (ret != 0) {
+                               return false;
+                       }
+
+                       if (S_ISDIR(sbuf.st_mode)) {
+                               return true;
+                       }
+               }
+
+               return false;
        }
 
        return true;