Make File/Path calls more robust

This commit is contained in:
Michael Steenbeek 2022-01-27 00:33:17 +01:00 committed by GitHub
parent 3752a5ab05
commit 46abcda068
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 14 deletions

View File

@ -27,7 +27,9 @@ namespace File
{
fs::path file = u8path(path);
log_verbose("Checking if file exists: %s", std::string(path).c_str());
return fs::exists(file);
std::error_code ec;
const auto result = fs::exists(file, ec);
return result && ec.value() == 0;
}
bool Copy(std::string_view srcPath, std::string_view dstPath, bool overwrite)
@ -38,25 +40,23 @@ namespace File
return false;
}
return fs::copy_file(u8path(srcPath), u8path(dstPath));
std::error_code ec;
const auto result = fs::copy_file(u8path(srcPath), u8path(dstPath), ec);
return result && ec.value() == 0;
}
bool Delete(std::string_view path)
{
return fs::remove(u8path(path));
std::error_code ec;
const auto result = fs::remove(u8path(path), ec);
return result && ec.value() == 0;
}
bool Move(std::string_view srcPath, std::string_view dstPath)
{
try
{
fs::rename(u8path(srcPath), u8path(dstPath));
return true;
}
catch (const fs::filesystem_error&)
{
return false;
}
std::error_code ec;
fs::rename(u8path(srcPath), u8path(dstPath), ec);
return ec.value() == 0;
}
std::vector<uint8_t> ReadAllBytes(std::string_view path)

View File

@ -66,7 +66,9 @@ namespace Path
bool DirectoryExists(std::string_view path)
{
return fs::is_directory(u8path(path));
std::error_code ec;
const auto result = fs::is_directory(u8path(path), ec);
return result && ec.value() == 0;
}
std::string GetFileName(std::string_view path)
@ -107,6 +109,8 @@ namespace Path
bool DeleteDirectory(std::string_view path)
{
return fs::remove_all(u8path(path)) > 0;
std::error_code ec;
const auto result = fs::remove_all(u8path(path), ec);
return (result > 0) && ec.value() == 0;
}
} // namespace Path