]> git.ipfire.org Git - pakfire.git/commitdiff
packages: Store paths locally and in SOLV data
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Apr 2021 11:19:33 +0000 (11:19 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 27 Apr 2021 11:19:33 +0000 (11:19 +0000)
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 <michael.tremer@ipfire.org>
src/_pakfire/package.c
src/libpakfire/archive.c
src/libpakfire/include/pakfire/package.h
src/libpakfire/libpakfire.sym
src/libpakfire/package.c
src/libpakfire/step.c
src/libpakfire/transaction.c

index e9c85dcafac9fff7d3432e2d48806c4369ed6b5a..efd9fb7e1f2768c271d61120021ed21335a9ac6b 100644 (file)
@@ -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
index a2ddbbf1ab6ae62e407bd6f5e1c26ae9c9491f99..77bbba0ca773d1e6afb884b7ecdfc8519d4d2743 100644 (file)
@@ -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");
index 8ab0824dd29c5c9121919fd17d56fa527f40bb88..2e3cdfba627e84d7a173f66c87aa349f8932a74d 100644 (file)
@@ -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);
index 4d0f7c85e96f9146edcad539cf4ed264c250b071..d1225162cb035b08719c8a85a986b2dba447bdd3 100644 (file)
@@ -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;
index f0f2bba2f7e452c68d21af9506dda30c42092678..62fe73afbdb5a7e7ccb24d4cee01104ac7a5f51a 100644 (file)
@@ -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;
index de1c93fdc617efe9d2a46c62523e00872bc52096..e9a69a905459ed3ed450b70803254c66a0862962 100644 (file)
@@ -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;
        }
index 5a8c17ef8adbfd7aa65683656c79f1f0e04d6f95..9b8d57ffddba46e66efbaf3c040cf9d4626ecdf8 100644 (file)
@@ -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)