Skip to content

Commit

Permalink
Make rcpputils::fs functions to behave like std::filesystem ones.
Browse files Browse the repository at this point in the history
Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic committed Jan 14, 2020
1 parent 6ac7378 commit 449e1a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
15 changes: 12 additions & 3 deletions include/rcpputils/filesystem_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ inline bool create_directories(const path & p)
#ifdef _WIN32
status = _mkdir(p_built.string().c_str());
#else
status = mkdir(p_built.string().c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
status = mkdir(p_built.string().c_str(), S_IRWXU | S_IRWXG | S_IRWXO);
#endif
}
}
Expand All @@ -204,9 +204,18 @@ inline bool create_directories(const path & p)
inline bool remove(const path & p)
{
#ifdef _WIN32
return _rmdir(p.string().c_str()) == 0;
struct _stat s;
if (_stat(p.string().c_str(), &s) == 0) {
if (s.st_mode & S_IFDIR) {
return _rmdir(p.string().c_str()) == 0;
}
if (s.st_mode & S_IFREG) {
return ::remove(p.string().c_str()) == 0;
}
}
return false;
#else
return rmdir(p.string().c_str()) == 0;
return ::remove(p.string().c_str()) == 0;
#endif
}

Expand Down
25 changes: 16 additions & 9 deletions test/test_filesystem_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include <gtest/gtest.h>

#include <fstream>
#include <string>

#include "rcpputils/filesystem_helper.hpp"
Expand Down Expand Up @@ -150,20 +151,26 @@ TEST(TestFilesystemHelper, is_empty)
}

/**
* Test directory manipulation API.
* Test filesystem manipulation API.
*
* NOTE: expects the current directory to be write-only, else test will fail.
*
*/
TEST(TestFilesystemHelper, directory_manipulation)
TEST(TestFilesystemHelper, filesystem_manipulation)
{
auto p = path(build_directory_path());
(void)rcpputils::fs::remove(p);
EXPECT_FALSE(rcpputils::fs::exists(p));
EXPECT_TRUE(rcpputils::fs::create_directories(p));
EXPECT_TRUE(rcpputils::fs::exists(p));
EXPECT_TRUE(rcpputils::fs::remove(p));
EXPECT_FALSE(rcpputils::fs::exists(p));
auto dir = path(build_directory_path());
(void)rcpputils::fs::remove(dir);
EXPECT_FALSE(rcpputils::fs::exists(dir));
EXPECT_TRUE(rcpputils::fs::create_directories(dir));
EXPECT_TRUE(rcpputils::fs::exists(dir));
auto file = dir / "test_file.txt";
std::ofstream(file.string()) << "test";
EXPECT_TRUE(rcpputils::fs::exists(file));
EXPECT_FALSE(rcpputils::fs::remove(dir));
EXPECT_TRUE(rcpputils::fs::remove(file));
EXPECT_TRUE(rcpputils::fs::remove(dir));
EXPECT_FALSE(rcpputils::fs::exists(file));
EXPECT_FALSE(rcpputils::fs::exists(dir));
}

TEST(TestFilesystemHelper, remove_extension)
Expand Down

0 comments on commit 449e1a4

Please sign in to comment.