From b17434596b6606687e31ed22a7ff8cc9939e3900 Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Wed, 26 Oct 2022 16:40:56 +0000 Subject: [PATCH] packages: Move pakfire_package_get/set_path into string function Signed-off-by: Michael Tremer --- src/libpakfire/archive.c | 4 +- src/libpakfire/include/pakfire/package.h | 3 +- src/libpakfire/package.c | 115 +++++++++++++---------- src/libpakfire/repo.c | 4 +- src/libpakfire/transaction.c | 4 +- 5 files changed, 70 insertions(+), 60 deletions(-) diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index cbcabb0ba..c1c5e9530 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -1073,7 +1073,7 @@ static int pakfire_archive_make_package_from_json(struct pakfire_archive* archiv #endif // Set path - pakfire_package_set_path(pkg, archive->path); + pakfire_package_set_string(pkg, PAKFIRE_PKG_PATH, archive->path); // Set digest switch (PAKFIRE_ARCHIVE_CHECKSUM) { @@ -1375,7 +1375,7 @@ static int pakfire_archive_make_legacy_package(struct pakfire_archive* archive, #endif // Set path - pakfire_package_set_path(pkg, archive->path); + pakfire_package_set_string(pkg, PAKFIRE_PKG_PATH, archive->path); // Set UUID r = pakfire_archive_package_set_string(archive, pkg, diff --git a/src/libpakfire/include/pakfire/package.h b/src/libpakfire/include/pakfire/package.h index bf633d0fa..71086d3bf 100644 --- a/src/libpakfire/include/pakfire/package.h +++ b/src/libpakfire/include/pakfire/package.h @@ -48,6 +48,7 @@ enum pakfire_package_key { PAKFIRE_PKG_VENDOR, PAKFIRE_PKG_DISTRO, PAKFIRE_PKG_MAINTAINER, + PAKFIRE_PKG_PATH, PAKFIRE_PKG_FILENAME, PAKFIRE_PKG_DOWNLOADSIZE, PAKFIRE_PKG_INSTALLSIZE, @@ -94,8 +95,6 @@ const unsigned char* pakfire_package_get_digest(struct pakfire_package* pkg, enum pakfire_digest_types* type, size_t* length); int pakfire_package_set_digest(struct pakfire_package* pkg, enum pakfire_digest_types type, const unsigned char* digest, const size_t length); -const char* pakfire_package_get_path(struct pakfire_package* pkg); -void pakfire_package_set_path(struct pakfire_package* pkg, const char* path); size_t pakfire_package_get_size(struct pakfire_package* pkg); char** pakfire_package_get_provides(struct pakfire_package* pkg); diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index 812a43e23..c24aeadd7 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -323,6 +323,23 @@ static const char* pakfire_package_make_filename(struct pakfire_package* pkg) { return pkg->filename; } +static int pakfire_package_make_cache_path(struct pakfire_package* pkg) { + const char* filename = pakfire_package_get_string(pkg, PAKFIRE_PKG_FILENAME); + + enum pakfire_digest_types digest_type = PAKFIRE_DIGEST_UNDEFINED; + size_t digest_length = 0; + + // Fetch the digest + const unsigned char* digest = pakfire_package_get_digest(pkg, + &digest_type, &digest_length); + + if (digest && digest_length >= 4) + return pakfire_cache_path(pkg->pakfire, pkg->path, + "%02x/%02x/%02x/%02x/%s", digest[0], digest[1], digest[2], digest[3], filename); + + return pakfire_cache_path(pkg->pakfire, pkg->path, "%s", filename); +} + PAKFIRE_EXPORT const char* pakfire_package_get_string( struct pakfire_package* pkg, const enum pakfire_package_key key) { const char* ret = NULL; @@ -388,6 +405,24 @@ PAKFIRE_EXPORT const char* pakfire_package_get_string( ret = solvable_lookup_str(s, SOLVABLE_PACKAGER); break; + case PAKFIRE_PKG_PATH: + if (!*pkg->path) { + const char* base = solvable_lookup_str(s, SOLVABLE_MEDIABASE); + if (base) { + const char* filename = pakfire_package_get_string(pkg, PAKFIRE_PKG_FILENAME); + if (!filename) + return NULL; + + pakfire_string_format(pkg->path, "%s/%s", base, filename); + } else { + r = pakfire_package_make_cache_path(pkg); + if (r) + return NULL; + } + } + + return pkg->path; + case PAKFIRE_PKG_FILENAME: ret = solvable_lookup_str(s, SOLVABLE_MEDIAFILE); @@ -448,6 +483,9 @@ PAKFIRE_EXPORT int pakfire_package_set_string( Pool* pool = pakfire_get_solv_pool(pkg->pakfire); Solvable* s = get_solvable(pkg); + const char* basename = NULL; + const char* dirname = NULL; + switch (key) { // Do not allow to change name, evr, or arch case PAKFIRE_PKG_NAME: @@ -515,6 +553,30 @@ PAKFIRE_EXPORT int pakfire_package_set_string( id = SOLVABLE_PACKAGER; break; + case PAKFIRE_PKG_PATH: + if (value) { + basename = pakfire_basename(value); + dirname = pakfire_dirname(value); + } + + if (basename) + solvable_set_str(s, SOLVABLE_MEDIAFILE, basename); + else + solvable_unset(s, SOLVABLE_MEDIAFILE); + + if (dirname) + solvable_set_str(s, SOLVABLE_MEDIABASE, dirname); + else + solvable_unset(s, SOLVABLE_MEDIABASE); + + // Cache the path + pakfire_string_set(pkg->path, value); + + // Mark the package as changed + pakfire_package_has_changed(pkg); + + return 0; + case PAKFIRE_PKG_FILENAME: id = SOLVABLE_MEDIAFILE; break; @@ -761,57 +823,6 @@ ERROR: return r; } -static int pakfire_package_make_cache_path(struct pakfire_package* pkg) { - const char* filename = pakfire_package_get_string(pkg, PAKFIRE_PKG_FILENAME); - - enum pakfire_digest_types digest_type = PAKFIRE_DIGEST_UNDEFINED; - size_t digest_length = 0; - - // Fetch the digest - const unsigned char* digest = pakfire_package_get_digest(pkg, - &digest_type, &digest_length); - - if (digest && digest_length >= 4) - return pakfire_cache_path(pkg->pakfire, pkg->path, - "%02x/%02x/%02x/%02x/%s", digest[0], digest[1], digest[2], digest[3], filename); - - return pakfire_cache_path(pkg->pakfire, pkg->path, "%s", filename); -} - -PAKFIRE_EXPORT const char* pakfire_package_get_path(struct pakfire_package* pkg) { - int r; - - if (!*pkg->path) { - const char* base = pakfire_package_get_string(pkg, SOLVABLE_MEDIABASE); - if (base) { - const char* filename = pakfire_package_get_string(pkg, PAKFIRE_PKG_FILENAME); - if (!filename) - return NULL; - - pakfire_string_format(pkg->path, "%s/%s", base, filename); - } else { - r = pakfire_package_make_cache_path(pkg); - if (r) - return NULL; - } - } - - return pkg->path; -} - -PAKFIRE_EXPORT void pakfire_package_set_path(struct pakfire_package* pkg, const char* path) { - const char* basename = pakfire_basename(path); - const char* dirname = pakfire_dirname(path); - - if (basename) - pakfire_package_set_string(pkg, SOLVABLE_MEDIAFILE, basename); - - if (dirname) - pakfire_package_set_string(pkg, SOLVABLE_MEDIABASE, dirname); - - pakfire_string_set(pkg->path, path); -} - int pakfire_package_is_installed(struct pakfire_package* pkg) { Pool* pool = pakfire_get_solv_pool(pkg->pakfire); Solvable* s = get_solvable(pkg); @@ -1305,7 +1316,7 @@ PAKFIRE_EXPORT struct pakfire_archive* pakfire_package_get_archive(struct pakfir struct pakfire_archive* archive = NULL; // Otherwise open the archive from the cache - const char* path = pakfire_package_get_path(pkg); + const char* path = pakfire_package_get_string(pkg, PAKFIRE_PKG_PATH); if (!path) return NULL; diff --git a/src/libpakfire/repo.c b/src/libpakfire/repo.c index 438d11940..dfbf158cc 100644 --- a/src/libpakfire/repo.c +++ b/src/libpakfire/repo.c @@ -1026,7 +1026,7 @@ static int pakfire_repo_delete_all_packages( return 1; const char* nevra = pakfire_package_get_string(pkg, PAKFIRE_PKG_NEVRA); - const char* path = pakfire_package_get_path(pkg); + const char* path = pakfire_package_get_string(pkg, PAKFIRE_PKG_PATH); // If prefix is set, skip anything that doesn't match if (prefix && !pakfire_string_startswith(path, prefix)) @@ -1498,7 +1498,7 @@ PAKFIRE_EXPORT int pakfire_repo_compose(struct pakfire* pakfire, const char* pat const char* repo_path = pakfire_path_relpath(path, destination_path); // Update package location relative to the repository directory - pakfire_package_set_path(package, repo_path); + pakfire_package_set_string(package, PAKFIRE_PKG_PATH, repo_path); OUT: if (archive) { diff --git a/src/libpakfire/transaction.c b/src/libpakfire/transaction.c index 33aebd36a..73a61e41d 100644 --- a/src/libpakfire/transaction.c +++ b/src/libpakfire/transaction.c @@ -1149,7 +1149,7 @@ static int pakfire_transaction_download_package(struct pakfire_transaction* tran goto ERROR; // Where to store the package? - const char* path = pakfire_package_get_path(pkg); + const char* path = pakfire_package_get_string(pkg, PAKFIRE_PKG_PATH); if (!path) { ERROR(transaction->pakfire, "Could not retrieve package path for %s: %m\n", nevra); goto ERROR; @@ -1200,7 +1200,7 @@ static int pakfire_transaction_package_needs_download( if (pakfire_package_is_installed(pkg)) return 0; - const char* path = pakfire_package_get_path(pkg); + const char* path = pakfire_package_get_string(pkg, PAKFIRE_PKG_PATH); // Does the file exist? int r = access(path, R_OK); -- 2.39.5