diff options
author | bdrewery <bdrewery@FreeBSD.org> | 2013-10-26 03:31:05 +0000 |
---|---|---|
committer | bdrewery <bdrewery@FreeBSD.org> | 2013-10-26 03:31:05 +0000 |
commit | 389ffca6d66dc27bd6424dccf90331a485dd7eaa (patch) | |
tree | 7465eb11eecd7e13e95edcbbbfe5cf707c488172 /usr.sbin | |
parent | e8e5ffc1183571be34ec075ebc30419dae5daba3 (diff) | |
download | FreeBSD-src-389ffca6d66dc27bd6424dccf90331a485dd7eaa.zip FreeBSD-src-389ffca6d66dc27bd6424dccf90331a485dd7eaa.tar.gz |
Add support for reading configuration files from /etc/pkg.
For now only /etc/pkg/FreeBSD.conf is supported. Its style is:
Repo: {
URL: "...",
MIRROR_TYPE: "...",
...
}
The configuration will be read from /usr/local/etc/pkg.conf if exists,
otherwise /etc/pkg/FreeBSD.conf
Approved by: bapt
MFC after: 2 days
Diffstat (limited to 'usr.sbin')
-rw-r--r-- | usr.sbin/pkg/config.c | 129 | ||||
-rw-r--r-- | usr.sbin/pkg/config.h | 5 |
2 files changed, 103 insertions, 31 deletions
diff --git a/usr.sbin/pkg/config.c b/usr.sbin/pkg/config.c index fb38e70..97eb095 100644 --- a/usr.sbin/pkg/config.c +++ b/usr.sbin/pkg/config.c @@ -460,7 +460,7 @@ subst_packagesite(const char *abi) } static void -config_parse(yaml_document_t *doc, yaml_node_t *node) +config_parse(yaml_document_t *doc, yaml_node_t *node, pkg_conf_file_t conftype) { yaml_node_pair_t *pair; yaml_node_t *key, *val; @@ -495,15 +495,33 @@ config_parse(yaml_document_t *doc, yaml_node_t *node) } sbuf_clear(buf); - for (j = 0; j < strlen(key->data.scalar.value); ++j) - sbuf_putc(buf, toupper(key->data.scalar.value[j])); - sbuf_finish(buf); + if (conftype == CONFFILE_PKG) { + for (j = 0; j < strlen(key->data.scalar.value); ++j) + sbuf_putc(buf, + toupper(key->data.scalar.value[j])); + sbuf_finish(buf); + } else if (conftype == CONFFILE_REPO) { + /* The CONFFILE_REPO type is more restrictive. Only + parse known elements. */ + if (strcasecmp(key->data.scalar.value, "url") == 0) + sbuf_cpy(buf, "PACKAGESITE"); + else if (strcasecmp(key->data.scalar.value, + "mirror_type") == 0) + sbuf_cpy(buf, "MIRROR_TYPE"); + else { /* Skip unknown entries for future use. */ + ++pair; + continue; + } + sbuf_finish(buf); + } + for (i = 0; i < CONFIG_SIZE; i++) { if (strcmp(sbuf_data(buf), c[i].key) == 0) break; } + /* Silently skip unknown keys to be future compatible. */ if (i == CONFIG_SIZE) { ++pair; continue; @@ -522,36 +540,53 @@ config_parse(yaml_document_t *doc, yaml_node_t *node) sbuf_delete(buf); } -int -config_init(void) +/*- + * Parse new repo style configs in style: + * Name: + * URL: + * MIRROR_TYPE: + * etc... + */ +static void +parse_repo_file(yaml_document_t *doc, yaml_node_t *node) { - FILE *fp; - yaml_parser_t parser; - yaml_document_t doc; - yaml_node_t *node; - const char *val; - int i; - const char *localbase; - char confpath[MAXPATHLEN]; - char abi[BUFSIZ]; + yaml_node_pair_t *pair; - for (i = 0; i < CONFIG_SIZE; i++) { - val = getenv(c[i].key); - if (val != NULL) { - c[i].val = val; - c[i].envset = true; + pair = node->data.mapping.pairs.start; + while (pair < node->data.mapping.pairs.top) { + yaml_node_t *key = yaml_document_get_node(doc, pair->key); + yaml_node_t *val = yaml_document_get_node(doc, pair->value); + + if (key->data.scalar.length <= 0) { + ++pair; + continue; } + + if (val->type != YAML_MAPPING_NODE) { + ++pair; + continue; + } + + config_parse(doc, val, CONFFILE_REPO); + ++pair; } +} - localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE; - snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf", localbase); + +static int +read_conf_file(const char *confpath, pkg_conf_file_t conftype) +{ + FILE *fp; + yaml_parser_t parser; + yaml_document_t doc; + yaml_node_t *node; if ((fp = fopen(confpath, "r")) == NULL) { if (errno != ENOENT) err(EXIT_FAILURE, "Unable to open configuration " "file %s", confpath); /* no configuration present */ - goto finalize; + return (1); } yaml_parser_initialize(&parser); @@ -560,20 +595,52 @@ config_init(void) node = yaml_document_get_root_node(&doc); - if (node != NULL) { - if (node->type != YAML_MAPPING_NODE) - warnx("Invalid configuration format, ignoring the " - "configuration file"); - else - config_parse(&doc, node); - } else { + if (node == NULL || node->type != YAML_MAPPING_NODE) warnx("Invalid configuration format, ignoring the " - "configuration file"); + "configuration file %s", confpath); + else { + if (conftype == CONFFILE_PKG) + config_parse(&doc, node, conftype); + else if (conftype == CONFFILE_REPO) + parse_repo_file(&doc, node); } yaml_document_delete(&doc); yaml_parser_delete(&parser); + return (0); +} + +int +config_init(void) +{ + const char *val; + int i; + const char *localbase; + char confpath[MAXPATHLEN]; + char abi[BUFSIZ]; + + for (i = 0; i < CONFIG_SIZE; i++) { + val = getenv(c[i].key); + if (val != NULL) { + c[i].val = val; + c[i].envset = true; + } + } + + localbase = getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE; + snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf", + localbase); + + if (access(confpath, F_OK) == 0 && read_conf_file(confpath, + CONFFILE_PKG)) + goto finalize; + + snprintf(confpath, sizeof(confpath), "/etc/pkg/FreeBSD.conf"); + if (access(confpath, F_OK) == 0 && read_conf_file(confpath, + CONFFILE_REPO)) + goto finalize; + finalize: if (c[ABI].val == NULL && c[ABI].value == NULL) { if (pkg_get_myabi(abi, BUFSIZ) != 0) diff --git a/usr.sbin/pkg/config.h b/usr.sbin/pkg/config.h index c3d877e..4936016 100644 --- a/usr.sbin/pkg/config.h +++ b/usr.sbin/pkg/config.h @@ -45,6 +45,11 @@ typedef enum { PKG_CONFIG_BOOL, } pkg_config_t; +typedef enum { + CONFFILE_PKG=0, + CONFFILE_REPO, +} pkg_conf_file_t; + int config_init(void); void config_finish(void); int config_string(pkg_config_key, const char **); |