1 /* opkg_conf.c - the opkg package management system
3 Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
6 Copyright (C) 2001 University of Southern California
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2, or (at
11 your option) any later version.
13 This program is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
20 #include <sys/types.h>
26 #include "opkg_conf.h"
30 #include "sprintf_alloc.h"
31 #include "opkg_message.h"
32 #include "file_util.h"
33 #include "opkg_defines.h"
34 #include "libbb/libbb.h"
37 static char *lock_file = NULL;
39 static opkg_conf_t _conf;
40 opkg_conf_t *conf = &_conf;
45 opkg_option_t options[] = {
46 {"cache", OPKG_OPT_TYPE_STRING, &_conf.cache},
47 {"force_defaults", OPKG_OPT_TYPE_BOOL, &_conf.force_defaults},
48 {"force_maintainer", OPKG_OPT_TYPE_BOOL, &_conf.force_maintainer},
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 {"force_postinstall", OPKG_OPT_TYPE_BOOL, &_conf.force_postinstall},
55 {"force_checksum", OPKG_OPT_TYPE_BOOL, &_conf.force_checksum},
56 {"check_signature", OPKG_OPT_TYPE_BOOL, &_conf.check_signature},
57 {"ftp_proxy", OPKG_OPT_TYPE_STRING, &_conf.ftp_proxy},
58 {"http_proxy", OPKG_OPT_TYPE_STRING, &_conf.http_proxy},
59 {"no_proxy", OPKG_OPT_TYPE_STRING, &_conf.no_proxy},
60 {"test", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
61 {"noaction", OPKG_OPT_TYPE_BOOL, &_conf.noaction},
62 {"download_only", OPKG_OPT_TYPE_BOOL, &_conf.download_only},
63 {"nodeps", OPKG_OPT_TYPE_BOOL, &_conf.nodeps},
64 {"nocase", OPKG_OPT_TYPE_BOOL, &_conf.nocase},
65 {"offline_root", OPKG_OPT_TYPE_STRING, &_conf.offline_root},
66 {"overlay_root", OPKG_OPT_TYPE_STRING, &_conf.overlay_root},
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 {"size", OPKG_OPT_TYPE_BOOL, &_conf.size},
71 {"tmp_dir", OPKG_OPT_TYPE_STRING, &_conf.tmp_dir},
72 {"verbosity", OPKG_OPT_TYPE_INT, &_conf.verbosity},
73 #if defined(HAVE_OPENSSL)
74 {"signature_ca_file", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_file},
75 {"signature_ca_path", OPKG_OPT_TYPE_STRING, &_conf.signature_ca_path},
80 static int resolve_pkg_dest_list(void)
82 nv_pair_list_elt_t *iter;
87 for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter;
88 iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) {
89 nv_pair = (nv_pair_t *) iter->data;
91 if (conf->offline_root) {
92 sprintf_alloc(&root_dir, "%s%s", conf->offline_root,
95 root_dir = xstrdup(nv_pair->value);
99 pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name,
100 root_dir, conf->lists_dir);
103 if (conf->default_dest == NULL)
104 conf->default_dest = dest;
106 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
107 conf->default_dest = dest;
108 conf->restrict_to_default_dest = 1;
112 if (conf->dest_str && !conf->restrict_to_default_dest) {
113 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
120 static int opkg_conf_set_option(const char *name, const char *value)
124 while (options[i].name) {
125 if (strcmp(options[i].name, name) == 0) {
126 switch (options[i].type) {
127 case OPKG_OPT_TYPE_BOOL:
128 if (*(int *)options[i].value) {
130 "Duplicate boolean option %s, "
131 "leaving this option on.\n",
135 *((int *const)options[i].value) = 1;
137 case OPKG_OPT_TYPE_INT:
139 if (*(int *)options[i].value) {
141 "Duplicate option %s, "
142 "using first seen value \"%d\".\n",
148 *((int *const)options[i].value) =
153 "Option %s needs an argument\n",
157 case OPKG_OPT_TYPE_STRING:
159 if (*(char **)options[i].value) {
161 "Duplicate option %s, "
162 "using first seen value \"%s\".\n",
164 *((char **)options[i].
168 *((char **const)options[i].value) =
173 "Option %s needs an argument\n",
182 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
187 opkg_conf_parse_file(const char *filename,
188 pkg_src_list_t * pkg_src_list,
189 pkg_src_list_t * dist_src_list)
194 regex_t valid_line_re, comment_re;
195 #define regmatch_size 14
196 regmatch_t regmatch[regmatch_size];
198 file = fopen(filename, "r");
200 opkg_perror(ERROR, "Failed to open %s", filename);
205 opkg_msg(INFO, "Loading conf file %s.\n", filename);
207 err = xregcomp(&comment_re,
208 "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED);
212 err = xregcomp(&valid_line_re,
213 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
214 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
215 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
216 "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
223 char *type, *name, *value, *extra;
227 line = file_read_line_alloc(file);
231 if (regexec(&comment_re, line, 0, 0, 0) == 0)
234 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) ==
236 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
237 filename, line_num, line);
241 /* This has to be so ugly to deal with optional quotation marks */
242 if (regmatch[2].rm_so > 0) {
243 type = xstrndup(line + regmatch[2].rm_so,
244 regmatch[2].rm_eo - regmatch[2].rm_so);
246 type = xstrndup(line + regmatch[3].rm_so,
247 regmatch[3].rm_eo - regmatch[3].rm_so);
250 if (regmatch[5].rm_so > 0) {
251 name = xstrndup(line + regmatch[5].rm_so,
252 regmatch[5].rm_eo - regmatch[5].rm_so);
254 name = xstrndup(line + regmatch[6].rm_so,
255 regmatch[6].rm_eo - regmatch[6].rm_so);
258 if (regmatch[8].rm_so > 0) {
259 value = xstrndup(line + regmatch[8].rm_so,
260 regmatch[8].rm_eo - regmatch[8].rm_so);
262 value = xstrndup(line + regmatch[9].rm_so,
263 regmatch[9].rm_eo - regmatch[9].rm_so);
267 if (regmatch[11].rm_so > 0) {
268 if (regmatch[13].rm_so > 0
269 && regmatch[13].rm_so != regmatch[13].rm_eo)
271 xstrndup(line + regmatch[11].rm_so,
275 extra = xstrndup(line + regmatch[11].rm_so,
280 if (regmatch[13].rm_so != regmatch[13].rm_eo
281 && strncmp(type, "dist", 4) != 0) {
283 "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
284 filename, line_num, line);
287 /* We use the conf->tmp_dest_list below instead of
288 conf->pkg_dest_list because we might encounter an
289 offline_root option later and that would invalidate the
290 directories we would have computed in
291 pkg_dest_list_init. (We do a similar thing with
292 tmp_src_nv_pair_list for sake of symmetry.) */
293 if (strcmp(type, "option") == 0) {
294 opkg_conf_set_option(name, value);
295 } else if (strcmp(type, "dist") == 0) {
296 if (!nv_pair_list_find
297 ((nv_pair_list_t *) dist_src_list, name)) {
298 pkg_src_list_append(dist_src_list, name,
302 "Duplicate dist declaration (%s %s). "
303 "Skipping.\n", name, value);
305 } else if (strcmp(type, "dist/gz") == 0) {
306 if (!nv_pair_list_find
307 ((nv_pair_list_t *) dist_src_list, name)) {
308 pkg_src_list_append(dist_src_list, name,
312 "Duplicate dist declaration (%s %s). "
313 "Skipping.\n", name, value);
315 } else if (strcmp(type, "src") == 0) {
316 if (!nv_pair_list_find
317 ((nv_pair_list_t *) pkg_src_list, name)) {
318 pkg_src_list_append(pkg_src_list, name,
322 "Duplicate src declaration (%s %s). "
323 "Skipping.\n", name, value);
325 } else if (strcmp(type, "src/gz") == 0) {
326 if (!nv_pair_list_find
327 ((nv_pair_list_t *) pkg_src_list, name)) {
328 pkg_src_list_append(pkg_src_list, name,
332 "Duplicate src declaration (%s %s). "
333 "Skipping.\n", name, value);
335 } else if (strcmp(type, "dest") == 0) {
336 nv_pair_list_append(&conf->tmp_dest_list, name,
338 } else if (strcmp(type, "lists_dir") == 0) {
339 conf->lists_dir = xstrdup(value);
340 } else if (strcmp(type, "arch") == 0) {
342 "Supported arch %s priority (%s)\n",
346 "No priority given for architecture %s,"
347 "defaulting to 10\n", name);
348 value = xstrdup("10");
350 nv_pair_list_append(&conf->arch_list, name,
354 "%s:%d: Ignoring invalid line: `%s'\n",
355 filename, line_num, line);
370 regfree(&valid_line_re);
372 regfree(&comment_re);
374 if (fclose(file) == EOF) {
375 opkg_perror(ERROR, "Couldn't close %s", filename);
382 int opkg_conf_write_status_files(void)
384 pkg_dest_list_elt_t *iter;
393 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
394 dest = (pkg_dest_t *) iter->data;
396 dest->status_fp = fopen(dest->status_file_name, "w");
397 if (dest->status_fp == NULL && errno != EROFS) {
398 opkg_perror(ERROR, "Can't open status file %s",
399 dest->status_file_name);
404 all = pkg_vec_alloc();
405 pkg_hash_fetch_available(all);
407 for (i = 0; i < all->len; i++) {
409 /* We don't need most uninstalled packages in the status file */
410 if (pkg->state_status == SS_NOT_INSTALLED
411 && (pkg->state_want == SW_UNKNOWN
412 || (pkg->state_want == SW_DEINSTALL
413 && pkg->state_flag != SF_HOLD)
414 || pkg->state_want == SW_PURGE)) {
417 if (pkg->dest == NULL) {
419 "Internal error: package %s has a NULL dest\n",
423 if (pkg->dest->status_fp)
424 pkg_print_status(pkg, pkg->dest->status_fp);
429 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
430 dest = (pkg_dest_t *) iter->data;
431 if (dest->status_fp && fclose(dest->status_fp) == EOF) {
432 opkg_perror(ERROR, "Couldn't close %s",
433 dest->status_file_name);
441 char *root_filename_alloc(char *filename)
444 sprintf_alloc(&root_filename, "%s%s",
445 (conf->offline_root ? conf->offline_root : ""), filename);
446 return root_filename;
449 static int glob_errfunc(const char *epath, int eerrno)
451 if (eerrno == ENOENT)
452 /* If leading dir does not exist, we get GLOB_NOMATCH. */
455 opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
459 int opkg_conf_init(void)
461 pkg_src_list_init(&conf->pkg_src_list);
462 pkg_src_list_init(&conf->dist_src_list);
463 pkg_dest_list_init(&conf->pkg_dest_list);
464 pkg_dest_list_init(&conf->tmp_dest_list);
465 nv_pair_list_init(&conf->arch_list);
470 int opkg_conf_load(void)
473 char *tmp, *tmp_dir_base, **tmp_val;
475 char *etc_opkg_conf_pattern;
477 conf->restrict_to_default_dest = 0;
478 conf->default_dest = NULL;
480 if (!conf->offline_root)
481 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
483 if (conf->conf_file) {
485 if (stat(conf->conf_file, &st) == -1) {
486 opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
489 if (opkg_conf_parse_file(conf->conf_file,
491 &conf->dist_src_list))
495 if (conf->offline_root)
496 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf",
499 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
500 if (conf_file_dir == NULL)
501 conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
502 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf",
506 memset(&globbuf, 0, sizeof(globbuf));
507 glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
508 if (glob_ret && glob_ret != GLOB_NOMATCH) {
509 free(etc_opkg_conf_pattern);
514 free(etc_opkg_conf_pattern);
516 for (i = 0; i < globbuf.gl_pathc; i++) {
517 if (globbuf.gl_pathv[i])
518 if (conf->conf_file &&
519 !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
521 if (opkg_conf_parse_file(globbuf.gl_pathv[i],
523 &conf->dist_src_list) < 0) {
531 if (conf->offline_root)
532 sprintf_alloc(&lock_file, "%s/%s", conf->offline_root,
535 sprintf_alloc(&lock_file, "%s", OPKGLOCKFILE);
537 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
539 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
543 if (lockf(lock_fd, F_TLOCK, (off_t) 0) == -1) {
544 opkg_perror(ERROR, "Could not lock %s", lock_file);
545 if (close(lock_fd) == -1)
546 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
553 tmp_dir_base = conf->tmp_dir;
555 tmp_dir_base = getenv("TMPDIR");
557 sprintf_alloc(&tmp, "%s/%s",
558 tmp_dir_base ? tmp_dir_base :
559 OPKG_CONF_DEFAULT_TMP_DIR_BASE, OPKG_CONF_TMP_DIR_SUFFIX);
562 conf->tmp_dir = mkdtemp(tmp);
563 if (conf->tmp_dir == NULL) {
564 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
569 hash_table_init("file-hash", &conf->file_hash,
570 OPKG_CONF_DEFAULT_HASH_LEN);
571 hash_table_init("obs-file-hash", &conf->obs_file_hash,
572 OPKG_CONF_DEFAULT_HASH_LEN / 16);
574 if (conf->lists_dir == NULL)
575 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
577 if (conf->offline_root) {
578 sprintf_alloc(&tmp, "%s/%s", conf->offline_root,
580 free(conf->lists_dir);
581 conf->lists_dir = tmp;
584 /* if no architectures were defined, then default all, noarch, and host architecture */
585 if (nv_pair_list_empty(&conf->arch_list)) {
586 nv_pair_list_append(&conf->arch_list, "all", "1");
587 nv_pair_list_append(&conf->arch_list, "noarch", "1");
588 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
591 /* Even if there is no conf file, we'll need at least one dest. */
592 if (nv_pair_list_empty(&conf->tmp_dest_list)) {
593 nv_pair_list_append(&conf->tmp_dest_list,
594 OPKG_CONF_DEFAULT_DEST_NAME,
595 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
598 if (resolve_pkg_dest_list())
601 nv_pair_list_deinit(&conf->tmp_dest_list);
606 free(conf->lists_dir);
609 hash_table_deinit(&conf->file_hash);
610 hash_table_deinit(&conf->obs_file_hash);
612 if (rmdir(conf->tmp_dir) == -1)
613 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
615 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
616 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
618 if (close(lock_fd) == -1)
619 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
621 if (unlink(lock_file) == -1)
622 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
629 pkg_src_list_deinit(&conf->pkg_src_list);
630 pkg_src_list_deinit(&conf->dist_src_list);
631 pkg_dest_list_deinit(&conf->pkg_dest_list);
632 nv_pair_list_deinit(&conf->arch_list);
634 for (i = 0; options[i].name; i++) {
635 if (options[i].type == OPKG_OPT_TYPE_STRING) {
636 tmp_val = (char **)options[i].value;
644 nv_pair_list_deinit(&conf->tmp_dest_list);
646 free(conf->dest_str);
648 free(conf->conf_file);
653 void opkg_conf_deinit(void)
662 free(conf->lists_dir);
665 free(conf->dest_str);
668 free(conf->conf_file);
670 pkg_src_list_deinit(&conf->pkg_src_list);
671 pkg_src_list_deinit(&conf->dist_src_list);
672 pkg_dest_list_deinit(&conf->pkg_dest_list);
673 nv_pair_list_deinit(&conf->arch_list);
675 for (i = 0; options[i].name; i++) {
676 if (options[i].type == OPKG_OPT_TYPE_STRING) {
677 tmp = (char **)options[i].value;
685 if (conf->verbosity >= DEBUG) {
686 hash_print_stats(&conf->pkg_hash);
687 hash_print_stats(&conf->file_hash);
688 hash_print_stats(&conf->obs_file_hash);
692 hash_table_deinit(&conf->file_hash);
693 hash_table_deinit(&conf->obs_file_hash);
696 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
697 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
699 if (close(lock_fd) == -1)
700 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
706 if (unlink(lock_file) == -1)
707 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);