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