opkg: adding cache support
[oweals/opkg-lede.git] / libopkg / opkg_conf.c
index 75ca48b1f81bf1b0cb27b6668fa501731fd89c60..b61fa3eb7af33bd1ceaac0b1ba057e82a1403247 100644 (file)
@@ -17,6 +17,7 @@
 
 #include "includes.h"
 #include "opkg_conf.h"
+#include "opkg_error.h"
 
 #include "xregex.h"
 #include "sprintf_alloc.h"
@@ -28,6 +29,9 @@
 #include <glob.h>
 #include "opkg_defines.h"
 
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 
 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
                                pkg_src_list_t *pkg_src_list,
@@ -45,6 +49,7 @@ static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
 int 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_depends", OPKG_OPT_TYPE_BOOL, &conf->force_depends },
          { "force_overwrite", OPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
@@ -53,7 +58,6 @@ int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
          { "force_space", OPKG_OPT_TYPE_BOOL, &conf->force_space },
          { "ftp_proxy", OPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
          { "http_proxy", OPKG_OPT_TYPE_STRING, &conf->http_proxy },
-         { "multiple_providers", OPKG_OPT_TYPE_BOOL, &conf->multiple_providers },
          { "no_proxy", OPKG_OPT_TYPE_STRING, &conf->no_proxy },
          { "test", OPKG_OPT_TYPE_INT, &conf->noaction },
          { "noaction", OPKG_OPT_TYPE_INT, &conf->noaction },
@@ -101,10 +105,10 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      int err;
      char *tmp_dir_base;
      nv_pair_list_t tmp_dest_nv_pair_list;
-     char * lists_dir =NULL;
+     char *lists_dir = NULL, *lock_file = NULL;
      glob_t globbuf;
      char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
-     char *pending_dir  =NULL;
+     char *pending_dir NULL;
 
      memset(conf, 0, sizeof(opkg_conf_t));
 
@@ -118,6 +122,22 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      conf->restrict_to_default_dest = 0;
      conf->default_dest = NULL;
 
+     /* check for lock file */
+     if (args->offline_root)
+       sprintf_alloc (&lock_file, "%s/%s/lock", args->offline_root, OPKG_STATE_DIR_PREFIX);
+     else
+       sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
+
+     conf->lock_fd = creat (lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
+     err = lockf (conf->lock_fd, F_TLOCK, 0);
+
+     free (lock_file);
+
+     if (err)
+     {
+       opkg_message (conf, OPKG_ERROR, "Could not obtain administrative lock\n");
+       return OPKG_CONF_ERR_LOCK;
+     }
 
      if (args->tmp_dir)
          tmp_dir_base = args->tmp_dir;
@@ -130,31 +150,9 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      if (conf->tmp_dir == NULL) {
          fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
                  __FUNCTION__, conf->tmp_dir, strerror(errno));
-         return errno;
+         return OPKG_CONF_ERR_TMP_DIR;
      }
 
-     conf->force_depends = 0;
-     conf->force_defaults = 0;
-     conf->force_overwrite = 0;
-     conf->force_downgrade = 0;
-     conf->force_reinstall = 0;
-     conf->force_space = 0;
-     conf->force_removal_of_essential_packages = 0;
-     conf->force_removal_of_dependent_packages = 0;
-     conf->nodeps = 0;
-     conf->offline_root = NULL;
-     conf->offline_root_pre_script_cmd = NULL;
-     conf->offline_root_post_script_cmd = NULL;
-     conf->multiple_providers = 0;
-     conf->verbosity = 1;
-     conf->noaction = 0;
-
-     conf->http_proxy = NULL;
-     conf->ftp_proxy = NULL;
-     conf->no_proxy = NULL;
-     conf->proxy_user = NULL;
-     conf->proxy_passwd = NULL;
-
      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);
@@ -167,19 +165,17 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
               if (opkg_conf_parse_file(conf, args->conf_file,
                                    &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
                    /* Memory leakage from opkg_conf_parse-file */
-                   return -1;
+                   return OPKG_CONF_ERR_PARSE;
                }
-                   
      }
 
-     /* if (!lists_dir ){*/
      if (strlen(lists_dir)<=1 ){
         lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
         sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
      }
 
      if (args->offline_root) {
-            char *tmp;// = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
+            char *tmp;
             sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
             free(lists_dir);
             lists_dir = tmp;
@@ -204,7 +200,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
                    if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
                                         &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
                         /* Memory leakage from opkg_conf_parse-file */
-                        return -1;
+                        return OPKG_CONF_ERR_PARSE;
                    }
          }
      }
@@ -264,9 +260,6 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      if (args->query_all) {
          conf->query_all = 1;
      }
-     if (args->multiple_providers) {
-         conf->multiple_providers = 1;
-     }
      if (args->verbosity != conf->verbosity) {
          conf->verbosity = args->verbosity;
      } 
@@ -278,6 +271,8 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
      opkg_conf_override_string(&conf->offline_root_post_script_cmd, 
                               args->offline_root_post_script_cmd);
 
+     opkg_conf_override_string(&conf->cache, args->cache);
+
 /* Pigi: added a flag to disable the checking of structures if the command does not need to 
          read anything from there.
 */
@@ -294,7 +289,7 @@ int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
         if (args->dest) {
             err = opkg_conf_set_default_dest(conf, args->dest);
             if (err) {
-                 return err;
+                 return OPKG_CONF_ERR_DEFAULT_DEST;
             }
         }
      }
@@ -346,6 +341,8 @@ void opkg_conf_deinit(opkg_conf_t *conf)
      opkg_conf_free_string(&conf->offline_root_pre_script_cmd);
      opkg_conf_free_string(&conf->offline_root_post_script_cmd);
 
+     opkg_conf_free_string(&conf->cache);
+
      if (conf->verbosity > 1) { 
          int i;
          hash_table_t *hashes[] = {