Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DM-39783: Reinforced the implementation to check if the desired folder exists #794

Merged
merged 1 commit into from
Jun 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/replica/FileUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,22 @@ void FileUtils::verifyFolders(string const& requestorContext, vector<string> con
}
boost::system::error_code ec;
if (createMissingFolders) {
if (!fs::create_directories(path, ec)) {
if (fs::exists(path, ec)) {
bool const isDirectory = fs::is_directory(path, ec);
if (ec.value() != 0) {
throw runtime_error(context + " failed to create folder '" + folder +
"' or its intermediate subfolders, error: " + ec.message());
throw runtime_error(context + " failed to check if the path '" + folder +
"' is a directory, error: " + ec.message());
}
if (!isDirectory) {
throw runtime_error(context + " specified path '" + folder +
"' is not a valid directory");
}
} else {
if (!fs::create_directories(path, ec)) {
if (ec.value() != 0) {
throw runtime_error(context + " failed to create folder '" + folder +
"' or its intermediate subfolders, error: " + ec.message());
}
}
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/replica/testFileUtils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,10 @@ BOOST_AUTO_TEST_CASE(FileUtils_verifyFolders) {
// Now launch the method to force create the folder.
BOOST_REQUIRE_NO_THROW({ FileUtils::verifyFolders("TEST", folders, createMissingFolders); });

// Repeat the preious operation. It should not fail since the method first checks
// if the path already exists and if it's a valid directory before creating the one.
BOOST_REQUIRE_NO_THROW({ FileUtils::verifyFolders("TEST", folders, createMissingFolders); });

// Make another run w/o attempting creating a folder
BOOST_REQUIRE_NO_THROW({ FileUtils::verifyFolders("TEST", folders, !createMissingFolders); });

Expand Down