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