Sort packages before listing them.
[oweals/opkg-lede.git] / libopkg / opkg.c
index 046b5a096d34cdf1247839efa17bc31c2f335aee..2321092e9f484efadbc38d2fe1f346d53594fb91 100644 (file)
@@ -20,7 +20,6 @@
 
 #include "opkg.h"
 #include "opkg_conf.h"
-#include "args.h"
 
 #include "opkg_install.h"
 #include "opkg_configure.h"
@@ -33,8 +32,6 @@
 
 #include <libbb/libbb.h>
 
-args_t *args;
-
 #define opkg_assert(expr) if (!(expr)) { \
     printf ("opkg: file %s: line %d (%s): Assertation '%s' failed",\
             __FILE__, __LINE__, __PRETTY_FUNCTION__, # expr); abort (); }
@@ -117,18 +114,22 @@ curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
 int
 opkg_new()
 {
-       int err;
+       if (opkg_conf_init())
+               goto err0;
 
-       args = xcalloc(1, sizeof(args_t));
-       args_init(args);
+       if (pkg_hash_load_feeds())
+               goto err1;
 
-       err = opkg_conf_init(args);
-       if (err) {
-               free(args);
-               return -1;
-       }
+       if (pkg_hash_load_status_files())
+               goto err1;
 
        return 0;
+
+err1:
+       pkg_hash_deinit();
+err0:
+       opkg_conf_deinit();
+       return -1;
 }
 
 void
@@ -138,19 +139,25 @@ opkg_free(void)
        opkg_curl_cleanup();
 #endif
        opkg_conf_deinit();
-       args_deinit(args);
-       free(args);
 }
 
 int
 opkg_re_read_config_files(void)
 {
-       /* Unfortunately, the easiest way to re-read the config files right now is to
-        * throw away conf and start again */
-       opkg_free();
-       memset(conf, '\0', sizeof(opkg_conf_t));
-       return opkg_new();
+       pkg_hash_deinit();
+       pkg_hash_init();
+
+       if (pkg_hash_load_feeds())
+               goto err;
+
+       if (pkg_hash_load_status_files())
+               goto err;
+
        return 0;
+
+err:
+       pkg_hash_deinit();
+       return -1;
 }
 
 void
@@ -690,6 +697,20 @@ opkg_update_package_lists(opkg_progress_callback_t progress_callback,
        return result;
 }
 
+static int
+pkg_compare_names_and_version(const void *a0, const void *b0)
+{
+       const pkg_t *a = *(const pkg_t **)a0;
+       const pkg_t *b = *(const pkg_t **)b0;
+       int ret;
+
+       ret = strcmp(a->name, b->name);
+
+       if (ret == 0)
+               ret = pkg_compare_versions(a, b);
+
+       return ret;
+}
 
 int
 opkg_list_packages(opkg_package_callback_t callback, void *user_data)
@@ -701,6 +722,9 @@ opkg_list_packages(opkg_package_callback_t callback, void *user_data)
 
        all = pkg_vec_alloc();
        pkg_hash_fetch_available(all);
+
+       pkg_vec_sort(all, pkg_compare_names_and_version);
+
        for (i = 0; i < all->len; i++) {
                pkg_t *pkg;