From: Michael Tremer Date: Sat, 6 Mar 2021 16:02:25 +0000 (+0000) Subject: packager: Add function that starts putting the whole archive together X-Git-Tag: 0.9.28~1285^2~618 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=436677a3d3f12801a8dce1475e23786b008347e9;p=pakfire.git packager: Add function that starts putting the whole archive together Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/include/pakfire/constants.h b/src/libpakfire/include/pakfire/constants.h index a6bef7260..24d79a6c1 100644 --- a/src/libpakfire/include/pakfire/constants.h +++ b/src/libpakfire/include/pakfire/constants.h @@ -21,6 +21,9 @@ #ifndef PAKFIRE_CONSTANTS_H #define PAKFIRE_CONSTANTS_H +#define _TO_STRING(x) #x +#define TO_STRING(x) _TO_STRING(x) + #define STRING_SIZE 1024 #define PAKFIRE_REPO_SYSTEM_NAME "@system" @@ -30,4 +33,11 @@ #define PAKFIRE_MACROS_DIR "/usr/lib/pakfire/macros" #define PAKFIRE_MACROS_GLOB_PATTERN PAKFIRE_MACROS_DIR "/*.macro" +#ifdef PAKFIRE_PRIVATE + +// The file format that this version generates +#define PACKAGE_FORMAT 5 + +#endif /* PAKFIRE_PRIVATE */ + #endif /* PAKFIRE_CONSTANTS_H */ diff --git a/src/libpakfire/include/pakfire/packager.h b/src/libpakfire/include/pakfire/packager.h index 9abccac70..3b9aff264 100644 --- a/src/libpakfire/include/pakfire/packager.h +++ b/src/libpakfire/include/pakfire/packager.h @@ -30,6 +30,8 @@ int pakfire_packager_create(struct pakfire_packager** packager, PakfirePackage p struct pakfire_packager* pakfire_packager_ref(struct pakfire_packager* packager); struct pakfire_packager* pakfire_packager_unref(struct pakfire_packager* packager); +char* pakfire_packager_finish(struct pakfire_packager* packager, FILE* f); + int pakfire_packager_add(struct pakfire_packager* packager, const char* path); #endif /* PAKFIRE_PACKAGER_H */ diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 215ff18e3..76640a60f 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -251,6 +251,7 @@ global: pakfire_packager_add; pakfire_packager_create; + pakfire_packager_finish; pakfire_packager_ref; pakfire_packager_unref; diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index 211470b2e..f7a29ffe8 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -150,6 +151,91 @@ PAKFIRE_EXPORT struct pakfire_packager* pakfire_packager_unref( return NULL; } +static int pakfire_packager_write_format(struct pakfire_packager* packager, + struct archive* a) { + const char buffer[] = TO_STRING(PACKAGE_FORMAT) "\n"; + + // Create a new file entry + struct archive_entry* entry = archive_entry_new(); + if (!entry) + return 1; + + // Set filename + archive_entry_set_pathname(entry, "pakfire-format"); + + // This is a regular file + archive_entry_set_filetype(entry, AE_IFREG); + archive_entry_set_perm(entry, 0644); + + // Set length + archive_entry_set_size(entry, strlen(buffer)); + + // This is the end of the header + int r = archive_write_header(a, entry); + if (r) { + ERROR(packager->pakfire, "Error writing header: %s\n", archive_error_string(a)); + archive_entry_free(entry); + return r; + } + + // Write content + r = archive_write_data(a, buffer, strlen(buffer)); + if (r < 0) { + ERROR(packager->pakfire, "Error writing data: %s\n", archive_error_string(a)); + archive_entry_free(entry); + return r; + } + + archive_entry_free(entry); + + return 0; +} + +/* + This function is being called at the end when all data has been added to the package. + + It will create a new archive and write the package to the given file descriptor. +*/ +PAKFIRE_EXPORT char* pakfire_packager_finish(struct pakfire_packager* packager, FILE* f) { + char* filename = NULL; + + // Open a new archive + struct archive* a = archive_write_new(); + if (!a) { + ERROR(packager->pakfire, "archive_write_new() failed\n"); + goto ERROR; + } + + // Use the PAX format + int 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; + } + + // 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) + goto ERROR; + + // XXX set filename + +ERROR: + if (a) + archive_free(a); + + return filename; +} + PAKFIRE_EXPORT int pakfire_packager_add(struct pakfire_packager* packager, const char* path) { FILE* f = NULL; diff --git a/tests/libpakfire/packager.c b/tests/libpakfire/packager.c index 7838cf0e8..624708428 100644 --- a/tests/libpakfire/packager.c +++ b/tests/libpakfire/packager.c @@ -46,6 +46,11 @@ static int test_create(const struct test* t) { r = pakfire_packager_add(packager, path); ASSERT(r == 0); + // Write archive + FILE* f = test_mktemp(); + r = pakfire_packager_finish(packager, f); + ASSERT(r == 0); + // Cleanup pakfire_packager_unref(packager); pakfire_package_unref(pkg); diff --git a/tests/testsuite.c b/tests/testsuite.c index ee02da21c..fc01ed45e 100644 --- a/tests/testsuite.c +++ b/tests/testsuite.c @@ -100,3 +100,13 @@ int testsuite_run() { return EXIT_SUCCESS; } + +FILE* test_mktemp() { + char path[] = "/tmp/.pakfire-test.XXXXXX"; + + int fd = mkstemp(path); + if (fd < 0) + return NULL; + + return fdopen(fd, "w"); +} diff --git a/tests/testsuite.h b/tests/testsuite.h index 4b180c074..02d3c0ea0 100644 --- a/tests/testsuite.h +++ b/tests/testsuite.h @@ -85,4 +85,7 @@ int testsuite_run(); } \ } while (0) +// Helper functions +FILE* test_mktemp(); + #endif /* PAKFIRE_TESTSUITE_H */