23void reportArchiveFailure(core::ports::diagnostics::IErrorReporter* reporter,
27 if (!reporter)
return;
29 core::errors::ErrorEvent event;
31 event.origin =
"infra::archive::ZipArchiveAdapter::create";
32 event.message = std::move(message);
34 event.context = std::move(context);
38bool createZipArchive(
const std::filesystem::path& sourceDir,
39 const std::filesystem::path& outputArchive,
40 core::ports::diagnostics::IErrorReporter* errorReporter)
43 zip_t* archive = zip_open(outputArchive.string().c_str(), ZIP_CREATE | ZIP_TRUNCATE, &errorCode);
45 reportArchiveFailure(errorReporter,
46 "failed to open output archive",
47 {{
"path", outputArchive.string()}, {
"zipErrorCode", std::to_string(errorCode)}});
52 for (
const auto& entry : std::filesystem::recursive_directory_iterator(sourceDir)) {
53 if (!entry.is_regular_file())
continue;
55 const std::filesystem::path absolutePath = entry.path();
56 std::filesystem::path relativePath;
58 relativePath = std::filesystem::relative(absolutePath, sourceDir);
60 relativePath = absolutePath.filename();
63 zip_source_t* source = zip_source_file(archive, absolutePath.string().c_str(), 0, 0);
65 reportArchiveFailure(errorReporter,
66 "failed to create zip source",
67 {{
"path", absolutePath.string()}});
72 const std::string zipPath = relativePath.generic_string();
73 if (zip_file_add(archive, zipPath.c_str(), source, ZIP_FL_OVERWRITE | ZIP_FL_ENC_UTF_8) < 0) {
74 zip_source_free(source);
75 reportArchiveFailure(errorReporter,
76 "failed to add file to zip archive",
77 {{
"path", absolutePath.string()}, {
"archivePath", zipPath}});
88 const bool closed = zip_close(archive) == 0;
89 const bool exists = std::filesystem::exists(outputArchive);
90 if (!closed || !exists) {
91 reportArchiveFailure(errorReporter,
92 "failed to finalize zip archive",
93 {{
"path", outputArchive.string()}});
95 return closed && exists;
101 : errorReporter_(std::move(errorReporter))
106 const std::filesystem::path& outputArchive,
110 reportArchiveFailure(errorReporter_.get(),
111 "unsupported archive format",
112 {{
"path", outputArchive.string()}});
116 return createZipArchive(sourceDirectory, outputArchive, errorReporter_.get());
120 "infra::archive::ZipArchiveAdapter::create",
121 std::current_exception());
Provides shared helpers for reporting structured errors and exceptions.
Declares the archive adapter used by export packaging.
ZipArchiveAdapter(std::shared_ptr< core::ports::diagnostics::IErrorReporter > errorReporter)
bool create(const std::filesystem::path &sourceDirectory, const std::filesystem::path &outputArchive, core::ports::exporting::PackageFormat format) const override
Creates a ZIP archive from the files contained in a source directory.
Defines shared structured error codes.
Precompiled header for the archive infrastructure module.
constexpr const char * GenericError
Fallback code for generic structured errors.
std::vector< std::pair< std::string, std::string > > ErrorContext
Key/value context attached to an error event.
void reportException(core::ports::diagnostics::IErrorReporter *reporter, ErrorSeverity severity, const char *origin, std::exception_ptr exception)
Report an exception through the reporter's default exception handling.
void report(core::ports::diagnostics::IErrorReporter *reporter, const ErrorEvent &event)
Report an already constructed error event when a reporter exists.