Ensure that the hash_table messages show useful information.
[oweals/opkg-lede.git] / libopkg / opkg_conf.c
1 /* opkg_conf.c - the opkg package management system
2
3    Carl D. Worth
4
5    Copyright (C) 2001 University of Southern California
6
7    This program is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License as
9    published by the Free Software Foundation; either version 2, or (at
10    your option) any later version.
11
12    This program is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16 */
17
18 #include "includes.h"
19 #include "opkg_conf.h"
20 #include "opkg_error.h"
21
22 #include "xregex.h"
23 #include "sprintf_alloc.h"
24 #include "args.h"
25 #include "opkg_message.h"
26 #include "file_util.h"
27 #include "str_util.h"
28 #include "xsystem.h"
29 #include "opkg_defines.h"
30 #include "libbb/libbb.h"
31
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <glob.h>
37
38 extern char *conf_file_dir;
39
40 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
41                                 pkg_src_list_t *pkg_src_list,
42                                 nv_pair_list_t *tmp_dest_nv_pair_list,
43                                 char **tmp_lists_dir);
44 static int opkg_conf_set_option(const opkg_option_t *options,
45                                 const char *name, const char *value);
46 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
47                                       const char *default_dest_name);
48 static int set_and_load_pkg_src_list(opkg_conf_t *conf,
49                                      pkg_src_list_t *nv_pair_list);
50 static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
51                                       nv_pair_list_t *nv_pair_list, char * lists_dir);
52
53 void opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
54 {
55      opkg_option_t tmp[] = {
56           { "cache", OPKG_OPT_TYPE_STRING, &conf->cache},
57           { "force_defaults", OPKG_OPT_TYPE_BOOL, &conf->force_defaults },
58           { "force_maintainer", OPKG_OPT_TYPE_BOOL, &conf->force_maintainer }, 
59           { "force_depends", OPKG_OPT_TYPE_BOOL, &conf->force_depends },
60           { "force_overwrite", OPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
61           { "force_downgrade", OPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
62           { "force_reinstall", OPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
63           { "force_space", OPKG_OPT_TYPE_BOOL, &conf->force_space },
64           { "check_signature", OPKG_OPT_TYPE_INT, &conf->check_signature }, 
65           { "ftp_proxy", OPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
66           { "http_proxy", OPKG_OPT_TYPE_STRING, &conf->http_proxy },
67           { "no_proxy", OPKG_OPT_TYPE_STRING, &conf->no_proxy },
68           { "test", OPKG_OPT_TYPE_INT, &conf->noaction },
69           { "noaction", OPKG_OPT_TYPE_INT, &conf->noaction },
70           { "nodeps", OPKG_OPT_TYPE_BOOL, &conf->nodeps },
71           { "offline_root", OPKG_OPT_TYPE_STRING, &conf->offline_root },
72           { "offline_root_path", OPKG_OPT_TYPE_STRING, &conf->offline_root_path },
73           { "offline_root_post_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
74           { "offline_root_pre_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
75           { "proxy_passwd", OPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
76           { "proxy_user", OPKG_OPT_TYPE_STRING, &conf->proxy_user },
77           { "query-all", OPKG_OPT_TYPE_BOOL, &conf->query_all },
78           { "verbosity", OPKG_OPT_TYPE_BOOL, &conf->verbosity },
79 #if defined(HAVE_OPENSSL)
80           { "signature_ca_file", OPKG_OPT_TYPE_STRING, &conf->signature_ca_file },
81           { "signature_ca_path", OPKG_OPT_TYPE_STRING, &conf->signature_ca_path },
82 #endif
83 #if defined(HAVE_PATHFINDER)
84           { "check_x509_path", OPKG_OPT_TYPE_INT, &conf->check_x509_path }, 
85 #endif
86 #if defined(HAVE_SSLCURL) && defined(HAVE_CURL)
87           { "ssl_engine", OPKG_OPT_TYPE_STRING, &conf->ssl_engine },
88           { "ssl_cert", OPKG_OPT_TYPE_STRING, &conf->ssl_cert },
89           { "ssl_cert_type", OPKG_OPT_TYPE_STRING, &conf->ssl_cert_type },
90           { "ssl_key", OPKG_OPT_TYPE_STRING, &conf->ssl_key },
91           { "ssl_key_type", OPKG_OPT_TYPE_STRING, &conf->ssl_key_type },
92           { "ssl_key_passwd", OPKG_OPT_TYPE_STRING, &conf->ssl_key_passwd },
93           { "ssl_ca_file", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_file },
94           { "ssl_ca_path", OPKG_OPT_TYPE_STRING, &conf->ssl_ca_path },
95           { "ssl_dont_verify_peer", OPKG_OPT_TYPE_BOOL, &conf->ssl_dont_verify_peer },
96 #endif
97           { NULL }
98      };
99
100      *options = xcalloc(1, sizeof(tmp));
101      memcpy(*options, tmp, sizeof(tmp));
102 };
103
104 static void opkg_conf_override_string(char **conf_str, char *arg_str) 
105 {
106      if (arg_str) {
107           if (*conf_str) {
108                free(*conf_str);
109           }
110           *conf_str = xstrdup(arg_str);
111      }
112 }
113
114 static void opkg_conf_free_string(char **conf_str)
115 {
116      if (*conf_str) {
117           free(*conf_str);
118           *conf_str = NULL;
119      }
120 }
121
122 int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
123 {
124      int err;
125      int errno_copy;
126      char *tmp_dir_base;
127      nv_pair_list_t tmp_dest_nv_pair_list;
128      char *lists_dir = NULL, *lock_file = NULL;
129      glob_t globbuf;
130      char *etc_opkg_conf_pattern;
131      char *pending_dir = NULL;
132
133      memset(conf, 0, sizeof(opkg_conf_t));
134
135 #if defined(HAVE_PATHFINDER)
136      conf->check_x509_path = 1;
137 #endif
138
139      pkg_src_list_init(&conf->pkg_src_list);
140
141      nv_pair_list_init(&tmp_dest_nv_pair_list);
142      pkg_dest_list_init(&conf->pkg_dest_list);
143
144      nv_pair_list_init(&conf->arch_list);
145
146      conf->restrict_to_default_dest = 0;
147      conf->default_dest = NULL;
148
149      /* check for lock file */
150      if (args->offline_root)
151        sprintf_alloc (&lock_file, "%s/%s/lock", args->offline_root, OPKG_STATE_DIR_PREFIX);
152      else
153        sprintf_alloc (&lock_file, "%s/lock", OPKG_STATE_DIR_PREFIX);
154
155      conf->lock_fd = creat (lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
156      err = lockf (conf->lock_fd, F_TLOCK, 0);
157      errno_copy = errno;
158
159      free (lock_file);
160
161      if (err)
162      {
163        if(args->offline_root) {
164          opkg_message (conf, OPKG_ERROR, "Could not obtain administrative lock for offline root (ERR: %s)  at %s/%s/lock\n",
165                  strerror(errno_copy), args->offline_root, OPKG_STATE_DIR_PREFIX);
166        } else {
167          opkg_message (conf, OPKG_ERROR, "Could not obtain administrative lock (ERR: %s) at %s/lock\n",
168                  strerror(errno_copy), OPKG_STATE_DIR_PREFIX);
169        }
170        return OPKG_CONF_ERR_LOCK;
171      }
172
173      if (args->tmp_dir)
174           tmp_dir_base = args->tmp_dir;
175      else 
176           tmp_dir_base = getenv("TMPDIR");
177      sprintf_alloc(&conf->tmp_dir, "%s/%s",
178                    tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
179                    OPKG_CONF_TMP_DIR_SUFFIX);
180      conf->tmp_dir = mkdtemp(conf->tmp_dir);
181      if (conf->tmp_dir == NULL) {
182           fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
183                   __FUNCTION__, conf->tmp_dir, strerror(errno));
184           return OPKG_CONF_ERR_TMP_DIR;
185      }
186
187      pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN);
188      hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
189      hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
190      lists_dir=xmalloc(1);
191      lists_dir[0]='\0';
192      if (args->conf_file) {
193           struct stat stat_buf;
194           err = stat(args->conf_file, &stat_buf);
195           if (err == 0)
196                if (opkg_conf_parse_file(conf, args->conf_file,
197                                     &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
198                    /* Memory leakage from opkg_conf_parse-file */
199                    return OPKG_CONF_ERR_PARSE;
200                }
201      }
202
203      if (strlen(lists_dir)<=1 ){
204         lists_dir = xrealloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
205         sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
206      }
207
208      if (args->offline_root) {
209             char *tmp;
210             sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
211             free(lists_dir);
212             lists_dir = tmp;
213      }
214
215      pending_dir = xcalloc(1, strlen(lists_dir)+strlen("/pending")+5);
216      snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
217
218      conf->lists_dir = xstrdup(lists_dir);
219      conf->pending_dir = xstrdup(pending_dir);
220
221      if (args->offline_root) 
222           sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", args->offline_root);
223      else
224           sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf", conf_file_dir);
225      memset(&globbuf, 0, sizeof(globbuf));
226      err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
227      free (etc_opkg_conf_pattern);
228      if (!err) {
229           int i;
230           for (i = 0; i < globbuf.gl_pathc; i++) {
231                if (globbuf.gl_pathv[i]) 
232                     if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i], 
233                                          &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
234                         /* Memory leakage from opkg_conf_parse-file */
235                         return OPKG_CONF_ERR_PARSE;
236                     }
237           }
238      }
239      globfree(&globbuf);
240
241      /* if no architectures were defined, then default all, noarch, and host architecture */
242      if (nv_pair_list_empty(&conf->arch_list)) {
243           nv_pair_list_append(&conf->arch_list, "all", "1");
244           nv_pair_list_append(&conf->arch_list, "noarch", "1");
245           nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
246      }
247
248      /* Even if there is no conf file, we'll need at least one dest. */
249      if (nv_pair_list_empty(&tmp_dest_nv_pair_list)) {
250           nv_pair_list_append(&tmp_dest_nv_pair_list,
251                               OPKG_CONF_DEFAULT_DEST_NAME,
252                               OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
253      }
254
255      /* After parsing the file, set options from command-line, (so that
256         command-line arguments take precedence) */
257      /* XXX: CLEANUP: The interaction between args.c and opkg_conf.c
258         really needs to be cleaned up. There is so much duplication
259         right now it is ridiculous. Maybe opkg_conf_t should just save
260         a pointer to args_t (which could then not be freed), rather
261         than duplicating every field here? */
262      if (args->autoremove) {
263           conf->autoremove = 1;
264      }
265      if (args->force_depends) {
266           conf->force_depends = 1;
267      }
268      if (args->force_defaults) {
269           conf->force_defaults = 1;
270      }
271      if (args->force_maintainer) {
272           conf->force_maintainer = 1;
273      }
274      if (args->force_overwrite) {
275           conf->force_overwrite = 1;
276      }
277      if (args->force_downgrade) {
278           conf->force_downgrade = 1;
279      }
280      if (args->force_space) {
281           conf->force_space = 1;
282      }
283      if (args->force_reinstall) {
284           conf->force_reinstall = 1;
285      }
286      if (args->force_removal_of_dependent_packages) {
287           conf->force_removal_of_dependent_packages = 1;
288      }
289      if (args->force_removal_of_essential_packages) {
290           conf->force_removal_of_essential_packages = 1;
291      }
292      if (args->nodeps) {
293           conf->nodeps = 1;
294      }
295      if (args->noaction) {
296           conf->noaction = 1;
297      }
298      if (args->query_all) {
299           conf->query_all = 1;
300      }
301      if (args->verbosity != conf->verbosity) {
302           conf->verbosity = args->verbosity;
303      } 
304
305      opkg_conf_override_string(&conf->offline_root, 
306                                args->offline_root);
307      opkg_conf_override_string(&conf->offline_root_path, 
308                                args->offline_root_path);
309      opkg_conf_override_string(&conf->offline_root_pre_script_cmd, 
310                                args->offline_root_pre_script_cmd);
311      opkg_conf_override_string(&conf->offline_root_post_script_cmd, 
312                                args->offline_root_post_script_cmd);
313
314      opkg_conf_override_string(&conf->cache, args->cache);
315
316 /* Pigi: added a flag to disable the checking of structures if the command does not need to 
317          read anything from there.
318 */
319      if ( !(args->nocheckfordirorfile)){
320         /* need to run load the source list before dest list -Jamey */
321         if ( !(args->noreadfeedsfile))
322            set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
323    
324         /* Now that we have resolved conf->offline_root, we can commit to
325            the directory names for the dests and load in all the package
326            lists. */
327         set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
328    
329         if (args->dest) {
330              err = opkg_conf_set_default_dest(conf, args->dest);
331              if (err) {
332                   return OPKG_CONF_ERR_DEFAULT_DEST;
333              }
334         }
335      }
336      nv_pair_list_deinit(&tmp_dest_nv_pair_list);
337      free(lists_dir);
338      free(pending_dir);
339
340      return 0;
341 }
342
343 void opkg_conf_deinit(opkg_conf_t *conf)
344 {
345 #ifdef OPKG_DEBUG_NO_TMP_CLEANUP
346 #error
347      fprintf(stderr, "%s: Not cleaning up %s since opkg compiled "
348              "with OPKG_DEBUG_NO_TMP_CLEANUP\n",
349              __FUNCTION__, conf->tmp_dir);
350 #else
351      int err;
352
353      err = rmdir(conf->tmp_dir);
354      if (err) {
355           if (errno == ENOTEMPTY) {
356                char *cmd;
357                sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
358                err = xsystem(cmd);
359                free(cmd);
360           }
361           if (err)
362                fprintf(stderr, "WARNING: Unable to remove temporary directory: %s: %s\n", conf->tmp_dir, strerror(errno));
363      }
364 #endif /* OPKG_DEBUG_NO_TMP_CLEANUP */
365
366      free(conf->tmp_dir); /*XXX*/
367      free(conf->lists_dir);
368      free(conf->pending_dir);
369
370      pkg_src_list_deinit(&conf->pkg_src_list);
371      pkg_dest_list_deinit(&conf->pkg_dest_list);
372      nv_pair_list_deinit(&conf->arch_list);
373
374      opkg_conf_free_string(&conf->offline_root);
375      opkg_conf_free_string(&conf->offline_root_path);
376      opkg_conf_free_string(&conf->offline_root_pre_script_cmd);
377      opkg_conf_free_string(&conf->offline_root_post_script_cmd);
378
379      opkg_conf_free_string(&conf->cache);
380
381 #if defined(HAVE_OPENSSL)
382      opkg_conf_free_string(&conf->signature_ca_file);
383      opkg_conf_free_string(&conf->signature_ca_path);
384 #endif
385
386 #if defined(HAVE_SSLCURL)
387      opkg_conf_free_string(&conf->ssl_engine);
388      opkg_conf_free_string(&conf->ssl_cert);
389      opkg_conf_free_string(&conf->ssl_cert_type);
390      opkg_conf_free_string(&conf->ssl_key);
391      opkg_conf_free_string(&conf->ssl_key_type);
392      opkg_conf_free_string(&conf->ssl_key_passwd);
393      opkg_conf_free_string(&conf->ssl_ca_file);
394      opkg_conf_free_string(&conf->ssl_ca_path);
395 #endif
396
397      if (conf->verbosity > 1) { 
398           int i;
399           hash_table_t *hashes[] = {
400                &conf->pkg_hash,
401                &conf->file_hash,
402                &conf->obs_file_hash };
403           for (i = 0; i < 3; i++) {
404                hash_table_t *hash = hashes[i];
405                int c = 0;
406                int n_conflicts = 0;
407                int j;
408                for (j = 0; j < hash->n_entries; j++) {
409                     int len = 0;
410                     hash_entry_t *e = &hash->entries[j];
411                     if (e->next)
412                          n_conflicts++;
413                     while (e && e->key) {
414                          len++;
415                          e = e->next;
416                     }
417                     if (len > c) 
418                          c = len;
419                }
420                opkg_message(conf, OPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n", 
421                             hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
422           }
423      }
424
425      if (&conf->pkg_hash)
426                     pkg_hash_deinit(&conf->pkg_hash);
427      if (&conf->file_hash)
428                     hash_table_deinit(&conf->file_hash);
429      if (&conf->obs_file_hash)
430                     hash_table_deinit(&conf->obs_file_hash);
431 }
432
433 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
434                                       const char *default_dest_name)
435 {
436      pkg_dest_list_elt_t *iter;
437      pkg_dest_t *dest;
438
439      for (iter = void_list_first(&conf->pkg_dest_list); iter; iter = void_list_next(&conf->pkg_dest_list, iter)) {
440           dest = (pkg_dest_t *)iter->data;
441           if (strcmp(dest->name, default_dest_name) == 0) {
442                conf->default_dest = dest;
443                conf->restrict_to_default_dest = 1;
444                return 0;
445           }
446      }
447
448      fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
449
450      return 1;
451 }
452
453 static int set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
454 {
455      pkg_src_list_elt_t *iter;
456      pkg_src_t *src;
457      char *list_file;
458
459      for (iter = void_list_first(pkg_src_list); iter; iter = void_list_next(pkg_src_list, iter)) {
460           src = (pkg_src_t *)iter->data;
461           if (src == NULL) {
462                continue;
463           }
464
465           sprintf_alloc(&list_file, "%s/%s", 
466                           conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 
467                           src->name);
468
469           if (file_exists(list_file)) {
470                pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
471           }
472           free(list_file);
473      }
474
475      return 0;
476 }
477
478 static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
479 {
480      nv_pair_list_elt_t *iter;
481      nv_pair_t *nv_pair;
482      pkg_dest_t *dest;
483      char *root_dir;
484
485      for (iter = nv_pair_list_first(nv_pair_list); iter; iter = nv_pair_list_next(nv_pair_list, iter)) {
486           nv_pair = (nv_pair_t *)iter->data;
487
488           if (conf->offline_root) {
489                sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
490           } else {
491                root_dir = xstrdup(nv_pair->value);
492           }
493           dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
494           free(root_dir);
495           if (dest == NULL) {
496                continue;
497           }
498           if (conf->default_dest == NULL) {
499                conf->default_dest = dest;
500           }
501           if (file_exists(dest->status_file_name)) {
502                pkg_hash_add_from_file(conf, dest->status_file_name,
503                                       NULL, dest, 1);
504           }
505      }
506
507      return 0;
508 }
509
510 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
511                                 pkg_src_list_t *pkg_src_list,
512                                 nv_pair_list_t *tmp_dest_nv_pair_list,
513                                 char **lists_dir)
514 {
515      int err;
516      opkg_option_t * options;
517      FILE *file = fopen(filename, "r");
518      regex_t valid_line_re, comment_re;
519 #define regmatch_size 12
520      regmatch_t regmatch[regmatch_size];
521
522      opkg_init_options_array(conf, &options);
523
524      if (file == NULL) {
525           fprintf(stderr, "%s: failed to open %s: %s\n",
526                   __FUNCTION__, filename, strerror(errno));
527           free(options);
528           return errno;
529      }
530      opkg_message(conf, OPKG_NOTICE, "loading conf file %s\n", filename);
531
532      err = xregcomp(&comment_re, 
533                     "^[[:space:]]*(#.*|[[:space:]]*)$",
534                     REG_EXTENDED);
535      if (err) {
536           free(options);
537           return err;
538      }
539      err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
540      if (err) {
541           free(options);
542           return err;
543      }
544
545      while(1) {
546           int line_num = 0;
547           char *line;
548           char *type, *name, *value, *extra;
549
550           line = file_read_line_alloc(file);
551           line_num++;
552           if (line == NULL) {
553                break;
554           }
555
556           str_chomp(line);
557
558           if (regexec(&comment_re, line, 0, 0, 0) == 0) {
559                goto NEXT_LINE;
560           }
561
562           if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
563                str_chomp(line);
564                fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
565                        filename, line_num, line);
566                goto NEXT_LINE;
567           }
568
569           /* This has to be so ugly to deal with optional quotation marks */
570           if (regmatch[2].rm_so > 0) {
571                type = xstrndup(line + regmatch[2].rm_so,
572                               regmatch[2].rm_eo - regmatch[2].rm_so);
573           } else {
574                type = xstrndup(line + regmatch[3].rm_so,
575                               regmatch[3].rm_eo - regmatch[3].rm_so);
576           }
577           if (regmatch[5].rm_so > 0) {
578                name = xstrndup(line + regmatch[5].rm_so,
579                               regmatch[5].rm_eo - regmatch[5].rm_so);
580           } else {
581                name = xstrndup(line + regmatch[6].rm_so,
582                               regmatch[6].rm_eo - regmatch[6].rm_so);
583           }
584           if (regmatch[8].rm_so > 0) {
585                value = xstrndup(line + regmatch[8].rm_so,
586                                regmatch[8].rm_eo - regmatch[8].rm_so);
587           } else {
588                value = xstrndup(line + regmatch[9].rm_so,
589                                regmatch[9].rm_eo - regmatch[9].rm_so);
590           }
591           extra = NULL;
592           if (regmatch[11].rm_so > 0) {
593                extra = xstrndup (line + regmatch[11].rm_so,
594                                 regmatch[11].rm_eo - regmatch[11].rm_so);
595           }
596
597           /* We use the tmp_dest_nv_pair_list below instead of
598              conf->pkg_dest_list because we might encounter an
599              offline_root option later and that would invalidate the
600              directories we would have computed in
601              pkg_dest_list_init. (We do a similar thing with
602              tmp_src_nv_pair_list for sake of symmetry.) */
603           if (strcmp(type, "option") == 0) {
604                opkg_conf_set_option(options, name, value);
605           } else if (strcmp(type, "src") == 0) {
606                if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
607                     pkg_src_list_append (pkg_src_list, name, value, extra, 0);
608                } else {
609                     opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
610                                  name, value);
611                }
612           } else if (strcmp(type, "src/gz") == 0) {
613                if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
614                     pkg_src_list_append (pkg_src_list, name, value, extra, 1);
615                } else {
616                     opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
617                                  name, value);
618                }
619           } else if (strcmp(type, "dest") == 0) {
620                nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
621           } else if (strcmp(type, "lists_dir") == 0) {
622                *lists_dir = xrealloc(*lists_dir,strlen(value)+1);
623                sprintf (*lists_dir,"%s",value);
624           } else if (strcmp(type, "arch") == 0) {
625                opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value);
626                if (!value) {
627                     opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
628                     value = xstrdup("10");
629                }
630                nv_pair_list_append(&conf->arch_list, name, value);
631           } else {
632                fprintf(stderr, "WARNING: Ignoring unknown configuration "
633                        "parameter: %s %s %s\n", type, name, value);
634                free(options);
635                return EINVAL;
636           }
637
638           free(type);
639           free(name);
640           free(value);
641           if (extra)
642                free (extra);
643
644      NEXT_LINE:
645           free(line);
646      }
647
648      free(options);
649      regfree(&comment_re);
650      regfree(&valid_line_re);
651      fclose(file);
652
653      return 0;
654 }
655
656 static int opkg_conf_set_option(const opkg_option_t *options,
657                                 const char *name, const char *value)
658 {
659      int i = 0;
660      while (options[i].name) {
661           if (strcmp(options[i].name, name) == 0) {
662                switch (options[i].type) {
663                case OPKG_OPT_TYPE_BOOL:
664                     *((int *)options[i].value) = 1;
665                     return 0;
666                case OPKG_OPT_TYPE_INT:
667                     if (value) {
668                          *((int *)options[i].value) = atoi(value);
669                          return 0;
670                     } else {
671                          printf("%s: Option %s need an argument\n",
672                                 __FUNCTION__, name);
673                          return EINVAL;
674                     }               
675                case OPKG_OPT_TYPE_STRING:
676                     if (value) {
677                          *((char **)options[i].value) = xstrdup(value);
678                          return 0;
679                     } else {
680                          printf("%s: Option %s need an argument\n",
681                                 __FUNCTION__, name);
682                          return EINVAL;
683                     }
684                }
685           }
686           i++;
687      }
688     
689      fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
690              __FUNCTION__, name, value);
691      return EINVAL;
692 }
693
694 int opkg_conf_write_status_files(opkg_conf_t *conf)
695 {
696      pkg_dest_t *dest;
697      pkg_vec_t *all;
698      pkg_t *pkg;
699      int i;
700      int err;
701      FILE * status_file=NULL;
702
703      if (conf->noaction)
704           return 0;
705
706      dest = (pkg_dest_t *)void_list_first(&conf->pkg_dest_list)->data;
707      status_file = fopen(dest->status_file_tmp_name, "w");
708      if (status_file == NULL) {
709          fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
710                  __FUNCTION__, dest->status_file_tmp_name, strerror(errno));
711      }
712
713      all = pkg_vec_alloc();
714      pkg_hash_fetch_available(&conf->pkg_hash, all);
715
716      for(i = 0; i < all->len; i++) {
717           pkg = all->pkgs[i];
718           /* We don't need most uninstalled packages in the status file */
719           if (pkg->state_status == SS_NOT_INSTALLED
720               && (pkg->state_want == SW_UNKNOWN
721                   || pkg->state_want == SW_DEINSTALL
722                   || pkg->state_want == SW_PURGE)) {
723                continue;
724           }
725           if (pkg->dest == NULL) {
726                fprintf(stderr, "%s: ERROR: Can't write status for "
727                        "package %s since it has a NULL dest\n",
728                        __FUNCTION__, pkg->name);
729                continue;
730           }
731           if (status_file) {
732                pkg_print_status(pkg, status_file);
733           }
734      }
735
736      pkg_vec_free(all);
737
738      if (status_file) {
739          err = ferror(status_file);
740          fclose(status_file);
741          if (!err) {
742              file_move(dest->status_file_tmp_name, dest->status_file_name);
743          } else {
744              fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
745                      "retaining old %s\n", __FUNCTION__,
746                      dest->status_file_tmp_name, dest->status_file_name);
747          }
748          status_file = NULL;
749      }
750      return 0;
751 }
752
753
754 char *root_filename_alloc(opkg_conf_t *conf, char *filename)
755 {
756      char *root_filename;
757      sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
758      return root_filename;
759 }