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