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