From 9f1acdd6f6073203d38c15dabe98143decf3e3ce Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Fri, 25 Oct 2024 11:35:41 +0000 Subject: [PATCH] file: Simplify creating new file objects 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 --- src/libpakfire/db.c | 30 ++------------ src/libpakfire/file.c | 60 ++++++++++++--------------- src/libpakfire/filelist.c | 1 + src/libpakfire/include/pakfire/file.h | 3 +- src/libpakfire/package.c | 11 ++--- src/libpakfire/packager.c | 14 +++---- src/python/package.c | 5 +-- tests/libpakfire/file.c | 18 +++----- tests/libpakfire/package.c | 5 +-- 9 files changed, 50 insertions(+), 97 deletions(-) diff --git a/src/libpakfire/db.c b/src/libpakfire/db.c index cdfd0bddd..a68e53a76 100644 --- a/src/libpakfire/db.c +++ b/src/libpakfire/db.c @@ -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); diff --git a/src/libpakfire/file.c b/src/libpakfire/file.c index ac7b54a13..d09512e9b 100644 --- a/src/libpakfire/file.c +++ b/src/libpakfire/file.c @@ -42,6 +42,7 @@ #include #include #include +#include #include #include #include @@ -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) { diff --git a/src/libpakfire/filelist.c b/src/libpakfire/filelist.c index 5dd3994be..5663fd906 100644 --- a/src/libpakfire/filelist.c +++ b/src/libpakfire/filelist.c @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include diff --git a/src/libpakfire/include/pakfire/file.h b/src/libpakfire/include/pakfire/file.h index ad35d0930..ce3fef8c2 100644 --- a/src/libpakfire/include/pakfire/file.h +++ b/src/libpakfire/include/pakfire/file.h @@ -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); diff --git a/src/libpakfire/package.c b/src/libpakfire/package.c index 50e2cfb79..4702281a8 100644 --- a/src/libpakfire/package.c +++ b/src/libpakfire/package.c @@ -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: diff --git a/src/libpakfire/packager.c b/src/libpakfire/packager.c index fe60feb5f..4c3f5e890 100644 --- a/src/libpakfire/packager.c +++ b/src/libpakfire/packager.c @@ -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 diff --git a/src/python/package.c b/src/python/package.c index 37832abd8..36f8ad8b4 100644 --- a/src/python/package.c +++ b/src/python/package.c @@ -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); diff --git a/tests/libpakfire/file.c b/tests/libpakfire/file.c index 443aae9e6..87ea5c35e 100644 --- a/tests/libpakfire/file.c +++ b/tests/libpakfire/file.c @@ -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)); diff --git a/tests/libpakfire/package.c b/tests/libpakfire/package.c index 769281be7..0832fcea8 100644 --- a/tests/libpakfire/package.c +++ b/tests/libpakfire/package.c @@ -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)); -- 2.39.5