]> git.ipfire.org Git - pakfire.git/commitdiff
compress: Create a unified function to create archives
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 16 Mar 2023 09:04:04 +0000 (09:04 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 16 Mar 2023 09:04:04 +0000 (09:04 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/compress.c
src/libpakfire/include/pakfire/compress.h
src/libpakfire/packager.c
src/libpakfire/snapshot.c

index 88dbdab2dd6bb4d259c00658f3f24b9dde657efb..43b19afabbe93e2f04b753938945bb2a3d9a673c 100644 (file)
@@ -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;
+}
index d7a0f1b7e50acd4834d86889733ce8b6bdade640..6bd942b0b1c868697586cdb4d3d6fd538a6229dd 100644 (file)
@@ -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 */
index 15c16a721dcea16426c5b941d38455c5df00990b..628692f5f3a7988fb544f93a51be5c72190cc7dc 100644 (file)
@@ -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) {
index fcc3de23461bd2442503a99e87ef3a9dd2f379ab..30a1995e4bc49cb614135ca100dae078a8587dda 100644 (file)
@@ -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"),