From 9227d990dc4325e290eef30b8e5ae8fffc7af0a5 Mon Sep 17 00:00:00 2001 From: Michael Friedrich Date: Wed, 22 Oct 2014 19:25:29 +0200 Subject: [PATCH] Add Utility::CopyFile() refs #7423 --- lib/base/utility.cpp | 22 ++++++++++++++++++++-- lib/base/utility.hpp | 2 ++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/base/utility.cpp b/lib/base/utility.cpp index a7ebcb8bc..728414401 100644 --- a/lib/base/utility.cpp +++ b/lib/base/utility.cpp @@ -30,6 +30,9 @@ #include #include #include +#include +#include +#include #ifdef __FreeBSD__ # include @@ -309,7 +312,7 @@ Utility::LoadExtensionLibrary(const String& library) #ifdef _WIN32 HMODULE hModule = LoadLibrary(path.CStr()); - + if (hModule == NULL) { BOOST_THROW_EXCEPTION(win32_error() << boost::errinfo_api_function("LoadLibrary") @@ -318,7 +321,7 @@ Utility::LoadExtensionLibrary(const String& library) } #else /* _WIN32 */ void *hModule = dlopen(path.CStr(), RTLD_NOW); - + if (hModule == NULL) { BOOST_THROW_EXCEPTION(std::runtime_error("Could not load library '" + path + "': " + dlerror())); } @@ -638,6 +641,21 @@ bool Utility::MkDirP(const String& path, int flags) return ret; } +bool Utility::CopyFile(const String& source, const String& target) +{ + if (Utility::PathExists(target)) { + Log(LogWarning, "Utility") + << "Target file '" << target << "' already exists."; + return false; + } + + std::ifstream ifs(source.CStr(), std::ios::binary); + std::ofstream ofs(target.CStr(), std::ios::binary); + + ofs << ifs.rdbuf(); + + return true; +} #ifndef _WIN32 void Utility::SetNonBlocking(int fd) diff --git a/lib/base/utility.hpp b/lib/base/utility.hpp index 108741c4a..eab224aeb 100644 --- a/lib/base/utility.hpp +++ b/lib/base/utility.hpp @@ -137,6 +137,8 @@ public: static bool PathExists(const String& path); + static bool CopyFile(const String& source, const String& target); + private: Utility(void); -- 2.40.0