From 46abcda068d175b3628f75cbe481cc3caad0b8d8 Mon Sep 17 00:00:00 2001 From: Michael Steenbeek Date: Thu, 27 Jan 2022 00:33:17 +0100 Subject: [PATCH] Make File/Path calls more robust --- src/openrct2/core/File.cpp | 24 ++++++++++++------------ src/openrct2/core/Path.cpp | 8 ++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/openrct2/core/File.cpp b/src/openrct2/core/File.cpp index 1ee75bc1b8..88116e1bbf 100644 --- a/src/openrct2/core/File.cpp +++ b/src/openrct2/core/File.cpp @@ -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 ReadAllBytes(std::string_view path) diff --git a/src/openrct2/core/Path.cpp b/src/openrct2/core/Path.cpp index 0a71bf9b05..380c602ab9 100644 --- a/src/openrct2/core/Path.cpp +++ b/src/openrct2/core/Path.cpp @@ -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