Fix concurent file creation
Summary: If someone was to call `EnsureDir` concurrently it might fail because the `fs::create_directories` returns false when the directory already exists with the `error_code` value being `0`. Reviewers: dgleich Reviewed By: dgleich Subscribers: pullbot Differential Revision: https://phabricator.memgraph.io/D1349
This commit is contained in:
parent
29234067d9
commit
753d57eb80
@ -14,9 +14,11 @@ namespace durability {
|
|||||||
namespace fs = std::experimental::filesystem;
|
namespace fs = std::experimental::filesystem;
|
||||||
/// Returns true if the given directory path exists or is succesfully created.
|
/// Returns true if the given directory path exists or is succesfully created.
|
||||||
bool EnsureDir(const fs::path &dir) {
|
bool EnsureDir(const fs::path &dir) {
|
||||||
if (fs::exists(dir)) return true;
|
|
||||||
std::error_code error_code; // Just for exception suppression.
|
std::error_code error_code; // Just for exception suppression.
|
||||||
return fs::create_directories(dir, error_code);
|
auto result = fs::create_directories(dir, error_code);
|
||||||
|
// The result will be false if the directory already exists. This is why we
|
||||||
|
// also check the error_code value.
|
||||||
|
return result || !error_code.value();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ensures the given durability directory exists and is ready for use. Creates
|
/// Ensures the given durability directory exists and is ready for use. Creates
|
||||||
@ -24,9 +26,9 @@ bool EnsureDir(const fs::path &dir) {
|
|||||||
void CheckDurabilityDir(const std::string &durability_dir) {
|
void CheckDurabilityDir(const std::string &durability_dir) {
|
||||||
namespace fs = std::experimental::filesystem;
|
namespace fs = std::experimental::filesystem;
|
||||||
if (fs::exists(durability_dir)) {
|
if (fs::exists(durability_dir)) {
|
||||||
CHECK(fs::is_directory(durability_dir))
|
CHECK(fs::is_directory(durability_dir)) << "The durability directory path '"
|
||||||
<< "The durability directory path '" << durability_dir
|
<< durability_dir
|
||||||
<< "' is not a directory!";
|
<< "' is not a directory!";
|
||||||
} else {
|
} else {
|
||||||
bool success = EnsureDir(durability_dir);
|
bool success = EnsureDir(durability_dir);
|
||||||
CHECK(success) << "Failed to create durability directory '"
|
CHECK(success) << "Failed to create durability directory '"
|
||||||
|
Loading…
Reference in New Issue
Block a user