]> git.ipfire.org Git - pakfire.git/commitdiff
repos: Add convenience function to download packages from cmdline
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Nov 2022 09:18:21 +0000 (09:18 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 4 Nov 2022 09:18:21 +0000 (09:18 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/build.c
src/libpakfire/include/pakfire/pakfire.h
src/libpakfire/include/pakfire/repo.h
src/libpakfire/pakfire.c
src/libpakfire/repo.c

index c5651f58fbb373ef06007a25cf7a58d0d6b074ec..a146cee4e20e1939e8926389b86edf3f4bd0561f 100644 (file)
@@ -1488,7 +1488,6 @@ ERROR:
 }
 
 PAKFIRE_EXPORT int pakfire_build_exec(struct pakfire_build* build, const char* path) {
-       struct pakfire_archive* archive = NULL;
        struct pakfire_package* package = NULL;
        struct pakfire_parser* makefile = NULL;
        char* buildroot = NULL;
@@ -1499,19 +1498,10 @@ PAKFIRE_EXPORT int pakfire_build_exec(struct pakfire_build* build, const char* p
        if (r)
                goto ERROR;
 
-       // Open source archive
-       r = pakfire_archive_open(&archive, build->pakfire, path);
-       if (r) {
-               ERROR(build->pakfire, "Could not open source archive %s: %m\n", path);
-               goto ERROR;
-       }
-
-       // Fetch package metadata
-       r = pakfire_archive_make_package(archive, NULL, &package);
-       if (r) {
-               ERROR(build->pakfire, "Could not read package metadata: %m\n");
+       // Open the source package
+       r = pakfire_commandline_add(build->pakfire, path, &package);
+       if (r)
                goto ERROR;
-       }
 
        const char* nevra = pakfire_package_get_string(package, PAKFIRE_PKG_NEVRA);
 
@@ -1571,8 +1561,6 @@ PAKFIRE_EXPORT int pakfire_build_exec(struct pakfire_build* build, const char* p
 ERROR:
        if (makefile)
                pakfire_parser_unref(makefile);
-       if (archive)
-               pakfire_archive_unref(archive);
        if (package)
                pakfire_package_unref(package);
 
index e557f9dee2fd9b8f88ab7b0bf9c83ed404678fe5..664adb097ad39ce2c7f3b5795d54300479bb90b6 100644 (file)
@@ -179,6 +179,9 @@ Pool* pakfire_get_solv_pool(struct pakfire* pakfire);
 
 struct pakfire_repo* pakfire_get_installed_repo(struct pakfire* pakfire);
 
+int pakfire_commandline_add(struct pakfire* pakfire, const char* path,
+       struct pakfire_package** package);
+
 typedef int (*pakfire_repo_walk_callback)
        (struct pakfire* pakfire, struct pakfire_repo* repo, void* p);
 int pakfire_repo_walk(struct pakfire* pakfire,
index cf96d5c1db14a84700ed936d25a15d44b558452b..8cce8faa2a18d1810d67c235223ec2615211fa94 100644 (file)
@@ -121,6 +121,9 @@ Id pakfire_repo_add_solvable(struct pakfire_repo* repo);
 int pakfire_repo_add_archive(struct pakfire_repo* repo,
        struct pakfire_archive* archive, struct pakfire_package** package);
 
+int pakfire_repo_add(struct pakfire_repo* repo, const char* path,
+       struct pakfire_package** package);
+
 struct pakfire_repo* pakfire_repo_create_from_repo(struct pakfire* pakfire, Repo* r);
 void pakfire_repo_free_all(struct pakfire* pakfire);
 
index b814b603c2e88f8381806fded42c3db86dde6ba5..11e908d9bbffb9b9e7cf8217422a5a347366f89d 100644 (file)
@@ -1365,6 +1365,33 @@ struct pakfire_repo* pakfire_get_installed_repo(struct pakfire* pakfire) {
        return pakfire_repo_create_from_repo(pakfire, pakfire->pool->installed);
 }
 
+/*
+       Convenience function to add a package to the @commandline repository
+*/
+int pakfire_commandline_add(struct pakfire* pakfire, const char* path,
+               struct pakfire_package** package) {
+       struct pakfire_repo* repo = NULL;
+       int r;
+
+       // Find the commandline repository
+       repo = pakfire_get_repo(pakfire, PAKFIRE_REPO_COMMANDLINE);
+       if (!repo) {
+               ERROR(pakfire, "Could not find the commandline repository: %m\n");
+               return 1;
+       }
+
+       // Add the package
+       r = pakfire_repo_add(repo, path, package);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       if (repo)
+               pakfire_repo_unref(repo);
+
+       return r;
+}
+
 static int pakfire_search_dep(struct pakfire* pakfire, Id type, const char* what, int flags,
                struct pakfire_packagelist** list) {
        int r;
index 619166e95f10c89150a09290c10233903127c178..2486fb71cdbc849a69892d4b309a87ad2bba3227 100644 (file)
@@ -104,6 +104,10 @@ int pakfire_repo_name_equals(struct pakfire_repo* repo, const char* name) {
        return strcmp(n, name) == 0;
 }
 
+static int pakfire_repo_is_commandline(struct pakfire_repo* repo) {
+       return pakfire_repo_name_equals(repo, PAKFIRE_REPO_COMMANDLINE);
+}
+
 static int pakfire_repo_retrieve(
                struct pakfire_repo* repo,
                const char* title,
@@ -897,6 +901,87 @@ PAKFIRE_EXPORT int pakfire_repo_is_installed_repo(struct pakfire_repo* repo) {
        return (r == 0);
 }
 
+static int pakfire_repo_download(struct pakfire_repo* repo, const char* url,
+               struct pakfire_package** package) {
+       struct pakfire_downloader* downloader = NULL;
+       FILE* f = NULL;
+       int r;
+
+       char path[PATH_MAX] = PAKFIRE_TMP_DIR "/pakfire-download.XXXXXX";
+
+       // Allocate a temporary file name
+       f = pakfire_mktemp(path, 0);
+       if (!f) {
+               r = 1;
+               goto ERROR;
+       }
+
+       // Create the downloader
+       r = pakfire_downloader_create(&downloader, repo->pakfire);
+       if (r)
+               goto ERROR;
+
+       // Download the file
+       r = pakfire_downloader_retrieve(downloader, NULL, NULL, NULL,
+               url, path, 0, NULL, 0, PAKFIRE_TRANSFER_NOTEMP);
+       if (r)
+               goto ERROR;
+
+       // Try to add it to this repository
+       r = pakfire_repo_add(repo, path, package);
+       if (r)
+               goto ERROR;
+
+ERROR:
+       if (downloader)
+               pakfire_downloader_unref(downloader);
+       if (f)
+               fclose(f);
+
+       return r;
+}
+
+int pakfire_repo_add(struct pakfire_repo* repo, const char* path,
+               struct pakfire_package** package) {
+       struct pakfire_archive* archive = NULL;
+       int r;
+
+       // This operation is only supported for the command line repository
+       if (!pakfire_repo_is_commandline(repo)) {
+               errno = ENOTSUP;
+               return 1;
+       }
+
+       // Download the package if we got given a URL
+       if (pakfire_string_is_url(path))
+               return pakfire_repo_download(repo, path, package);
+
+       // Try to open the archive
+       r = pakfire_archive_open(&archive, repo->pakfire, path);
+       if (r)
+               goto ERROR;
+
+       // Add it to this repository
+       r = pakfire_repo_add_archive(repo, archive, package);
+       if (r)
+               goto ERROR;
+
+       const char* nevra = pakfire_package_get_string(*package, PAKFIRE_PKG_NEVRA);
+       if (!nevra) {
+               r = 1;
+               goto ERROR;
+       }
+
+       DEBUG(repo->pakfire, "Added %s to repository '%s'\n",
+               nevra, pakfire_repo_get_name(repo));
+
+ERROR:
+       if (archive)
+               pakfire_archive_unref(archive);
+
+       return r;
+}
+
 PAKFIRE_EXPORT int pakfire_repo_read_solv(struct pakfire_repo* repo, FILE *f, int flags) {
        f = pakfire_xfopen(f, "r");
        if (!f)