From 2602f52e34594493e670deccbff829faa67d1481 Mon Sep 17 00:00:00 2001 From: Ezekiel Warren Date: Thu, 12 Sep 2024 12:32:35 -0700 Subject: [PATCH] fix: add create file error handling --- .../recipe-bundle/build_recipe_bundle.cc | 38 +++++++++++++++++-- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/ecsact/cli/commands/recipe-bundle/build_recipe_bundle.cc b/ecsact/cli/commands/recipe-bundle/build_recipe_bundle.cc index 493849e..16ddd0e 100644 --- a/ecsact/cli/commands/recipe-bundle/build_recipe_bundle.cc +++ b/ecsact/cli/commands/recipe-bundle/build_recipe_bundle.cc @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -117,15 +118,42 @@ static auto read_file(fs::path path) -> std::optional> { return file_buffer; } -static auto write_file(fs::path path, std::span data) -> void { +static auto write_file(fs::path path, std::span data) -> bool { if(path.has_parent_path()) { auto ec = std::error_code{}; fs::create_directories(path.parent_path(), ec); + if(ec) { + ecsact::cli::report_error( + "failed to create directory {}: {}", + path.generic_string(), + ec.message() + ); + return false; + } } auto file = std::ofstream(path, std::ios_base::binary | std::ios_base::trunc); - assert(file); + if(!file) { + ecsact::cli::report_error( + "failed to open file {}: {}", + path.generic_string(), + std::strerror(errno) + ); + return false; + } file.write(reinterpret_cast(data.data()), data.size()); + + if(!file) { + ecsact::cli::report_error( + "failed to write file {}: {}", + path.generic_string(), + std::strerror(errno) + ); + return false; + } + + file.flush(); + return true; } ecsact::build_recipe_bundle::build_recipe_bundle() = default; @@ -399,7 +427,11 @@ auto ecsact::build_recipe_bundle::extract( // return std::logic_error{std::format("Failed to read {}", path)}; } - write_file(dir / path, data); + if(!write_file(dir / path, data)) { + return std::logic_error{ + std::format("Failed to extract {}", (dir / path).generic_string()) + }; + } } archive_entry_free(entry);