Ensure that the hash_table messages show useful information.
[oweals/opkg-lede.git] / libopkg / opkg_conf.c
index ac785c8b805733bfb86e11065432c15508832ff1..4b2ad03bbb10498af65cdb2e367528d384bddebc 100644 (file)
 
 #include "xregex.h"
 #include "sprintf_alloc.h"
-#include "opkg_conf.h"
+#include "args.h"
 #include "opkg_message.h"
 #include "file_util.h"
 #include "str_util.h"
 #include "xsystem.h"
-#include <glob.h>
 #include "opkg_defines.h"
+#include "libbb/libbb.h"
 
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#include <errno.h>
+#include <glob.h>
+
+extern char *conf_file_dir;
 
 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
                                pkg_src_list_t *pkg_src_list,
@@ -46,11 +50,12 @@ static int set_and_load_pkg_src_list(opkg_conf_t *conf,
 static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
                                      nv_pair_list_t *nv_pair_list, char * lists_dir);
 
-int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
+void opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
 {
      opkg_option_t tmp[] = {
          { "cache", OPKG_OPT_TYPE_STRING, &conf->cache},
          { "force_defaults", OPKG_OPT_TYPE_BOOL, &conf->force_defaults },
+          { "force_maintainer", OPKG_OPT_TYPE_BOOL, &conf->force_maintainer }, 
          { "force_depends", OPKG_OPT_TYPE_BOOL, &conf->force_depends },
          { "force_overwrite", OPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
          { "force_downgrade", OPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
@@ -71,17 +76,29 @@ int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
          { "proxy_user", OPKG_OPT_TYPE_STRING, &conf->proxy_user },
          { "query-all", OPKG_OPT_TYPE_BOOL, &conf->query_all },
          { "verbosity", OPKG_OPT_TYPE_BOOL, &conf->verbosity },
+#if defined(HAVE_OPENSSL)
+         { "signature_ca_file", OPKG_OPT_TYPE_STRING, &conf->signature_ca_file },
+         { "signature_ca_path", OPKG_OPT_TYPE_STRING, &conf->signature_ca_path },
+#endif
+#if defined(HAVE_PATHFINDER)
+          { "check_x509_path", OPKG_OPT_TYPE_INT, &conf->check_x509_path }, 
+#endif
+#if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
+          { "ssl_engine", OPKG_OPT_TYPE_STRING, &conf->ssl_engine },
+          { "ssl_cert", OPKG_OPT_TYPE_STRING, &conf->ssl_cert },
+          { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &conf->ssl_cert_type },
+          { "ssl_key", OPKG_OPT_TYPE_STRING, &conf->ssl_key },
+          { "ssl_key_type", OPKG_OPT_TYPE_STRING, &conf->ssl_key_type },
+          { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &conf->ssl_key_passwd },
+          { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_file },
+          { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_path },
+          { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &conf->ssl_dont_verify_peer },
+#endif
          { NULL }
      };
 
-     *options = (opkg_option_t *)calloc(1, sizeof(tmp));
-     if ( options == NULL ){
-        fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
-        return -1;
-     }
-
+     *options = xcalloc(1, sizeof(tmp));
      memcpy(*options, tmp, sizeof(tmp));
-     return 0;
 };
 
 static void opkg_conf_override_string(char **conf_str, char *arg_str) 
@@ -90,7 +107,7 @@ static void opkg_conf_override_string(char **conf_str, char *arg_str)
          if (*conf_str) {
               free(*conf_str);
          }
-         *conf_str = strdup(arg_str);
+         *conf_str = xstrdup(arg_str);
      }
 }
 
