FileUtil: Return success bool from CopyDir

This commit is contained in:
Dentomologist
2022-08-23 17:10:07 -07:00
parent 12b204d92a
commit 2808db7f2f
2 changed files with 18 additions and 13 deletions

View File

@ -635,15 +635,19 @@ bool DeleteDirRecursively(const std::string& directory)
return success; return success;
} }
// Create directory and copy contents (does not overwrite existing files) // Create directory and copy contents (optionally overwrites existing files)
void CopyDir(const std::string& source_path, const std::string& dest_path, bool destructive) bool CopyDir(const std::string& source_path, const std::string& dest_path, const bool destructive)
{ {
if (source_path == dest_path) if (source_path == dest_path)
return; return true;
if (!Exists(source_path)) if (!Exists(source_path))
return; return false;
// Shouldn't be used to short circuit operations after an earlier failure
bool everything_copied = true;
if (!Exists(dest_path)) if (!Exists(dest_path))
File::CreateFullPath(dest_path); everything_copied = File::CreateFullPath(dest_path) && everything_copied;
#ifdef _WIN32 #ifdef _WIN32
WIN32_FIND_DATA ffd; WIN32_FIND_DATA ffd;
@ -652,7 +656,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool
if (hFind == INVALID_HANDLE_VALUE) if (hFind == INVALID_HANDLE_VALUE)
{ {
FindClose(hFind); FindClose(hFind);
return; return false;
} }
do do
@ -661,7 +665,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool
#else #else
DIR* dirp = opendir(source_path.c_str()); DIR* dirp = opendir(source_path.c_str());
if (!dirp) if (!dirp)
return; return false;
while (dirent* result = readdir(dirp)) while (dirent* result = readdir(dirp))
{ {
@ -671,21 +675,21 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool
if (virtualName == "." || virtualName == "..") if (virtualName == "." || virtualName == "..")
continue; continue;
std::string source = source_path + DIR_SEP + virtualName; const std::string source = source_path + DIR_SEP + virtualName;
std::string dest = dest_path + DIR_SEP + virtualName; const std::string dest = dest_path + DIR_SEP + virtualName;
if (IsDirectory(source)) if (IsDirectory(source))
{ {
if (!Exists(dest)) if (!Exists(dest))
File::CreateFullPath(dest + DIR_SEP); File::CreateFullPath(dest + DIR_SEP);
CopyDir(source, dest, destructive); everything_copied = CopyDir(source, dest, destructive) && everything_copied;
} }
else if (!destructive && !Exists(dest)) else if (!destructive && !Exists(dest))
{ {
Copy(source, dest); everything_copied = Copy(source, dest) && everything_copied;
} }
else if (destructive) else if (destructive)
{ {
Rename(source, dest); everything_copied = Rename(source, dest) && everything_copied;
} }
#ifdef _WIN32 #ifdef _WIN32
} while (FindNextFile(hFind, &ffd) != 0); } while (FindNextFile(hFind, &ffd) != 0);
@ -694,6 +698,7 @@ void CopyDir(const std::string& source_path, const std::string& dest_path, bool
} }
closedir(dirp); closedir(dirp);
#endif #endif
return everything_copied;
} }
// Returns the current directory // Returns the current directory

View File

@ -193,7 +193,7 @@ bool DeleteDirRecursively(const std::string& directory);
std::string GetCurrentDir(); std::string GetCurrentDir();
// Create directory and copy contents (optionally overwrites existing files) // Create directory and copy contents (optionally overwrites existing files)
void CopyDir(const std::string& source_path, const std::string& dest_path, bool CopyDir(const std::string& source_path, const std::string& dest_path,
bool destructive = false); bool destructive = false);
// Set the current directory to given directory // Set the current directory to given directory