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 {"no_check_certificate", OPKG_OPT_TYPE_BOOL
, &_conf
.no_check_certificate
},
58 {"ftp_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.ftp_proxy
},
59 {"http_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.http_proxy
},
60 {"http_timeout", OPKG_OPT_TYPE_STRING
, &_conf
.http_timeout
},
61 {"no_proxy", OPKG_OPT_TYPE_STRING
, &_conf
.no_proxy
},
62 {"test", OPKG_OPT_TYPE_BOOL
, &_conf
.noaction
},
63 {"noaction", OPKG_OPT_TYPE_BOOL
, &_conf
.noaction
},
64 {"download_only", OPKG_OPT_TYPE_BOOL
, &_conf
.download_only
},
65 {"nodeps", OPKG_OPT_TYPE_BOOL
, &_conf
.nodeps
},
66 {"nocase", OPKG_OPT_TYPE_BOOL
, &_conf
.nocase
},
67 {"offline_root", OPKG_OPT_TYPE_STRING
, &_conf
.offline_root
},
68 {"overlay_root", OPKG_OPT_TYPE_STRING
, &_conf
.overlay_root
},
69 {"proxy_passwd", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_passwd
},
70 {"proxy_user", OPKG_OPT_TYPE_STRING
, &_conf
.proxy_user
},
71 {"query-all", OPKG_OPT_TYPE_BOOL
, &_conf
.query_all
},
72 {"size", OPKG_OPT_TYPE_BOOL
, &_conf
.size
},
73 {"tmp_dir", OPKG_OPT_TYPE_STRING
, &_conf
.tmp_dir
},
74 {"verbosity", OPKG_OPT_TYPE_INT
, &_conf
.verbosity
},
75 {"verify_program", OPKG_OPT_TYPE_STRING
, &_conf
.verify_program
},
79 static int resolve_pkg_dest_list(void)
81 nv_pair_list_elt_t
*iter
;
86 for (iter
= nv_pair_list_first(&conf
->tmp_dest_list
); iter
;
87 iter
= nv_pair_list_next(&conf
->tmp_dest_list
, iter
)) {
88 nv_pair
= (nv_pair_t
*) iter
->data
;
90 if (conf
->offline_root
) {
91 sprintf_alloc(&root_dir
, "%s%s", conf
->offline_root
,
94 root_dir
= xstrdup(nv_pair
->value
);
98 pkg_dest_list_append(&conf
->pkg_dest_list
, nv_pair
->name
,
99 root_dir
, conf
->lists_dir
);
102 if (conf
->default_dest
== NULL
)
103 conf
->default_dest
= dest
;
105 if (conf
->dest_str
&& !strcmp(dest
->name
, conf
->dest_str
)) {
106 conf
->default_dest
= dest
;
107 conf
->restrict_to_default_dest
= 1;
111 if (conf
->dest_str
&& !conf
->restrict_to_default_dest
) {
112 opkg_msg(ERROR
, "Unknown dest name: `%s'.\n", conf
->dest_str
);
119 static int opkg_conf_set_option(const char *name
, const char *value
)
123 while (options
[i
].name
) {
124 if (strcmp(options
[i
].name
, name
) == 0) {
125 switch (options
[i
].type
) {
126 case OPKG_OPT_TYPE_BOOL
:
127 if (*(int *)options
[i
].value
) {
129 "Duplicate boolean option %s, "
130 "leaving this option on.\n",
134 *((int *const)options
[i
].value
) = 1;
136 case OPKG_OPT_TYPE_INT
:
138 if (*(int *)options
[i
].value
) {
140 "Duplicate option %s, "
141 "using first seen value \"%d\".\n",
147 *((int *const)options
[i
].value
) =
152 "Option %s needs an argument\n",
156 case OPKG_OPT_TYPE_STRING
:
158 if (*(char **)options
[i
].value
) {
160 "Duplicate option %s, "
161 "using first seen value \"%s\".\n",
163 *((char **)options
[i
].
167 *((char **const)options
[i
].value
) =
172 "Option %s needs an argument\n",
181 opkg_msg(ERROR
, "Unrecognized option: %s=%s\n", name
, value
);
186 opkg_conf_parse_file(const char *filename
,
187 pkg_src_list_t
* pkg_src_list
)
192 regex_t valid_line_re
, comment_re
;
193 #define regmatch_size 14
194 regmatch_t regmatch
[regmatch_size
];
196 file
= fopen(filename
, "r");
198 opkg_perror(ERROR
, "Failed to open %s", filename
);
203 opkg_msg(INFO
, "Loading conf file %s.\n", filename
);
205 err
= xregcomp(&comment_re
,
206 "^[[:space:]]*(#.*|[[:space:]]*)$", REG_EXTENDED
);
210 err
= xregcomp(&valid_line_re
,
211 "^[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
212 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
213 "[[:space:]]*(\"([^\"]*)\"|([^[:space:]]*))"
214 "([[:space:]]+([^[:space:]]+))?([[:space:]]+(.*))?[[:space:]]*$",
221 char *type
, *name
, *value
, *extra
;
225 line
= file_read_line_alloc(file
);
229 if (regexec(&comment_re
, line
, 0, 0, 0) == 0)
232 if (regexec(&valid_line_re
, line
, regmatch_size
, regmatch
, 0) ==
234 opkg_msg(ERROR
, "%s:%d: Ignoring invalid line: `%s'\n",
235 filename
, line_num
, line
);
239 /* This has to be so ugly to deal with optional quotation marks */
240 if (regmatch
[2].rm_so
> 0) {
241 type
= xstrndup(line
+ regmatch
[2].rm_so
,
242 regmatch
[2].rm_eo
- regmatch
[2].rm_so
);
244 type
= xstrndup(line
+ regmatch
[3].rm_so
,
245 regmatch
[3].rm_eo
- regmatch
[3].rm_so
);
248 if (regmatch
[5].rm_so
> 0) {
249 name
= xstrndup(line
+ regmatch
[5].rm_so
,
250 regmatch
[5].rm_eo
- regmatch
[5].rm_so
);
252 name
= xstrndup(line
+ regmatch
[6].rm_so
,
253 regmatch
[6].rm_eo
- regmatch
[6].rm_so
);
256 if (regmatch
[8].rm_so
> 0) {
257 value
= xstrndup(line
+ regmatch
[8].rm_so
,
258 regmatch
[8].rm_eo
- regmatch
[8].rm_so
);
260 value
= xstrndup(line
+ regmatch
[9].rm_so
,
261 regmatch
[9].rm_eo
- regmatch
[9].rm_so
);
265 if (regmatch
[11].rm_so
> 0) {
266 if (regmatch
[13].rm_so
> 0
267 && regmatch
[13].rm_so
!= regmatch
[13].rm_eo
)
269 xstrndup(line
+ regmatch
[11].rm_so
,
273 extra
= xstrndup(line
+ regmatch
[11].rm_so
,
278 if (regmatch
[13].rm_so
!= regmatch
[13].rm_eo
279 && strncmp(type
, "dist", 4) != 0) {
281 "%s:%d: Ignoring config line with trailing garbage: `%s'\n",
282 filename
, line_num
, line
);
285 /* We use the conf->tmp_dest_list below instead of
286 conf->pkg_dest_list because we might encounter an
287 offline_root option later and that would invalidate the
288 directories we would have computed in
289 pkg_dest_list_init. (We do a similar thing with
290 tmp_src_nv_pair_list for sake of symmetry.) */
291 if (strcmp(type
, "option") == 0) {
292 opkg_conf_set_option(name
, value
);
293 } else if (strcmp(type
, "src") == 0) {
294 if (!nv_pair_list_find
295 ((nv_pair_list_t
*) pkg_src_list
, name
)) {
296 pkg_src_list_append(pkg_src_list
, name
,
300 "Duplicate src declaration (%s %s). "
301 "Skipping.\n", name
, value
);
303 } else if (strcmp(type
, "src/gz") == 0) {
304 if (!nv_pair_list_find
305 ((nv_pair_list_t
*) pkg_src_list
, name
)) {
306 pkg_src_list_append(pkg_src_list
, name
,
310 "Duplicate src declaration (%s %s). "
311 "Skipping.\n", name
, value
);
313 } else if (strcmp(type
, "dest") == 0) {
314 nv_pair_list_append(&conf
->tmp_dest_list
, name
,
316 } else if (strcmp(type
, "lists_dir") == 0) {
317 conf
->lists_dir
= xstrdup(value
);
318 } else if (strcmp(type
, "arch") == 0) {
320 "Supported arch %s priority (%s)\n",
324 "No priority given for architecture %s,"
325 "defaulting to 10\n", name
);
326 value
= xstrdup("10");
328 nv_pair_list_append(&conf
->arch_list
, name
,
332 "%s:%d: Ignoring invalid line: `%s'\n",
333 filename
, line_num
, line
);
348 regfree(&valid_line_re
);
350 regfree(&comment_re
);
352 if (fclose(file
) == EOF
) {
353 opkg_perror(ERROR
, "Couldn't close %s", filename
);
360 int opkg_conf_write_status_files(void)
362 pkg_dest_list_elt_t
*iter
;
371 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
372 dest
= (pkg_dest_t
*) iter
->data
;
374 dest
->status_fp
= fopen(dest
->status_file_name
, "w");
375 if (dest
->status_fp
== NULL
&& errno
!= EROFS
) {
376 opkg_perror(ERROR
, "Can't open status file %s",
377 dest
->status_file_name
);
382 all
= pkg_vec_alloc();
383 pkg_hash_fetch_available(all
);
385 for (i
= 0; i
< all
->len
; i
++) {
387 /* We don't need most uninstalled packages in the status file */
388 if (pkg
->state_status
== SS_NOT_INSTALLED
389 && (pkg
->state_want
== SW_UNKNOWN
390 || (pkg
->state_want
== SW_DEINSTALL
391 && pkg
->state_flag
!= SF_HOLD
)
392 || pkg
->state_want
== SW_PURGE
)) {
395 if (pkg
->dest
== NULL
) {
397 "Internal error: package %s has a NULL dest\n",
401 if (pkg
->dest
->status_fp
)
402 pkg_print_status(pkg
, pkg
->dest
->status_fp
);
407 list_for_each_entry(iter
, &conf
->pkg_dest_list
.head
, node
) {
408 dest
= (pkg_dest_t
*) iter
->data
;
409 if (dest
->status_fp
&& fclose(dest
->status_fp
) == EOF
) {
410 opkg_perror(ERROR
, "Couldn't close %s",
411 dest
->status_file_name
);
419 char *root_filename_alloc(char *filename
)
422 sprintf_alloc(&root_filename
, "%s%s",
423 (conf
->offline_root
? conf
->offline_root
: ""), filename
);
424 return root_filename
;
427 static int glob_errfunc(const char *epath
, int eerrno
)
429 if (eerrno
== ENOENT
)
430 /* If leading dir does not exist, we get GLOB_NOMATCH. */
433 opkg_msg(ERROR
, "glob failed for %s: %s\n", epath
, strerror(eerrno
));
437 int opkg_conf_init(void)
439 pkg_src_list_init(&conf
->pkg_src_list
);
440 pkg_dest_list_init(&conf
->pkg_dest_list
);
441 pkg_dest_list_init(&conf
->tmp_dest_list
);
442 nv_pair_list_init(&conf
->arch_list
);
447 int opkg_conf_load(void)
450 char *tmp
, *tmp_dir_base
, **tmp_val
;
452 char *etc_opkg_conf_pattern
;
454 conf
->restrict_to_default_dest
= 0;
455 conf
->default_dest
= NULL
;
457 if (!conf
->offline_root
)
458 conf
->offline_root
= xstrdup(getenv("OFFLINE_ROOT"));
460 if (conf
->conf_file
) {
462 if (stat(conf
->conf_file
, &st
) == -1) {
463 opkg_perror(ERROR
, "Couldn't stat %s", conf
->conf_file
);
466 if (opkg_conf_parse_file(conf
->conf_file
,
467 &conf
->pkg_src_list
))
471 if (conf
->offline_root
)
472 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/etc/opkg/*.conf",
475 const char *conf_file_dir
= getenv("OPKG_CONF_DIR");
476 if (conf_file_dir
== NULL
)
477 conf_file_dir
= OPKG_CONF_DEFAULT_CONF_FILE_DIR
;
478 sprintf_alloc(&etc_opkg_conf_pattern
, "%s/*.conf",
482 memset(&globbuf
, 0, sizeof(globbuf
));
483 glob_ret
= glob(etc_opkg_conf_pattern
, 0, glob_errfunc
, &globbuf
);
484 if (glob_ret
&& glob_ret
!= GLOB_NOMATCH
) {
485 free(etc_opkg_conf_pattern
);
490 free(etc_opkg_conf_pattern
);
492 for (i
= 0; i
< globbuf
.gl_pathc
; i
++) {
493 if (globbuf
.gl_pathv
[i
])
494 if (conf
->conf_file
&&
495 !strcmp(conf
->conf_file
, globbuf
.gl_pathv
[i
]))
497 if (opkg_conf_parse_file(globbuf
.gl_pathv
[i
],
498 &conf
->pkg_src_list
) < 0) {
506 if (conf
->offline_root
)
507 sprintf_alloc(&lock_file
, "%s/%s", conf
->offline_root
,
510 sprintf_alloc(&lock_file
, "%s", OPKGLOCKFILE
);
512 lock_fd
= creat(lock_file
, S_IRUSR
| S_IWUSR
| S_IRGRP
);
514 opkg_perror(ERROR
, "Could not create lock file %s", lock_file
);
518 if (lockf(lock_fd
, F_TLOCK
, (off_t
) 0) == -1) {
519 opkg_perror(ERROR
, "Could not lock %s", lock_file
);
520 if (close(lock_fd
) == -1)
521 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
528 tmp_dir_base
= conf
->tmp_dir
;
530 tmp_dir_base
= getenv("TMPDIR");
532 sprintf_alloc(&tmp
, "%s/%s",
533 tmp_dir_base
? tmp_dir_base
:
534 OPKG_CONF_DEFAULT_TMP_DIR_BASE
, OPKG_CONF_TMP_DIR_SUFFIX
);
537 conf
->tmp_dir
= mkdtemp(tmp
);
538 if (conf
->tmp_dir
== NULL
) {
539 opkg_perror(ERROR
, "Creating temp dir %s failed", tmp
);
544 hash_table_init("file-hash", &conf
->file_hash
,
545 OPKG_CONF_DEFAULT_HASH_LEN
);
546 hash_table_init("obs-file-hash", &conf
->obs_file_hash
,
547 OPKG_CONF_DEFAULT_HASH_LEN
/ 16);
549 if (conf
->lists_dir
== NULL
)
550 conf
->lists_dir
= xstrdup(OPKG_CONF_LISTS_DIR
);
552 if (conf
->verify_program
== NULL
)
553 conf
->verify_program
= xstrdup(OPKG_CONF_DEFAULT_VERIFY_PROGRAM
);
555 if (conf
->offline_root
) {
556 sprintf_alloc(&tmp
, "%s/%s", conf
->offline_root
,
558 free(conf
->lists_dir
);
559 conf
->lists_dir
= tmp
;
562 /* if no architectures were defined, then default all, noarch, and host architecture */
563 if (nv_pair_list_empty(&conf
->arch_list
)) {
564 nv_pair_list_append(&conf
->arch_list
, "all", "1");
565 nv_pair_list_append(&conf
->arch_list
, "noarch", "1");
566 nv_pair_list_append(&conf
->arch_list
, HOST_CPU_STR
, "10");
569 /* Even if there is no conf file, we'll need at least one dest. */
570 if (nv_pair_list_empty(&conf
->tmp_dest_list
)) {
571 nv_pair_list_append(&conf
->tmp_dest_list
,
572 OPKG_CONF_DEFAULT_DEST_NAME
,
573 OPKG_CONF_DEFAULT_DEST_ROOT_DIR
);
576 if (resolve_pkg_dest_list())
579 nv_pair_list_deinit(&conf
->tmp_dest_list
);
584 free(conf
->lists_dir
);
587 hash_table_deinit(&conf
->file_hash
);
588 hash_table_deinit(&conf
->obs_file_hash
);
590 if (rmdir(conf
->tmp_dir
) == -1)
591 opkg_perror(ERROR
, "Couldn't remove dir %s", conf
->tmp_dir
);
593 if (lockf(lock_fd
, F_ULOCK
, (off_t
) 0) == -1)
594 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
596 if (close(lock_fd
) == -1)
597 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
599 if (unlink(lock_file
) == -1)
600 opkg_perror(ERROR
, "Couldn't unlink %s", lock_file
);
607 pkg_src_list_deinit(&conf
->pkg_src_list
);
608 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
609 nv_pair_list_deinit(&conf
->arch_list
);
611 for (i
= 0; options
[i
].name
; i
++) {
612 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
613 tmp_val
= (char **)options
[i
].value
;
621 nv_pair_list_deinit(&conf
->tmp_dest_list
);
623 free(conf
->dest_str
);
625 free(conf
->conf_file
);
630 void opkg_conf_deinit(void)
639 free(conf
->lists_dir
);
642 free(conf
->dest_str
);
645 free(conf
->conf_file
);
647 pkg_src_list_deinit(&conf
->pkg_src_list
);
648 pkg_dest_list_deinit(&conf
->pkg_dest_list
);
649 nv_pair_list_deinit(&conf
->arch_list
);
651 for (i
= 0; options
[i
].name
; i
++) {
652 if (options
[i
].type
== OPKG_OPT_TYPE_STRING
) {
653 tmp
= (char **)options
[i
].value
;
661 if (conf
->verbosity
>= DEBUG
) {
662 hash_print_stats(&conf
->pkg_hash
);
663 hash_print_stats(&conf
->file_hash
);
664 hash_print_stats(&conf
->obs_file_hash
);
668 hash_table_deinit(&conf
->file_hash
);
669 hash_table_deinit(&conf
->obs_file_hash
);
672 if (lockf(lock_fd
, F_ULOCK
, (off_t
) 0) == -1)
673 opkg_perror(ERROR
, "Couldn't unlock %s", lock_file
);
675 if (close(lock_fd
) == -1)
676 opkg_perror(ERROR
, "Couldn't close descriptor %d (%s)",
682 if (unlink(lock_file
) == -1)
683 opkg_perror(ERROR
, "Couldn't unlink %s", lock_file
);