The branch, master has been updated
       via  83f0ac5 torture: Add test for directory_create_or_exist
       via  9f60a77 lib: Fix lstat check in directory_create_or_exist
      from  58b8f2a ctdb-common: Clean up comments in TCP packet parsing

https://git.samba.org/?p=samba.git;a=shortlog;h=master


- Log -----------------------------------------------------------------
commit 83f0ac5c6da48c1a68f6a7b29992398735abb421
Author: Christof Schmitt <[email protected]>
Date:   Wed Aug 29 12:07:42 2018 -0700

    torture: Add test for directory_create_or_exist
    
    Signed-off-by: Christof Schmitt <[email protected]>
    Reviewed-by: Andreas Schneider <[email protected]>
    
    Autobuild-User(master): Christof Schmitt <[email protected]>
    Autobuild-Date(master): Thu Aug 30 21:19:31 CEST 2018 on sn-devel-144

commit 9f60a77e0b4c8ad06a8156ff5b5ebc5902d4ec17
Author: Christof Schmitt <[email protected]>
Date:   Wed Aug 29 12:04:29 2018 -0700

    lib: Fix lstat check in directory_create_or_exist
    
    The lstat check in directory_create_or_exist did not verify whether an
    existing object is actually a directory. Also move the check to only
    apply when mkdir returns EEXIST; this fixes CID 241930 Time of check
    time of use.
    
    Signed-off-by: Christof Schmitt <[email protected]>
    Reviewed-by: Andreas Schneider <[email protected]>

-----------------------------------------------------------------------

Summary of changes:
 lib/util/tests/util.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/util/util.c       | 28 ++++++++++--------------
 2 files changed, 71 insertions(+), 17 deletions(-)


Changeset truncated at 500 lines:

diff --git a/lib/util/tests/util.c b/lib/util/tests/util.c
index ff98738..10d475d 100644
--- a/lib/util/tests/util.c
+++ b/lib/util/tests/util.c
@@ -2,6 +2,7 @@
  * Tests for strv_util
  *
  * Copyright Martin Schwenke <[email protected]> 2016
+ * Copyright Christof Schmitt <[email protected]> 2018
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -18,6 +19,7 @@
  */
 
 #include <talloc.h>
+#include <fcntl.h>
 
 #include "replace.h"
 
@@ -364,6 +366,61 @@ static bool test_trim_string(struct torture_context *tctx)
        return true;
 }
 
+static bool test_directory_create_or_exist(struct torture_context *tctx)
+{
+       char *path = NULL, *new_path = NULL, *file_path = NULL;
+       bool ret = true, b = true;
+       int fd;
+       NTSTATUS status;
+       const mode_t perms = 0741;
+
+       status = torture_temp_dir(tctx, "util_dir", &path);;
+       torture_assert_ntstatus_ok_goto(tctx, status, ret, done,
+                                       "Creating test directory failed.\n");
+
+       b = directory_create_or_exist(path, perms);
+       torture_assert_goto(tctx, b == true, ret, done,
+                           "directory_create_or_exist on "
+                           "existing directory failed.\n");
+
+       new_path = talloc_asprintf(tctx, "%s/%s", path, "dir");
+       torture_assert_goto(tctx, new_path != NULL, ret, done,
+                           "Could not allocate memory for directory path\n");
+
+       b = directory_exist(new_path);
+       torture_assert_goto(tctx, b == false, ret, done,
+                           "Check for non-existing directory failed.\n");
+
+       b = directory_create_or_exist(new_path, perms);
+       torture_assert_goto(tctx, b == true, ret, done,
+                           "directory_create_or_exist for "
+                           "new directory failed.\n");
+
+       b = directory_exist(new_path);
+       torture_assert_goto(tctx, b == true, ret, done,
+                           "Check for existing directory failed.\n");
+
+       b = file_check_permissions(new_path, geteuid(), perms, NULL);
+       torture_assert_goto(tctx, b == true, ret, done,
+                           "Permission check for directory failed.\n");
+
+       file_path = talloc_asprintf(tctx, "%s/%s", path, "file");
+       torture_assert_goto(tctx, file_path != NULL, ret, done,
+                           "Could not allocate memory for file path\n");
+       fd = creat(file_path, perms);
+       torture_assert_goto(tctx, fd != -1, ret, done,
+                           "Creating file failed.\n");
+       close(fd);
+
+       b = directory_create_or_exist(file_path, perms);
+       torture_assert_goto(tctx, b == false, ret, done,
+                           "directory_create_or_exist for "
+                           "existing file failed.\n");
+
+done:
+       return ret;
+}
+
 struct torture_suite *torture_local_util(TALLOC_CTX *mem_ctx)
 {
        struct torture_suite *suite =
@@ -372,5 +429,8 @@ struct torture_suite *torture_local_util(TALLOC_CTX 
*mem_ctx)
        torture_suite_add_simple_test(suite,
                                      "trim_string",
                                      test_trim_string);
+       torture_suite_add_simple_test(suite,
+                                     "directory_create_or_exist",
+                                     test_directory_create_or_exist);
        return suite;
 }
diff --git a/lib/util/util.c b/lib/util/util.c
index 4291bfa..f52f69c 100644
--- a/lib/util/util.c
+++ b/lib/util/util.c
@@ -194,20 +194,8 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname,
                                        mode_t dir_perms)
 {
        int ret;
-       struct stat st;
        mode_t old_umask;
 
-       ret = lstat(dname, &st);
-       if (ret == 0) {
-               return true;
-       }
-
-       if (errno != ENOENT) {
-               DBG_WARNING("lstat failed on directory %s: %s\n",
-                           dname, strerror(errno));
-               return false;
-       }
-
        /* Create directory */
        old_umask = umask(0);
        ret = mkdir(dname, dir_perms);
@@ -220,11 +208,17 @@ _PUBLIC_ bool directory_create_or_exist(const char *dname,
        }
        umask(old_umask);
 
-       ret = lstat(dname, &st);
-       if (ret == -1) {
-               DEBUG(0, ("lstat failed on created directory %s: %s\n",
-                         dname, strerror(errno)));
-               return false;
+       if (ret != 0 && errno == EEXIST) {
+               struct stat sbuf;
+
+               ret = lstat(dname, &sbuf);
+               if (ret != 0) {
+                       return false;
+               }
+
+               if (!S_ISDIR(sbuf.st_mode)) {
+                       return false;
+               }
        }
 
        return true;


-- 
Samba Shared Repository

Reply via email to