]> git.ipfire.org Git - pakfire.git/commitdiff
archive: Expose filelist as property
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Jul 2022 12:33:25 +0000 (12:33 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 15 Jul 2022 12:33:25 +0000 (12:33 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/_pakfire/archive.c

index 5d96188426c767804065e8f026e09a414e94d257..b11cb403d6494b4655f73aa7eb2cb365c1bf03d8 100644 (file)
@@ -207,6 +207,56 @@ static PyObject* Archive_get_path(ArchiveObject* self) {
        return PyUnicode_FromString(path);
 }
 
+static PyObject* Archive_get_filelist(ArchiveObject* self) {
+       int r;
+
+       // Fetch the filelist
+       struct pakfire_filelist* filelist = pakfire_archive_get_filelist(self->archive);
+       if (!filelist) {
+               PyErr_SetFromErrno(PyExc_OSError);
+               return NULL;
+       }
+
+       const size_t size = pakfire_filelist_size(filelist);
+
+       // Create a new Python list
+       PyObject* list = PyList_New(0);
+       if (!list)
+               goto ERROR;
+
+       for (unsigned int i = 0; i < size; i++) {
+               struct pakfire_file* file = pakfire_filelist_get(filelist, i);
+               if (!file)
+                       goto ERROR;
+
+               // Fetch the path
+               const char* path = pakfire_file_get_path(file);
+
+               // Convert path to string
+               PyObject* obj = PyUnicode_FromString(path);
+
+               // Append the new object to the list
+               r = PyList_Append(list, obj);
+               Py_DECREF(obj);
+
+               // If we could not append to the list, we will break
+               if (r) {
+                       Py_DECREF(list);
+                       list = NULL;
+                       goto ERROR;
+               }
+
+               // Free file
+               pakfire_file_unref(file);
+       }
+
+ERROR:
+       if (filelist)
+               pakfire_filelist_unref(filelist);
+
+       return list;
+}
+
 static struct PyMethodDef Archive_methods[] = {
        {
                "extract",
@@ -248,6 +298,13 @@ static struct PyMethodDef Archive_methods[] = {
 };
 
 static struct PyGetSetDef Archive_getsetters[] = {
+       {
+               "filelist",
+               (getter)Archive_get_filelist,
+               NULL,
+               NULL,
+               NULL
+       },
        {
                "format",
                (getter)Archive_get_format,