libopkg: support passing callbacks to feed parsing functions
authorJo-Philipp Wich <jo@mein.io>
Sun, 3 May 2020 19:39:56 +0000 (21:39 +0200)
committerJo-Philipp Wich <jo@mein.io>
Thu, 7 May 2020 20:24:19 +0000 (22:24 +0200)
Extend pkg_hash_add_from_file(), pkg_hash_load_feeds() and
pkg_hash_load_status_files() to accept a per-package callback
function andan associated userdata pointer.

Signed-off-by: Jo-Philipp Wich <jo@mein.io>
libopkg/opkg.c
libopkg/opkg_cmd.c
libopkg/pkg_hash.c
libopkg/pkg_hash.h
src/opkg-cl.c
tests/opkg_hash_test.c

index aba6364c8fa1906fb5b25be87052d5453d295760..3583488780221b6fc566aa3b39ce378950a0503b 100644 (file)
@@ -87,10 +87,10 @@ int opkg_new()
        if (opkg_conf_load())
                goto err0;
 
-       if (pkg_hash_load_feeds(0))
+       if (pkg_hash_load_feeds(0, NULL, NULL))
                goto err1;
 
-       if (pkg_hash_load_status_files())
+       if (pkg_hash_load_status_files(NULL, NULL))
                goto err1;
 
        return 0;
index c823df8b6006bffa2516443fab3718cd112ae3b3..8de22f3ab06819b12434265e159a3fa1e08530e8 100644 (file)
@@ -458,7 +458,7 @@ static int opkg_install_cmd(int argc, char **argv)
        }
 
        pkg_hash_load_package_details();
