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