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