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