Skip to content

Commit

Permalink
Non throwing directory creation function. (osquery#5206)
Browse files Browse the repository at this point in the history
Directory creation function to osquery/filesystem.h

Status was used as a return value, as far as all function in this file operate with Status not Expected. Let's move from Status to Expected in another PR.
  • Loading branch information
akindyakov authored Sep 12, 2018
1 parent e2f1a11 commit 9b3e147
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 3 deletions.
13 changes: 13 additions & 0 deletions include/osquery/filesystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,19 @@ Status movePath(const boost::filesystem::path& from,
*/
Status isDirectory(const boost::filesystem::path& path);

/**
* @brief Create the directory
*
* @param path to the intended directory
* @param recursive - make parent directories as needed
* @param ignore_existence - no error if directory already exists
*
* @return Status of operation
*/
Status createDirectory(const boost::filesystem::path& path,
bool recursive = false,
bool ignore_existence = false);

/**
* @brief Return a vector of all home directories on the system.
*
Expand Down
26 changes: 26 additions & 0 deletions osquery/filesystem/filesystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,32 @@ Status isDirectory(const fs::path& path) {
return Status(ec.value(), ec.message());
}

Status createDirectory(const boost::filesystem::path& dir_path,
bool const recursive,
bool const ignore_existence) {
auto err = boost::system::error_code{};
bool is_created = false;
if (recursive) {
is_created = boost::filesystem::create_directories(dir_path, err);
} else {
is_created = boost::filesystem::create_directory(dir_path, err);
}
if (is_created) {
return Status::success();
}
if (ignore_existence && isDirectory(dir_path).ok()) {
return Status::success();
}
auto msg = std::string{"Could not create directory \""};
msg += dir_path.string();
msg += '"';
if (err) {
msg += ": ";
msg += err.message();
}
return Status::failure(msg);
}

std::set<fs::path> getHomeDirectories() {
std::set<fs::path> results;

Expand Down
88 changes: 85 additions & 3 deletions osquery/filesystem/tests/filesystem_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,12 @@ TEST_F(FilesystemTests, test_remove_path) {
fs::path test_file(kTestWorkingDirectory + "rmdir/rmfile");
writeTextFile(test_file, "testcontent");

ASSERT_TRUE(pathExists(test_file));
ASSERT_TRUE(pathExists(test_file).ok());

// Try to remove the directory.
EXPECT_TRUE(removePath(test_dir));
EXPECT_FALSE(pathExists(test_file));
EXPECT_FALSE(pathExists(test_dir));
EXPECT_FALSE(pathExists(test_file).ok());
EXPECT_FALSE(pathExists(test_dir).ok());
}

TEST_F(FilesystemTests, test_write_file) {
Expand Down Expand Up @@ -492,4 +492,86 @@ TEST_F(FilesystemTests, test_read_urandom) {
EXPECT_NE(first, second);
}
}

TEST_F(FilesystemTests, create_directory) {
auto const recursive = false;
auto const ignore_existence = false;
const auto tmp_path =
fs::temp_directory_path() /
fs::unique_path("osquery.tests.create_directory.%%%%.%%%%");
ASSERT_FALSE(fs::exists(tmp_path));
ASSERT_TRUE(createDirectory(tmp_path, recursive, ignore_existence).ok());
ASSERT_TRUE(fs::exists(tmp_path));
ASSERT_TRUE(fs::is_directory(tmp_path));
ASSERT_FALSE(createDirectory(tmp_path).ok());
fs::remove(tmp_path);
}

TEST_F(FilesystemTests, create_directory_without_parent) {
auto const recursive = false;
auto const ignore_existence = false;
const auto tmp_root_path =
fs::temp_directory_path() /
fs::unique_path(
"osquery.tests.create_directory_without_parent.%%%%.%%%%");
ASSERT_FALSE(fs::exists(tmp_root_path));
auto const tmp_path = tmp_root_path / "one_more";
ASSERT_FALSE(fs::exists(tmp_path));
ASSERT_FALSE(createDirectory(tmp_path, recursive, ignore_existence).ok());
ASSERT_FALSE(fs::exists(tmp_path));
ASSERT_FALSE(fs::is_directory(tmp_path));
fs::remove_all(tmp_root_path);
}

TEST_F(FilesystemTests, create_directory_recursive) {
auto const recursive = true;
auto const ignore_existence = false;
const auto tmp_root_path =
fs::temp_directory_path() /
fs::unique_path("osquery.tests.create_directory_recursive.%%%%.%%%%");
ASSERT_FALSE(fs::exists(tmp_root_path));
auto const tmp_path = tmp_root_path / "one_more";
ASSERT_FALSE(fs::exists(tmp_path));
ASSERT_TRUE(createDirectory(tmp_path, recursive, ignore_existence).ok());
ASSERT_TRUE(fs::exists(tmp_path));
ASSERT_TRUE(fs::is_directory(tmp_path));
fs::remove_all(tmp_root_path);
}

TEST_F(FilesystemTests, create_directory_recursive_on_existing_dir) {
auto const recursive = true;
auto const ignore_existence = false;
const auto tmp_root_path =
fs::temp_directory_path() /
fs::unique_path(
"osquery.tests.create_directory_recursive_on_existing_dir.%%%%.%%%%");
auto const tmp_path = tmp_root_path / "one_more";
fs::create_directories(tmp_path);

ASSERT_TRUE(fs::exists(tmp_path));
ASSERT_TRUE(fs::is_directory(tmp_path));
ASSERT_FALSE(createDirectory(tmp_path, recursive, ignore_existence).ok());
ASSERT_TRUE(fs::exists(tmp_path));
ASSERT_TRUE(fs::is_directory(tmp_path));
fs::remove_all(tmp_root_path);
}

TEST_F(FilesystemTests, create_dir_recursive_ignore_existence) {
auto const recursive = true;
auto const ignore_existence = true;
const auto tmp_root_path =
fs::temp_directory_path() /
fs::unique_path(
"osquery.tests.create_dir_recursive_ignore_existence.%%%%.%%%%");
auto const tmp_path = tmp_root_path / "one_more";
fs::create_directories(tmp_path);

ASSERT_TRUE(fs::exists(tmp_path));
ASSERT_TRUE(fs::is_directory(tmp_path));
ASSERT_TRUE(createDirectory(tmp_path, recursive, ignore_existence).ok());
ASSERT_TRUE(fs::exists(tmp_path));
ASSERT_TRUE(fs::is_directory(tmp_path));
fs::remove_all(tmp_root_path);
}

} // namespace osquery

0 comments on commit 9b3e147

Please sign in to comment.