-       pkg_hash_load_status_files();
+       pkg_hash_load_status_files(NULL, NULL);
 
        if (conf->force_reinstall) {
                int saved_force_depends = conf->force_depends;
index 611f3b901f5e6bd03a9cef4e7ac376904d1dfcb2..52c64ff137ba49f38d7bc31d0601f3037ad8a10d 100644 (file)
@@ -79,7 +79,7 @@ int dist_hash_add_from_file(const char *lists_dir, pkg_src_t * dist)
                sprintf_alloc(&list_file, "%s/%s", lists_dir, subname);
 
                if (file_exists(list_file)) {
-                       if (pkg_hash_add_from_file(list_file, dist, NULL, 0, 0)) {
+                       if (pkg_hash_add_from_file(list_file, dist, NULL, 0, 0, NULL, NULL)) {
                                free(list_file);
                                return -1;
                        }
@@ -95,7 +95,8 @@ int dist_hash_add_from_file(const char *lists_dir, pkg_src_t * dist)
 
 int
 pkg_hash_add_from_file(const char *file_name,
-                      pkg_src_t * src, pkg_dest_t * dest, int is_status_file, int state_flags)
+                      pkg_src_t * src, pkg_dest_t * dest, int is_status_file, int state_flags,
+                      void (*cb)(pkg_t *, void *), void *priv)
 {
        pkg_t *pkg;
        FILE *fp;
@@ -158,7 +159,10 @@ pkg_hash_add_from_file(const char *file_name,
                        continue;
                }
 
-               hash_insert_pkg(pkg, is_status_file);
+               if (cb)
+                       cb(pkg, priv);
+               else
+                       hash_insert_pkg(pkg, is_status_file);
 
        } while (!feof(fp));
 
@@ -174,7 +178,7 @@ pkg_hash_add_from_file(const char *file_name,
 /*
  * Load in feed files from the cached "src" and/or "src/gz" locations.
  */
-int pkg_hash_load_feeds(int state_flags)
+int pkg_hash_load_feeds(int state_flags, void (*cb)(pkg_t *, void *), void *priv)
 {
        pkg_src_list_elt_t *iter;
        pkg_src_t *src;
@@ -193,7 +197,7 @@ int pkg_hash_load_feeds(int state_flags)
                sprintf_alloc(&list_file, "%s/%s", lists_dir, src->name);
 
                if (file_exists(list_file)) {
-                       if (pkg_hash_add_from_file(list_file, src, NULL, 0, state_flags)) {
+                       if (pkg_hash_add_from_file(list_file, src, NULL, 0, state_flags, cb, priv)) {
                                free(list_file);
                                return -1;
                        }
@@ -207,7 +211,7 @@ int pkg_hash_load_feeds(int state_flags)
 /*
  * Load in status files from the configured "dest"s.
  */
-int pkg_hash_load_status_files(void)
+int pkg_hash_load_status_files(void (*cb)(pkg_t *, void *), void *priv)
 {
        pkg_dest_list_elt_t *iter;
        pkg_dest_t *dest;
@@ -221,7 +225,7 @@ int pkg_hash_load_status_files(void)
 
                if (file_exists(dest->status_file_name)) {
                        if (pkg_hash_add_from_file
-                           (dest->status_file_name, NULL, dest, 1, SF_NEED_DETAIL))
+                           (dest->status_file_name, NULL, dest, 1, SF_NEED_DETAIL, cb, priv))
                                return -1;
                }
        }
@@ -255,7 +259,7 @@ int pkg_hash_load_package_details(void)
        int n_need_detail;
 
        while (1) {
-               pkg_hash_load_feeds(0);
+               pkg_hash_load_feeds(0, NULL, NULL);
 
                n_need_detail = 0;
                hash_table_foreach(&conf->pkg_hash, pkg_hash_load_package_details_helper, &n_need_detail);
index e849160b68929513e37a8756b3688195cd1d8f27..e5ab2e0974fe1ac59576eecab0f3bb9dfcaa29af 100644 (file)
@@ -30,9 +30,10 @@ void pkg_hash_fetch_available(pkg_vec_t * available);
 
 int dist_hash_add_from_file(const char *file_name, pkg_src_t * dist);
 int pkg_hash_add_from_file(const char *file_name, pkg_src_t * src,
-                          pkg_dest_t * dest, int is_status_file, int state_flags);
-int pkg_hash_load_feeds(int state_flags);
-int pkg_hash_load_status_files(void);
+                          pkg_dest_t * dest, int is_status_file, int state_flags,
+                          void (*cb)(pkg_t *, void *), void *priv);
+int pkg_hash_load_feeds(int state_flags, void (*cb)(pkg_t *, void *), void *priv);
+int pkg_hash_load_status_files(void (*cb)(pkg_t *, void *), void *priv);
 int pkg_hash_load_package_details(void);
 
 void hash_insert_pkg(pkg_t * pkg, int set_status);
index 351574b175bb049b8c445ed0306a6c23f027d408..21318833cd4f0184d1b3e8832232e6b44cdb65e7 100644 (file)
@@ -431,11 +431,11 @@ int main(int argc, char *argv[])
 
        if (!nocheckfordirorfile) {
                if (!noreadfeedsfile) {
-                       if (pkg_hash_load_feeds(SF_NEED_DETAIL))
+                       if (pkg_hash_load_feeds(SF_NEED_DETAIL, NULL, NULL))
                                goto err1;
                }
 
-               if (pkg_hash_load_status_files())
+               if (pkg_hash_load_status_files(NULL, NULL))
                        goto err1;
        }
 
index 6c67e594dbb68bb8766c77cf0ad5668341a68a7e..df55bad4b2cfe5aa094a62a793d2ab06c613f500 100644 (file)
@@ -35,8 +35,8 @@ int main(int argc, char *argv[])
        }
        pkg_hash_init("test", hash, 1024);
 
-       pkg_hash_add_from_file(&conf, argv[1], NULL, NULL, 0);
-       pkg_hash_add_from_file(&conf, argv[2], NULL, NULL, 0);
+       pkg_hash_add_from_file(&conf, argv[1], NULL, NULL, 0, NULL, NULL);
+       pkg_hash_add_from_file(&conf, argv[2], NULL, NULL, 0, NULL, NULL);
 
        if (argc < 4) {
                pkg_print_info(pkg_hash_fetch_by_name_version