]> git.ipfire.org Git - pakfire.git/commitdiff
file: Simplify creating new file objects
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 11:35:41 +0000 (11:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 25 Oct 2024 11:35:41 +0000 (11:35 +0000)
The create function will now take the path relative to the Pakfire
instance. It will also automatically compute the absolute path.

Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/db.c
src/libpakfire/file.c
src/libpakfire/filelist.c
src/libpakfire/include/pakfire/file.h
src/libpakfire/package.c
src/libpakfire/packager.c
src/python/package.c
tests/libpakfire/file.c
tests/libpakfire/package.c

index cdfd0bdddb2119e77902c30f75782b92beb44fa9..a68e53a76a990c9ec1c003ada69ab368be2ccb33 100644 (file)
@@ -2197,44 +2197,22 @@ static int pakfire_db_load_file_digest(struct pakfire_db* db, struct pakfire_fil
 static int pakfire_db_load_file(struct pakfire_db* db, struct pakfire_filelist* filelist,
                sqlite3_stmt* stmt) {
        struct pakfire_file* file = NULL;
-       char abspath[PATH_MAX];
        int r;
 
-       // Create a new file object
-       r = pakfire_file_create(&file, db->pakfire);
-       if (r)
-               goto ERROR;
-
        // Path
        const char* path = (const char*)sqlite3_column_text(stmt, 0);
 
        // Abort if no path is set
        if (!path) {
                CTX_ERROR(db->ctx, "File has no path\n");
-               r = 1;
-               goto ERROR;
-       }
-
-       // Set path
-       r = pakfire_file_set_path(file, path);
-       if (r) {
-               CTX_ERROR(db->ctx, "%s: Could not set path '%s': %m\n", path, path);
+               r = -errno;
                goto ERROR;
        }
 
-       // Make the absolute path
-       r = pakfire_path(db->pakfire, abspath, "%s", path);
-       if (r) {
-               CTX_ERROR(db->ctx, "%s: Could not make absolute path: %m\n", path);
-               goto ERROR;
-       }
-
-       // Set the absolute path
-       r = pakfire_file_set_abspath(file, abspath);
-       if (r) {
-               CTX_ERROR(db->ctx, "%s: Could not set absolute path %s: %m\n", path, abspath);
+       // Create a new file object
+       r = pakfire_file_create(&file, db->pakfire, path);
+       if (r < 0)
                goto ERROR;
-       }
 
        // Size
        size_t size = sqlite3_column_int64(stmt, 1);
index ac7b54a1362f917725dbcf7f349ddfca7719a976..d09512e9b90ab250f9f0bbb4ba41b71977fa5706 100644 (file)
@@ -42,6 +42,7 @@
 #include <pakfire/file.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/path.h>
 #include <pakfire/private.h>
 #include <pakfire/string.h>
 #include <pakfire/util.h>
@@ -370,7 +371,8 @@ ERROR:
        return r;
 }
 
-PAKFIRE_EXPORT int pakfire_file_create(struct pakfire_file** file, struct pakfire* pakfire) {
+PAKFIRE_EXPORT int pakfire_file_create(struct pakfire_file** file,
+               struct pakfire* pakfire, const char* path) {
        struct pakfire_file* f = NULL;
        int r = 0;
 
@@ -398,6 +400,13 @@ PAKFIRE_EXPORT int pakfire_file_create(struct pakfire_file** file, struct pakfir
                goto ERROR;
        }
 
+       // Store path
+       if (path) {
+               r = pakfire_file_set_path(f, path);
+               if (r < 0)
+                       goto ERROR;
+       }
+
        // Return the pointer
        *file = pakfire_file_ref(f);
 
@@ -416,9 +425,7 @@ int pakfire_file_read(struct pakfire_file* file, struct archive* reader, const c
                return -EINVAL;
 
        // Set abspath
-       r = pakfire_file_set_abspath(file, path);
-       if (r)
-               return r;
+       archive_entry_copy_sourcepath(file->entry, path);
 
        // Read everything
        r = archive_read_disk_entry_from_file(reader, file->entry, -1, NULL);
@@ -435,7 +442,7 @@ int pakfire_file_create_from_archive_entry(struct pakfire_file** file, struct pa
        struct pakfire_file* f = NULL;
        int r;
 
-       r = pakfire_file_create(&f, pakfire);
+       r = pakfire_file_create(&f, pakfire, NULL);
        if (r < 0)
                goto ERROR;
 
@@ -865,17 +872,6 @@ const char* pakfire_file_get_abspath(struct pakfire_file* file) {
        return archive_entry_sourcepath(file->entry);
 }
 
-int pakfire_file_set_abspath(struct pakfire_file* file, const char* path) {
-       // Check if path is set and absolute
-       if (!path || *path != '/')
-               return -EINVAL;
-
-       // Store the abspath
-       archive_entry_copy_sourcepath(file->entry, path);
-
-       return 0;
-}
-
 PAKFIRE_EXPORT const char* pakfire_file_get_path(struct pakfire_file* file) {
        return archive_entry_pathname(file->entry);
 }
@@ -892,27 +888,25 @@ PAKFIRE_EXPORT int pakfire_file_set_path(struct pakfire_file* file, const char*
        if (pakfire_string_startswith(path, "./"))
                path++;
 
-       switch (*path) {
-               // Just store the path if it is absolute
-               case '/':
-                       archive_entry_set_pathname(file->entry, path);
-                       break;
+       // Make the path absolute
+       r = pakfire_path_absolute(buffer, path);
+       if (r < 0)
+               return r;
 
-               // Handle relative paths
-               default:
-                       r = pakfire_string_format(buffer, "/%s", path);
-                       if (r)
-                               goto ERROR;
+       // Store the path
+       archive_entry_set_pathname(file->entry, buffer);
 
-                       archive_entry_set_pathname(file->entry, buffer);
-                       break;
-       }
+       // Make the absolute path
+       r = pakfire_path(file->pakfire, buffer, "%s", buffer);
+       if (r < 0)
+               return r;
 
-       return r;
+       // Store the abspath
+       archive_entry_copy_sourcepath(file->entry, buffer);
 
-ERROR:
-       CTX_ERROR(file->ctx, "Could not set path '%s': %m\n", path);
-       return r;
+       printf("PATH = %s, %s\n", archive_entry_pathname(file->entry), archive_entry_sourcepath(file->entry));
+
+       return 0;
 }
 
 PAKFIRE_EXPORT const char* pakfire_file_get_hardlink(struct pakfire_file* file) {
index 5dd3994be46d83036e276a0f8c5b8ec889fdb603..5663fd9065580353cc85fd70294a052ae02d17b5 100644 (file)
@@ -31,6 +31,7 @@
 #include <pakfire/i18n.h>
 #include <pakfire/logging.h>
 #include <pakfire/pakfire.h>
+#include <pakfire/path.h>
 #include <pakfire/private.h>
 #include <pakfire/progress.h>
 #include <pakfire/string.h>
index ad35d0930ec0949fe1517eeb7c3a8cb2e771dfb1..ce3fef8c24268b46dad71482d476d5873fb697c5 100644 (file)
@@ -34,7 +34,7 @@ enum pakfire_file_flags {
        PAKFIRE_FILE_CONFIG               = (1 << 0),
 };
 
-int pakfire_file_create(struct pakfire_file** file, struct pakfire* pakfire);
+int pakfire_file_create(struct pakfire_file** file, struct pakfire* pakfire, const char* path);
 
 struct pakfire_file* pakfire_file_ref(struct pakfire_file* file);
 struct pakfire_file* pakfire_file_unref(struct pakfire_file* file);
@@ -164,7 +164,6 @@ enum pakfire_file_dump_flags {
 char* pakfire_file_dump(struct pakfire_file* file, int flags);
 
 const char* pakfire_file_get_abspath(struct pakfire_file* file);
-int pakfire_file_set_abspath(struct pakfire_file* file, const char* path);
 
 FILE* pakfire_file_open(struct pakfire_file* file);
 
index 50e2cfb795e3cecfe3cad1765c2ca34c612ff967..4702281a83a153c9e32aa5bd980595e2d7db03c5 100644 (file)
@@ -1858,18 +1858,13 @@ static int __pakfire_package_fetch_filelist(void* data, Solvable* s, Repodata* r
        }
 
        // Create a new file entry
-       r = pakfire_file_create(&file, search->pakfire);
-       if (r)
-               goto ERROR;
-
-       // Set path
-       r = pakfire_file_set_path(file, path);
-       if (r)
+       r = pakfire_file_create(&file, search->pakfire, path);
+       if (r < 0)
                goto ERROR;
 
        // Append the file to the filelist
        r = pakfire_filelist_add(search->filelist, file);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
 ERROR:
index fe60feb5fe66ec2f87d9d2c644f31a70dd71ee39..4c3f5e8906f0cb86d784ae1f38ec789e0675a20d 100644 (file)
@@ -622,22 +622,22 @@ int pakfire_packager_add(struct pakfire_packager* packager,
        struct pakfire_file* file = NULL;
        int r;
 
+       if (!path)
+               path = sourcepath;
+
        // Create a new file object
-       r = pakfire_file_create(&file, packager->pakfire);
-       if (r)
+       r = pakfire_file_create(&file, packager->pakfire, path);
+       if (r < 0)
                goto ERROR;
 
        // Read the meta information
        r = pakfire_file_read(file, packager->reader, sourcepath);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
-       if (!path)
-               path = sourcepath;
-
        // Assign a new path for inside the archive
        r = pakfire_file_set_path(file, path);
-       if (r)
+       if (r < 0)
                goto ERROR;
 
        // Call the main function
index 37832abd8561cbf87d43eb046d86c633c4fc8d57..36f8ad8b4f7cc2882ff464a06cac25d18ee3ba3d 100644 (file)
@@ -650,7 +650,7 @@ static int Package_set_filelist(PackageObject* self, PyObject* value) {
                // Create a new file
                struct pakfire_file* file;
 
-               r = pakfire_file_create(&file, pakfire);
+               r = pakfire_file_create(&file, pakfire, path);
                if (r) {
                        errno = -r;
                        PyErr_SetFromErrno(PyExc_OSError);
@@ -659,9 +659,6 @@ static int Package_set_filelist(PackageObject* self, PyObject* value) {
                        return -1;
                }
 
-               // Set the path
-               pakfire_file_set_path(file, path);
-
                // Append the file to the filelist
                pakfire_filelist_add(filelist, file);
                pakfire_file_unref(file);
index 443aae9e6cbd3490fbf492f51b1504f6c0f66129..87ea5c35e178a81ada0e8e77927daebc465a7b99 100644 (file)
@@ -27,7 +27,7 @@ static int test_create(const struct test* t) {
        struct pakfire_file* file = NULL;
 
        // Create a new file
-       ASSERT_SUCCESS(pakfire_file_create(&file, t->pakfire));
+       ASSERT_SUCCESS(pakfire_file_create(&file, t->pakfire, NULL));
 
        // Set path & check
        ASSERT_SUCCESS(pakfire_file_set_path(file, "/abc"));
@@ -46,14 +46,11 @@ static int test_create_invalid(const struct test* t) {
        struct pakfire_file* file = NULL;
 
        // Create a new file
-       ASSERT_SUCCESS(pakfire_file_create(&file, t->pakfire));
+       ASSERT_SUCCESS(pakfire_file_create(&file, t->pakfire, NULL));
 
        // Set path
        ASSERT(pakfire_file_set_path(file, NULL) == -EINVAL);
 
-       // It should not be possible to set relative absolute paths
-       ASSERT(pakfire_file_set_abspath(file, "abc/abc") == -EINVAL);
-
        // Destroy it
        ASSERT_NULL(pakfire_file_unref(file));
 
@@ -74,14 +71,9 @@ static int test_create_filelist(const struct test* t) {
        ASSERT_SUCCESS(pakfire_filelist_create(&list, t->pakfire));
 
        // Create some files
-       ASSERT_SUCCESS(pakfire_file_create(&file1, t->pakfire));
-       ASSERT_SUCCESS(pakfire_file_create(&file2, t->pakfire));
-       ASSERT_SUCCESS(pakfire_file_create(&file3, t->pakfire));
-
-       // Set some paths
-       ASSERT_SUCCESS(pakfire_file_set_path(file1, "/1"));
-       ASSERT_SUCCESS(pakfire_file_set_path(file2, "/2"));
-       ASSERT_SUCCESS(pakfire_file_set_path(file3, "/3"));
+       ASSERT_SUCCESS(pakfire_file_create(&file1, t->pakfire, "/1"));
+       ASSERT_SUCCESS(pakfire_file_create(&file2, t->pakfire, "/2"));
+       ASSERT_SUCCESS(pakfire_file_create(&file3, t->pakfire, "/3"));
 
        // Add the files to the list
        ASSERT_SUCCESS(pakfire_filelist_add(list, file1));
index 769281be797e1ddafbac6b32396ef2654cbef633..0832fcea8aed85a2be197591a6f69afe648250c2 100644 (file)
@@ -257,10 +257,7 @@ static int test_filelist(const struct test* t) {
        ASSERT_SUCCESS(pakfire_filelist_create(&filelist, t->pakfire));
 
        // Create a file
-       ASSERT_SUCCESS(pakfire_file_create(&file, t->pakfire));
-
-       // Store some path
-       ASSERT_SUCCESS(pakfire_file_set_path(file, "/bin/bash"));
+       ASSERT_SUCCESS(pakfire_file_create(&file, t->pakfire, "/bin/bash"));
 
        // Append the file to the filelist
        ASSERT_SUCCESS(pakfire_filelist_add(filelist, file));