@@ -105,15 +122,20 @@ static void opkg_conf_free_string(char **conf_str)
 int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
 {
      int err;
+     int errno_copy;
      char *tmp_dir_base;
      nv_pair_list_t tmp_dest_nv_pair_list;
      char *lists_dir = NULL, *lock_file = NULL;
      glob_t globbuf;
-     char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
+     char *etc_opkg_conf_pattern;
      char *pending_dir = NULL;
 
      memset(conf, 0, sizeof(opkg_conf_t));
 
+#if defined(HAVE_PATHFINDER)
+     conf->check_x509_path = 1;
+#endif
+
      pkg_src_list_init(&conf->pkg_src_list);
 
      nv_pair_list_init(&tmp_dest_nv_pair_list);
@@ -132,12 +154,19 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
 
      conf->lock_fd = creat (lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
      err = lockf (conf->lock_fd, F_TLOCK, 0);
+     errno_copy = errno;
 
      free (lock_file);
 
      if (err)
      {
-       opkg_message (conf, OPKG_ERROR, "Could not obtain administrative lock\n");
+       if(args->offline_root) {
+         opkg_message (conf, OPKG_ERROR, "Could not obtain administrative lock for offline root (ERR: %s)  at %s/%s/lock\n",
+                 strerror(errno_copy), args->offline_root, OPKG_STATE_DIR_PREFIX);
+       } else {
+         opkg_message (conf, OPKG_ERROR, "Could not obtain administrative lock (ERR: %s) at %s/lock\n",
+                 strerror(errno_copy), OPKG_STATE_DIR_PREFIX);
+       }
        return OPKG_CONF_ERR_LOCK;
      }
 
@@ -158,7 +187,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN);
      hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
      hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
-     lists_dir=(char *)malloc(1);
+     lists_dir=xmalloc(1);
      lists_dir[0]='\0';
      if (args->conf_file) {
          struct stat stat_buf;
@@ -172,7 +201,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      }
 
      if (strlen(lists_dir)<=1 ){
-        lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
+        lists_dir = xrealloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
         sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
      }
 
@@ -183,18 +212,19 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
             lists_dir = tmp;
      }
 
-     pending_dir = calloc(1, strlen(lists_dir)+strlen("/pending")+5);
+     pending_dir = xcalloc(1, strlen(lists_dir)+strlen("/pending")+5);
      snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
 
-     conf->lists_dir = strdup(lists_dir);
-     conf->pending_dir = strdup(pending_dir);
+     conf->lists_dir = xstrdup(lists_dir);
+     conf->pending_dir = xstrdup(pending_dir);
 
      if (args->offline_root) 
          sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", args->offline_root);
+     else
+         sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
      memset(&globbuf, 0, sizeof(globbuf));
      err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
