From 45e4175ba68a3e2e11a1b6c40ce0596c59284680 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 29 Nov 2019 19:49:00 +0800 Subject: [PATCH] Add moveDirectoryInto function --- src/io/file_manager.cpp | 30 ++++++++++++++++++++++++++++++ src/io/file_manager.hpp | 3 +++ 2 files changed, 33 insertions(+) diff --git a/src/io/file_manager.cpp b/src/io/file_manager.cpp index 1f5a97446..e430bc4f0 100644 --- a/src/io/file_manager.cpp +++ b/src/io/file_manager.cpp @@ -1602,3 +1602,33 @@ bool FileManager::fileIsNewer(const std::string& f1, const std::string& f2) cons FileUtils::statU8Path(f2, &stat2); return stat1.st_mtime > stat2.st_mtime; } // fileIsNewer + +// ---------------------------------------------------------------------------- +/** Move the source directory into the target directory location. +* The target directory must be on the same drive as the source. +*/ +bool FileManager::moveDirectoryInto(std::string source, std::string target) +{ + if (!isDirectory(source) || !isDirectory(target)) + return false; + + // Remove the last '/' + if (source[source.size() - 1] == '/') + source.erase(source.end() - 1, source.end()); + std::string folder = StringUtils::getBasename(source); + if (target[target.size() - 1] != '/') + target += "/"; + target += folder; + + // The result target directory must not already exist + if (isDirectory(target)) + return false; + +#if defined(WIN32) + return MoveFileExW(StringUtils::utf8ToWide(source).c_str(), + StringUtils::utf8ToWide(target).c_str(), + MOVEFILE_WRITE_THROUGH) != 0; +#else + return rename(source.c_str(), target.c_str()) != -1; +#endif +} // moveDirectoryInto diff --git a/src/io/file_manager.hpp b/src/io/file_manager.hpp index 82137bf0a..5226f357c 100644 --- a/src/io/file_manager.hpp +++ b/src/io/file_manager.hpp @@ -163,6 +163,9 @@ public: bool isDirectory(const std::string &path) const; bool removeFile(const std::string &name) const; bool removeDirectory(const std::string &name) const; + // ------------------------------------------------------------------------ + bool moveDirectoryInto(std::string source, std::string target); + // ------------------------------------------------------------------------ bool copyFile(const std::string &source, const std::string &dest); std::vectorgetMusicDirs() const; std::string getAssetChecked(AssetType type, const std::string& name,