]> granicus.if.org Git - icinga2/commitdiff
Add Utility::CopyFile()
authorMichael Friedrich <michael.friedrich@netways.de>
Wed, 22 Oct 2014 17:25:29 +0000 (19:25 +0200)
committerMichael Friedrich <michael.friedrich@netways.de>
Wed, 22 Oct 2014 17:25:29 +0000 (19:25 +0200)
refs #7423

lib/base/utility.cpp
lib/base/utility.hpp

index a7ebcb8bce2bd6db86039714dec2ce201371dfed..728414401531e1645fdf542255f93946455115b9 100644 (file)
@@ -30,6 +30,9 @@
 #include <boost/algorithm/string/split.hpp>
 #include <boost/algorithm/string/classification.hpp>
 #include <boost/algorithm/string/trim.hpp>
+#include <ios>
+#include <fstream>
+#include <iostream>
 
 #ifdef __FreeBSD__
 #      include <pthread_np.h>
@@ -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)
index 108741c4ac6069ae542524539e7a1d5118e2002f..eab224aeb607219dc99632f768c85593d667ecf9 100644 (file)
@@ -137,6 +137,8 @@ public:
 
        static bool PathExists(const String& path);
 
+       static bool CopyFile(const String& source, const String& target);
+
 private:
        Utility(void);