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