From fe92d3e431ab86c1b2f434138f8312a929d57072 Mon Sep 17 00:00:00 2001 From: GeorgH93 Date: Fri, 11 Dec 2020 13:06:44 +0100 Subject: [PATCH] Add error handling --- openVulkanoCpp/IO/Archive/ArchiveBase.cpp | 30 +++++++++++++ openVulkanoCpp/IO/Archive/ArchiveBase.hpp | 38 ++++++++++++++++ openVulkanoCpp/IO/Archive/ArchiveWriter.cpp | 43 ++++++++++--------- openVulkanoCpp/IO/Archive/ArchiveWriter.hpp | 14 +++--- .../IO/Archive/LibArchiveHelper.hpp | 2 +- 5 files changed, 97 insertions(+), 30 deletions(-) create mode 100644 openVulkanoCpp/IO/Archive/ArchiveBase.cpp create mode 100644 openVulkanoCpp/IO/Archive/ArchiveBase.hpp diff --git a/openVulkanoCpp/IO/Archive/ArchiveBase.cpp b/openVulkanoCpp/IO/Archive/ArchiveBase.cpp new file mode 100644 index 0000000..995d564 --- /dev/null +++ b/openVulkanoCpp/IO/Archive/ArchiveBase.cpp @@ -0,0 +1,30 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#include "ArchiveBase.hpp" +#include "LibArchiveHelper.hpp" + +namespace openVulkanoCpp +{ + ArchiveBase::ArchiveBase(archive* arch, archive_entry* archEntry, const std::shared_ptr& logger) + : m_archive(arch), m_archiveEntry(archEntry), m_logger(logger) + { + if (!m_logger) + { + m_logger = Logger::FILESYS; + } + } + + ArchiveBase::~ArchiveBase() + { + ChkErr(archive_free(m_archive)); + } + + bool ArchiveBase::ChkErr(int result) const + { + return LibArchiveHelper::CheckError(result, m_archive, m_logger); + } +} \ No newline at end of file diff --git a/openVulkanoCpp/IO/Archive/ArchiveBase.hpp b/openVulkanoCpp/IO/Archive/ArchiveBase.hpp new file mode 100644 index 0000000..ed3f9f9 --- /dev/null +++ b/openVulkanoCpp/IO/Archive/ArchiveBase.hpp @@ -0,0 +1,38 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "IO/FileDescription.hpp" +#include + +struct archive; +struct archive_entry; + +namespace spdlog +{ + class logger; +} + +namespace openVulkanoCpp +{ + class ArchiveBase + { + protected: + archive* m_archive; + archive_entry* m_archiveEntry; + std::shared_ptr m_logger; + + public: + ArchiveBase(archive* arch, archive_entry* archEntry, const std::shared_ptr& logger); + virtual ~ArchiveBase(); + + bool ChkErr(int result) const; + + void SetLogger(const std::shared_ptr& logger) { m_logger = logger; } + [[nodiscard]] std::shared_ptr GetLogger() const { return m_logger; } + }; +} \ No newline at end of file diff --git a/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp b/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp index 22ac2ce..949439f 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp +++ b/openVulkanoCpp/IO/Archive/ArchiveWriter.cpp @@ -80,30 +80,30 @@ namespace openVulkanoCpp return ac; } - ArchiveWriter::ArchiveWriter(const char* fileName) : ArchiveWriter(fileName, ArchiveConfiguration::FromFileName(fileName)) + ArchiveWriter::ArchiveWriter(const char* fileName, const std::shared_ptr& logger) + : ArchiveWriter(fileName, ArchiveConfiguration::FromFileName(fileName), logger) {} - ArchiveWriter::ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration) - : m_archive(archive_write_new()), m_archiveEntry(archive_entry_new()) + ArchiveWriter::ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration, const std::shared_ptr& logger) + : ArchiveBase(archive_write_new(), archive_entry_new(), logger) { //TODO error handling - archive_write_set_format(m_archive, TYPE_MAP[static_cast(archiveConfiguration.type)]); + ChkErr(archive_write_set_format(m_archive, TYPE_MAP[static_cast(archiveConfiguration.type)])); if (archiveConfiguration.type == ArchiveConfiguration::ArchiveType::TAR) { - archive_write_add_filter(m_archive, COMP_MAP[static_cast(archiveConfiguration.compression)]); + ChkErr(archive_write_add_filter(m_archive, COMP_MAP[static_cast(archiveConfiguration.compression)])); } if (archiveConfiguration.compression != ArchiveConfiguration::CompressionType::NONE && archiveConfiguration.compressionLevel > 0) { std::string level = "compression-level=" + std::to_string(archiveConfiguration.compressionLevel); - archive_write_set_options(m_archive, level.c_str()); + ChkErr(archive_write_set_options(m_archive, level.c_str())); } - archive_write_open_filename(m_archive, fileName); + ChkErr(archive_write_open_filename(m_archive, fileName)); } ArchiveWriter::~ArchiveWriter() { archive_entry_free(m_archiveEntry); - archive_write_free(m_archive); } void ArchiveWriter::Close() @@ -129,17 +129,16 @@ namespace openVulkanoCpp { archive_entry_clear(m_archiveEntry); std::unique_ptr archiveDisk(archive_read_disk_new(), archive_free); - - archive_entry_clear(m_archiveEntry); - archive_read_disk_open(archiveDisk.get(), fileName); - archive_read_next_header2(archiveDisk.get(), m_archiveEntry); + ChkErr(archive_read_disk_open(archiveDisk.get(), fileName)); + ChkErr(archive_read_next_header2(archiveDisk.get(), m_archiveEntry)); //archive_read_disk_entry_from_file(archiveDisk, m_archiveEntry, -1, nullptr); archive_entry_set_pathname(m_archiveEntry, inArchiveName); - archive_write_header(m_archive, m_archiveEntry); - if (LibArchiveHelper::CopyArchiveData(archiveDisk.get(), m_archive) != ARCHIVE_OK) - { - //TODO handle error - } + ChkErr(archive_write_header(m_archive, m_archiveEntry)); + int result = LibArchiveHelper::CopyArchiveData(archiveDisk.get(), m_archive); + ChkErr(result); + LibArchiveHelper::CheckError(result, archiveDisk.get(), m_logger); + ChkErr(archive_write_finish_entry(m_archive)); + m_bytesWritten += archive_entry_size(m_archiveEntry); return true; } @@ -152,7 +151,9 @@ namespace openVulkanoCpp bool ArchiveWriter::AddFile(const FileDescription& description, const void* buffer) { WriteHeader(description); - return archive_write_data(m_archive, buffer, description.size) == ARCHIVE_OK; + bool ok = ChkErr(archive_write_data(m_archive, buffer, description.size)); + ChkErr(archive_write_finish_entry(m_archive)); + return ok; } bool ArchiveWriter::AddFile(const char* fileName, const std::vector>& buffers) @@ -173,11 +174,12 @@ namespace openVulkanoCpp for(const auto& buffer : buffers) { if (buffer.first == nullptr) continue; - if (archive_write_data(m_archive, buffer.first, buffer.second) != ARCHIVE_OK) + if (!ChkErr(archive_write_data(m_archive, buffer.first, buffer.second))) { return false; } } + ChkErr(archive_write_finish_entry(m_archive)); return true; } @@ -190,7 +192,8 @@ namespace openVulkanoCpp archive_entry_set_perm(m_archiveEntry, static_cast(fileDescription.permissions)); archive_entry_set_ctime(m_archiveEntry, fileDescription.createTime, 0); archive_entry_set_mtime(m_archiveEntry, fileDescription.modTime, 0); - archive_write_header(m_archive, m_archiveEntry); + ChkErr(archive_write_header(m_archive, m_archiveEntry)); + m_bytesWritten += fileDescription.size; } bool ArchiveWriter::AddFiles(const char* dirName) diff --git a/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp b/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp index bd09fa3..de87189 100644 --- a/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp +++ b/openVulkanoCpp/IO/Archive/ArchiveWriter.hpp @@ -6,13 +6,11 @@ #pragma once +#include "ArchiveBase.hpp" #include "IO/FileDescription.hpp" #include #include -struct archive; -struct archive_entry; - namespace openVulkanoCpp { struct ArchiveConfiguration @@ -28,17 +26,15 @@ namespace openVulkanoCpp static ArchiveConfiguration FromFileName(const char* fileName); }; - class ArchiveWriter + class ArchiveWriter final : public ArchiveBase { - archive* m_archive; - archive_entry* m_archiveEntry; size_t m_bytesWritten = 0; public: - ArchiveWriter(const char* fileName); - ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration); + ArchiveWriter(const char* fileName, const std::shared_ptr& logger = nullptr); + ArchiveWriter(const char* fileName, ArchiveConfiguration archiveConfiguration, const std::shared_ptr& logger = nullptr); - ~ArchiveWriter(); + ~ArchiveWriter() override; void Close(); diff --git a/openVulkanoCpp/IO/Archive/LibArchiveHelper.hpp b/openVulkanoCpp/IO/Archive/LibArchiveHelper.hpp index 662df94..9b71d82 100644 --- a/openVulkanoCpp/IO/Archive/LibArchiveHelper.hpp +++ b/openVulkanoCpp/IO/Archive/LibArchiveHelper.hpp @@ -68,7 +68,7 @@ namespace openVulkanoCpp if (archiveResult <= ARCHIVE_FATAL) lvl = spdlog::level::level_enum::critical; else if (archiveResult <= ARCHIVE_FAILED) lvl = spdlog::level::level_enum::err; const char* errorString = archive_error_string(arch); - logger->log(lvl, errorString); + if (logger) logger->log(lvl, errorString); if (archiveResult == ARCHIVE_FAILED) throw std::runtime_error(errorString); return false; }