From: Michael Tremer Date: Sun, 30 Mar 2025 16:28:07 +0000 (+0000) Subject: archive: Add function to fetch the desired filename X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=49382282058faa319928be4d11aa87e822e41403;p=pakfire.git archive: Add function to fetch the desired filename Signed-off-by: Michael Tremer --- diff --git a/src/pakfire/archive.c b/src/pakfire/archive.c index 40dc5ce8..588d83f8 100644 --- a/src/pakfire/archive.c +++ b/src/pakfire/archive.c @@ -1414,6 +1414,27 @@ const char* pakfire_archive_get_path(struct pakfire_archive* archive) { return archive->path; } +const char* pakfire_archive_get_filename(struct pakfire_archive* self) { + struct pakfire_package* pkg = NULL; + const char* filename = NULL; + + // Fetch the package + pkg = pakfire_archive_get_package(self); + if (!pkg) + goto ERROR; + + // Fetch the filename from the package + filename = pakfire_package_get_filename(pkg); + if (!filename) + goto ERROR; + +ERROR: + if (pkg) + pakfire_package_unref(pkg); + + return filename; +} + unsigned int pakfire_archive_get_format(struct pakfire_archive* archive) { return archive->format; } diff --git a/src/pakfire/archive.h b/src/pakfire/archive.h index ad62fe0a..30e578d7 100644 --- a/src/pakfire/archive.h +++ b/src/pakfire/archive.h @@ -49,6 +49,7 @@ int pakfire_archive_extract(struct pakfire_archive* archive, const char* path, const int flags, struct pakfire_progress* parent); const char* pakfire_archive_get_path(struct pakfire_archive* archive); +const char* pakfire_archive_get_filename(struct pakfire_archive* self); unsigned int pakfire_archive_get_format(struct pakfire_archive* archive); diff --git a/src/python/archive.c b/src/python/archive.c index 1947c013..9c60aa79 100644 --- a/src/python/archive.c +++ b/src/python/archive.c @@ -190,6 +190,14 @@ static PyObject* Archive_get_path(ArchiveObject* self) { return PyUnicode_FromString(path); } +static PyObject* Archive_get_filename(ArchiveObject* self) { + const char* filename = pakfire_archive_get_filename(self->archive); + if (!filename) + Py_RETURN_NONE; + + return PyUnicode_FromString(filename); +} + static PyObject* Archive_get_filelist(ArchiveObject* self) { struct pakfire_filelist* filelist = NULL; int r; @@ -253,6 +261,13 @@ static struct PyGetSetDef Archive_getsetters[] = { NULL, NULL }, + { + "filename", + (getter)Archive_get_filename, + NULL, + NULL, + NULL + }, { "format", (getter)Archive_get_format, diff --git a/tests/libpakfire/makefile.c b/tests/libpakfire/makefile.c index b34f59a3..5ea58749 100644 --- a/tests/libpakfire/makefile.c +++ b/tests/libpakfire/makefile.c @@ -199,6 +199,9 @@ static int test_dist_dummy(const struct test* t) { free(dump); } + // Check filename + ASSERT_STRING_EQUALS(pakfire_archive_get_filename(archive), "dummy-1.0-1.test1.src.pfm"); + // Check name const char* name = pakfire_package_get_string(package, PAKFIRE_PKG_NAME); ASSERT_STRING_EQUALS(name, "dummy"); diff --git a/tests/python/dist.py b/tests/python/dist.py index 410a5cde..6a4f3ffd 100755 --- a/tests/python/dist.py +++ b/tests/python/dist.py @@ -46,6 +46,9 @@ class DistTests(tests.TestCase): self.assertIsInstance(archive, pakfire.Archive) + # Check for the correct filename + self.assertEqual(archive.filename, "dummy-1.0-1.test1.src.pfm") + if __name__ == "__main__": tests.main()