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