From bf9b62e6092452fa8ca902539ca7810fe6ba20e3 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 4 Nov 2022 09:18:21 +0000 Subject: [PATCH] repos: Add convenience function to download packages from cmdline Signed-off-by: Michael Tremer --- src/libpakfire/build.c | 18 +---- src/libpakfire/include/pakfire/pakfire.h | 3 + src/libpakfire/include/pakfire/repo.h | 3 + src/libpakfire/pakfire.c | 27 ++++++++ src/libpakfire/repo.c | 85 ++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 15 deletions(-) diff --git a/src/libpakfire/build.c b/src/libpakfire/build.c index c5651f58f..a146cee4e 100644 --- a/src/libpakfire/build.c +++ b/src/libpakfire/build.c @@ -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); diff --git a/src/libpakfire/include/pakfire/pakfire.h b/src/libpakfire/include/pakfire/pakfire.h index e557f9dee..664adb097 100644 --- a/src/libpakfire/include/pakfire/pakfire.h +++ b/src/libpakfire/include/pakfire/pakfire.h @@ -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, diff --git a/src/libpakfire/include/pakfire/repo.h b/src/libpakfire/include/pakfire/repo.h index cf96d5c1d..8cce8faa2 100644 --- a/src/libpakfire/include/pakfire/repo.h +++ b/src/libpakfire/include/pakfire/repo.h @@ -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); diff --git a/src/libpakfire/pakfire.c b/src/libpakfire/pakfire.c index b814b603c..11e908d9b 100644 --- a/src/libpakfire/pakfire.c +++ b/src/libpakfire/pakfire.c @@ -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; diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 619166e95..2486fb71c 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -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) -- 2.39.5