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