ba0daf5de1c58dfb4c43835c860edcd06a2a916f
[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           { "no_proxy", OPKG_OPT_TYPE_STRING, &conf->no_proxy },
57           { "test", OPKG_OPT_TYPE_INT, &conf->noaction },
58           { "noaction", OPKG_OPT_TYPE_INT, &conf->noaction },
59           { "nodeps", OPKG_OPT_TYPE_BOOL, &conf->nodeps },
60           { "offline_root", OPKG_OPT_TYPE_STRING, &conf->offline_root },
61           { "offline_root_post_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
62           { "offline_root_pre_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
63           { "proxy_passwd", OPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
64           { "proxy_user", OPKG_OPT_TYPE_STRING, &conf->proxy_user },
65           { "query-all", OPKG_OPT_TYPE_BOOL, &conf->query_all },
66           { "verbosity", OPKG_OPT_TYPE_BOOL, &conf->verbosity },
67           { NULL }
68      };
69
70      *options = (opkg_option_t *)malloc(sizeof(tmp));
71      if ( options == NULL ){
72         fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
73         return -1;
74      }
75
76      memcpy(*options, tmp, sizeof(tmp));
77      return 0;
78 };
79
80 static void opkg_conf_override_string(char **conf_str, char *arg_str) 
81 {
82      if (arg_str) {
83           if (*conf_str) {
84                free(*conf_str);
85           }
86           *conf_str = strdup(arg_str);
87      }
88 }
89
90 static void opkg_conf_free_string(char **conf_str)
91 {
92      if (*conf_str) {
93           free(*conf_str);
94           *conf_str = NULL;
95      }
96 }
97
98 int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
99 {
100      int err;
101      char *tmp_dir_base;
102      nv_pair_list_t tmp_dest_nv_pair_list;
103      char * lists_dir =NULL;
104      glob_t globbuf;
105      char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
106      char *pending_dir  =NULL;
107
108      memset(conf, 0, sizeof(opkg_conf_t));
109
110      pkg_src_list_init(&conf->pkg_src_list);
111
112      nv_pair_list_init(&tmp_dest_nv_pair_list);
113      pkg_dest_list_init(&conf->pkg_dest_list);
114
115      nv_pair_list_init(&conf->arch_list);
116
117      conf->restrict_to_default_dest = 0;
118      conf->default_dest = NULL;
119
120
121      if (args->tmp_dir)
122           tmp_dir_base = args->tmp_dir;
123      else 
124           tmp_dir_base = getenv("TMPDIR");
125      sprintf_alloc(&conf->tmp_dir, "%s/%s",
126                    tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
127                    OPKG_CONF_TMP_DIR_SUFFIX);
128      conf->tmp_dir = mkdtemp(conf->tmp_dir);
129      if (conf->tmp_dir == NULL) {
130           fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
131                   __FUNCTION__, conf->tmp_dir, strerror(errno));
132           return errno;
133      }
134
135      conf->force_depends = 0;
136      conf->force_defaults = 0;
137      conf->force_overwrite = 0;
138      conf->force_downgrade = 0;
139      conf->force_reinstall = 0;
140      conf->force_space = 0;
141      conf->force_removal_of_essential_packages = 0;
142      conf->force_removal_of_dependent_packages = 0;
143      conf->nodeps = 0;
144      conf->offline_root = NULL;
145      conf->offline_root_pre_script_cmd = NULL;
146      conf->offline_root_post_script_cmd = NULL;
147      conf->verbosity = 1;
148      conf->noaction = 0;
149
150      conf->http_proxy = NULL;
151      conf->ftp_proxy = NULL;
152      conf->no_proxy = NULL;
153      conf->proxy_user = NULL;
154      conf->proxy_passwd = NULL;
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 -1;
169                }
170                    
171      }
172
173      /* if (!lists_dir ){*/
174      if (strlen(lists_dir)<=1 ){
175         lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
176         sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
177      }
178
179      if (args->offline_root) {
180             char *tmp;// = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
181             sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
182             free(lists_dir);
183             lists_dir = tmp;
184      }
185
186      pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
187      snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
188
189      conf->lists_dir = strdup(lists_dir);
190      conf->pending_dir = strdup(pending_dir);
191
192      if (args->offline_root) 
193           sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", args->offline_root);
194      memset(&globbuf, 0, sizeof(globbuf));
195      err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
196      if (args->offline_root)
197           free (etc_opkg_conf_pattern);
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->verbosity != conf->verbosity) {
266           conf->verbosity = args->verbosity;
267      } 
268
269      opkg_conf_override_string(&conf->offline_root, 
270                                args->offline_root);
271      opkg_conf_override_string(&conf->offline_root_pre_script_cmd, 
272                                args->offline_root_pre_script_cmd);
273      opkg_conf_override_string(&conf->offline_root_post_script_cmd, 
274                                args->offline_root_post_script_cmd);
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 err;
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      if (conf->verbosity > 1) { 
345           int i;
346           hash_table_t *hashes[] = {
347                &conf->pkg_hash,
348                &conf->file_hash,
349                &conf->obs_file_hash };
350           for (i = 0; i < 3; i++) {
351                hash_table_t *hash = hashes[i];
352                int c = 0;
353                int n_conflicts = 0;
354                int j;
355                for (j = 0; j < hash->n_entries; j++) {
356                     int len = 0;
357                     hash_entry_t *e = &hash->entries[j];
358                     if (e->next)
359                          n_conflicts++;
360                     while (e && e->key) {
361                          len++;
362                          e = e->next;
363                     }
364                     if (len > c) 
365                          c = len;
366                }
367                opkg_message(conf, OPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n", 
368                             hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
369                hash_table_deinit(hash);
370           }
371      }
372 }
373
374 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
375                                       const char *default_dest_name)
376 {
377      pkg_dest_list_elt_t *iter;
378      pkg_dest_t *dest;
379
380      for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
381           dest = iter->data;
382           if (strcmp(dest->name, default_dest_name) == 0) {
383                conf->default_dest = dest;
384                conf->restrict_to_default_dest = 1;
385                return 0;
386           }
387      }
388
389      fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
390
391      return 1;
392 }
393
394 static int set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
395 {
396      pkg_src_list_elt_t *iter;
397      pkg_src_t *src;
398      char *list_file;
399
400      for (iter = pkg_src_list->head; iter; iter = iter->next) {
401           src = iter->data;
402           if (src == NULL) {
403                continue;
404           }
405
406           sprintf_alloc(&list_file, "%s/%s", 
407                           conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir, 
408                           src->name);
409
410           if (file_exists(list_file)) {
411                pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
412           }
413           free(list_file);
414      }
415
416      return 0;
417 }
418
419 static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
420 {
421      nv_pair_list_elt_t *iter;
422      nv_pair_t *nv_pair;
423      pkg_dest_t *dest;
424      char *root_dir;
425
426      for (iter = nv_pair_list->head; iter; iter = iter->next) {
427           nv_pair = iter->data;
428
429           if (conf->offline_root) {
430                sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
431           } else {
432                root_dir = strdup(nv_pair->value);
433           }
434           dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
435           free(root_dir);
436           if (dest == NULL) {
437                continue;
438           }
439           if (conf->default_dest == NULL) {
440                conf->default_dest = dest;
441           }
442           if (file_exists(dest->status_file_name)) {
443                pkg_hash_add_from_file(conf, dest->status_file_name,
444                                       NULL, dest, 1);
445           }
446      }
447
448      return 0;
449 }
450
451 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
452                                 pkg_src_list_t *pkg_src_list,
453                                 nv_pair_list_t *tmp_dest_nv_pair_list,
454                                 char **lists_dir)
455 {
456      int err;
457      opkg_option_t * options;
458      FILE *file = fopen(filename, "r");
459      regex_t valid_line_re, comment_re;
460 #define regmatch_size 12
461      regmatch_t regmatch[regmatch_size];
462
463      if (opkg_init_options_array(conf, &options)<0)
464         return ENOMEM;
465
466      if (file == NULL) {
467           fprintf(stderr, "%s: failed to open %s: %s\n",
468                   __FUNCTION__, filename, strerror(errno));
469           free(options);
470           return errno;
471      }
472      opkg_message(conf, OPKG_NOTICE, "loading conf file %s\n", filename);
473
474      err = xregcomp(&comment_re, 
475                     "^[[:space:]]*(#.*|[[:space:]]*)$",
476                     REG_EXTENDED);
477      if (err) {
478           free(options);
479           return err;
480      }
481      err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
482      if (err) {
483           free(options);
484           return err;
485      }
486
487      while(1) {
488           int line_num = 0;
489           char *line;
490           char *type, *name, *value, *extra;
491
492           line = file_read_line_alloc(file);
493           line_num++;
494           if (line == NULL) {
495                break;
496           }
497
498           str_chomp(line);
499
500           if (regexec(&comment_re, line, 0, 0, 0) == 0) {
501                goto NEXT_LINE;
502           }
503
504           if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
505                str_chomp(line);
506                fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
507                        filename, line_num, line);
508                goto NEXT_LINE;
509           }
510
511           /* This has to be so ugly to deal with optional quotation marks */
512           if (regmatch[2].rm_so > 0) {
513                type = strndup(line + regmatch[2].rm_so,
514                               regmatch[2].rm_eo - regmatch[2].rm_so);
515           } else {
516                type = strndup(line + regmatch[3].rm_so,
517                               regmatch[3].rm_eo - regmatch[3].rm_so);
518           }
519           if (regmatch[5].rm_so > 0) {
520                name = strndup(line + regmatch[5].rm_so,
521                               regmatch[5].rm_eo - regmatch[5].rm_so);
522           } else {
523                name = strndup(line + regmatch[6].rm_so,
524                               regmatch[6].rm_eo - regmatch[6].rm_so);
525           }
526           if (regmatch[8].rm_so > 0) {
527                value = strndup(line + regmatch[8].rm_so,
528                                regmatch[8].rm_eo - regmatch[8].rm_so);
529           } else {
530                value = strndup(line + regmatch[9].rm_so,
531                                regmatch[9].rm_eo - regmatch[9].rm_so);
532           }
533           extra = NULL;
534           if (regmatch[11].rm_so > 0) {
535                extra = strndup (line + regmatch[11].rm_so,
536                                 regmatch[11].rm_eo - regmatch[11].rm_so);
537           }
538
539           /* We use the tmp_dest_nv_pair_list below instead of
540              conf->pkg_dest_list because we might encounter an
541              offline_root option later and that would invalidate the
542              directories we would have computed in
543              pkg_dest_list_init. (We do a similar thing with
544              tmp_src_nv_pair_list for sake of symmetry.) */
545           if (strcmp(type, "option") == 0) {
546                opkg_conf_set_option(options, name, value);
547           } else if (strcmp(type, "src") == 0) {
548                if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
549                     pkg_src_list_append (pkg_src_list, name, value, extra, 0);
550                } else {
551                     opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
552                                  name, value);
553                }
554           } else if (strcmp(type, "src/gz") == 0) {
555                if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
556                     pkg_src_list_append (pkg_src_list, name, value, extra, 1);
557                } else {
558                     opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration.  Skipping:\n\t src %s %s\n",
559                                  name, value);
560                }
561           } else if (strcmp(type, "dest") == 0) {
562                nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
563           } else if (strcmp(type, "lists_dir") == 0) {
564                *lists_dir = realloc(*lists_dir,strlen(value)+1);
565                if (*lists_dir == NULL) {
566                     opkg_message(conf, OPKG_ERROR, "ERROR: Not enough memory\n");
567                     free(options);
568                     return EINVAL;
569                }
570                sprintf (*lists_dir,"%s",value);
571           } else if (strcmp(type, "arch") == 0) {
572                opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value);
573                if (!value) {
574                     opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
575                     value = strdup("10");
576                }
577                nv_pair_list_append(&conf->arch_list, name, value);
578           } else {
579                fprintf(stderr, "WARNING: Ignoring unknown configuration "
580                        "parameter: %s %s %s\n", type, name, value);
581                free(options);
582                return EINVAL;
583           }
584
585           free(type);
586           free(name);
587           free(value);
588           if (extra)
589                free (extra);
590
591      NEXT_LINE:
592           free(line);
593      }
594
595      free(options);
596      regfree(&comment_re);
597      regfree(&valid_line_re);
598      fclose(file);
599
600      return 0;
601 }
602
603 static int opkg_conf_set_option(const opkg_option_t *options,
604                                 const char *name, const char *value)
605 {
606      int i = 0;
607      while (options[i].name) {
608           if (strcmp(options[i].name, name) == 0) {
609                switch (options[i].type) {
610                case OPKG_OPT_TYPE_BOOL:
611                     *((int *)options[i].value) = 1;
612                     return 0;
613                case OPKG_OPT_TYPE_INT:
614                     if (value) {
615                          *((int *)options[i].value) = atoi(value);
616                          return 0;
617                     } else {
618                          printf("%s: Option %s need an argument\n",
619                                 __FUNCTION__, name);
620                          return EINVAL;
621                     }               
622                case OPKG_OPT_TYPE_STRING:
623                     if (value) {
624                          *((char **)options[i].value) = strdup(value);
625                          return 0;
626                     } else {
627                          printf("%s: Option %s need an argument\n",
628                                 __FUNCTION__, name);
629                          return EINVAL;
630                     }
631                }
632           }
633           i++;
634      }
635     
636      fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
637              __FUNCTION__, name, value);
638      return EINVAL;
639 }
640
641 int opkg_conf_write_status_files(opkg_conf_t *conf)
642 {
643      pkg_dest_list_elt_t *iter;
644      pkg_dest_t *dest;
645      pkg_vec_t *all;
646      pkg_t *pkg;
647      register int i;
648      int err;
649
650      if (conf->noaction)
651           return 0;
652      for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
653           dest = iter->data;
654           dest->status_file = fopen(dest->status_file_tmp_name, "w");
655           if (dest->status_file == NULL) {
656                fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
657                        __FUNCTION__, dest->status_file_name, strerror(errno));
658           }
659      }
660
661      all = pkg_vec_alloc();
662      pkg_hash_fetch_available(&conf->pkg_hash, all);
663
664      for(i = 0; i < all->len; i++) {
665           pkg = all->pkgs[i];
666           /* We don't need most uninstalled packages in the status file */
667           if (pkg->state_status == SS_NOT_INSTALLED
668               && (pkg->state_want == SW_UNKNOWN
669                   || pkg->state_want == SW_DEINSTALL
670                   || pkg->state_want == SW_PURGE)) {
671                continue;
672           }
673           if (!pkg) {
674             fprintf(stderr, "Null package\n");
675           }
676           if (pkg->dest == NULL) {
677                fprintf(stderr, "%s: ERROR: Can't write status for "
678                        "package %s since it has a NULL dest\n",
679                        __FUNCTION__, pkg->name);
680                continue;
681           }
682           if (pkg->dest->status_file) {
683                pkg_print_status(pkg, pkg->dest->status_file);
684           }
685      }
686
687      pkg_vec_free(all);
688
689      for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
690           dest = iter->data;
691           if (dest->status_file) {
692                err = ferror(dest->status_file);
693                fclose(dest->status_file);
694                dest->status_file = NULL;
695                if (!err) {
696                     file_move(dest->status_file_tmp_name, dest->status_file_name);
697                } else {
698                     fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
699                             "retaining old %s\n", __FUNCTION__, 
700                             dest->status_file_tmp_name, dest->status_file_name);
701                }
702           }
703      }
704
705      return 0;
706 }
707
708
709 char *root_filename_alloc(opkg_conf_t *conf, char *filename)
710 {
711      char *root_filename;
712      sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
713      return root_filename;
714 }