Use Boost.Filesystem

refs #7101
This commit is contained in:
Alexander A. Klimov 2019-04-10 11:21:05 +02:00 committed by Michael Friedrich
parent 4ccc152990
commit af78cd6050
2 changed files with 17 additions and 102 deletions

View File

@ -11,7 +11,10 @@
#include "base/objectlock.hpp" #include "base/objectlock.hpp"
#include <cstdint> #include <cstdint>
#include <mmatch.h> #include <mmatch.h>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/lexical_cast.hpp> #include <boost/lexical_cast.hpp>
#include <boost/system/error_code.hpp>
#include <boost/thread/tss.hpp> #include <boost/thread/tss.hpp>
#include <boost/algorithm/string/trim.hpp> #include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/replace.hpp> #include <boost/algorithm/string/replace.hpp>
@ -249,47 +252,7 @@ bool Utility::CidrMatch(const String& pattern, const String& ip)
*/ */
String Utility::DirName(const String& path) String Utility::DirName(const String& path)
{ {
char *dir; return boost::filesystem::path(path).parent_path().string();
#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;
} }
/** /**
@ -300,21 +263,7 @@ String Utility::DirName(const String& path)
*/ */
String Utility::BaseName(const String& path) String Utility::BaseName(const String& path)
{ {
char *dir = strdup(path.CStr()); return boost::filesystem::path(path).filename().string();
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;
} }
/** /**
@ -755,36 +704,9 @@ void Utility::MkDirP(const String& path, int mode)
void Utility::RemoveDirRecursive(const String& path) void Utility::RemoveDirRecursive(const String& path)
{ {
std::vector<String> paths; namespace fs = boost::filesystem;
Utility::GlobRecursive(path, "*", std::bind(&Utility::CollectPaths, _1, std::ref(paths)), GlobFile | GlobDirectory);
/* This relies on the fact that GlobRecursive lists the parent directory (void)fs::remove_all(fs::path(path));
* 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<String>& paths)
{
paths.push_back(path);
} }
/* /*
@ -793,10 +715,9 @@ void Utility::CollectPaths(const String& path, std::vector<String>& paths)
*/ */
void Utility::CopyFile(const String& source, const String& target) void Utility::CopyFile(const String& source, const String& target)
{ {
std::ifstream ifs(source.CStr(), std::ios::binary); namespace fs = boost::filesystem;
std::ofstream ofs(target.CStr(), std::ios::binary | std::ios::trunc);
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) bool Utility::PathExists(const String& path)
{ {
#ifndef _WIN32 namespace fs = boost::filesystem;
struct stat statbuf;
return (lstat(path.CStr(), &statbuf) >= 0); boost::system::error_code ec;
#else /* _WIN32 */
struct _stat statbuf; return fs::exists(fs::path(path), ec) && !ec;
return (_stat(path.CStr(), &statbuf) >= 0);
#endif /* _WIN32 */
} }
Value Utility::LoadJsonFile(const String& path) 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) void Utility::SaveJsonFile(const String& path, int mode, const Value& value)
{ {
namespace fs = boost::filesystem;
std::fstream fp; std::fstream fp;
String tempFilename = Utility::CreateTempFile(path + ".XXXXXX", mode, 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()); _unlink(path.CStr());
#endif /* _WIN32 */ #endif /* _WIN32 */
if (rename(tempFilename.CStr(), path.CStr()) < 0) { fs::rename(fs::path(tempFilename), fs::path(path));
BOOST_THROW_EXCEPTION(posix_error()
<< boost::errinfo_api_function("rename")
<< boost::errinfo_errno(errno)
<< boost::errinfo_file_name(tempFilename));
}
} }
static void HexEncode(char ch, std::ostream& os) static void HexEncode(char ch, std::ostream& os)

View File

@ -143,7 +143,6 @@ public:
private: private:
Utility(); Utility();
static void CollectPaths(const String& path, std::vector<String>& paths);
#ifdef _WIN32 #ifdef _WIN32
static int MksTemp (char *tmpl); static int MksTemp (char *tmpl);