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