-     if (args->offline_root)
-          free (etc_opkg_conf_pattern);
+     free (etc_opkg_conf_pattern);
      if (!err) {
          int i;
          for (i = 0; i < globbuf.gl_pathc; i++) {
@@ -238,12 +268,18 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      if (args->force_defaults) {
          conf->force_defaults = 1;
      }
+     if (args->force_maintainer) {
+          conf->force_maintainer = 1;
+     }
      if (args->force_overwrite) {
          conf->force_overwrite = 1;
      }
      if (args->force_downgrade) {
          conf->force_downgrade = 1;
      }
+     if (args->force_space) {
+         conf->force_space = 1;
+     }
      if (args->force_reinstall) {
          conf->force_reinstall = 1;
      }
@@ -334,12 +370,6 @@ void opkg_conf_deinit(opkg_conf_t *conf)
      pkg_src_list_deinit(&conf->pkg_src_list);
      pkg_dest_list_deinit(&conf->pkg_dest_list);
      nv_pair_list_deinit(&conf->arch_list);
-     if (&conf->pkg_hash)
-                   pkg_hash_deinit(&conf->pkg_hash);
-     if (&conf->file_hash)
-                   hash_table_deinit(&conf->file_hash);
-     if (&conf->obs_file_hash)
-                   hash_table_deinit(&conf->obs_file_hash);
 
      opkg_conf_free_string(&conf->offline_root);
      opkg_conf_free_string(&conf->offline_root_path);
@@ -348,6 +378,22 @@ void opkg_conf_deinit(opkg_conf_t *conf)
 
      opkg_conf_free_string(&conf->cache);
 
+#if defined(HAVE_OPENSSL)
+     opkg_conf_free_string(&conf->signature_ca_file);
+     opkg_conf_free_string(&conf->signature_ca_path);
+#endif
+
+#if defined(HAVE_SSLCURL)
+     opkg_conf_free_string(&conf->ssl_engine);
+     opkg_conf_free_string(&conf->ssl_cert);
+     opkg_conf_free_string(&conf->ssl_cert_type);
+     opkg_conf_free_string(&conf->ssl_key);
+     opkg_conf_free_string(&conf->ssl_key_type);
+     opkg_conf_free_string(&conf->ssl_key_passwd);
+     opkg_conf_free_string(&conf->ssl_ca_file);
+     opkg_conf_free_string(&conf->ssl_ca_path);
+#endif
+
      if (conf->verbosity > 1) { 
          int i;
          hash_table_t *hashes[] = {
@@ -373,9 +419,15 @@ void opkg_conf_deinit(opkg_conf_t *conf)
               }
               opkg_message(conf, OPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n", 
                            hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
-              hash_table_deinit(hash);
          }
      }
+
+     if (&conf->pkg_hash)
+                   pkg_hash_deinit(&conf->pkg_hash);
+     if (&conf->file_hash)
+                   hash_table_deinit(&conf->file_hash);
+     if (&conf->obs_file_hash)
+                   hash_table_deinit(&conf->obs_file_hash);
 }
 
 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
@@ -436,7 +488,7 @@ static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair
          if (conf->offline_root) {
               sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
          } else {
-              root_dir = strdup(nv_pair->value);
+              root_dir = xstrdup(nv_pair->value);
          }
          dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
          free(root_dir);
@@ -467,8 +519,7 @@ static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
 #define regmatch_size 12
      regmatch_t regmatch[regmatch_size];
 
-     if (opkg_init_options_array(conf, &options)<0)
-        return ENOMEM;
+     opkg_init_options_array(conf, &options);
 
      if (file == NULL) {
          fprintf(stderr, "%s: failed to open %s: %s\n",
@@ -517,29 +568,29 @@ static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
 
          /* This has to be so ugly to deal with optional quotation marks */
          if (regmatch[2].rm_so > 0) {
-              type = strndup(line + regmatch[2].rm_so,
+              type = xstrndup(line + regmatch[2].rm_so,
                              regmatch[2].rm_eo - regmatch[2].rm_so);
          } else {
-              type = strndup(line + regmatch[3].rm_so,
+              type = xstrndup(line + regmatch[3].rm_so,
                              regmatch[3].rm_eo - regmatch[3].rm_so);
          }
          if (regmatch[5].rm_so > 0) {
-              name = strndup(line + regmatch[5].rm_so,
+              name = xstrndup(line + regmatch[5].rm_so,
                              regmatch[5].rm_eo - regmatch[5].rm_so);
          } else {
-              name = strndup(line + regmatch[6].rm_so,
+              name = xstrndup(line + regmatch[6].rm_so,
                              regmatch[6].rm_eo - regmatch[6].rm_so);
          }
          if (regmatch[8].rm_so > 0) {
-              value = strndup(line + regmatch[8].rm_so,
+              value = xstrndup(line + regmatch[8].rm_so,
                               regmatch[8].rm_eo - regmatch[8].rm_so);
          } else {
-              value = strndup(line + regmatch[9].rm_so,
+              value = xstrndup(line + regmatch[9].rm_so,
                               regmatch[9].rm_eo - regmatch[9].rm_so);
          }
          extra = NULL;
          if (regmatch[11].rm_so > 0) {
-              extra = strndup (line + regmatch[11].rm_so,
+              extra = xstrndup (line + regmatch[11].rm_so,
                                regmatch[11].rm_eo - regmatch[11].rm_so);
          }
 
@@ -568,18 +619,13 @@ static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
          } else if (strcmp(type, "dest") == 0) {
               nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
          } else if (strcmp(type, "lists_dir") == 0) {
-              *lists_dir = realloc(*lists_dir,strlen(value)+1);
-               if (*lists_dir == NULL) {
-                   opkg_message(conf, OPKG_ERROR, "ERROR: Not enough memory\n");
-                   free(options);
-                   return EINVAL;
-               }
+              *lists_dir = xrealloc(*lists_dir,strlen(value)+1);
                sprintf (*lists_dir,"%s",value);
          } else if (strcmp(type, "arch") == 0) {
               opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value);
               if (!value) {
                    opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
-                   value = strdup("10");
+                   value = xstrdup("10");
               }
               nv_pair_list_append(&conf->arch_list, name, value);
          } else {
@@ -628,7 +674,7 @@ static int opkg_conf_set_option(const opkg_option_t *options,
                    }               
               case OPKG_OPT_TYPE_STRING:
                    if (value) {
-                        *((char **)options[i].value) = strdup(value);
+                        *((char **)options[i].value) = xstrdup(value);
                         return 0;
                    } else {
                         printf("%s: Option %s need an argument\n",
@@ -647,22 +693,21 @@ static int opkg_conf_set_option(const opkg_option_t *options,
 
 int opkg_conf_write_status_files(opkg_conf_t *conf)
 {
-     pkg_dest_list_elt_t *iter;
      pkg_dest_t *dest;
      pkg_vec_t *all;
      pkg_t *pkg;
-     register int i;
+     int i;
      int err;
+     FILE * status_file=NULL;
 
      if (conf->noaction)
          return 0;
-     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;
-         dest->status_file = fopen(dest->status_file_tmp_name, "w");
-         if (dest->status_file == NULL) {
-              fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
-                      __FUNCTION__, dest->status_file_name, strerror(errno));
-         }
+
+     dest = (pkg_dest_t *)void_list_first(&conf->pkg_dest_list)->data;
+     status_file = fopen(dest->status_file_tmp_name, "w");
+     if (status_file == NULL) {
+         fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
+                 __FUNCTION__, dest->status_file_tmp_name, strerror(errno));
      }
 
      all = pkg_vec_alloc();
@@ -677,38 +722,31 @@ int opkg_conf_write_status_files(opkg_conf_t *conf)
                  || pkg->state_want == SW_PURGE)) {
               continue;
          }
-         if (!pkg) {
-           fprintf(stderr, "Null package\n");
-         }
          if (pkg->dest == NULL) {
               fprintf(stderr, "%s: ERROR: Can't write status for "
                       "package %s since it has a NULL dest\n",
                       __FUNCTION__, pkg->name);
               continue;
          }
-         if (pkg->dest->status_file) {
-              pkg_print_status(pkg, pkg->dest->status_file);
+         if (status_file) {
+              pkg_print_status(pkg, status_file);
          }
      }
 
      pkg_vec_free(all);
 
-     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 (dest->status_file) {
-              err = ferror(dest->status_file);
-              fclose(dest->status_file);
-              dest->status_file = NULL;
-              if (!err) {
-                   file_move(dest->status_file_tmp_name, dest->status_file_name);
-              } else {
-                   fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
-                           "retaining old %s\n", __FUNCTION__, 
-                           dest->status_file_tmp_name, dest->status_file_name);
-              }
-         }
+     if (status_file) {
+         err = ferror(status_file);
+         fclose(status_file);
+         if (!err) {
+             file_move(dest->status_file_tmp_name, dest->status_file_name);
+         } else {
+             fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
+                     "retaining old %s\n", __FUNCTION__,
+                     dest->status_file_tmp_name, dest->status_file_name);
+         }
+         status_file = NULL;
      }
-
      return 0;
 }