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},
76 static int resolve_pkg_dest_list(void)
78 nv_pair_list_elt_t *iter;
83 for (iter = nv_pair_list_first(&conf->tmp_dest_list); iter;
84 iter = nv_pair_list_next(&conf->tmp_dest_list, iter)) {
85 nv_pair = (nv_pair_t *) iter->data;
87 if (conf->offline_root) {
88 sprintf_alloc(&root_dir, "%s%s", conf->offline_root,
91 root_dir = xstrdup(nv_pair->value);
95 pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name,
96 root_dir, conf->lists_dir);
99 if (conf->default_dest == NULL)
100 conf->default_dest = dest;
102 if (conf->dest_str && !strcmp(dest->name, conf->dest_str)) {
103 conf->default_dest = dest;
104 conf->restrict_to_default_dest = 1;
108 if (conf->dest_str && !conf->restrict_to_default_dest) {
109 opkg_msg(ERROR, "Unknown dest name: `%s'.\n", conf->dest_str);
116 static int opkg_conf_set_option(const char *name, const char *value)
120 while (options[i].name) {
121 if (strcmp(options[i].name, name) == 0) {
122 switch (options[i].type) {
123 case OPKG_OPT_TYPE_BOOL:
124 if (*(int *)options[i].value) {
126 "Duplicate boolean option %s, "
127 "leaving this option on.\n",
131 *((int *const)options[i].value) = 1;
133 case OPKG_OPT_TYPE_INT:
135 if (*(int *)options[i].value) {
137 "Duplicate option %s, "
138 "using first seen value \"%d\".\n",
144 *((int *const)options[i].value) =
149 "Option %s needs an argument\n",
153 case OPKG_OPT_TYPE_STRING:
155 if (*(char **)options[i].value) {
157 "Duplicate option %s, "
158 "using first seen value \"%s\".\n",
160 *((char **)options[i].
164 *((char **const)options[i].value) =
169 "Option %s needs an argument\n",
178 opkg_msg(ERROR, "Unrecognized option: %s=%s\n", name, value);
183 opkg_conf_parse_file(const char *filename,
184 pkg_src_list_t * pkg_src_list,
185 pkg_src_list_t * dist_src_list)
190 regex_t valid_line_re, comment_re;
191 #define regmatch_size 14
192 regmatch_t regmatch[regmatch_size];
194 file = fopen(filename, "r");
196 opkg_perror(ERROR, "Failed to open %s", filename);
201 opkg_msg(INFO, "Loading conf file %s.\n", filename);
203 err = xregcomp(&comment_re,
204 "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED);
208 err = xregcomp(&valid_line_re,
209 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
210 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
211 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
212 "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
219 char *type, *name, *value, *extra;
223 line = file_read_line_alloc(file);
227 if (regexec(&comment_re, line, 0, 0, 0) == 0)
230 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) ==
232 opkg_msg(ERROR, "%s:%d: Ignoring invalid line: `%s'\n",
233 filename, line_num, line);
237 /* This has to be so ugly to deal with optional quotation marks */
238 if (regmatch[2].rm_so > 0) {
239 type = xstrndup(line + regmatch[2].rm_so,
240 regmatch[2].rm_eo - regmatch[2].rm_so);
242 type = xstrndup(line + regmatch[3].rm_so,
243 regmatch[3].rm_eo - regmatch[3].rm_so);
246 if (regmatch[5].rm_so > 0) {
247 name = xstrndup(line + regmatch[5].rm_so,
248 regmatch[5].rm_eo - regmatch[5].rm_so);
250 name = xstrndup(line + regmatch[6].rm_so,
251 regmatch[6].rm_eo - regmatch[6].rm_so);
254 if (regmatch[8].rm_so > 0) {
255 value = xstrndup(line + regmatch[8].rm_so,
256 regmatch[8].rm_eo - regmatch[8].rm_so);
258 value = xstrndup(line + regmatch[9].rm_so,
259 regmatch[9].rm_eo - regmatch[9].rm_so);
263 if (regmatch[11].rm_so > 0) {
264 if (regmatch[13].rm_so > 0
265 && regmatch[13].rm_so != regmatch[13].rm_eo)
267 xstrndup(line + regmatch[11].rm_so,
271 extra = xstrndup(line + regmatch[11].rm_so,
276 if (regmatch[13].rm_so != regmatch[13].rm_eo
277 && strncmp(type, "dist", 4) != 0) {
279 "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
280 filename, line_num, line);
283 /* We use the conf->tmp_dest_list below instead of
284 conf->pkg_dest_list because we might encounter an
285 offline_root option later and that would invalidate the
286 directories we would have computed in
287 pkg_dest_list_init. (We do a similar thing with
288 tmp_src_nv_pair_list for sake of symmetry.) */
289 if (strcmp(type, "option") == 0) {
290 opkg_conf_set_option(name, value);
291 } else if (strcmp(type, "dist") == 0) {
292 if (!nv_pair_list_find
293 ((nv_pair_list_t *) dist_src_list, name)) {
294 pkg_src_list_append(dist_src_list, name,
298 "Duplicate dist declaration (%s %s). "
299 "Skipping.\n", name, value);
301 } else if (strcmp(type, "dist/gz") == 0) {
302 if (!nv_pair_list_find
303 ((nv_pair_list_t *) dist_src_list, name)) {
304 pkg_src_list_append(dist_src_list, name,
308 "Duplicate dist declaration (%s %s). "
309 "Skipping.\n", name, value);
311 } else if (strcmp(type, "src") == 0) {
312 if (!nv_pair_list_find
313 ((nv_pair_list_t *) pkg_src_list, name)) {
314 pkg_src_list_append(pkg_src_list, name,
318 "Duplicate src declaration (%s %s). "
319 "Skipping.\n", name, value);
321 } else if (strcmp(type, "src/gz") == 0) {
322 if (!nv_pair_list_find
323 ((nv_pair_list_t *) pkg_src_list, name)) {
324 pkg_src_list_append(pkg_src_list, name,
328 "Duplicate src declaration (%s %s). "
329 "Skipping.\n", name, value);
331 } else if (strcmp(type, "dest") == 0) {
332 nv_pair_list_append(&conf->tmp_dest_list, name,
334 } else if (strcmp(type, "lists_dir") == 0) {
335 conf->lists_dir = xstrdup(value);
336 } else if (strcmp(type, "arch") == 0) {
338 "Supported arch %s priority (%s)\n",
342 "No priority given for architecture %s,"
343 "defaulting to 10\n", name);
344 value = xstrdup("10");
346 nv_pair_list_append(&conf->arch_list, name,
350 "%s:%d: Ignoring invalid line: `%s'\n",
351 filename, line_num, line);
366 regfree(&valid_line_re);
368 regfree(&comment_re);
370 if (fclose(file) == EOF) {
371 opkg_perror(ERROR, "Couldn't close %s", filename);
378 int opkg_conf_write_status_files(void)
380 pkg_dest_list_elt_t *iter;
389 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
390 dest = (pkg_dest_t *) iter->data;
392 dest->status_fp = fopen(dest->status_file_name, "w");
393 if (dest->status_fp == NULL && errno != EROFS) {
394 opkg_perror(ERROR, "Can't open status file %s",
395 dest->status_file_name);
400 all = pkg_vec_alloc();
401 pkg_hash_fetch_available(all);
403 for (i = 0; i < all->len; i++) {
405 /* We don't need most uninstalled packages in the status file */
406 if (pkg->state_status == SS_NOT_INSTALLED
407 && (pkg->state_want == SW_UNKNOWN
408 || (pkg->state_want == SW_DEINSTALL
409 && pkg->state_flag != SF_HOLD)
410 || pkg->state_want == SW_PURGE)) {
413 if (pkg->dest == NULL) {
415 "Internal error: package %s has a NULL dest\n",
419 if (pkg->dest->status_fp)
420 pkg_print_status(pkg, pkg->dest->status_fp);
425 list_for_each_entry(iter, &conf->pkg_dest_list.head, node) {
426 dest = (pkg_dest_t *) iter->data;
427 if (dest->status_fp && fclose(dest->status_fp) == EOF) {
428 opkg_perror(ERROR, "Couldn't close %s",
429 dest->status_file_name);
437 char *root_filename_alloc(char *filename)
440 sprintf_alloc(&root_filename, "%s%s",
441 (conf->offline_root ? conf->offline_root : ""), filename);
442 return root_filename;
445 static int glob_errfunc(const char *epath, int eerrno)
447 if (eerrno == ENOENT)
448 /* If leading dir does not exist, we get GLOB_NOMATCH. */
451 opkg_msg(ERROR, "glob failed for %s: %s\n", epath, strerror(eerrno));
455 int opkg_conf_init(void)
457 pkg_src_list_init(&conf->pkg_src_list);
458 pkg_src_list_init(&conf->dist_src_list);
459 pkg_dest_list_init(&conf->pkg_dest_list);
460 pkg_dest_list_init(&conf->tmp_dest_list);
461 nv_pair_list_init(&conf->arch_list);
466 int opkg_conf_load(void)
469 char *tmp, *tmp_dir_base, **tmp_val;
471 char *etc_opkg_conf_pattern;
473 conf->restrict_to_default_dest = 0;
474 conf->default_dest = NULL;
476 if (!conf->offline_root)
477 conf->offline_root = xstrdup(getenv("OFFLINE_ROOT"));
479 if (conf->conf_file) {
481 if (stat(conf->conf_file, &st) == -1) {
482 opkg_perror(ERROR, "Couldn't stat %s", conf->conf_file);
485 if (opkg_conf_parse_file(conf->conf_file,
487 &conf->dist_src_list))
491 if (conf->offline_root)
492 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf",
495 const char *conf_file_dir = getenv("OPKG_CONF_DIR");
496 if (conf_file_dir == NULL)
497 conf_file_dir = OPKG_CONF_DEFAULT_CONF_FILE_DIR;
498 sprintf_alloc(&etc_opkg_conf_pattern, "%s/*.conf",
502 memset(&globbuf, 0, sizeof(globbuf));
503 glob_ret = glob(etc_opkg_conf_pattern, 0, glob_errfunc, &globbuf);
504 if (glob_ret && glob_ret != GLOB_NOMATCH) {
505 free(etc_opkg_conf_pattern);
510 free(etc_opkg_conf_pattern);
512 for (i = 0; i < globbuf.gl_pathc; i++) {
513 if (globbuf.gl_pathv[i])
514 if (conf->conf_file &&
515 !strcmp(conf->conf_file, globbuf.gl_pathv[i]))
517 if (opkg_conf_parse_file(globbuf.gl_pathv[i],
519 &conf->dist_src_list) < 0) {
527 if (conf->offline_root)
528 sprintf_alloc(&lock_file, "%s/%s", conf->offline_root,
531 sprintf_alloc(&lock_file, "%s", OPKGLOCKFILE);
533 lock_fd = creat(lock_file, S_IRUSR | S_IWUSR | S_IRGRP);
535 opkg_perror(ERROR, "Could not create lock file %s", lock_file);
539 if (lockf(lock_fd, F_TLOCK, (off_t) 0) == -1) {
540 opkg_perror(ERROR, "Could not lock %s", lock_file);
541 if (close(lock_fd) == -1)
542 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
549 tmp_dir_base = conf->tmp_dir;
551 tmp_dir_base = getenv("TMPDIR");
553 sprintf_alloc(&tmp, "%s/%s",
554 tmp_dir_base ? tmp_dir_base :
555 OPKG_CONF_DEFAULT_TMP_DIR_BASE, OPKG_CONF_TMP_DIR_SUFFIX);
558 conf->tmp_dir = mkdtemp(tmp);
559 if (conf->tmp_dir == NULL) {
560 opkg_perror(ERROR, "Creating temp dir %s failed", tmp);
565 hash_table_init("file-hash", &conf->file_hash,
566 OPKG_CONF_DEFAULT_HASH_LEN);
567 hash_table_init("obs-file-hash", &conf->obs_file_hash,
568 OPKG_CONF_DEFAULT_HASH_LEN / 16);
570 if (conf->lists_dir == NULL)
571 conf->lists_dir = xstrdup(OPKG_CONF_LISTS_DIR);
573 if (conf->offline_root) {
574 sprintf_alloc(&tmp, "%s/%s", conf->offline_root,
576 free(conf->lists_dir);
577 conf->lists_dir = tmp;
580 /* if no architectures were defined, then default all, noarch, and host architecture */
581 if (nv_pair_list_empty(&conf->arch_list)) {
582 nv_pair_list_append(&conf->arch_list, "all", "1");
583 nv_pair_list_append(&conf->arch_list, "noarch", "1");
584 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
587 /* Even if there is no conf file, we'll need at least one dest. */
588 if (nv_pair_list_empty(&conf->tmp_dest_list)) {
589 nv_pair_list_append(&conf->tmp_dest_list,
590 OPKG_CONF_DEFAULT_DEST_NAME,
591 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
594 if (resolve_pkg_dest_list())
597 nv_pair_list_deinit(&conf->tmp_dest_list);
602 free(conf->lists_dir);
605 hash_table_deinit(&conf->file_hash);
606 hash_table_deinit(&conf->obs_file_hash);
608 if (rmdir(conf->tmp_dir) == -1)
609 opkg_perror(ERROR, "Couldn't remove dir %s", conf->tmp_dir);
611 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
612 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
614 if (close(lock_fd) == -1)
615 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
617 if (unlink(lock_file) == -1)
618 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);
625 pkg_src_list_deinit(&conf->pkg_src_list);
626 pkg_src_list_deinit(&conf->dist_src_list);
627 pkg_dest_list_deinit(&conf->pkg_dest_list);
628 nv_pair_list_deinit(&conf->arch_list);
630 for (i = 0; options[i].name; i++) {
631 if (options[i].type == OPKG_OPT_TYPE_STRING) {
632 tmp_val = (char **)options[i].value;
640 nv_pair_list_deinit(&conf->tmp_dest_list);
642 free(conf->dest_str);
644 free(conf->conf_file);
649 void opkg_conf_deinit(void)
658 free(conf->lists_dir);
661 free(conf->dest_str);
664 free(conf->conf_file);
666 pkg_src_list_deinit(&conf->pkg_src_list);
667 pkg_src_list_deinit(&conf->dist_src_list);
668 pkg_dest_list_deinit(&conf->pkg_dest_list);
669 nv_pair_list_deinit(&conf->arch_list);
671 for (i = 0; options[i].name; i++) {
672 if (options[i].type == OPKG_OPT_TYPE_STRING) {
673 tmp = (char **)options[i].value;
681 if (conf->verbosity >= DEBUG) {
682 hash_print_stats(&conf->pkg_hash);
683 hash_print_stats(&conf->file_hash);
684 hash_print_stats(&conf->obs_file_hash);
688 hash_table_deinit(&conf->file_hash);
689 hash_table_deinit(&conf->obs_file_hash);
692 if (lockf(lock_fd, F_ULOCK, (off_t) 0) == -1)
693 opkg_perror(ERROR, "Couldn't unlock %s", lock_file);
695 if (close(lock_fd) == -1)
696 opkg_perror(ERROR, "Couldn't close descriptor %d (%s)",
702 if (unlink(lock_file) == -1)
703 opkg_perror(ERROR, "Couldn't unlink %s", lock_file);