]> git.ipfire.org Git - pakfire.git/commitdiff
repo: Implement the reverse of pakfire_package_get_path()
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 22:07:21 +0000 (22:07 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 30 Jan 2025 22:07:21 +0000 (22:07 +0000)
This hopefully fixes that we will copy/link packages to themselves.

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

index 0915c8aca5efa73a2c03be06381702fce7c1d423..a04aa81f45cdc7ea734570868d373d49d5148f45 100644 (file)
@@ -1545,6 +1545,7 @@ static int pakfire_archive_import_filelist_from_json(
 static int pakfire_archive_make_package_from_json(struct pakfire_archive* archive,
                struct pakfire_repo* repo, struct pakfire_package** package) {
        struct pakfire_package* pkg = NULL;
+       char path[PATH_MAX];
        int r;
 
        // Calculate digest
@@ -1569,8 +1570,15 @@ static int pakfire_archive_make_package_from_json(struct pakfire_archive* archiv
                nevra, pkg, archive);
 #endif
 
+       // Make the path where this archive should be stored
+       r = pakfire_repo_make_path(repo, path, archive, pkg);
+       if (r < 0)
+               goto ERROR;
+
        // Set path
-       pakfire_package_set_string(pkg, PAKFIRE_PKG_PATH, archive->path);
+       r = pakfire_package_set_string(pkg, PAKFIRE_PKG_PATH, path);
+       if (r < 0)
+               goto ERROR;
 
        // Set digest
        switch (PAKFIRE_ARCHIVE_CHECKSUM) {
index f97e4825ffcb2fe12cbde899f9a6532981d00f63..516d905cde7765cbdc70eca98d9603c9e271efcd 100644 (file)
@@ -227,16 +227,6 @@ int __pakfire_repo_path(struct pakfire_repo* repo,
        return __pakfire_cache_path(repo->pakfire, path, length, "%s/%s", name, buffer);
 }
 
-#define pakfire_repo_relpath(repo, result, path) \
-       __pakfire_repo_relpath(repo, result, sizeof(result), path);
-
-static int __pakfire_repo_relpath(struct pakfire_repo* self, char* result, size_t length, const char* path) {
-       // Fetch the repo's base path
-       const char* basepath = pakfire_repo_get_path(self);
-
-       return __pakfire_path_relative(result, length, basepath, path);
-}
-
 static int pakfire_repo_xfer_create(struct pakfire_xfer** xfer, struct pakfire_repo* repo,
        const char* url, ...) __attribute__((format(printf, 3, 4)));
 
@@ -414,18 +404,9 @@ Id pakfire_repo_add_solvable(struct pakfire_repo* repo) {
        return id;
 }
 
-int pakfire_repo_import_archive(struct pakfire_repo* self,
-               struct pakfire_archive* archive, struct pakfire_package** package) {
-       struct pakfire_package* pkg = NULL;
+int __pakfire_repo_make_path(struct pakfire_repo* self, char* path, size_t length,
+               struct pakfire_archive* archive, struct pakfire_package* pkg) {
        const char* uuid = NULL;
-       char relpath[PATH_MAX];
-       char path[PATH_MAX];
-       int r;
-
-       // Add the metadata
-       r = pakfire_archive_make_package(archive, self, &pkg);
-       if (r < 0)
-               goto ERROR;
 
        // Fetch NEVRA
        const char* nevra = pakfire_package_get_string(pkg, PAKFIRE_PKG_NEVRA);
@@ -435,51 +416,57 @@ int pakfire_repo_import_archive(struct pakfire_repo* self,
        // Fetch the filename this package should have
        const char* filename = pakfire_package_get_filename(pkg);
        if (!filename)
-               return -EINVAL;
+               return -ENODATA;
 
        // Compose the destination path
        switch (self->appdata->fs_layout) {
                case PAKFIRE_REPO_FLAT:
-                       r = pakfire_repo_path(self, path, "%s", filename);
-                       if (r < 0)
-                               goto ERROR;
-                       break;
+                       return __pakfire_repo_path(self, path, length, "%s", filename);
 
                case PAKFIRE_REPO_UUID:
                        // Fetch the UUID
                        uuid = pakfire_package_get_string(pkg, PAKFIRE_PKG_UUID);
                        if (!uuid) {
                                ERROR(self->ctx, "%s does not have a UUID\n", nevra);
-                               r = -EINVAL;
-                               goto ERROR;
+                               return -ENODATA;
                        }
 
-                       r = pakfire_repo_path(self, path, "%s/%s", uuid, filename);
-                       if (r < 0)
-                               goto ERROR;
-                       break;
+                       return __pakfire_repo_path(self, path, length, "%s/%s", uuid, filename);
 
                // In virtual mode, we don't actually import the file
                case PAKFIRE_REPO_VIRT:
-                       goto END;
+                       return __pakfire_string_set(path, length, pakfire_archive_get_path(archive));
        }
 
-       // Compose the path of the archive relative to the repository path
-       r = pakfire_repo_relpath(self, relpath, path);
-       if (r < 0)
-               goto ERROR;
+       return -EINVAL;
+}
 
-       // Copy (or link) the archive
-       r = pakfire_archive_link_or_copy(archive, path);
-       if (r < 0)
-               goto ERROR;
+int pakfire_repo_import_archive(struct pakfire_repo* self,
+               struct pakfire_archive* archive, struct pakfire_package** package) {
+       struct pakfire_package* pkg = NULL;
+       char path[PATH_MAX];
+       int r;
 
-       // Reset the path
-       r = pakfire_package_set_string(pkg, PAKFIRE_PKG_PATH, relpath);
+       // This only works on internal or local repositories
+       if (!pakfire_repo_is_internal(self) && !pakfire_repo_is_local(self))
+               return -ENOTSUP;
+
+       // Add the metadata
+       r = pakfire_archive_make_package(archive, self, &pkg);
        if (r < 0)
                goto ERROR;
 
-END:
+       // Fetch package path
+       const char* arc_path = pakfire_archive_get_path(archive);
+       const char* pkg_path = pakfire_package_get_path(pkg);
+
+       // Copy (or link) the archive
+       if (!pakfire_string_equals(arc_path, pkg_path)) {
+               r = pakfire_archive_link_or_copy(archive, path);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Return the package if requested
        if (package)
                *package = pakfire_package_ref(pkg);
index df2bcbc9a3bbb2fec22a6fb774175e81ee36e8af..3c703f63f7d852cbd7e4d2b30ae39d8fa3d4b0f7 100644 (file)
@@ -127,6 +127,11 @@ void pakfire_repo_has_changed(struct pakfire_repo* repo);
 int pakfire_repo_internalize(struct pakfire_repo* repo, int flags);
 Id pakfire_repo_add_solvable(struct pakfire_repo* repo);
 
+#define pakfire_repo_make_path(repo, path, archive, pkg) \
+       __pakfire_repo_make_path(repo, path, sizeof(path), archive, pkg)
+int __pakfire_repo_make_path(struct pakfire_repo* self,
+       char* path, size_t length, struct pakfire_archive* archive, struct pakfire_package* pkg);
+
 int pakfire_repo_import_archive(struct pakfire_repo* self,
        struct pakfire_archive* archive, struct pakfire_package** package);