summaryrefslogtreecommitdiff
path: root/source4/lib/util.c
diff options
context:
space:
mode:
Diffstat (limited to 'source4/lib/util.c')
-rw-r--r--source4/lib/util.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/source4/lib/util.c b/source4/lib/util.c
index ed384572fe..7d3f21c1e8 100644
--- a/source4/lib/util.c
+++ b/source4/lib/util.c
@@ -88,6 +88,49 @@ BOOL directory_exist(const char *dname)
return ret;
}
+BOOL directory_create_or_exist(const char *dname, uid_t uid,
+ mode_t dir_perms)
+{
+ mode_t old_umask;
+ struct stat st;
+
+ old_umask = umask(0);
+ if (lstat(dname, &st) == -1) {
+ if (errno == ENOENT) {
+ /* Create directory */
+ if (mkdir(dname, dir_perms) == -1) {
+ DEBUG(0, ("error creating directory "
+ "%s: %s\n", dname,
+ strerror(errno)));
+ umask(old_umask);
+ return False;
+ }
+ } else {
+ DEBUG(0, ("lstat failed on directory %s: %s\n",
+ dname, strerror(errno)));
+ umask(old_umask);
+ return False;
+ }
+ } else {
+ /* Check ownership and permission on existing directory */
+ if (!S_ISDIR(st.st_mode)) {
+ DEBUG(0, ("directory %s isn't a directory\n",
+ dname));
+ umask(old_umask);
+ return False;
+ }
+ if ((st.st_uid != uid) ||
+ ((st.st_mode & 0777) != dir_perms)) {
+ DEBUG(0, ("invalid permissions on directory "
+ "%s\n", dname));
+ umask(old_umask);
+ return False;
+ }
+ }
+ return True;
+}
+
+
/*******************************************************************
Returns the size in bytes of the named file.
********************************************************************/