diff options
author | Andreas Schneider <asn@samba.org> | 2013-01-08 14:21:00 +0100 |
---|---|---|
committer | Andrew Bartlett <abartlet@samba.org> | 2013-01-09 09:11:20 +0100 |
commit | 6039388fc1f3671bb60db06211814f7edfc62285 (patch) | |
tree | 4589a58f438fda9bff87e4998ccb8500cb033b44 /lib | |
parent | 5d721de7fdc250c6cb423c553134dd687590c1a0 (diff) | |
download | samba-6039388fc1f3671bb60db06211814f7edfc62285.tar.gz samba-6039388fc1f3671bb60db06211814f7edfc62285.tar.bz2 samba-6039388fc1f3671bb60db06211814f7edfc62285.zip |
util: Add a strict directory_create_or_exist function.
Reviewed-by: Andrew Bartlett <abartlet@samba.org>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/util/samba_util.h | 4 | ||||
-rw-r--r-- | lib/util/util.c | 49 |
2 files changed, 48 insertions, 5 deletions
diff --git a/lib/util/samba_util.h b/lib/util/samba_util.h index e553ec1342..27c2e6ed3c 100644 --- a/lib/util/samba_util.h +++ b/lib/util/samba_util.h @@ -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 diff --git a/lib/util/util.c b/lib/util/util.c index b50d28afcf..d49e20e6cd 100644 --- a/lib/util/util.c +++ b/lib/util/util.c @@ -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; -} +} /** |