You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ba...@apache.org on 2019/11/14 13:47:15 UTC

[nifi-minifi-cpp] branch master updated: MINIFICPP-1083 - Creating directory on Win fails in case it already exists

This is an automated email from the ASF dual-hosted git repository.

bakaid pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git


The following commit(s) were added to refs/heads/master by this push:
     new 672f441  MINIFICPP-1083 - Creating directory on Win fails in case it already exists
672f441 is described below

commit 672f441594375c51a95dc125c577b319232b77ce
Author: Arpad Boda <ab...@apache.org>
AuthorDate: Wed Nov 13 13:50:30 2019 +0100

    MINIFICPP-1083 - Creating directory on Win fails in case it already exists
    
    Signed-off-by: Daniel Bakai <ba...@apache.org>
    
    This closes #680
---
 libminifi/include/utils/file/FileUtils.h | 28 +++++++++++++---------------
 libminifi/test/unit/FileUtilsTests.cpp   | 13 +++++++++++++
 2 files changed, 26 insertions(+), 15 deletions(-)

diff --git a/libminifi/include/utils/file/FileUtils.h b/libminifi/include/utils/file/FileUtils.h
index bedfee8..7b90978 100644
--- a/libminifi/include/utils/file/FileUtils.h
+++ b/libminifi/include/utils/file/FileUtils.h
@@ -25,6 +25,7 @@
 #else
 #include <cstring>
 #include <cstdlib>
+#include <errno.h>
 #ifdef WIN32
 #ifndef WIN32_LEAN_AND_MEAN
 	#define WIN32_LEAN_AND_MEAN
@@ -38,7 +39,6 @@
 #include <sys/types.h>
 #include <utime.h>
 #include <dirent.h>
-#include <errno.h>
 #endif
 #endif
 #include <cstdio>
@@ -82,6 +82,14 @@ namespace file {
  *
  */
 class FileUtils {
+ private:
+  static inline int platform_create_dir(const std::string &path) {
+#ifdef WIN32
+    return _mkdir(path.c_str());
+#else
+    return mkdir(path.c_str(), 0700);
+#endif
+  }
  public:
 
   FileUtils() = delete;
@@ -302,28 +310,19 @@ class FileUtils {
       return -1;
     }
 #else
-#ifdef WIN32
-    struct _stat dir_stat;
-    if (_stat(path.c_str(), &dir_stat)) {
-      _mkdir(path.c_str());
-      return 0;
-    }
-#else
     if (!recursive) {
-        if (mkdir(path.c_str(), 0700) != 0 && errno != EEXIST) {
+        if (platform_create_dir(path) != 0 && errno != EEXIST) {
             return -1;
         }
         return 0;
     }
-
-    int ret = mkdir(path.c_str(), 0700);
-    if (ret == 0) {
+    if (platform_create_dir(path) == 0) {
         return 0;
     }
 
     switch (errno) {
     case ENOENT: {
-        size_t found = path.find_last_of(get_separator(0));
+        size_t found = path.find_last_of(get_separator());
 
         if (found == std::string::npos) {
             return -1;
@@ -334,7 +333,7 @@ class FileUtils {
         if (res < 0) {
             return -1;
         }
-        return mkdir(path.c_str(), 0700);
+        return platform_create_dir(path);
     }
     case EEXIST: {
         if (is_directory(path.c_str())) {
@@ -345,7 +344,6 @@ class FileUtils {
     default:
         return -1;
     }
-#endif
     return -1;
 #endif
   }
diff --git a/libminifi/test/unit/FileUtilsTests.cpp b/libminifi/test/unit/FileUtilsTests.cpp
index ca888e5..d2a4b9c 100644
--- a/libminifi/test/unit/FileUtilsTests.cpp
+++ b/libminifi/test/unit/FileUtilsTests.cpp
@@ -133,3 +133,16 @@ TEST_CASE("TestFileUtils::get_executable_dir", "[TestGetExecutableDir]") {
   std::cerr << "Executable dir: " << executable_dir << std::endl;
   REQUIRE(FileUtils::get_parent_path(executable_path) == executable_dir);
 }
+
+TEST_CASE("TestFileUtils::create_dir", "[TestCreateDir]") {
+  TestController testController;
+
+  char format[] = "/tmp/gt.XXXXXX";
+  auto dir = testController.createTempDirectory(format);
+
+  std::string test_dir_path = std::string(dir) + FileUtils::get_separator() + "random_dir";
+
+  REQUIRE(FileUtils::create_dir(test_dir_path) == 0);  // Dir has to be created successfully
+  REQUIRE(FileUtils::create_dir(test_dir_path) == 0);  // Dir already exists, success should be returned
+  REQUIRE(FileUtils::delete_dir(test_dir_path) == 0);  // Delete should be successful as welll
+}