Try working with libarchive better
This commit is contained in:
parent
0ba157d434
commit
de65c444a8
@ -14,8 +14,12 @@
|
||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
#include "common.h"
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
|
||||
extern "C" {
|
||||
#include <archive.h>
|
||||
#include <archive_entry.h>
|
||||
}
|
||||
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <sstream>
|
||||
@ -25,12 +29,14 @@
|
||||
#include <filesystem>
|
||||
#include <chrono>
|
||||
#include <regex>
|
||||
#include <optional>
|
||||
|
||||
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 +153,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<mode_t>(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<char> buffer((std::istreambuf_iterator<char>(fileStream)), std::istreambuf_iterator<char>());
|
||||
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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user