From: Michael Tremer Date: Tue, 27 Apr 2021 11:19:33 +0000 (+0000) Subject: packages: Store paths locally and in SOLV data X-Git-Tag: 0.9.28~1285^2~229 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b12a5d7db6809ef54d6c43af7710ade7f4f6c647;p=pakfire.git packages: Store paths locally and in SOLV data pakfire_package_get_path either returns the path to the archive file on disk or where it should be. It is up to the caller to determine if that file exists or not. Signed-off-by: Michael Tremer --- diff --git a/src/_pakfire/package.c b/src/_pakfire/package.c index e9c85dcaf..efd9fb7e1 100644 --- a/src/_pakfire/package.c +++ b/src/_pakfire/package.c @@ -408,14 +408,6 @@ static void Package_set_buildtime(PackageObject* self, PyObject* value) { pakfire_package_set_build_time(self->package, build_time); } -static PyObject* Package_get_cache_path(PackageObject* self) { - char* cache_path = pakfire_package_get_cache_path(self->package); - PyObject* ret = PyUnicode_FromString(cache_path); - free(cache_path); - - return ret; -} - static PyObject* Package_get_repo(PackageObject* self) { PakfireRepo repo = pakfire_package_get_repo(self->package); if (!repo) @@ -427,13 +419,14 @@ static PyObject* Package_get_repo(PackageObject* self) { return obj; } -static PyObject* Package_get_location(PackageObject* self) { - char* location = pakfire_package_get_location(self->package); - - PyObject* str = PyUnicode_FromString(location); - free(location); +static PyObject* Package_get_path(PackageObject* self) { + const char* path = pakfire_package_get_path(self->package); + if (!path) { + PyErr_SetFromErrno(PyExc_OSError); + return NULL; + } - return str; + return PyUnicode_FromString(path); } static PyObject* PyList_FromRelationList(PakfireRelationList relationlist) { @@ -1030,13 +1023,6 @@ static struct PyGetSetDef Package_getsetters[] = { NULL, NULL }, - { - "cache_path", - (getter)Package_get_cache_path, - NULL, - NULL, - NULL - }, // Dependencies { @@ -1091,8 +1077,8 @@ static struct PyGetSetDef Package_getsetters[] = { NULL }, { - "location", - (getter)Package_get_location, + "path", + (getter)Package_get_path, NULL, NULL, NULL diff --git a/src/libpakfire/archive.c b/src/libpakfire/archive.c index a2ddbbf1a..77bbba0ca 100644 --- a/src/libpakfire/archive.c +++ b/src/libpakfire/archive.c @@ -1386,12 +1386,8 @@ PAKFIRE_EXPORT PakfirePackage pakfire_archive_make_package(PakfireArchive archiv free(nevra); #endif - // Set filename - char* filename = pakfire_basename(archive->path); - if (filename) { - pakfire_package_set_filename(pkg, filename); - free(filename); - } + // Set path + pakfire_package_set_path(pkg, archive->path); // Set UUID char* uuid = pakfire_archive_get(archive, "package", "uuid"); diff --git a/src/libpakfire/include/pakfire/package.h b/src/libpakfire/include/pakfire/package.h index 8ab0824dd..2e3cdfba6 100644 --- a/src/libpakfire/include/pakfire/package.h +++ b/src/libpakfire/include/pakfire/package.h @@ -74,6 +74,8 @@ void pakfire_package_set_vendor(PakfirePackage pkg, const char* vendor); const char* pakfire_package_get_maintainer(PakfirePackage pkg); void pakfire_package_set_maintainer(PakfirePackage pkg, const char* maintainer); const char* pakfire_package_get_filename(PakfirePackage pkg); +const char* pakfire_package_get_path(PakfirePackage pkg); +void pakfire_package_set_path(PakfirePackage pkg, const char* path); void pakfire_package_set_filename(PakfirePackage pkg, const char* filename); int pakfire_package_is_installed(PakfirePackage pkg); unsigned long long pakfire_package_get_downloadsize(PakfirePackage pkg); @@ -115,16 +117,10 @@ void pakfire_package_set_enhances(PakfirePackage pkg, PakfireRelationList relati PakfireRepo pakfire_package_get_repo(PakfirePackage pkg); -char* pakfire_package_get_location(PakfirePackage pkg); - char* pakfire_package_dump(PakfirePackage pkg, int flags); PakfireArchive pakfire_package_get_archive(PakfirePackage pkg); -int pakfire_package_is_cached(PakfirePackage pkg); -char* pakfire_package_get_cache_path(PakfirePackage pkg); -char* pakfire_package_get_cache_full_path(PakfirePackage pkg); - PakfireFilelist pakfire_package_get_filelist(PakfirePackage pkg); int pakfire_package_set_filelist(PakfirePackage pkg, PakfireFilelist filelist); int pakfire_package_set_filelist_from_string(PakfirePackage pkg, const char* files); diff --git a/src/libpakfire/libpakfire.sym b/src/libpakfire/libpakfire.sym index 4d0f7c85e..d1225162c 100644 --- a/src/libpakfire/libpakfire.sym +++ b/src/libpakfire/libpakfire.sym @@ -183,6 +183,7 @@ global: pakfire_package_get_nevra; pakfire_package_get_obsoletes; pakfire_package_get_pakfire; + pakfire_package_get_path; pakfire_package_get_prerequires; pakfire_package_get_provides; pakfire_package_get_recommends; @@ -219,6 +220,7 @@ global: pakfire_package_set_maintainer; pakfire_package_set_name; pakfire_package_set_obsoletes; + pakfire_package_set_path; pakfire_package_set_prerequires; pakfire_package_set_provides; pakfire_package_set_recommends; diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index f0f2bba2f..62fe73afb 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -48,6 +48,8 @@ struct _PakfirePackage { Id id; int nrefs; + char filename[NAME_MAX]; + char path[PATH_MAX]; PakfireRepo repo; }; @@ -507,8 +509,94 @@ PAKFIRE_EXPORT void pakfire_package_set_maintainer(PakfirePackage pkg, const cha pakfire_package_set_string(pkg, SOLVABLE_PACKAGER, maintainer); } +static int pakfire_package_make_cache_path(PakfirePackage pkg) { + const char* filename = pakfire_package_get_filename(pkg); + const char* checksum = pakfire_package_get_checksum(pkg); + + if (strlen(checksum) < 3) + return 1; + + return pakfire_make_cache_path(pkg->pakfire, pkg->path, + "%c%c/%s/%s", checksum[0], checksum[1], checksum + 2, filename); +} + +PAKFIRE_EXPORT const char* pakfire_package_get_path(PakfirePackage pkg) { + int r; + + if (!*pkg->path) { + const char* base = pakfire_package_get_string(pkg, SOLVABLE_MEDIABASE); + if (base) { + const char* filename = pakfire_package_get_filename(pkg); + 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(PakfirePackage pkg, const char* path) { + char* basename = pakfire_basename(path); + char* dirname = pakfire_dirname(path); + + if (basename) { + pakfire_package_set_string(pkg, SOLVABLE_MEDIAFILE, basename); + free(basename); + } + + if (dirname) { + pakfire_package_set_string(pkg, SOLVABLE_MEDIABASE, dirname); + free(dirname); + } + + pakfire_string_set(pkg->path, path); +} + +// Removes epoch +static const char* evr2vr(const char* evr) { + const char* p = evr; + + // Skip any leading digits + for (; *p >= '0' && *p <= '9'; p++); + + // If after the leading digits, we found :, we return the rest of the string + if (p != evr && *p == ':') + return ++p; + + return evr; +} + +static const char* pakfire_package_make_filename(PakfirePackage pkg) { + if (!*pkg->filename) { + const char* name = pakfire_package_get_name(pkg); + const char* evr = pakfire_package_get_evr(pkg); + const char* arch = pakfire_package_get_arch(pkg); + + if (!name || !evr || !arch) + return NULL; + + const char* vr = evr2vr(evr); + + pakfire_string_format(pkg->filename, "%s-%s.%s.pfm", name, vr, arch); + } + + return pkg->filename; +} + PAKFIRE_EXPORT const char* pakfire_package_get_filename(PakfirePackage pkg) { - return pakfire_package_get_string(pkg, SOLVABLE_MEDIAFILE); + const char* filename = pakfire_package_get_string(pkg, SOLVABLE_MEDIAFILE); + + // Generate the filename if not set + if (!filename) + filename = pakfire_package_make_filename(pkg); + + return filename; } PAKFIRE_EXPORT void pakfire_package_set_filename(PakfirePackage pkg, const char* filename) { @@ -728,26 +816,6 @@ PAKFIRE_EXPORT PakfireRepo pakfire_package_get_repo(PakfirePackage pkg) { return pakfire_repo_ref(pkg->repo); } -PAKFIRE_EXPORT void pakfire_package_set_repo(PakfirePackage pkg, PakfireRepo repo) { - // Free old repository - if (pkg->repo) - pakfire_repo_unref(pkg->repo); - - Solvable* s = get_solvable(pkg); - s->repo = pakfire_repo_get_repo(repo); - - pkg->repo = pakfire_repo_ref(repo); -} - -PAKFIRE_EXPORT char* pakfire_package_get_location(PakfirePackage pkg) { - pakfire_package_internalize_repo(pkg); - - Solvable* s = get_solvable(pkg); - - const char* location = solvable_get_location(s, NULL); - return strdup(location); -} - static void pakfire_package_dump_add_line(char** str, const char* key, const char* val) { if (val) asprintf(str, "%s%-15s: %s\n", *str, key ? key : "", val); @@ -938,42 +1006,16 @@ PAKFIRE_EXPORT char* pakfire_package_dump(PakfirePackage pkg, int flags) { return string; } -PAKFIRE_EXPORT int pakfire_package_is_cached(PakfirePackage pkg) { - char* path = pakfire_package_get_cache_path(pkg); - - // Check if the file is readable - int r = access(path, R_OK); - free(path); - - return (r == 0); -} - -PAKFIRE_EXPORT char* pakfire_package_get_cache_path(PakfirePackage pkg) { - char path[PATH_MAX]; - - const char* filename = pakfire_package_get_filename(pkg); - const char* checksum = pakfire_package_get_checksum(pkg); - - if (strlen(checksum) < 3) - return NULL; - - pakfire_make_cache_path(pkg->pakfire, path, - "%c%c/%s/%s", checksum[0], checksum[1], checksum + 2, filename); - - return strdup(path); -} - PAKFIRE_EXPORT PakfireArchive pakfire_package_get_archive(PakfirePackage pkg) { + PakfireArchive archive; + // Otherwise open the archive from the cache - char* path = pakfire_package_get_cache_path(pkg); + const char* path = pakfire_package_get_path(pkg); if (!path) return NULL; - PakfireArchive archive = NULL; - // Open archive int r = pakfire_archive_open(&archive, pkg->pakfire, path); - free(path); if (r == 0) return archive; diff --git a/src/libpakfire/step.c b/src/libpakfire/step.c index de1c93fdc..e9a69a905 100644 --- a/src/libpakfire/step.c +++ b/src/libpakfire/step.c @@ -182,8 +182,11 @@ PAKFIRE_EXPORT int pakfire_step_needs_download(PakfireStep step) { if (pakfire_repo_is_installed_repo(repo) == 0) return false; - // Return false if package is in cache. - if (pakfire_package_is_cached(step->package)) + const char* path = pakfire_package_get_path(step->package); + + // Does the file exist? + int r = access(path, R_OK); + if (r == 0) return false; return true; @@ -198,13 +201,9 @@ static int pakfire_step_verify(PakfireStep step) { step->archive = pakfire_package_get_archive(step->package); if (!step->archive) { char* nevra = pakfire_package_get_nevra(step->package); - char* cache_path = pakfire_package_get_cache_path(step->package); - ERROR(step->pakfire, "Could not open package archive for %s: %s\n", - nevra, cache_path); - + nevra, strerror(errno)); free(nevra); - free(cache_path); return -1; } diff --git a/src/libpakfire/transaction.c b/src/libpakfire/transaction.c index 5a8c17ef8..9b8d57ffd 100644 --- a/src/libpakfire/transaction.c +++ b/src/libpakfire/transaction.c @@ -410,7 +410,6 @@ static int pakfire_transaction_download_package(PakfireTransaction transaction, int r = 1; PakfireRepo repo = NULL; struct pakfire_mirrorlist* mirrorlist = NULL; - char* cache_path = NULL; char* nevra = NULL; // Fetch the repository to download from @@ -425,8 +424,8 @@ static int pakfire_transaction_download_package(PakfireTransaction transaction, mirrorlist = pakfire_repo_get_mirrorlist(repo); // Where to store the package? - cache_path = pakfire_package_get_cache_path(pkg); - if (!cache_path) + const char* path = pakfire_package_get_path(pkg); + if (!path) goto ERROR; // What file to download? @@ -440,13 +439,11 @@ static int pakfire_transaction_download_package(PakfireTransaction transaction, // Add transfer to downloader r = pakfire_downloader_add_transfer(downloader, baseurl, mirrorlist, - nevra, filename, cache_path, 0); + nevra, filename, path, 0); ERROR: if (nevra) free(nevra); - if (cache_path) - free(cache_path); if (mirrorlist) pakfire_mirrorlist_unref(mirrorlist); if (repo)