From 1ba186f3435ad0488890d5c087bb07a2199d349e Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Thu, 24 Sep 2020 13:01:50 -0400 Subject: [PATCH 1/3] Make sure that an existing path is a directory for create_directories Signed-off-by: Christophe Bedard --- include/rcpputils/filesystem_helper.hpp | 2 +- test/test_filesystem_helper.cpp | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/include/rcpputils/filesystem_helper.hpp b/include/rcpputils/filesystem_helper.hpp index 507fdba9..815da204 100644 --- a/include/rcpputils/filesystem_helper.hpp +++ b/include/rcpputils/filesystem_helper.hpp @@ -511,7 +511,7 @@ inline bool create_directories(const path & p) #endif } } - return status == 0; + return p_built.is_directory() && status == 0; } /** diff --git a/test/test_filesystem_helper.cpp b/test/test_filesystem_helper.cpp index 11952d66..dc697b69 100644 --- a/test/test_filesystem_helper.cpp +++ b/test/test_filesystem_helper.cpp @@ -286,6 +286,7 @@ TEST(TestFilesystemHelper, filesystem_manipulation) EXPECT_TRUE(rcpputils::fs::exists(file)); EXPECT_TRUE(rcpputils::fs::is_regular_file(file)); EXPECT_FALSE(rcpputils::fs::is_directory(file)); + EXPECT_FALSE(rcpputils::fs::create_directories(file)); EXPECT_GE(rcpputils::fs::file_size(file), expected_file_size); EXPECT_THROW(rcpputils::fs::file_size(dir), std::system_error) << "file_size is only applicable for files!"; From a903aaa848b7fe09015d6e0c65fcd2a4bc9cf3b7 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Mon, 28 Sep 2020 09:18:10 -0400 Subject: [PATCH 2/3] Add test call to fs::create_directories with empty path Signed-off-by: Christophe Bedard --- test/test_filesystem_helper.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test/test_filesystem_helper.cpp b/test/test_filesystem_helper.cpp index dc697b69..a83047f0 100644 --- a/test/test_filesystem_helper.cpp +++ b/test/test_filesystem_helper.cpp @@ -353,6 +353,9 @@ TEST(TestFilesystemHelper, filesystem_manipulation) ASSERT_FALSE(rcpputils::fs::exists(file)); } ASSERT_FALSE(rcpputils::fs::exists(dir)); + + // Empty path/directory cannot be created + EXPECT_FALSE(rcpputils::fs::create_directories(rcpputils::fs::path(""))); } TEST(TestFilesystemHelper, remove_extension) From 2e0315375c8f0bfc350d96ea08d981a81ceb8363 Mon Sep 17 00:00:00 2001 From: Christophe Bedard Date: Mon, 28 Sep 2020 10:39:30 -0400 Subject: [PATCH 3/3] Check status variable first to possibly avoid calling is_directory() Signed-off-by: Christophe Bedard --- include/rcpputils/filesystem_helper.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rcpputils/filesystem_helper.hpp b/include/rcpputils/filesystem_helper.hpp index 815da204..93fa580d 100644 --- a/include/rcpputils/filesystem_helper.hpp +++ b/include/rcpputils/filesystem_helper.hpp @@ -511,7 +511,7 @@ inline bool create_directories(const path & p) #endif } } - return p_built.is_directory() && status == 0; + return status == 0 && p_built.is_directory(); } /**