]> git.ipfire.org Git - pakfire.git/commitdiff
archive: Fetch filelist from JSON for new packages
authorMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Oct 2022 14:09:04 +0000 (14:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Tue, 25 Oct 2022 14:09:04 +0000 (14:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/archive.c

index 4d8ea0fc5aa5be6244dc8e5821e22ce857bd4652..9841e0f374948328d317d88eaa4b6e5dbc0d0bd9 100644 (file)
@@ -973,8 +973,80 @@ int pakfire_archive_check_digest(struct pakfire_archive* archive,
        return r;
 }
 
+static int pakfire_archive_make_filelist_from_json(struct pakfire_archive* archive,
+               struct pakfire_filelist** filelist) {
+       struct json_object* array = NULL;
+       int r;
+
+       struct pakfire_filelist* list = NULL;
+       struct pakfire_file* file = NULL;
+
+       // Fetch the array with the filelist
+       array = pakfire_archive_metadata_get_object(archive, "filelist", NULL);
+       if (!array) {
+               ERROR(archive->pakfire, "Archive has no filelist: %m\n");
+               return 1;
+       }
+
+       // Determine the length of the array
+       const size_t length = json_object_array_length(array);
+
+       // End here if the array is empty
+       if (!length)
+               return 0;
+
+       // Create a new filelist object
+       r = pakfire_filelist_create(&list, archive->pakfire);
+       if (r)
+               goto ERROR;
+
+       // Walk through all items in this array
+       for (unsigned int i = 0; i < length; i++) {
+               struct json_object* item = json_object_array_get_idx(array, i);
+               if (!item)
+                       continue;
+
+               // Extract the path value
+               const char* path = json_object_get_string(item);
+               if (!path)
+                       continue;
+
+               // Create a new file object
+               r = pakfire_file_create(&file, archive->pakfire);
+               if (r)
+                       goto ERROR;
+
+               // Set path
+               r = pakfire_file_set_path(file, path);
+               if (r) {
+                       pakfire_file_unref(file);
+                       goto ERROR;
+               }
+
+               // Append the file to the filelist
+               r = pakfire_filelist_append(list, file);
+               if (r) {
+                       pakfire_file_unref(file);
+                       goto ERROR;
+               }
+
+               // Cleanup
+               pakfire_file_unref(file);
+       }
+
+       // Reference the filelist
+       *filelist = pakfire_filelist_ref(list);
+
+ERROR:
+       if (list)
+               pakfire_filelist_unref(list);
+
+       return r;
+}
+
 static int pakfire_archive_make_package_from_json(struct pakfire_archive* archive,
                struct pakfire_repo* repo, struct pakfire_package** package) {
+       struct pakfire_filelist* filelist = NULL;
        int r;
 
        // Calculate digest
@@ -1143,11 +1215,26 @@ static int pakfire_archive_make_package_from_json(struct pakfire_archive* archiv
                }
        }
 
+       // Fetch the filelist
+       r = pakfire_archive_make_filelist_from_json(archive, &filelist);
+       if (r)
+               goto ERROR;
+
+       // Store the filelist
+       if (filelist) {
+               r = pakfire_package_set_filelist(pkg, filelist);
+               if (r)
+                       goto ERROR;
+       }
+
        // Success!
        *package = pkg;
-       return 0;
+       r = 0;
 
 ERROR:
+       if (filelist)
+               pakfire_filelist_unref(filelist);
+
        return r;
 }