From 9a456fde5e12ccc0276628c17058cdee9751c357 Mon Sep 17 00:00:00 2001 From: Simon Quigley Date: Wed, 18 Dec 2024 16:46:00 -0600 Subject: [PATCH] Try working with libarchive better --- cpp/common.cpp | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/cpp/common.cpp b/cpp/common.cpp index 965a3aa..f2cc67a 100644 --- a/cpp/common.cpp +++ b/cpp/common.cpp @@ -14,6 +14,7 @@ // along with this program. If not, see . #include "common.h" + #include #include #include @@ -25,12 +26,14 @@ #include #include #include +#include namespace fs = std::filesystem; static void log_info(const std::string &msg) { std::cout << "[INFO] " << msg << "\n"; } + static void log_error(const std::string &msg) { std::cerr << "[ERROR] " << msg << "\n"; } @@ -147,21 +150,48 @@ void create_tarball(const std::string& tarballPath, const std::string& directory archive_entry_set_pathname(entry, relativePath.c_str()); archive_entry_set_size(entry, fs::file_size(path)); archive_entry_set_filetype(entry, AE_IFREG); - archive_entry_set_perm(entry, static_cast(fs::status(path).permissions())); - archive_write_header(a, entry); + // Convert std::filesystem::perms to mode_t + std::filesystem::perms p = fs::status(path).permissions(); + mode_t mode = 0; + if ((p & fs::perms::owner_read) != fs::perms::none) mode |= S_IRUSR; + if ((p & fs::perms::owner_write) != fs::perms::none) mode |= S_IWUSR; + if ((p & fs::perms::owner_exec) != fs::perms::none) mode |= S_IXUSR; + if ((p & fs::perms::group_read) != fs::perms::none) mode |= S_IRGRP; + if ((p & fs::perms::group_write) != fs::perms::none) mode |= S_IWGRP; + if ((p & fs::perms::group_exec) != fs::perms::none) mode |= S_IXGRP; + if ((p & fs::perms::others_read) != fs::perms::none) mode |= S_IROTH; + if ((p & fs::perms::others_write) != fs::perms::none) mode |= S_IWOTH; + if ((p & fs::perms::others_exec) != fs::perms::none) mode |= S_IXOTH; + + archive_entry_set_perm(entry, mode); + + if (archive_write_header(a, entry) != ARCHIVE_OK) { + archive_entry_free(entry); + throw std::runtime_error("Could not write header for " + relativePath + ": " + archive_error_string(a)); + } // Write file contents std::ifstream fileStream(path, std::ios::binary); + if (!fileStream) { + archive_entry_free(entry); + throw std::runtime_error("Could not open file " + relativePath + " for reading"); + } std::vector buffer((std::istreambuf_iterator(fileStream)), std::istreambuf_iterator()); - archive_write_data(a, buffer.data(), buffer.size()); + if (archive_write_data(a, buffer.data(), buffer.size()) < 0) { + archive_entry_free(entry); + throw std::runtime_error("Could not write data for " + relativePath + ": " + archive_error_string(a)); + } archive_entry_free(entry); } } // Finalize and clean up - archive_write_close(a); + if (archive_write_close(a) != ARCHIVE_OK) { + archive_write_free(a); + throw std::runtime_error("Could not close archive: " + std::string(archive_error_string(a))); + } archive_write_free(a); std::cout << "[INFO] Tarball created and compressed: " << tarballPath << std::endl;