Move loading of feeds and status files out of opkg_conf_init().
authorgraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Fri, 11 Dec 2009 03:04:35 +0000 (03:04 +0000)
committergraham.gower <graham.gower@e8e0d7a0-c8d9-11dd-a880-a1081c7ac358>
Fri, 11 Dec 2009 03:04:35 +0000 (03:04 +0000)
git-svn-id: http://opkg.googlecode.com/svn/trunk@483 e8e0d7a0-c8d9-11dd-a880-a1081c7ac358

libopkg/args.h
libopkg/libopkg.c
libopkg/opkg.c
libopkg/opkg_cmd.c
libopkg/opkg_conf.c
libopkg/pkg_hash.c
libopkg/pkg_hash.h

index 176d354d2cb94efd7afdb2725ac2481d3316d58f..82bae56fda03e73b11593e6e9f380814d1339f0e 100644 (file)
@@ -22,9 +22,6 @@ struct args
 {
     char *conf_file;
     char *dest;
-
-    int nocheckfordirorfile;
-    int noreadfeedsfile;
 };
 typedef struct args args_t;
 
index f6fc251426e1a0d40c6f5d44c6d1ea4d7361e37e..f6f6fe945cd9875155852dc1a58fd4694435bee9 100644 (file)
@@ -33,6 +33,8 @@ opkg_op (int argc, char *argv[])
        args_t args;
        char *cmd_name;
        opkg_cmd_t *cmd;
+       int nocheckfordirorfile = 0;
+        int noreadfeedsfile = 0;
 
        args_init (&args);
 
@@ -48,7 +50,7 @@ opkg_op (int argc, char *argv[])
              !strcmp(cmd_name,"print_architecture") ||
              !strcmp(cmd_name,"print-installation-architecture") ||
              !strcmp(cmd_name,"print_installation_architecture") )
-           args.nocheckfordirorfile = 1;
+           nocheckfordirorfile = 1;
 
         if ( !strcmp(cmd_name,"flag") ||
              !strcmp(cmd_name,"configure") ||
@@ -60,7 +62,7 @@ opkg_op (int argc, char *argv[])
              !strcmp(cmd_name,"list_installed") ||
              !strcmp(cmd_name,"list-installed") ||
              !strcmp(cmd_name,"status") )
-           args.noreadfeedsfile = 1;
+           noreadfeedsfile = 1;
 
        cmd = opkg_cmd_find (cmd_name);
        if (cmd == NULL)
@@ -75,10 +77,16 @@ opkg_op (int argc, char *argv[])
        err = opkg_conf_init (&args);
        args_deinit (&args);
        if (err)
-       {
-               print_error_list();
-               free_error_list();
-               return err;
+               goto err0;
+
+       if (!nocheckfordirorfile) {
+               if (!noreadfeedsfile) {
+                       if ((err = pkg_hash_load_feeds()))
+                               goto err1;
+               }
+   
+               if ((err = pkg_hash_load_status_files()))
+                       goto err1;
        }
 
        if (cmd->requires_args && opts == argc)
@@ -91,10 +99,16 @@ opkg_op (int argc, char *argv[])
 
        err = opkg_cmd_exec (cmd, argc - opts, (const char **) (argv + opts));
 
+
 #ifdef HAVE_CURL
        opkg_curl_cleanup();
 #endif
+err1:
        opkg_conf_deinit ();
 
+err0:
+       print_error_list();
+       free_error_list();
+
        return err;
 }
