1 /* opkg_conf.c - the opkg 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_conf_set_option(const opkg_option_t *options,
37 const char *name, const char *value);
38 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
39 const char *default_dest_name);
40 static int set_and_load_pkg_src_list(opkg_conf_t *conf,
41 pkg_src_list_t *nv_pair_list);
42 static int set_and_load_pkg_dest_list(opkg_conf_t *conf,
43 nv_pair_list_t *nv_pair_list, char * lists_dir);
45 int opkg_init_options_array(const opkg_conf_t *conf, opkg_option_t **options)
47 opkg_option_t tmp[] = {
48 { "force_defaults", OPKG_OPT_TYPE_BOOL, &conf->force_defaults },
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 { "ftp_proxy", OPKG_OPT_TYPE_STRING, &conf->ftp_proxy },
55 { "http_proxy", OPKG_OPT_TYPE_STRING, &conf->http_proxy },
56 { "no_proxy", OPKG_OPT_TYPE_STRING, &conf->no_proxy },
57 { "test", OPKG_OPT_TYPE_INT, &conf->noaction },
58 { "noaction", OPKG_OPT_TYPE_INT, &conf->noaction },
59 { "nodeps", OPKG_OPT_TYPE_BOOL, &conf->nodeps },
60 { "offline_root", OPKG_OPT_TYPE_STRING, &conf->offline_root },
61 { "offline_root_post_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_post_script_cmd },
62 { "offline_root_pre_script_cmd", OPKG_OPT_TYPE_STRING, &conf->offline_root_pre_script_cmd },
63 { "proxy_passwd", OPKG_OPT_TYPE_STRING, &conf->proxy_passwd },
64 { "proxy_user", OPKG_OPT_TYPE_STRING, &conf->proxy_user },
65 { "query-all", OPKG_OPT_TYPE_BOOL, &conf->query_all },
66 { "verbosity", OPKG_OPT_TYPE_BOOL, &conf->verbosity },
70 *options = (opkg_option_t *)malloc(sizeof(tmp));
71 if ( options == NULL ){
72 fprintf(stderr,"%s: Unable to allocate memory\n",__FUNCTION__);
76 memcpy(*options, tmp, sizeof(tmp));
80 static void opkg_conf_override_string(char **conf_str, char *arg_str)
86 *conf_str = strdup(arg_str);
90 static void opkg_conf_free_string(char **conf_str)
98 int opkg_conf_init(opkg_conf_t *conf, const args_t *args)
102 nv_pair_list_t tmp_dest_nv_pair_list;
103 char * lists_dir =NULL;
105 char *etc_opkg_conf_pattern = "/etc/opkg/*.conf";
106 char *pending_dir =NULL;
108 memset(conf, 0, sizeof(opkg_conf_t));
110 pkg_src_list_init(&conf->pkg_src_list);
112 nv_pair_list_init(&tmp_dest_nv_pair_list);
113 pkg_dest_list_init(&conf->pkg_dest_list);
115 nv_pair_list_init(&conf->arch_list);
117 conf->restrict_to_default_dest = 0;
118 conf->default_dest = NULL;
122 tmp_dir_base = args->tmp_dir;
124 tmp_dir_base = getenv("TMPDIR");
125 sprintf_alloc(&conf->tmp_dir, "%s/%s",
126 tmp_dir_base ? tmp_dir_base : OPKG_CONF_DEFAULT_TMP_DIR_BASE,
127 OPKG_CONF_TMP_DIR_SUFFIX);
128 conf->tmp_dir = mkdtemp(conf->tmp_dir);
129 if (conf->tmp_dir == NULL) {
130 fprintf(stderr, "%s: Failed to create temporary directory `%s': %s\n",
131 __FUNCTION__, conf->tmp_dir, strerror(errno));
135 conf->force_depends = 0;
136 conf->force_defaults = 0;
137 conf->force_overwrite = 0;
138 conf->force_downgrade = 0;
139 conf->force_reinstall = 0;
140 conf->force_space = 0;
141 conf->force_removal_of_essential_packages = 0;
142 conf->force_removal_of_dependent_packages = 0;
144 conf->offline_root = NULL;
145 conf->offline_root_pre_script_cmd = NULL;
146 conf->offline_root_post_script_cmd = NULL;
150 conf->http_proxy = NULL;
151 conf->ftp_proxy = NULL;
152 conf->no_proxy = NULL;
153 conf->proxy_user = NULL;
154 conf->proxy_passwd = NULL;
156 pkg_hash_init("pkg-hash", &conf->pkg_hash, OPKG_CONF_DEFAULT_HASH_LEN);
157 hash_table_init("file-hash", &conf->file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
158 hash_table_init("obs-file-hash", &conf->obs_file_hash, OPKG_CONF_DEFAULT_HASH_LEN);
159 lists_dir=(char *)malloc(1);
161 if (args->conf_file) {
162 struct stat stat_buf;
163 err = stat(args->conf_file, &stat_buf);
165 if (opkg_conf_parse_file(conf, args->conf_file,
166 &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
167 /* Memory leakage from opkg_conf_parse-file */
173 /* if (!lists_dir ){*/
174 if (strlen(lists_dir)<=1 ){
175 lists_dir = realloc(lists_dir,strlen(OPKG_CONF_LISTS_DIR)+2);
176 sprintf (lists_dir,"%s",OPKG_CONF_LISTS_DIR);
179 if (args->offline_root) {
180 char *tmp;// = malloc(strlen(lists_dir) + strlen(args->offline_root) + 1);
181 sprintf_alloc(&tmp, "%s/%s",args->offline_root,lists_dir);
186 pending_dir = malloc(strlen(lists_dir)+strlen("/pending")+5);
187 snprintf(pending_dir,strlen(lists_dir)+strlen("/pending") ,"%s%s",lists_dir,"/pending");
189 conf->lists_dir = strdup(lists_dir);
190 conf->pending_dir = strdup(pending_dir);
192 if (args->offline_root)
193 sprintf_alloc(&etc_opkg_conf_pattern, "%s/etc/opkg/*.conf", args->offline_root);
194 memset(&globbuf, 0, sizeof(globbuf));
195 err = glob(etc_opkg_conf_pattern, 0, NULL, &globbuf);
196 if (args->offline_root)
197 free (etc_opkg_conf_pattern);
200 for (i = 0; i < globbuf.gl_pathc; i++) {
201 if (globbuf.gl_pathv[i])
202 if ( opkg_conf_parse_file(conf, globbuf.gl_pathv[i],
203 &conf->pkg_src_list, &tmp_dest_nv_pair_list,&lists_dir)<0) {
204 /* Memory leakage from opkg_conf_parse-file */
211 /* if no architectures were defined, then default all, noarch, and host architecture */
212 if (nv_pair_list_empty(&conf->arch_list)) {
213 nv_pair_list_append(&conf->arch_list, "all", "1");
214 nv_pair_list_append(&conf->arch_list, "noarch", "1");
215 nv_pair_list_append(&conf->arch_list, HOST_CPU_STR, "10");
218 /* Even if there is no conf file, we'll need at least one dest. */
219 if (tmp_dest_nv_pair_list.head == NULL) {
220 nv_pair_list_append(&tmp_dest_nv_pair_list,
221 OPKG_CONF_DEFAULT_DEST_NAME,
222 OPKG_CONF_DEFAULT_DEST_ROOT_DIR);
225 /* After parsing the file, set options from command-line, (so that
226 command-line arguments take precedence) */
227 /* XXX: CLEANUP: The interaction between args.c and opkg_conf.c
228 really needs to be cleaned up. There is so much duplication
229 right now it is ridiculous. Maybe opkg_conf_t should just save
230 a pointer to args_t (which could then not be freed), rather
231 than duplicating every field here? */
232 if (args->autoremove) {
233 conf->autoremove = 1;
235 if (args->force_depends) {
236 conf->force_depends = 1;
238 if (args->force_defaults) {
239 conf->force_defaults = 1;
241 if (args->force_overwrite) {
242 conf->force_overwrite = 1;
244 if (args->force_downgrade) {
245 conf->force_downgrade = 1;
247 if (args->force_reinstall) {
248 conf->force_reinstall = 1;
250 if (args->force_removal_of_dependent_packages) {
251 conf->force_removal_of_dependent_packages = 1;
253 if (args->force_removal_of_essential_packages) {
254 conf->force_removal_of_essential_packages = 1;
259 if (args->noaction) {
262 if (args->query_all) {
265 if (args->verbosity != conf->verbosity) {
266 conf->verbosity = args->verbosity;
269 opkg_conf_override_string(&conf->offline_root,
271 opkg_conf_override_string(&conf->offline_root_pre_script_cmd,
272 args->offline_root_pre_script_cmd);
273 opkg_conf_override_string(&conf->offline_root_post_script_cmd,
274 args->offline_root_post_script_cmd);
276 /* Pigi: added a flag to disable the checking of structures if the command does not need to
277 read anything from there.
279 if ( !(args->nocheckfordirorfile)){
280 /* need to run load the source list before dest list -Jamey */
281 if ( !(args->noreadfeedsfile))
282 set_and_load_pkg_src_list(conf, &conf->pkg_src_list);
284 /* Now that we have resolved conf->offline_root, we can commit to
285 the directory names for the dests and load in all the package
287 set_and_load_pkg_dest_list(conf, &tmp_dest_nv_pair_list,lists_dir);
290 err = opkg_conf_set_default_dest(conf, args->dest);
296 nv_pair_list_deinit(&tmp_dest_nv_pair_list);
303 void opkg_conf_deinit(opkg_conf_t *conf)
305 #ifdef OPKG_DEBUG_NO_TMP_CLEANUP
307 fprintf(stderr, "%s: Not cleaning up %s since opkg compiled "
308 "with OPKG_DEBUG_NO_TMP_CLEANUP\n",
309 __FUNCTION__, conf->tmp_dir);
313 err = rmdir(conf->tmp_dir);
315 if (errno == ENOTEMPTY) {
317 sprintf_alloc(&cmd, "rm -fr %s\n", conf->tmp_dir);
322 fprintf(stderr, "WARNING: Unable to remove temporary directory: %s: %s\n", conf->tmp_dir, strerror(errno));
324 #endif /* OPKG_DEBUG_NO_TMP_CLEANUP */
326 free(conf->tmp_dir); /*XXX*/
327 free(conf->lists_dir);
328 free(conf->pending_dir);
330 pkg_src_list_deinit(&conf->pkg_src_list);
331 pkg_dest_list_deinit(&conf->pkg_dest_list);
332 nv_pair_list_deinit(&conf->arch_list);
334 pkg_hash_deinit(&conf->pkg_hash);
335 if (&conf->file_hash)
336 hash_table_deinit(&conf->file_hash);
337 if (&conf->obs_file_hash)
338 hash_table_deinit(&conf->obs_file_hash);
340 opkg_conf_free_string(&conf->offline_root);
341 opkg_conf_free_string(&conf->offline_root_pre_script_cmd);
342 opkg_conf_free_string(&conf->offline_root_post_script_cmd);
344 if (conf->verbosity > 1) {
346 hash_table_t *hashes[] = {
349 &conf->obs_file_hash };
350 for (i = 0; i < 3; i++) {
351 hash_table_t *hash = hashes[i];
355 for (j = 0; j < hash->n_entries; j++) {
357 hash_entry_t *e = &hash->entries[j];
360 while (e && e->key) {
367 opkg_message(conf, OPKG_DEBUG, "hash_table[%s] n_buckets=%d n_elements=%d max_conflicts=%d n_conflicts=%d\n",
368 hash->name, hash->n_entries, hash->n_elements, c, n_conflicts);
369 hash_table_deinit(hash);
374 static int opkg_conf_set_default_dest(opkg_conf_t *conf,
375 const char *default_dest_name)
377 pkg_dest_list_elt_t *iter;
380 for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
382 if (strcmp(dest->name, default_dest_name) == 0) {
383 conf->default_dest = dest;
384 conf->restrict_to_default_dest = 1;
389 fprintf(stderr, "ERROR: Unknown dest name: `%s'\n", default_dest_name);
394 static int set_and_load_pkg_src_list(opkg_conf_t *conf, pkg_src_list_t *pkg_src_list)
396 pkg_src_list_elt_t *iter;
400 for (iter = pkg_src_list->head; iter; iter = iter->next) {
406 sprintf_alloc(&list_file, "%s/%s",
407 conf->restrict_to_default_dest ? conf->default_dest->lists_dir : conf->lists_dir,
410 if (file_exists(list_file)) {
411 pkg_hash_add_from_file(conf, list_file, src, NULL, 0);
419 static int set_and_load_pkg_dest_list(opkg_conf_t *conf, nv_pair_list_t *nv_pair_list, char *lists_dir )
421 nv_pair_list_elt_t *iter;
426 for (iter = nv_pair_list->head; iter; iter = iter->next) {
427 nv_pair = iter->data;
429 if (conf->offline_root) {
430 sprintf_alloc(&root_dir, "%s%s", conf->offline_root, nv_pair->value);
432 root_dir = strdup(nv_pair->value);
434 dest = pkg_dest_list_append(&conf->pkg_dest_list, nv_pair->name, root_dir, lists_dir);
439 if (conf->default_dest == NULL) {
440 conf->default_dest = dest;
442 if (file_exists(dest->status_file_name)) {
443 pkg_hash_add_from_file(conf, dest->status_file_name,
451 static int opkg_conf_parse_file(opkg_conf_t *conf, const char *filename,
452 pkg_src_list_t *pkg_src_list,
453 nv_pair_list_t *tmp_dest_nv_pair_list,
457 opkg_option_t * options;
458 FILE *file = fopen(filename, "r");
459 regex_t valid_line_re, comment_re;
460 #define regmatch_size 12
461 regmatch_t regmatch[regmatch_size];
463 if (opkg_init_options_array(conf, &options)<0)
467 fprintf(stderr, "%s: failed to open %s: %s\n",
468 __FUNCTION__, filename, strerror(errno));
472 opkg_message(conf, OPKG_NOTICE, "loading conf file %s\n", filename);
474 err = xregcomp(&comment_re,
475 "^[[:space:]]*(#.*|[[:space:]]*)$",
481 err = xregcomp(&valid_line_re, "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))([[:space:]]+([^[:space:]]+))?[[:space:]]*$", REG_EXTENDED);
490 char *type, *name, *value, *extra;
492 line = file_read_line_alloc(file);
500 if (regexec(&comment_re, line, 0, 0, 0) == 0) {
504 if (regexec(&valid_line_re, line, regmatch_size, regmatch, 0) == REG_NOMATCH) {
506 fprintf(stderr, "%s:%d: Ignoring invalid line: `%s'\n",
507 filename, line_num, line);
511 /* This has to be so ugly to deal with optional quotation marks */
512 if (regmatch[2].rm_so > 0) {
513 type = strndup(line + regmatch[2].rm_so,
514 regmatch[2].rm_eo - regmatch[2].rm_so);
516 type = strndup(line + regmatch[3].rm_so,
517 regmatch[3].rm_eo - regmatch[3].rm_so);
519 if (regmatch[5].rm_so > 0) {
520 name = strndup(line + regmatch[5].rm_so,
521 regmatch[5].rm_eo - regmatch[5].rm_so);
523 name = strndup(line + regmatch[6].rm_so,
524 regmatch[6].rm_eo - regmatch[6].rm_so);
526 if (regmatch[8].rm_so > 0) {
527 value = strndup(line + regmatch[8].rm_so,
528 regmatch[8].rm_eo - regmatch[8].rm_so);
530 value = strndup(line + regmatch[9].rm_so,
531 regmatch[9].rm_eo - regmatch[9].rm_so);
534 if (regmatch[11].rm_so > 0) {
535 extra = strndup (line + regmatch[11].rm_so,
536 regmatch[11].rm_eo - regmatch[11].rm_so);
539 /* We use the tmp_dest_nv_pair_list below instead of
540 conf->pkg_dest_list because we might encounter an
541 offline_root option later and that would invalidate the
542 directories we would have computed in
543 pkg_dest_list_init. (We do a similar thing with
544 tmp_src_nv_pair_list for sake of symmetry.) */
545 if (strcmp(type, "option") == 0) {
546 opkg_conf_set_option(options, name, value);
547 } else if (strcmp(type, "src") == 0) {
548 if (!nv_pair_list_find((nv_pair_list_t*) pkg_src_list, name)) {
549 pkg_src_list_append (pkg_src_list, name, value, extra, 0);
551 opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
554 } else if (strcmp(type, "src/gz") == 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, 1);
558 opkg_message(conf, OPKG_ERROR, "ERROR: duplicate src declaration. Skipping:\n\t src %s %s\n",
561 } else if (strcmp(type, "dest") == 0) {
562 nv_pair_list_append(tmp_dest_nv_pair_list, name, value);
563 } else if (strcmp(type, "lists_dir") == 0) {
564 *lists_dir = realloc(*lists_dir,strlen(value)+1);
565 if (*lists_dir == NULL) {
566 opkg_message(conf, OPKG_ERROR, "ERROR: Not enough memory\n");
570 sprintf (*lists_dir,"%s",value);
571 } else if (strcmp(type, "arch") == 0) {
572 opkg_message(conf, OPKG_INFO, "supported arch %s priority (%s)\n", name, value);
574 opkg_message(conf, OPKG_NOTICE, "defaulting architecture %s priority to 10\n", name);
575 value = strdup("10");
577 nv_pair_list_append(&conf->arch_list, name, value);
579 fprintf(stderr, "WARNING: Ignoring unknown configuration "
580 "parameter: %s %s %s\n", type, name, value);
596 regfree(&comment_re);
597 regfree(&valid_line_re);
603 static int opkg_conf_set_option(const opkg_option_t *options,
604 const char *name, const char *value)
607 while (options[i].name) {
608 if (strcmp(options[i].name, name) == 0) {
609 switch (options[i].type) {
610 case OPKG_OPT_TYPE_BOOL:
611 *((int *)options[i].value) = 1;
613 case OPKG_OPT_TYPE_INT:
615 *((int *)options[i].value) = atoi(value);
618 printf("%s: Option %s need an argument\n",
622 case OPKG_OPT_TYPE_STRING:
624 *((char **)options[i].value) = strdup(value);
627 printf("%s: Option %s need an argument\n",
636 fprintf(stderr, "%s: Unrecognized option: %s=%s\n",
637 __FUNCTION__, name, value);
641 int opkg_conf_write_status_files(opkg_conf_t *conf)
643 pkg_dest_list_elt_t *iter;
652 for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
654 dest->status_file = fopen(dest->status_file_tmp_name, "w");
655 if (dest->status_file == NULL) {
656 fprintf(stderr, "%s: Can't open status file: %s for writing: %s\n",
657 __FUNCTION__, dest->status_file_name, strerror(errno));
661 all = pkg_vec_alloc();
662 pkg_hash_fetch_available(&conf->pkg_hash, all);
664 for(i = 0; i < all->len; i++) {
666 /* We don't need most uninstalled packages in the status file */
667 if (pkg->state_status == SS_NOT_INSTALLED
668 && (pkg->state_want == SW_UNKNOWN
669 || pkg->state_want == SW_DEINSTALL
670 || pkg->state_want == SW_PURGE)) {
674 fprintf(stderr, "Null package\n");
676 if (pkg->dest == NULL) {
677 fprintf(stderr, "%s: ERROR: Can't write status for "
678 "package %s since it has a NULL dest\n",
679 __FUNCTION__, pkg->name);
682 if (pkg->dest->status_file) {
683 pkg_print_status(pkg, pkg->dest->status_file);
689 for (iter = conf->pkg_dest_list.head; iter; iter = iter->next) {
691 if (dest->status_file) {
692 err = ferror(dest->status_file);
693 fclose(dest->status_file);
694 dest->status_file = NULL;
696 file_move(dest->status_file_tmp_name, dest->status_file_name);
698 fprintf(stderr, "%s: ERROR: An error has occurred writing %s, "
699 "retaining old %s\n", __FUNCTION__,
700 dest->status_file_tmp_name, dest->status_file_name);
709 char *root_filename_alloc(opkg_conf_t *conf, char *filename)
712 sprintf_alloc(&root_filename, "%s%s", (conf->offline_root ? conf->offline_root : ""), filename);
713 return root_filename;