Add moveDirectoryInto function

This commit is contained in:
Benau 2019-11-29 19:49:00 +08:00
parent 81ca14d549
commit 45e4175ba6
2 changed files with 33 additions and 0 deletions

View File

@ -1602,3 +1602,33 @@ bool FileManager::fileIsNewer(const std::string& f1, const std::string& f2) cons
FileUtils::statU8Path(f2, &stat2); FileUtils::statU8Path(f2, &stat2);
return stat1.st_mtime > stat2.st_mtime; return stat1.st_mtime > stat2.st_mtime;
} // fileIsNewer } // 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

View File

@ -163,6 +163,9 @@ public:
bool isDirectory(const std::string &path) const; bool isDirectory(const std::string &path) const;
bool removeFile(const std::string &name) const; bool removeFile(const std::string &name) const;
bool removeDirectory(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); bool copyFile(const std::string &source, const std::string &dest);
std::vector<std::string>getMusicDirs() const; std::vector<std::string>getMusicDirs() const;
std::string getAssetChecked(AssetType type, const std::string& name, std::string getAssetChecked(AssetType type, const std::string& name,