1 /* opkg_conf.c - the itsy package management system
5 Copyright (C) 2001 University of Southern California
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.
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.
19 #include "opkg_conf.h"
22 #include "sprintf_alloc.h"
23 #include "opkg_conf.h"
24 #include "opkg_message.h"
25 #include "file_util.h"
29 #include "opkg_defines.h"
32 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
33 pkg_src_list_t *pkg_src_list,
34 nv_pair_list_t *tmp_dest_nv_pair_list,
35 char **tmp_lists_dir);
36 static int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options);
37 static int opkg_conf_set_option(const opkg_option_t *options,
38 const char *name, const char *value);
39 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
40 const char *default_dest_name);
41 static int set_and_load_pkg_src_list(opkg_conf_t *conf,
42 pkg_src_list_t *nv_pair_list);
43 static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
44 nv_pair_list_t *nv_pair_list, char * lists_dir);
46 int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
48 opkg_option_t tmp[] = {
49 { "force_defaults", OPKG_OPT_TYPE_BOOL, &conf->force_defaults },
50 { "force_depends", OPKG_OPT_TYPE_BOOL, &conf->force_depends },
51 { "force_overwrite", OPKG_OPT_TYPE_BOOL, &conf->force_overwrite },
52 { "force_downgrade", OPKG_OPT_TYPE_BOOL, &conf->force_downgrade },
53 { "force_reinstall", OPKG_OPT_TYPE_BOOL, &conf->force_reinstall },
54 { "force_space", OPKG_OPT_TYPE_BOOL, &conf->force_space },
55 { "ftp_proxy", OPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
56 { "http_proxy", OPKG_OPT_TYPE_STRING, &conf->http_proxy },
57 { "multiple_providers", OPKG_OPT_TYPE_BOOL, &conf->multiple_providers },
58 { "no_proxy", OPKG_OPT_TYPE_STRING, &conf->no_proxy },
59 { "test", OPKG_OPT_TYPE_INT, &conf->noaction },
60 { "noaction", OPKG_OPT_TYPE_INT, &conf->noaction },
61 { "nodeps", OPKG_OPT_TYPE_BOOL, &conf->nodeps },
62 { "offline_root", OPKG_OPT_TYPE_STRING, &conf->offline_root },
63 { "offline_root_post_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
64 { "offline_root_pre_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
65 { "proxy_passwd", OPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
66 { "proxy_user", OPKG_OPT_TYPE_STRING, &conf->proxy_user },
67 { "query-all", OPKG_OPT_TYPE_BOOL, &conf->query_all },
68 { "verbose-wget", OPKG_OPT_TYPE_BOOL, &conf->verbose_wget },
69 { "verbosity", OPKG_OPT_TYPE_BOOL, &conf->verbosity },
73 *options = (opkg_option_t *)malloc(sizeof(tmp));
74 if ( options == NULL ){
75 fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
79 memcpy(*options, tmp, sizeof(tmp));
83 static void opkg_conf_override_string(char **conf_str, char *arg_str)
89 *conf_str = strdup(arg_str);
93 static void opkg_conf_free_string(char **conf_str)
101 int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
105 nv_pair_list_t tmp_dest_nv_pair_list;
106 char * lists_dir =NULL;
108 char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
109 char *pending_dir =NULL;
111 memset(conf, 0, sizeof(opkg_conf_t));
113 pkg_src_list_init(&conf->pkg_src_list);
115 nv_pair_list_init(&tmp_dest_nv_pair_list);
116 pkg_dest_list_init(&conf->pkg_dest_list);
118 nv_pair_list_init(&conf->arch_list);
120 conf->restrict_to_default_dest = 0;
121 conf->default_dest = NULL;
125 tmp_dir_base = args->tmp_dir;
127 tmp_dir_base = getenv("TMPDIR");
128 sprintf_alloc(&conf->tmp_dir, "%s/%s",
129 tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
130 OPKG_CONF_TMP_DIR_SUFFIX);
131 conf->tmp_dir = mkdtemp(conf->tmp_dir);
132 if (conf->tmp_dir == NULL) {
133 fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
134 __FUNCTION__, conf->tmp_dir, strerror(errno));
138 conf->force_depends = 0;
139 conf->force_defaults = 0;
140 conf->force_overwrite = 0;
141 conf->force_downgrade = 0;
142 conf->force_reinstall = 0;
143 conf->force_space = 0;
144 conf->force_removal_of_essential_packages = 0;
145 conf->force_removal_of_dependent_packages = 0;
147 conf->verbose_wget = 0;
148 conf->offline_root = NULL;
149 conf->offline_root_pre_script_cmd = NULL;
150 conf->offline_root_post_script_cmd = NULL;
151 conf->multiple_providers = 0;
155 conf->http_proxy = NULL;
156 conf->ftp_proxy = NULL;
157 conf->no_proxy = NULL;
158 conf->proxy_user = NULL;
159 conf->proxy_passwd = NULL;
161 pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN);
162 hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
163 hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
164 lists_dir=(char *)malloc(1);
166 if (args->conf_file) {
167 struct stat stat_buf;
168 err = stat(args->conf_file, &stat_buf);
170 if (opkg_conf_parse_file(conf, args->conf_file,
171 &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
172 /* Memory leakage from opkg_conf_parse-file */
178 /* if (!lists_dir ){*/
179 if (strlen(lists_dir)<=1 ){
180 lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
181 sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
184 if (args->offline_root) {
185 char *tmp = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
186 sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
191 pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
192 snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
194 conf->lists_dir = strdup(lists_dir);
195 conf->pending_dir = strdup(pending_dir);
197 if (args->offline_root)
198 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", args->offline_root);
199 memset(&globbuf, 0, sizeof(globbuf));
200 err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
203 for (i = 0; i < globbuf.gl_pathc; i++) {
204 if (globbuf.gl_pathv[i])
205 if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i],
206 &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
207 /* Memory leakage from opkg_conf_parse-file */
214 /* if no architectures were defined, then default all, noarch, and host architecture */
215 if (nv_pair_list_empty(&conf->arch_list)) {
216 nv_pair_list_append(&conf->arch_list, "all", "1");
217 nv_pair_list_append(&conf->arch_list, "noarch", "1");
218 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
221 /* Even if there is no conf file, we'll need at least one dest. */
222 if (tmp_dest_nv_pair_list.head == NULL) {
223 nv_pair_list_append(&tmp_dest_nv_pair_list,
224 OPKG_CONF_DEFAULT_DEST_NAME,
225 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
228 /* After parsing the file, set options from command-line, (so that
229 command-line arguments take precedence) */
230 /* XXX: CLEANUP: The interaction between args.c and opkg_conf.c
231 really needs to be cleaned up. There is so much duplication
232 right now it is ridiculous. Maybe opkg_conf_t should just save
233 a pointer to args_t (which could then not be freed), rather
234 than duplicating every field here? */
235 if (args->autoremove) {
236 conf->autoremove = 1;
238 if (args->force_depends) {
239 conf->force_depends = 1;
241 if (args->force_defaults) {
242 conf->force_defaults = 1;
244 if (args->force_overwrite) {
245 conf->force_overwrite = 1;
247 if (args->force_downgrade) {
248 conf->force_downgrade = 1;
250 if (args->force_reinstall) {
251 conf->force_reinstall = 1;
253 if (args->force_removal_of_dependent_packages) {
254 conf->force_removal_of_dependent_packages = 1;
256 if (args->force_removal_of_essential_packages) {
257 conf->force_removal_of_essential_packages = 1;
262 if (args->noaction) {
265 if (args->query_all) {
268 if (args->verbose_wget) {
269 conf->verbose_wget = 1;
271 if (args->multiple_providers) {
272 conf->multiple_providers = 1;
274 if (args->verbosity != conf->verbosity) {
275 conf->verbosity = args->verbosity;
278 opkg_conf_override_string(&conf->offline_root,
280 opkg_conf_override_string(&conf->offline_root_pre_script_cmd,
281 args->offline_root_pre_script_cmd);
282 opkg_conf_override_string(&conf->offline_root_post_script_cmd,
283 args->offline_root_post_script_cmd);
285 /* Pigi: added a flag to disable the checking of structures if the command does not need to
286 read anything from there.
288 if ( !(args->nocheckfordirorfile)){
289 /* need to run load the source list before dest list -Jamey */
290 if ( !(args->noreadfeedsfile))
291 set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
293 /* Now that we have resolved conf->offline_root, we can commit to
294 the directory names for the dests and load in all the package
296 set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
299 err = opkg_conf_set_default_dest(conf, args->dest);
305 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
312 void opkg_conf_deinit(opkg_conf_t *conf)
314 #ifdef OPKG_DEBUG_NO_TMP_CLEANUP
316 fprintf(stderr, "%s: Not cleaning up %s since opkg compiled "
317 "with OPKG_DEBUG_NO_TMP_CLEANUP\n",
318 __FUNCTION__, conf->tmp_dir);
322 err = rmdir(conf->tmp_dir);
324 if (errno == ENOTEMPTY) {
326 sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
331 fprintf(stderr, "WARNING: Unable to remove temporary directory: %s: %s\n", conf->tmp_dir, strerror(errno));
333 #endif /* OPKG_DEBUG_NO_TMP_CLEANUP */
335 free(conf->tmp_dir); /*XXX*/
337 pkg_src_list_deinit(&conf->pkg_src_list);
338 pkg_dest_list_deinit(&conf->pkg_dest_list);
339 nv_pair_list_deinit(&conf->arch_list);
341 pkg_hash_deinit(&conf->pkg_hash);
342 if (&conf->file_hash)
343 hash_table_deinit(&conf->file_hash);
344 if (&conf->obs_file_hash)
345 hash_table_deinit(&conf->obs_file_hash);
347 opkg_conf_free_string(&conf->offline_root);
348 opkg_conf_free_string(&conf->offline_root_pre_script_cmd);
349 opkg_conf_free_string(&conf->offline_root_post_script_cmd);
351 if (conf->verbosity > 1) {
353 hash_table_t *hashes[] = {
356 &conf->obs_file_hash };
357 for (i = 0; i < 3; i++) {
358 hash_table_t *hash = hashes[i];
362 for (j = 0; j < hash->n_entries; j++) {
364 hash_entry_t *e = &hash->entries[j];
367 while (e && e->key) {
374 opkg_message(conf, OPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n",
375 hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
376 hash_table_deinit(hash);
381 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
382 const char *default_dest_name)
384 pkg_dest_list_elt_t *iter;
387 for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
389 if (strcmp(dest->name, default_dest_name) == 0) {
390 conf->default_dest = dest;
391 conf->restrict_to_default_dest = 1;
396 fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
401 static int set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
403 pkg_src_list_elt_t *iter;
407 for (iter = pkg_src_list->head; iter; iter = iter->next) {
413 sprintf_alloc(&list_file, "%s/%s",
414 conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir,
417 if (file_exists(list_file)) {
418 pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
426 static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
428 nv_pair_list_elt_t *iter;
433 for (iter = nv_pair_list->head; iter; iter = iter->next) {
434 nv_pair = iter->data;
436 if (conf->offline_root) {
437 sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
439 root_dir = strdup(nv_pair->value);
441 dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
446 if (conf->default_dest == NULL) {
447 conf->default_dest = dest;
449 if (file_exists(dest->status_file_name)) {
450 pkg_hash_add_from_file(conf, dest->status_file_name,
458 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
459 pkg_src_list_t *pkg_src_list,
460 nv_pair_list_t *tmp_dest_nv_pair_list,
464 opkg_option_t * options;
465 FILE *file = fopen(filename, "r");
466 regex_t valid_line_re, comment_re;
467 #define regmatch_size 12
468 regmatch_t regmatch[regmatch_size];
470 if (opkg_init_options_array(conf, &options)<0)
474 fprintf(stderr, "%s: failed to open %s: %s\n",
475 __FUNCTION__, filename, strerror(errno));
479 opkg_message(conf, OPKG_NOTICE, "loading conf file %s\n", filename);
481 err = xregcomp(&comment_re,
482 "^[[:space:]]*(#.*|[[:space:]]*)$",
488 err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
497 char *type, *name, *value, *extra;
499 line = file_read_line_alloc(file);
507 if (regexec(&comment_re, line, 0, 0, 0) == 0) {
511 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
513 fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
514 filename, line_num, line);
518 /* This has to be so ugly to deal with optional quotation marks */
519 if (regmatch[2].rm_so > 0) {
520 type = strndup(line + regmatch[2].rm_so,
521 regmatch[2].rm_eo - regmatch[2].rm_so);
523 type = strndup(line + regmatch[3].rm_so,
524 regmatch[3].rm_eo - regmatch[3].rm_so);
526 if (regmatch[5].rm_so > 0) {
527 name = strndup(line + regmatch[5].rm_so,
528 regmatch[5].rm_eo - regmatch[5].rm_so);
530 name = strndup(line + regmatch[6].rm_so,
531 regmatch[6].rm_eo - regmatch[6].rm_so);
533 if (regmatch[8].rm_so > 0) {
534 value = strndup(line + regmatch[8].rm_so,
535 regmatch[8].rm_eo - regmatch[8].rm_so);
537 value = strndup(line + regmatch[9].rm_so,
538 regmatch[9].rm_eo - regmatch[9].rm_so);
541 if (regmatch[11].rm_so > 0) {
542 extra = strndup (line + regmatch[11].rm_so,
543 regmatch[11].rm_eo - regmatch[11].rm_so);
546 /* We use the tmp_dest_nv_pair_list below instead of
547 conf->pkg_dest_list because we might encounter an
548 offline_root option later and that would invalidate the
549 directories we would have computed in
550 pkg_dest_list_init. (We do a similar thing with
551 tmp_src_nv_pair_list for sake of symmetry.) */
552 if (strcmp(type, "option") == 0) {
553 opkg_conf_set_option(options, name, value);
554 } else if (strcmp(type, "src") == 0) {
555 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
556 pkg_src_list_append (pkg_src_list, name, value, extra, 0);
558 opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
561 } else if (strcmp(type, "src/gz") == 0) {
562 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
563 pkg_src_list_append (pkg_src_list, name, value, extra, 1);
565 opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
568 } else if (strcmp(type, "dest") == 0) {
569 nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
570 } else if (strcmp(type, "lists_dir") == 0) {
571 *lists_dir = realloc(*lists_dir,strlen(value)+1);
572 if (*lists_dir == NULL) {
573 opkg_message(conf, OPKG_ERROR, "ERROR: Not enough memory\n");
577 sprintf (*lists_dir,"%s",value);
578 } else if (strcmp(type, "arch") == 0) {
579 opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value);
581 opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
582 value = strdup("10");
584 nv_pair_list_append(&conf->arch_list, strdup(name), strdup(value));
586 fprintf(stderr, "WARNING: Ignoring unknown configuration "
587 "parameter: %s %s %s\n", type, name, value);
603 regfree(&comment_re);
604 regfree(&valid_line_re);
610 static int opkg_conf_set_option(const opkg_option_t *options,
611 const char *name, const char *value)
614 while (options[i].name) {
615 if (strcmp(options[i].name, name) == 0) {
616 switch (options[i].type) {
617 case OPKG_OPT_TYPE_BOOL:
618 *((int *)options[i].value) = 1;
620 case OPKG_OPT_TYPE_INT:
622 *((int *)options[i].value) = atoi(value);
625 printf("%s: Option %s need an argument\n",
629 case OPKG_OPT_TYPE_STRING:
631 *((char **)options[i].value) = strdup(value);
634 printf("%s: Option %s need an argument\n",
643 fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
644 __FUNCTION__, name, value);
648 int opkg_conf_write_status_files(opkg_conf_t *conf)
650 pkg_dest_list_elt_t *iter;
659 for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
661 dest->status_file = fopen(dest->status_file_tmp_name, "w");
662 if (dest->status_file == NULL) {
663 fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
664 __FUNCTION__, dest->status_file_name, strerror(errno));
668 all = pkg_vec_alloc();
669 pkg_hash_fetch_available(&conf->pkg_hash, all);
671 for(i = 0; i < all->len; i++) {
673 /* We don't need most uninstalled packages in the status file */
674 if (pkg->state_status == SS_NOT_INSTALLED
675 && (pkg->state_want == SW_UNKNOWN
676 || pkg->state_want == SW_DEINSTALL
677 || pkg->state_want == SW_PURGE)) {
681 fprintf(stderr, "Null package\n");
683 if (pkg->dest == NULL) {
684 fprintf(stderr, "%s: ERROR: Can't write status for "
685 "package %s since it has a NULL dest\n",
686 __FUNCTION__, pkg->name);
689 if (pkg->dest->status_file) {
690 pkg_print_status(pkg, pkg->dest->status_file);
696 for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
698 if (dest->status_file) {
699 err = ferror(dest->status_file);
700 fclose(dest->status_file);
701 dest->status_file = NULL;
703 file_move(dest->status_file_tmp_name, dest->status_file_name);
705 fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
706 "retaining old %s\n", __FUNCTION__,
707 dest->status_file_tmp_name, dest->status_file_name);
716 char *root_filename_alloc(opkg_conf_t *conf, char *filename)
719 sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
720 return root_filename;