util: Add a strict directory_create_or_exist function.
authorAndreas Schneider <asn@samba.org>
Tue, 8 Jan 2013 13:21:00 +0000 (14:21 +0100)
committerAndrew Bartlett <abartlet@samba.org>
Wed, 9 Jan 2013 08:11:20 +0000 (09:11 +0100)
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
lib/util/samba_util.h
lib/util/util.c

index e553ec1342db4096726cde4137939fca388914c4..27c2e6ed3c43f8126cbfb721bf41fe0c9d237f53 100644 (file)
@@ -631,6 +631,10 @@ _PUBLIC_ bool directory_exist(const char *dname);
 _PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid, 
                               mode_t dir_perms);
 
+_PUBLIC_ bool directory_create_or_exist_strict(const char *dname,
+                                              uid_t uid,
+                                              mode_t dir_perms);
+
 /**
  Set a fd into blocking/nonblocking mode. Uses POSIX O_NONBLOCK if available,
  else
index b50d28afcf34b9c2df1f25891125733f0a3072d2..d49e20e6cd5f99bc4b74edf5d8e3062d34ccfb18 100644 (file)
@@ -143,12 +143,13 @@ _PUBLIC_ bool directory_exist(const char *dname)
  * @retval true if the directory already existed and has the right permissions 
  * or was successfully created.
  */
-_PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid, 
-                              mode_t dir_perms)
+_PUBLIC_ bool directory_create_or_exist(const char *dname,
+                                       uid_t uid,
+                                       mode_t dir_perms)
 {
        int ret;
-       struct stat st;
-      
+       struct stat st;
+
        ret = lstat(dname, &st);
        if (ret == -1) {
                mode_t old_umask;
@@ -179,6 +180,44 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid,
                }
        }
 
+       return true;
+}
+
+/**
+ * @brief Try to create a specified directory if it doesn't exist.
+ *
+ * The function creates a directory with the given uid and permissions if it
+ * doesn't exixt. If it exists it makes sure the uid and permissions are
+ * correct and it will fail if they are different.
+ *
+ * @param[in]  dname  The directory to create.
+ *
+ * @param[in]  uid    The uid the directory needs to belong too.
+ *
+ * @param[in]  dir_perms  The expected permissions of the directory.
+ *
+ * @return True on success, false on error.
+ */
+_PUBLIC_ bool directory_create_or_exist_strict(const char *dname,
+                                              uid_t uid,
+                                              mode_t dir_perms)
+{
+       struct stat st;
+       bool ok;
+       int rc;
+
+       ok = directory_create_or_exist(dname, uid, dir_perms);
+       if (!ok) {
+               return false;
+       }
+
+       rc = lstat(dname, &st);
+       if (rc == -1) {
+               DEBUG(0, ("lstat failed on created directory %s: %s\n",
+                         dname, strerror(errno)));
+               return false;
+       }
+
        /* Check ownership and permission on existing directory */
        if (!S_ISDIR(st.st_mode)) {
                DEBUG(0, ("directory %s isn't a directory\n",
@@ -198,7 +237,7 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname, uid_t uid,
        }
 
        return true;
-}       
+}
 
 
 /**