Cleanup tarball creation with try/catch, and remove duplicate file handling

This commit is contained in:
Simon Quigley 2025-02-07 17:12:26 -06:00
parent f08b3ee199
commit c8889e4b51

View File

@ -419,10 +419,9 @@ std::vector<std::string> extract_files_excluded(const std::string& filepath) {
void create_tarball(const std::string& tarballPath, const std::string& directory, const std::vector<std::string>& exclusions, std::shared_ptr<Log> log) { void create_tarball(const std::string& tarballPath, const std::string& directory, const std::vector<std::string>& exclusions, std::shared_ptr<Log> log) {
log->append("Creating tarball: " + tarballPath); log->append("Creating tarball: " + tarballPath);
try {
struct archive* a = archive_write_new(); struct archive* a = archive_write_new();
if (!a) { if (!a) throw std::runtime_error("Failed to create a new archive.");
throw std::runtime_error("Failed to create a new archive.");
}
if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) { if (archive_write_add_filter_gzip(a) != ARCHIVE_OK) {
std::string err = "Failed to add gzip filter: "; std::string err = "Failed to add gzip filter: ";
@ -445,15 +444,13 @@ void create_tarball(const std::string& tarballPath, const std::string& directory
throw std::runtime_error(err); throw std::runtime_error(err);
} }
// Initialize a set to track added relative paths to prevent duplication
std::unordered_set<std::string> added_paths;
// Iterate through the directory recursively without following symlinks // Iterate through the directory recursively without following symlinks
for (auto it = fs::recursive_directory_iterator( for (auto it = fs::recursive_directory_iterator(
directory, directory,
fs::directory_options::skip_permission_denied); fs::directory_options::skip_permission_denied);
it != fs::recursive_directory_iterator(); ++it) { it != fs::recursive_directory_iterator(); ++it) {
const auto& path = it->path(); const auto& path = it->path();
try {
std::error_code ec; std::error_code ec;
fs::path relative_path = fs::relative(path, directory, ec); fs::path relative_path = fs::relative(path, directory, ec);
@ -466,12 +463,6 @@ void create_tarball(const std::string& tarballPath, const std::string& directory
fs::path normalized_relative_path = relative_path.lexically_normal(); fs::path normalized_relative_path = relative_path.lexically_normal();
std::string relative_path_str = normalized_relative_path.string(); std::string relative_path_str = normalized_relative_path.string();
// Check if this path has already been added
if (!added_paths.insert(relative_path_str).second) {
log->append("Duplicate path detected and skipped: " + relative_path_str);
continue; // Skip adding this duplicate path
}
// Exclusion logic (if any exclusions are provided) // Exclusion logic (if any exclusions are provided)
bool excluded = std::any_of(exclusions.begin(), exclusions.end(), [&relative_path_str](const std::string& exclusion) { bool excluded = std::any_of(exclusions.begin(), exclusions.end(), [&relative_path_str](const std::string& exclusion) {
return relative_path_str.find(exclusion) != std::string::npos; return relative_path_str.find(exclusion) != std::string::npos;
@ -584,6 +575,10 @@ void create_tarball(const std::string& tarballPath, const std::string& directory
} }
archive_entry_free(entry); archive_entry_free(entry);
} catch (const std::exception &e) {
log->append("Failed to add the following file to the tarball: " + path.string());
log->append(e.what());
}
} }
if (archive_write_close(a) != ARCHIVE_OK) { if (archive_write_close(a) != ARCHIVE_OK) {
@ -598,6 +593,10 @@ void create_tarball(const std::string& tarballPath, const std::string& directory
err += archive_error_string(a); err += archive_error_string(a);
throw std::runtime_error(err); throw std::runtime_error(err);
} }
} catch (const std::exception &e) {
log->append("Failed to create tarball: " + tarballPath);
log->append(e.what());
}
log->append("Tarball created and compressed: " + tarballPath); log->append("Tarball created and compressed: " + tarballPath);
} }