From 21866a033a1a9e8ace36403baa6e62285e1272a0 Mon Sep 17 00:00:00 2001 From: ConfiG Date: Thu, 14 Sep 2023 17:38:31 +0300 Subject: [PATCH] improve utils::file::read* error checking --- loader/src/utils/file.cpp | 71 ++++++++++++++++++++------------------- 1 file changed, 37 insertions(+), 34 deletions(-) diff --git a/loader/src/utils/file.cpp b/loader/src/utils/file.cpp index 8df76c0d..1f8a7ad6 100644 --- a/loader/src/utils/file.cpp +++ b/loader/src/utils/file.cpp @@ -21,48 +21,51 @@ using namespace geode::prelude; using namespace geode::utils::file; Result utils::file::readString(ghc::filesystem::path const& path) { + if (!ghc::filesystem::exists(path)) + return Err("File does not exist"); + #if _WIN32 std::ifstream in(path.wstring(), std::ios::in | std::ios::binary); #else std::ifstream in(path.string(), std::ios::in | std::ios::binary); #endif - if (in) { - std::string contents; - in.seekg(0, std::ios::end); - contents.resize((const size_t)in.tellg()); - in.seekg(0, std::ios::beg); - in.read(&contents[0], contents.size()); - in.close(); - return Ok(contents); - } - return Err("Unable to open file"); + if (!in) + return Err("Unable to open file"); + + std::string contents; + in.seekg(0, std::ios::end); + contents.resize((const size_t)in.tellg()); + in.seekg(0, std::ios::beg); + in.read(&contents[0], contents.size()); + in.close(); + return Ok(contents); } Result utils::file::readJson(ghc::filesystem::path const& path) { - auto str = utils::file::readString(path); - - if (str) { - try { - return Ok(json::parse(str.value())); - } catch(std::exception const& e) { - return Err("Unable to parse JSON: " + std::string(e.what())); - } - } else { - return Err("Unable to open file"); + if (!str) + return Err(str.unwrapErr()); + try { + return Ok(json::parse(str.value())); + } + catch(std::exception const& e) { + return Err("Unable to parse JSON: " + std::string(e.what())); } } Result utils::file::readBinary(ghc::filesystem::path const& path) { + if (!ghc::filesystem::exists(path)) + return Err("File does not exist"); + #if _WIN32 std::ifstream in(path.wstring(), std::ios::in | std::ios::binary); #else std::ifstream in(path.string(), std::ios::in | std::ios::binary); #endif - if (in) { - return Ok(ByteVector(std::istreambuf_iterator(in), {})); - } - return Err("Unable to open file"); + if (!in) + return Err("Unable to open file"); + + return Ok(ByteVector(std::istreambuf_iterator(in), {})); } Result<> utils::file::writeString(ghc::filesystem::path const& path, std::string const& data) { @@ -72,14 +75,14 @@ Result<> utils::file::writeString(ghc::filesystem::path const& path, std::string #else file.open(path.string()); #endif - if (file.is_open()) { - file << data; + if (!file.is_open()) { file.close(); - - return Ok(); + return Err("Unable to open file"); } + + file << data; file.close(); - return Err("Unable to open file"); + return Ok(); } Result<> utils::file::writeBinary(ghc::filesystem::path const& path, ByteVector const& data) { @@ -89,14 +92,14 @@ Result<> utils::file::writeBinary(ghc::filesystem::path const& path, ByteVector #else file.open(path.string(), std::ios::out | std::ios::binary); #endif - if (file.is_open()) { - file.write(reinterpret_cast(data.data()), data.size()); + if (!file.is_open()) { file.close(); - - return Ok(); + return Err("Unable to open file"); } + + file.write(reinterpret_cast(data.data()), data.size()); file.close(); - return Err("Unable to open file"); + return Ok(); } Result<> utils::file::createDirectory(ghc::filesystem::path const& path) {