]> git.ipfire.org Git - pakfire.git/commitdiff
packages: Introduce a function to return the expected path
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 20:05:07 +0000 (20:05 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 20:05:07 +0000 (20:05 +0000)
Since we need the paths in other places, this is the best way.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/pakfire/package.c
src/pakfire/package.h

index c048cec5aa806a879f5110214b624ab9fbc2931c..9dd22650f4b4f9873e1b271cc1fde8dc728e7b1c 100644 (file)
@@ -69,6 +69,7 @@ struct pakfire_package {
        char filename[NAME_MAX];
        char path[PATH_MAX];
        char cache_path[PATH_MAX];
+       char local_path[PATH_MAX];
 
        struct {
                pcre2_code* requires;
@@ -466,6 +467,81 @@ const char* pakfire_package_get_filename(struct pakfire_package* pkg) {
        return pkg->filename;
 }
 
+static int pakfire_package_make_path_internal(struct pakfire_package* self) {
+       const char* path = NULL;
+
+       // Fetch the path
+       path = pakfire_package_get_string(self, PAKFIRE_PKG_PATH);
+       if (!path)
+               return -EINVAL;
+
+       return pakfire_string_set(self->local_path, path);
+}
+
+static int pakfire_package_make_path_local(struct pakfire_package* self, struct pakfire_repo* repo) {
+       const char* path = NULL;
+
+       // Fetch the path
+       path = pakfire_package_get_string(self, PAKFIRE_PKG_PATH);
+       if (!path)
+               return -EINVAL;
+
+       // Compose path relative to the repository's base
+       return pakfire_repo_path(repo, self->local_path, "%s", path);
+}
+
+static int pakfire_package_make_path_cache(struct pakfire_package* self) {
+       const char* path = NULL;
+
+       // Fetch the cache path
+       path = pakfire_package_get_string(self, PAKFIRE_PKG_CACHE_PATH);
+       if (!path)
+               return -EINVAL;
+
+       return pakfire_string_set(self->local_path, path);
+}
+
+/*
+       Returns the path this package is or should be stored.
+*/
+const char* pakfire_package_get_path(struct pakfire_package* self) {
+       struct pakfire_repo* repo = NULL;
+       int r;
+
+       // Return the cached value if we have one
+       if (*self->local_path)
+               return self->local_path;
+
+       // Fetch NEVRA
+       const char* nevra = pakfire_package_get_string(self, PAKFIRE_PKG_NEVRA);
+
+       // Fetch the repository
+       repo = pakfire_package_get_repo(self);
+       if (!repo) {
+               ERROR(self->ctx, "Could not find repository for %s\n", nevra);
+               r = -ENOTSUP;
+               goto ERROR;
+       }
+
+       // For internal repositories, we return the stored path
+       if (pakfire_repo_is_internal(repo))
+               r = pakfire_package_make_path_internal(self);
+
+       // Check local repositories
+       else if (pakfire_repo_is_local(repo))
+               r = pakfire_package_make_path_local(self, repo);
+
+       // Everything else should be stored in the cache
+       else
+               r = pakfire_package_make_path_cache(self);
+
+ERROR:
+       if (repo)
+               pakfire_repo_unref(repo);
+
+       return (r == 0) ? self->local_path : NULL;
+}
+
 static int pakfire_package_make_cache_path(struct pakfire_package* pkg) {
        const char* nevra = pakfire_package_get_string(pkg, PAKFIRE_PKG_NEVRA);
 
@@ -1304,10 +1380,19 @@ ERROR:
        return r;
 }
 
-static int pakfire_package_is_available_exists(struct pakfire_package* self, const char* path) {
+/*
+       This function checks if the archive is available.
+*/
+int pakfire_package_is_available(struct pakfire_package* self) {
+       const char* path = NULL;
        struct stat st = {};
        int r;
 
+       // Fetch the path
+       path = pakfire_package_get_path(self);
+       if (!path)
+               return -EINVAL;
+
        // Stat the path
        r = stat(path, &st);
        if (r < 0) {
@@ -1343,72 +1428,6 @@ static int pakfire_package_is_available_exists(struct pakfire_package* self, con
        return 1;
 }
 
-static int pakfire_package_is_available_local(
-               struct pakfire_package* self, struct pakfire_repo* repo) {
-       char path[PATH_MAX];
-       int r;
-
-       const char* pkg_path = pakfire_package_get_string(self, PAKFIRE_PKG_PATH);
-
-       // Compose path
-       r = pakfire_repo_path(repo, path, "%s", pkg_path);
-       if (r < 0) {
-               ERROR(self->ctx, "Could not compose package path: %s\n", strerror(-r));
-               return r;
-       }
-
-       // Check if the package actually exists
-       return pakfire_package_is_available_exists(self, path);
-}
-
-static int pakfire_package_is_available_cache(struct pakfire_package* self) {
-       // Fetch the path the package should be in the cache
-       const char* path = pakfire_package_get_string(self, PAKFIRE_PKG_CACHE_PATH);
-
-       // Check if the package actually exists
-       return pakfire_package_is_available_exists(self, path);
-}
-
-/*
-       This function checks if the archive is available.
-
-       For local repositories, the path is checked, otherwise we will check the cache.
-*/
-int pakfire_package_is_available(struct pakfire_package* self) {
-       struct pakfire_repo* repo = NULL;
-       int r;
-
-       // Fetch NEVRA
-       const char* nevra = pakfire_package_get_string(self, PAKFIRE_PKG_NEVRA);
-
-       // Fetch the repository
-       repo = pakfire_package_get_repo(self);
-       if (!repo) {
-               ERROR(self->ctx, "Could not find repository for %s\n", nevra);
-               r = -ENOTSUP;
-               goto ERROR;
-       }
-
-       // Check local repositories
-       if (pakfire_repo_is_local(repo)) {
-               r = pakfire_package_is_available_local(self, repo);
-               if (r < 0)
-                       goto ERROR;
-
-       // Check remote repositories
-       } else {
-               r = pakfire_package_is_available_cache(self);
-               if (r < 0)
-                       goto ERROR;
-       }
-
-ERROR:
-       if (repo)
-               pakfire_repo_unref(repo);
-
-       return r;
-}
-
 int pakfire_package_is_installed(struct pakfire_package* pkg) {
        Pool* pool = pakfire_get_solv_pool(pkg->pakfire);
        Solvable* s = get_solvable(pkg);
index 96c499eb8040219da45ed7eea597418b2e036608..1e0c3ddffe0a7876e73d59f8f3016af35ea96614 100644 (file)
@@ -164,6 +164,7 @@ int pakfire_package_set_strings_from_string(struct pakfire_package* pkg,
        const enum pakfire_package_key key, const char* value);
 
 const char* pakfire_package_get_filename(struct pakfire_package* pkg);
+const char* pakfire_package_get_path(struct pakfire_package* self);
 
 int pakfire_package_is_source(struct pakfire_package* pkg);
 int pakfire_package_supports_build_arch(struct pakfire_package* pkg, const char* arch);