Skip to content

Commit

Permalink
Fix filesystem helpers for directory manipulation. (#30)
Browse files Browse the repository at this point in the history
* Fix filesystem helpers for directory manipulation.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>

* Adjust style.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>

* Make rcpputils::fs functions to behave like std::filesystem ones.

Signed-off-by: Michel Hidalgo <michel@ekumenlabs.com>
  • Loading branch information
hidmic authored and Prajakta Gokhale committed Jan 15, 2020
1 parent 33e2edb commit f601ee5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
36 changes: 30 additions & 6 deletions include/rcpputils/filesystem_helper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,17 +182,41 @@ inline bool exists(const path & path_to_check)
inline bool create_directories(const path & p)
{
path p_built;
int status = 0;

for (auto it = p.cbegin(); it != p.cend(); ++it) {
p_built /= *it;

for (auto it = p.cbegin(); it != p.cend() && status == 0; ++it) {
if (p_built.empty()) {
p_built = *it;
} else {
p_built /= *it;
}
if (!p_built.exists()) {
#ifdef _WIN32
_mkdir(p_built.string().c_str());
status = _mkdir(p_built.string().c_str());
#else
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
}
}
return status == 0;
}

inline bool remove(const path & p)
{
#ifdef _WIN32
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 true;
return false;
#else
return ::remove(p.string().c_str()) == 0;
#endif
}

/**
Expand Down
20 changes: 16 additions & 4 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,15 +151,26 @@ TEST(TestFilesystemHelper, is_empty)
}

/**
* Test create directories.
* Test filesystem manipulation API.
*
* NOTE: expects the current directory to be write-only, else test will fail.
*
*/
TEST(TestFilesystemHelper, create_directories)
TEST(TestFilesystemHelper, filesystem_manipulation)
{
auto p = path(build_directory_path());
EXPECT_TRUE(rcpputils::fs::create_directories(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 f601ee5

Please sign in to comment.