index 046b5a096d34cdf1247839efa17bc31c2f335aee..383dfb5893d9b31a4d8dd33b6274946584fc896a 100644 (file)
@@ -33,8 +33,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 +115,28 @@ curl_progress_cb(struct _curl_cb_data *cb_data, double t, /* dltotal */
 int
 opkg_new()
 {
-       int err;
+       args_t args;
 
-       args = xcalloc(1, sizeof(args_t));
-       args_init(args);
+       args_init(&args);
 
-       err = opkg_conf_init(args);
-       if (err) {
-               free(args);
-               return -1;
-       }
+       if (opkg_conf_init(&args))
+               goto err0;
+
+       args_deinit(&args);
+
+       if (pkg_hash_load_feeds())
+               goto err1;
+
+       if (pkg_hash_load_status_files())
+               goto err1;
 
        return 0;
+
+err1:
+       pkg_hash_deinit();
+err0:
+       opkg_conf_deinit();
+       return -1;
 }
 
 void
@@ -138,19 +146,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
index 30d312637ceaa6d25f29a35f0ceb636f95c9ecb5..439a8606ed29e7352d5aafbc6e509a74d5adc725 100644 (file)
@@ -1213,12 +1213,5 @@ opkg_cmd_find(const char *name)
 int
 opkg_cmd_exec(opkg_cmd_t *cmd, int argc, const char **argv)
 {
-       int result;
-
-       result = (cmd->fun)(argc, argv);
-
-       print_error_list();
-       free_error_list();
-
-       return result;
+       return (cmd->fun)(argc, argv);
 }
index 47b54380177c458dbd728fda437927a4b1cdf4c3..1afa612e924676a14ff711f16be3c799f57c8754 100644 (file)
@@ -84,56 +84,7 @@ opkg_option_t options[] = {
 };
 
 static int
-opkg_conf_set_default_dest(const char *default_dest_name)
-{
-     pkg_dest_list_elt_t *iter;
-     pkg_dest_t *dest;
-
-     for (iter = void_list_first(&conf->pkg_dest_list); iter; iter = void_list_next(&conf->pkg_dest_list, iter)) {
-         dest = (pkg_dest_t *)iter->data;
-         if (strcmp(dest->name, default_dest_name) == 0) {
-              conf->default_dest = dest;
-              conf->restrict_to_default_dest = 1;
-              return 0;
-         }
-     }
-
-     opkg_msg(ERROR, "Unknown dest name: `%s'.\n", default_dest_name);
-
-     return 1;
-}
-
-static int
-set_and_load_pkg_src_list(pkg_src_list_t *pkg_src_list)
-{
-     pkg_src_list_elt_t *iter;
-     pkg_src_t *src;
-     char *list_file;
-
-     for (iter = void_list_first(pkg_src_list); iter; iter = void_list_next(pkg_src_list, iter)) {
-          src = (pkg_src_t *)iter->data;
-         if (src == NULL) {
-              continue;
-         }
-
-         sprintf_alloc(&list_file, "%s/%s", 
-                         conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 
-                         src->name);
-
-         if (file_exists(list_file)) {
-              if (pkg_hash_add_from_file(list_file, src, NULL, 0)) {
-                   free(list_file);
-                   return -1;
-              }
-         }
-         free(list_file);
-     }
-
-     return 0;
-}
-
-static int
-set_and_load_pkg_dest_list(nv_pair_list_t *nv_pair_list)
+resolve_pkg_dest_list(nv_pair_list_t *nv_pair_list, const char *default_dest_name)
 {
      nv_pair_list_elt_t *iter;
      nv_pair_t *nv_pair;
@@ -148,19 +99,22 @@ set_and_load_pkg_dest_list(nv_pair_list_t *nv_pair_list)
          } else {
               root_dir = xstrdup(nv_pair->value);
          }
+
          dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, conf->lists_dir);
          free(root_dir);
-         if (dest == NULL) {
-              continue;
-         }
-         if (conf->default_dest == NULL) {
+
+         if (conf->default_dest == NULL)
               conf->default_dest = dest;
+
+         if (default_dest_name && !strcmp(dest->name, default_dest_name)) {
+              conf->default_dest = dest;
+              conf->restrict_to_default_dest = 1;
          }
-         if (file_exists(dest->status_file_name)) {
-              if (pkg_hash_add_from_file(dest->status_file_name,
-                                     NULL, dest, 1))
-                      return -1;
-         }
+     }
+
+     if (default_dest_name && !conf->restrict_to_default_dest) {
+         opkg_msg(ERROR, "Unknown dest name: `%s'.\n", default_dest_name);
+         return -1;
      }
 
      return 0;
