diff --git a/src/base/c_utils_posix.h b/src/base/c_utils_posix.h index a296a3d5..08b5320d 100644 --- a/src/base/c_utils_posix.h +++ b/src/base/c_utils_posix.h @@ -104,6 +104,7 @@ inline bool GetSelfPath(String& result, String* error) { return true; } +// TODO: refactor on |std::filesystem|. inline bool SetPermissions(const String& path, int mask, String* error) { if (chmod(path.c_str(), mask) == -1) { GetLastError(error); diff --git a/src/base/file_utils.h b/src/base/file_utils.h index 29a7c066..30e4ee38 100644 --- a/src/base/file_utils.h +++ b/src/base/file_utils.h @@ -7,15 +7,20 @@ namespace dist_clang { namespace base { +// TODO: refactor on |std::filesystem|. void WalkDirectory( const Path& path, Fn visitor, String* error = nullptr); + +// TODO: refactor on |std::filesystem|. ui64 CalculateDirectorySize(const Path& path, String* error = nullptr); +// TODO: refactor on |std::filesystem| and |std::chrono|. Pair GetModificationTime( const String& path, String* error = nullptr); +// TODO: refactor on |std::filesystem|. inline bool RemoveEmptyDirectory(const String& path) { return !rmdir(path.c_str()); } @@ -28,7 +33,17 @@ inline bool ChangeOwner(const String& path, ui32 uid, String* error = nullptr) { return true; } -bool CreateDirectory(const String& path, String* error = nullptr); +inline bool CreateDirectory(const Path& path, String* error = nullptr) { + std::error_code ec; + std::experimental::filesystem::create_directories(path, ec); + if (ec) { + if (error) { + *error = ec.message(); + } + return false; + } + return true; +} inline bool ChangeCurrentDir(const Path& path, String* error = nullptr) { std::error_code ec; diff --git a/src/base/file_utils_posix.cc b/src/base/file_utils_posix.cc index 2cb5260e..e34624a8 100644 --- a/src/base/file_utils_posix.cc +++ b/src/base/file_utils_posix.cc @@ -95,22 +95,5 @@ Pair GetModificationTime(const String& path, String* error) { return {time_spec.tv_sec, time_spec.tv_nsec}; } -bool CreateDirectory(const String& path, String* error) { - for (size_t i = 1; i < path.size(); ++i) { - if (path[i] == '/' && mkdir(path.substr(0, i).c_str(), 0755) == -1 && - errno != EEXIST) { - GetLastError(error); - return false; - } - } - - if (mkdir(path.c_str(), 0755) == -1 && errno != EEXIST) { - GetLastError(error); - return false; - } - - return true; -} - } // namespace base } // namespace dist_clang diff --git a/src/base/file_utils_test.cc b/src/base/file_utils_test.cc index 69126d16..0250ebf3 100644 --- a/src/base/file_utils_test.cc +++ b/src/base/file_utils_test.cc @@ -61,6 +61,7 @@ TEST(FileUtilsTest, CreateDirectory) { const TemporaryDir temp_dir; const auto temp = temp_dir.path() / "1" / "2" / "3"; + ASSERT_TRUE(CreateDirectory(temp, &error)) << error; ASSERT_TRUE(CreateDirectory(temp, &error)) << error; DIR* dir = opendir(temp.c_str());