From f2dd3adae945f9742a4571df09dfb48bc893873b Mon Sep 17 00:00:00 2001 From: KiritoDv Date: Mon, 26 Oct 2026 12:26:37 -0600 Subject: Handled Jeod comments --- src/archive/ZWrapper.cpp | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) (limited to 'src/archive/ZWrapper.cpp') diff --git a/src/archive/ZWrapper.cpp b/src/archive/ZWrapper.cpp index 9d68359..335a3a9 100644 --- a/src/archive/ZWrapper.cpp +++ b/src/archive/ZWrapper.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include "spdlog/spdlog.h" #include @@ -12,6 +13,7 @@ namespace fs = std::filesystem; ZWrapper::ZWrapper(const std::string& path) { this->mPath = path; + this->mTempPath = path + ".tmp"; } ZWrapper::~ZWrapper() { @@ -22,16 +24,19 @@ ZWrapper::~ZWrapper() { delete reinterpret_cast(this->mZip); this->mZip = nullptr; } + if (!this->mFinalized) { + std::error_code ec; + fs::remove(this->mTempPath, ec); + } } int32_t ZWrapper::CreateArchive(void) { auto* zip = new mz_zip_archive; std::memset(zip, 0, sizeof(mz_zip_archive)); - if (!mz_zip_writer_init_file(zip, this->mPath.c_str(), 0)) { - SPDLOG_ERROR("[ZWrapper] Could not open '{}' for writing", this->mPath); + if (!mz_zip_writer_init_file(zip, this->mTempPath.c_str(), 0)) { delete zip; - return -1; + throw std::runtime_error("[ZWrapper] Could not open '" + this->mTempPath + "' for writing"); } this->mZip = reinterpret_cast(zip); @@ -55,17 +60,16 @@ bool ZWrapper::AddFile(const std::string& path, std::vector data) { } if (this->mZip == nullptr || !this->mOpen) { - SPDLOG_ERROR("[ZWrapper] Cannot add '{}': the archive is not open", path); - return false; + throw std::runtime_error("[ZWrapper] Cannot add '" + path + "': the archive is not open"); } std::lock_guard guard(this->mMutex); if (!mz_zip_writer_add_mem(reinterpret_cast(this->mZip), path.c_str(), data.data(), fileSize, - MZ_DEFAULT_COMPRESSION)) { + MZ_BEST_COMPRESSION)) { // miniz's own error says nothing useful, so log which entry blew up and how big. SPDLOG_ERROR("[ZWrapper] Failed to add '{}' ({} bytes)", path, fileSize); - return false; + throw std::runtime_error("[ZWrapper] Failed to add '" + path + "'"); } return true; @@ -77,14 +81,21 @@ int32_t ZWrapper::Close(void) { } auto* zip = reinterpret_cast(this->mZip); - int32_t result = 0; - - if (!mz_zip_writer_finalize_archive(zip)) { - SPDLOG_ERROR("[ZWrapper] Failed to finalize '{}'", this->mPath); - result = -1; - } + const bool finalized = mz_zip_writer_finalize_archive(zip); mz_zip_writer_end(zip); this->mOpen = false; - return result; + if (!finalized) { + throw std::runtime_error("[ZWrapper] Failed to finalize '" + this->mPath + "'"); + } + + std::error_code ec; + fs::rename(this->mTempPath, this->mPath, ec); + if (ec) { + throw std::runtime_error("[ZWrapper] Could not move '" + this->mTempPath + "' to '" + this->mPath + + "': " + ec.message()); + } + this->mFinalized = true; + + return 0; } -- cgit v1.2.3