@@ -548,33 +502,12 @@ opkg_conf_init(const args_t *args)
                              OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
      }
 
-     if (!(args->nocheckfordirorfile)) {
-
-        if (!(args->noreadfeedsfile)) {
-           if (set_and_load_pkg_src_list(&conf->pkg_src_list)) {
-               nv_pair_list_deinit(&tmp_dest_nv_pair_list);
-              return -1;
-          }
-       }
-   
-        /* Now that we have resolved conf->offline_root, we can commit to
-          the directory names for the dests and load in all the package
-          lists. */
-        if (set_and_load_pkg_dest_list(&tmp_dest_nv_pair_list)) {
-               nv_pair_list_deinit(&tmp_dest_nv_pair_list);
-              return -1;
-       }
-   
-        if (args->dest) {
-            err = opkg_conf_set_default_dest(args->dest);
-            if (err) {
-                  nv_pair_list_deinit(&tmp_dest_nv_pair_list);
-                 return -1;
-            }
-        }
-     }
+     err = resolve_pkg_dest_list(&tmp_dest_nv_pair_list, args->dest);
      nv_pair_list_deinit(&tmp_dest_nv_pair_list);
 
+     if (err)
+       return -1;
+
      return 0;
 }
 
index 5b4fe4ba0376a66d53be6d69a92f58dfed106d9c..f5d478221c96d3bbdabded40103c976b41a79c4a 100644 (file)
@@ -28,6 +28,8 @@
 #include "pkg_hash.h"
 #include "pkg_parse.h"
 #include "opkg_utils.h"
+#include "sprintf_alloc.h"
+#include "file_util.h"
 #include "libbb/libbb.h"
 
 void
@@ -126,6 +128,65 @@ pkg_hash_add_from_file(const char *file_name,
        return ret;
 }
 
+/*
+ * Load in feed files from the cached "src" and/or "src/gz" locations.
+ */
+int
+pkg_hash_load_feeds(void)
+{
+       pkg_src_list_elt_t *iter;
+       pkg_src_t *src;
+       char *list_file, *lists_dir;
+
+       opkg_msg(INFO, "\n");
+
+       lists_dir = conf->restrict_to_default_dest ?
+               conf->default_dest->lists_dir : conf->lists_dir;
+
+       for (iter = void_list_first(&conf->pkg_src_list); iter;
+                       iter = void_list_next(&conf->pkg_src_list, iter)) {
+
+               src = (pkg_src_t *)iter->data;
+
+               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)) {
+                               free(list_file);
+                               return -1;
+                       }
+               }
+               free(list_file);
+       }
+
+       return 0;
+}
+
+/*
+ * Load in status files from the configured "dest"s.
+ */
+int
+pkg_hash_load_status_files(void)
+{
+       pkg_dest_list_elt_t *iter;
+       pkg_dest_t *dest;
+
+       opkg_msg(INFO, "\n");
+
+       for (iter = void_list_first(&conf->pkg_dest_list); iter;
+                       iter = void_list_next(&conf->pkg_dest_list, iter)) {
+       
+               dest = (pkg_dest_t *)iter->data;
+
+               if (file_exists(dest->status_file_name)) {
+                       if (pkg_hash_add_from_file(dest->status_file_name, NULL, dest, 1))
+                               return -1;
+               }
+       }
+
+       return 0;
+}
+
 static abstract_pkg_t *
 abstract_pkg_fetch_by_name(const char * pkg_name)
 {
index 67134d20f1eaef480e865d8006450324419ea663..0ff18aa5e355eb1ece2b596895148643fda6dad0 100644 (file)
@@ -32,6 +32,9 @@ void pkg_hash_fetch_available(pkg_vec_t *available);
 
 int pkg_hash_add_from_file(const char *file_name, pkg_src_t *src,
                pkg_dest_t *dest, int is_status_file);
+int pkg_hash_load_feeds(void);
+int pkg_hash_load_status_files(void);
+
 void hash_insert_pkg(pkg_t *pkg, int set_status);
 
 abstract_pkg_t * ensure_abstract_pkg_by_name(const char * pkg_name);