From: Michael Tremer Date: Thu, 16 Mar 2023 09:04:04 +0000 (+0000) Subject: compress: Create a unified function to create archives X-Git-Url: http://git.ipfire.org/gitweb/gitweb.cgi?a=commitdiff_plain;h=e66c6bbcb041eae8bcc74eef91301180dc84e139;p=people%2Fstevee%2Fpakfire.git compress: Create a unified function to create archives Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/compress.c b/src/libpakfire/compress.c index 88dbdab2..43b19afa 100644 --- a/src/libpakfire/compress.c +++ b/src/libpakfire/compress.c @@ -1093,3 +1093,80 @@ ERROR: return r; } + +int pakfire_compress_create_archive(struct pakfire* pakfire, struct archive** archive, + FILE* f, const enum pakfire_compressions compression, const unsigned int level) { + struct archive* a = NULL; + char value[16]; + int r; + + // Open a new archive + a = archive_write_new(); + if (!a) { + ERROR(pakfire, "archive_write_new() failed\n"); + r = 1; + goto ERROR; + } + + // Use the PAX format + r = archive_write_set_format_pax(a); + if (r) { + ERROR(pakfire, "Could not set format to PAX: %s\n", archive_error_string(a)); + goto ERROR; + } + + // Store any extended attributes in the SCHILY headers + r = archive_write_set_format_option(a, "pax", "xattrheader", "SCHILY"); + if (r) { + ERROR(pakfire, "Could not set xattrheader option: %s\n", archive_error_string(a)); + goto ERROR; + } + + switch (compression) { + case PAKFIRE_COMPRESS_ZSTD: + // Enable Zstd + r = archive_write_add_filter_zstd(a); + if (r) { + ERROR(pakfire, "Could not enable Zstandard compression: %s\n", + archive_error_string(a)); + goto ERROR; + } + + // Do not pad the last block + archive_write_set_bytes_in_last_block(a, 1); + + // Set compression level + if (level) { + r = pakfire_string_format(value, "%u", level); + if (r) + goto ERROR; + + r = archive_write_set_filter_option(a, NULL, "compression-level", value); + if (r) { + ERROR(pakfire, "Could not set Zstandard compression level: %s\n", + archive_error_string(a)); + goto ERROR; + } + } + } + + // Write archive to f + if (f) { + r = archive_write_open_FILE(a, f); + if (r) { + ERROR(pakfire, "archive_write_open_FILE() failed: %s\n", archive_error_string(a)); + goto ERROR; + } + } + + // Success + *archive = a; + + return 0; + +ERROR: + if (a) + archive_write_free(a); + + return r; +} diff --git a/src/libpakfire/include/pakfire/compress.h b/src/libpakfire/include/pakfire/compress.h index d7a0f1b7..6bd942b0 100644 --- a/src/libpakfire/include/pakfire/compress.h +++ b/src/libpakfire/include/pakfire/compress.h @@ -72,6 +72,11 @@ int pakfire_extract(struct pakfire* pakfire, struct archive* archive, const char* message, pakfire_walk_filter_callback filter_callback, int flags); +// Algorithms +enum pakfire_compressions { + PAKFIRE_COMPRESS_ZSTD, +}; + // Compress enum pakfire_compress_flags { PAKFIRE_COMPRESS_NO_PROGRESS = (1 << 1), @@ -81,6 +86,9 @@ enum pakfire_compress_flags { int pakfire_compress(struct pakfire* pakfire, struct archive* archive, struct pakfire_filelist* filelist, const char* message, int flags, int digests); +int pakfire_compress_create_archive(struct pakfire* pakfire, struct archive** archive, + FILE* f, const enum pakfire_compressions compression, const unsigned int level); + #endif #endif /* PAKFIRE_COMPRESS_H */ diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index 15c16a72..628692f5 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -372,7 +372,7 @@ static int pakfire_packager_write_scriptlet(struct pakfire_packager* packager, */ int pakfire_packager_finish(struct pakfire_packager* packager, FILE* f) { struct archive* a = NULL; - const char* comp_level = NULL; + unsigned int level = -1; int r = 1; const char* nevra = pakfire_package_get_string(packager->pkg, PAKFIRE_PKG_NEVRA); @@ -397,67 +397,22 @@ int pakfire_packager_finish(struct pakfire_packager* packager, FILE* f) { if (r) goto ERROR; - // Open a new archive - a = archive_write_new(); - if (!a) { - ERROR(packager->pakfire, "archive_write_new() failed\n"); - r = 1; - goto ERROR; - } - - // Use the PAX format - r = archive_write_set_format_pax(a); - if (r) { - ERROR(packager->pakfire, "Could not set format to PAX: %s\n", - archive_error_string(a)); - goto ERROR; - } - - // Store any extended attributes in the SCHILY headers - r = archive_write_set_format_option(a, "pax", "xattrheader", "SCHILY"); - if (r) { - ERROR(packager->pakfire, "Could not set xattrheader option: %s\n", - archive_error_string(a)); - return r; - } - - // Enable Zstd - r = archive_write_add_filter_zstd(a); - if (r) { - ERROR(packager->pakfire, "Could not enable Zstandard compression: %s\n", - archive_error_string(a)); - return r; - } - // Select compression level if (pakfire_package_is_source(packager->pkg)) - comp_level = "1"; + level = 1; else - comp_level = "22"; + level = 22; - // Set compression level to highest - r = archive_write_set_filter_option(a, NULL, "compression-level", comp_level); - if (r) { - ERROR(packager->pakfire, "Could not set Zstandard compression level: %s\n", - archive_error_string(a)); - return r; - } + // Create a new archive that is being compressed as fast as possible + r = pakfire_compress_create_archive(packager->pakfire, &a, f, + PAKFIRE_COMPRESS_ZSTD, level); + if (r) + goto ERROR; // Add feature marker pakfire_package_add_dep(packager->pkg, PAKFIRE_PKG_REQUIRES, "pakfire(Compress-Zstandard)"); - // Do not pad the last block - archive_write_set_bytes_in_last_block(a, 1); - - // Write archive to f - r = archive_write_open_FILE(a, f); - if (r) { - ERROR(packager->pakfire, "archive_write_open_FILE() failed: %s\n", - archive_error_string(a)); - goto ERROR; - } - // Start with the format file r = pakfire_packager_write_format(packager, a); if (r) { diff --git a/src/libpakfire/snapshot.c b/src/libpakfire/snapshot.c index fcc3de23..30a1995e 100644 --- a/src/libpakfire/snapshot.c +++ b/src/libpakfire/snapshot.c @@ -51,52 +51,6 @@ static int __pakfire_snapshot_path(struct pakfire* pakfire, char* path, const si return r; } -static struct archive* pakfire_snapshot_create_archive(struct pakfire* pakfire, FILE* f) { - struct archive* a = archive_write_new(); - if (!a) { - ERROR(pakfire, "archive_write_new() failed\n"); - return NULL; - } - - // Use the PAX format - int r = archive_write_set_format_pax(a); - if (r) { - ERROR(pakfire, "Could not set format to PAX: %s\n", archive_error_string(a)); - goto ERROR; - } - - // Enable Zstd - r = archive_write_add_filter_zstd(a); - if (r) { - ERROR(pakfire, "Could not enable Zstandard compression: %s\n", - archive_error_string(a)); - goto ERROR; - } - - // Set compression level to fastest - r = archive_write_set_filter_option(a, NULL, "compression-level", "1"); - if (r) { - ERROR(pakfire, "Could not set Zstandard compression level: %s\n", - archive_error_string(a)); - goto ERROR; - } - - // Do not pad the last block - archive_write_set_bytes_in_last_block(a, 1); - - // Write archive to file - r = archive_write_open_FILE(a, f); - if (r) - goto ERROR; - - return a; - -ERROR: - archive_write_free(a); - - return NULL; -} - int pakfire_snapshot_compress(struct pakfire* pakfire, FILE* f) { struct pakfire_filelist* filelist = NULL; struct archive* archive = NULL; @@ -129,12 +83,10 @@ int pakfire_snapshot_compress(struct pakfire* pakfire, FILE* f) { goto ERROR; } - // Create a new archive - archive = pakfire_snapshot_create_archive(pakfire, f); - if (!archive) { - ERROR(pakfire, "Could not open archive for writing\n"); + // Create a new archive that is being compressed as fast as possible + r = pakfire_compress_create_archive(pakfire, &archive, f, PAKFIRE_COMPRESS_ZSTD, 1); + if (r) goto ERROR; - } // Write the payload to the archive r = pakfire_compress(pakfire, archive, filelist, _("Storing Snapshot"),