From af78cd60503bd7b99390605fb70efded64aa8931 Mon Sep 17 00:00:00 2001 From: "Alexander A. Klimov" Date: Wed, 10 Apr 2019 11:21:05 +0200 Subject: [PATCH] Use Boost.Filesystem refs #7101 --- lib/base/utility.cpp | 118 +++++++------------------------------------ lib/base/utility.hpp | 1 - 2 files changed, 17 insertions(+), 102 deletions(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index 331af3832..ffd55e72c 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -11,7 +11,10 @@ #include "base/objectlock.hpp" #include #include +#include +#include #include +#include #include #include #include @@ -249,47 +252,7 @@ bool Utility::CidrMatch(const String& pattern, const String& ip) */ String Utility::DirName(const String& path) { - char *dir; - -#ifdef _WIN32 - String dupPath = path; - - /* PathRemoveFileSpec doesn't properly handle forward slashes. */ - for (char& ch : dupPath) { - if (ch == '/') - ch = '\\'; - } - - dir = strdup(dupPath.CStr()); -#else /* _WIN32 */ - dir = strdup(path.CStr()); -#endif /* _WIN32 */ - - if (!dir) - BOOST_THROW_EXCEPTION(std::bad_alloc()); - - String result; - -#ifndef _WIN32 - result = dirname(dir); -#else /* _WIN32 */ - if (dir[0] != 0 && !PathRemoveFileSpec(dir)) { - free(dir); - - BOOST_THROW_EXCEPTION(win32_error() - << boost::errinfo_api_function("PathRemoveFileSpec") - << errinfo_win32_error(GetLastError())); - } - - result = dir; - - if (result.IsEmpty()) - result = "."; -#endif /* _WIN32 */ - - free(dir); - - return result; + return boost::filesystem::path(path).parent_path().string(); } /** @@ -300,21 +263,7 @@ String Utility::DirName(const String& path) */ String Utility::BaseName(const String& path) { - char *dir = strdup(path.CStr()); - String result; - - if (!dir) - BOOST_THROW_EXCEPTION(std::bad_alloc()); - -#ifndef _WIN32 - result = basename(dir); -#else /* _WIN32 */ - result = PathFindFileName(dir); -#endif /* _WIN32 */ - - free(dir); - - return result; + return boost::filesystem::path(path).filename().string(); } /** @@ -755,36 +704,9 @@ void Utility::MkDirP(const String& path, int mode) void Utility::RemoveDirRecursive(const String& path) { - std::vector paths; - Utility::GlobRecursive(path, "*", std::bind(&Utility::CollectPaths, _1, std::ref(paths)), GlobFile | GlobDirectory); + namespace fs = boost::filesystem; - /* This relies on the fact that GlobRecursive lists the parent directory - * first before recursing into subdirectories. - */ - std::reverse(paths.begin(), paths.end()); - - for (const String& path : paths) { - if (remove(path.CStr()) < 0) - BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("remove") - << boost::errinfo_errno(errno) - << boost::errinfo_file_name(path)); - } - -#ifndef _WIN32 - if (rmdir(path.CStr()) < 0) -#else /* _WIN32 */ - if (_rmdir(path.CStr()) < 0) -#endif /* _WIN32 */ - BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("rmdir") - << boost::errinfo_errno(errno) - << boost::errinfo_file_name(path)); -} - -void Utility::CollectPaths(const String& path, std::vector& paths) -{ - paths.push_back(path); + (void)fs::remove_all(fs::path(path)); } /* @@ -793,10 +715,9 @@ void Utility::CollectPaths(const String& path, std::vector& paths) */ void Utility::CopyFile(const String& source, const String& target) { - std::ifstream ifs(source.CStr(), std::ios::binary); - std::ofstream ofs(target.CStr(), std::ios::binary | std::ios::trunc); + namespace fs = boost::filesystem; - ofs << ifs.rdbuf(); + fs::copy_file(fs::path(source), fs::path(target), fs::copy_option::overwrite_if_exists); } /* @@ -1339,13 +1260,11 @@ tm Utility::LocalTime(time_t ts) bool Utility::PathExists(const String& path) { -#ifndef _WIN32 - struct stat statbuf; - return (lstat(path.CStr(), &statbuf) >= 0); -#else /* _WIN32 */ - struct _stat statbuf; - return (_stat(path.CStr(), &statbuf) >= 0); -#endif /* _WIN32 */ + namespace fs = boost::filesystem; + + boost::system::error_code ec; + + return fs::exists(fs::path(path), ec) && !ec; } Value Utility::LoadJsonFile(const String& path) @@ -1365,6 +1284,8 @@ Value Utility::LoadJsonFile(const String& path) void Utility::SaveJsonFile(const String& path, int mode, const Value& value) { + namespace fs = boost::filesystem; + std::fstream fp; String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", mode, fp); @@ -1376,12 +1297,7 @@ void Utility::SaveJsonFile(const String& path, int mode, const Value& value) _unlink(path.CStr()); #endif /* _WIN32 */ - if (rename(tempFilename.CStr(), path.CStr()) < 0) { - BOOST_THROW_EXCEPTION(posix_error() - << boost::errinfo_api_function("rename") - << boost::errinfo_errno(errno) - << boost::errinfo_file_name(tempFilename)); - } + fs::rename(fs::path(tempFilename), fs::path(path)); } static void HexEncode(char ch, std::ostream& os) diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index 1f4059b3f..5b5de46fc 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -143,7 +143,6 @@ public: private: Utility(); - static void CollectPaths(const String& path, std::vector& paths); #ifdef _WIN32 static int MksTemp (char *tmpl);