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