Date: Sat, 26 Oct 2013 03:31:05 +0000 (UTC) From: Bryan Drewery <bdrewery@FreeBSD.org> To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r257145 - in head: etc etc/pkg usr.sbin/pkg Message-ID: <201310260331.r9Q3V5NQ077773@svn.freebsd.org>
next in thread | raw e-mail | index | archive | help
Author: bdrewery (ports committer) Date: Sat Oct 26 03:31:05 2013 New Revision: 257145 URL: http://svnweb.freebsd.org/changeset/base/257145 Log: 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 Added: head/etc/pkg/ head/etc/pkg/FreeBSD.conf (contents, props changed) head/etc/pkg/Makefile (contents, props changed) Modified: head/etc/Makefile head/usr.sbin/pkg/config.c head/usr.sbin/pkg/config.h Modified: head/etc/Makefile ============================================================================== --- head/etc/Makefile Sat Oct 26 03:22:57 2013 (r257144) +++ head/etc/Makefile Sat Oct 26 03:31:05 2013 (r257145) @@ -225,6 +225,9 @@ distribution: ${_+_}cd ${.CURDIR}/devd; ${MAKE} install ${_+_}cd ${.CURDIR}/gss; ${MAKE} install ${_+_}cd ${.CURDIR}/periodic; ${MAKE} install +.if ${MK_PKGBOOTSTRAP} != "no" + ${_+_}cd ${.CURDIR}/pkg; ${MAKE} install +.endif ${_+_}cd ${.CURDIR}/rc.d; ${MAKE} install ${_+_}cd ${.CURDIR}/../gnu/usr.bin/send-pr; ${MAKE} etc-gnats-freefall ${_+_}cd ${.CURDIR}/../share/termcap; ${MAKE} etc-termcap Added: head/etc/pkg/FreeBSD.conf ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/etc/pkg/FreeBSD.conf Sat Oct 26 03:31:05 2013 (r257145) @@ -0,0 +1,6 @@ +# $FreeBSD$ +FreeBSD: { + url: "pkg+http://pkg.freebsd.org/${ABI}/latest", + mirror_type: "srv", + enabled: "yes" +} Added: head/etc/pkg/Makefile ============================================================================== --- /dev/null 00:00:00 1970 (empty, because file is newly added) +++ head/etc/pkg/Makefile Sat Oct 26 03:31:05 2013 (r257145) @@ -0,0 +1,10 @@ +# $FreeBSD$ + +NO_OBJ= + +FILES= FreeBSD.conf + +FILESDIR= /etc/pkg +FILESMODE= 644 + +.include <bsd.prog.mk> Modified: head/usr.sbin/pkg/config.c ============================================================================== --- head/usr.sbin/pkg/config.c Sat Oct 26 03:22:57 2013 (r257144) +++ head/usr.sbin/pkg/config.c Sat Oct 26 03:31:05 2013 (r257145) @@ -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_ } 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_ 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) Modified: head/usr.sbin/pkg/config.h ============================================================================== --- head/usr.sbin/pkg/config.h Sat Oct 26 03:22:57 2013 (r257144) +++ head/usr.sbin/pkg/config.h Sat Oct 26 03:31:05 2013 (r257145) @@ -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 **);
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201310260331.r9Q3V5NQ077773>