Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 5 May 2021 08:25:30 GMT
From:      Baptiste Daroussin <bapt@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org
Subject:   git: 15ed3e7d70fc - stable/12 - pkg(7): add an -r reponame option for bootstrap and add
Message-ID:  <202105050825.1458PU8Y028833@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch stable/12 has been updated by bapt:

URL: https://cgit.FreeBSD.org/src/commit/?id=15ed3e7d70fc13273271f0fe454dc6d43bc6de6a

commit 15ed3e7d70fc13273271f0fe454dc6d43bc6de6a
Author:     Kyle Evans <kevans@FreeBSD.org>
AuthorDate: 2021-02-12 00:58:26 +0000
Commit:     Baptiste Daroussin <bapt@FreeBSD.org>
CommitDate: 2021-05-05 08:17:31 +0000

    pkg(7): add an -r reponame option for bootstrap and add
    
    This is limited to bootstrap/add because some real pkg(8) commands
    have -r flags with an incompatible meaning/usage, e.g., pkg-audit.
    pkg(7) will still commence the search as it has, but it will ignore any
    repo objects without the given name so that overrides and whatnot still
    work as expected.
    
    The use of it for add is noted in the manpage; notably, that the
    signature config for that repository will be used over global config if
    it's specified. i.e., pkg(7) should assume that the given pkg did come
    from that repository and treat it appropriately.
    
    Reviewed by:    bapt, manu
    MFC after:      1 week
    Differential Revision:  https://reviews.freebsd.org/D28524
---
 usr.sbin/pkg/config.c | 24 +++++++++++++++---------
 usr.sbin/pkg/config.h |  2 +-
 usr.sbin/pkg/pkg.7    | 37 +++++++++++++++++++++++++++++++------
 usr.sbin/pkg/pkg.c    | 47 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 91 insertions(+), 19 deletions(-)

diff --git a/usr.sbin/pkg/config.c b/usr.sbin/pkg/config.c
index 68c3e7d63d1a..cba8ff49e774 100644
--- a/usr.sbin/pkg/config.c
+++ b/usr.sbin/pkg/config.c
@@ -341,7 +341,7 @@ cleanup:
  * etc...
  */
 static void
-parse_repo_file(ucl_object_t *obj)
+parse_repo_file(ucl_object_t *obj, const char *requested_repo)
 {
 	ucl_object_iter_t it = NULL;
 	const ucl_object_t *cur;
@@ -356,13 +356,17 @@ parse_repo_file(ucl_object_t *obj)
 		if (cur->type != UCL_OBJECT)
 			continue;
 
+		if (requested_repo != NULL && strcmp(requested_repo, key) != 0)
+			continue;
+
 		config_parse(cur, CONFFILE_REPO);
 	}
 }
 
 
 static int
-read_conf_file(const char *confpath, pkg_conf_file_t conftype)
+read_conf_file(const char *confpath, const char *requested_repo,
+    pkg_conf_file_t conftype)
 {
 	struct ucl_parser *p;
 	ucl_object_t *obj = NULL;
@@ -386,7 +390,7 @@ read_conf_file(const char *confpath, pkg_conf_file_t conftype)
 		if (conftype == CONFFILE_PKG)
 			config_parse(obj, conftype);
 		else if (conftype == CONFFILE_REPO)
-			parse_repo_file(obj);
+			parse_repo_file(obj, requested_repo);
 	}
 
 	ucl_object_unref(obj);
@@ -396,7 +400,7 @@ read_conf_file(const char *confpath, pkg_conf_file_t conftype)
 }
 
 static int
-load_repositories(const char *repodir)
+load_repositories(const char *repodir, const char *requested_repo)
 {
 	struct dirent *ent;
 	DIR *d;
@@ -420,8 +424,10 @@ load_repositories(const char *repodir)
 			    repodir,
 			    repodir[strlen(repodir) - 1] == '/' ? "" : "/",
 			    ent->d_name);
-			if (access(path, F_OK) == 0 &&
-			    read_conf_file(path, CONFFILE_REPO)) {
+			if (access(path, F_OK) != 0)
+				continue;
+			if (read_conf_file(path, requested_repo,
+			    CONFFILE_REPO)) {
 				ret = 1;
 				goto cleanup;
 			}
@@ -435,7 +441,7 @@ cleanup:
 }
 
 int
-config_init(void)
+config_init(const char *requested_repo)
 {
 	char *val;
 	int i;
@@ -477,7 +483,7 @@ config_init(void)
 	snprintf(confpath, sizeof(confpath), "%s/etc/pkg.conf",
 	    localbase);
 
-	if (access(confpath, F_OK) == 0 && read_conf_file(confpath,
+	if (access(confpath, F_OK) == 0 && read_conf_file(confpath, NULL,
 	    CONFFILE_PKG))
 		goto finalize;
 
@@ -495,7 +501,7 @@ config_init(void)
 	}
 
 	STAILQ_FOREACH(cv, c[REPOS_DIR].list, next)
-		if (load_repositories(cv->value))
+		if (load_repositories(cv->value, requested_repo))
 			goto finalize;
 
 finalize:
diff --git a/usr.sbin/pkg/config.h b/usr.sbin/pkg/config.h
index b5553111c610..a813ceebc26b 100644
--- a/usr.sbin/pkg/config.h
+++ b/usr.sbin/pkg/config.h
@@ -59,7 +59,7 @@ typedef enum {
 	CONFFILE_REPO,
 } pkg_conf_file_t;
 
-int config_init(void);
+int config_init(const char *);
 void config_finish(void);
 int config_string(pkg_config_key, const char **);
 int config_bool(pkg_config_key, bool *);
diff --git a/usr.sbin/pkg/pkg.7 b/usr.sbin/pkg/pkg.7
index c672306ac08d..b92f5c9820cd 100644
--- a/usr.sbin/pkg/pkg.7
+++ b/usr.sbin/pkg/pkg.7
@@ -24,7 +24,7 @@
 .\"
 .\" $FreeBSD$
 .\"
-.Dd December 31, 2020
+.Dd February 7, 2021
 .Dt PKG 7
 .Os
 .Sh NAME
@@ -36,6 +36,7 @@
 .Nm
 add
 .Op Fl f
+.Op Fl r Ar reponame
 .Op Fl y
 .Ao Pa pkg.txz Ac
 .Nm
@@ -44,6 +45,7 @@ add
 .Op Fl 4 | Fl 6
 bootstrap
 .Op Fl f
+.Op Fl r Ar reponame
 .Op Fl y
 .Sh DESCRIPTION
 .Nm
@@ -68,7 +70,7 @@ is not installed yet, it will be fetched, have its signature verified,
 installed, and then have the original command forwarded to it.
 If already installed, the command requested will be forwarded to the real
 .Xr pkg 8 .
-.It Nm Li add Oo Fl f Oc Oo Fl y Oc Ao Pa pkg.txz Ac
+.It Nm Li add Oo Fl f Oc Oo Fl r Ar reponame Oc Oo Fl y Oc Ao Pa pkg.txz Ac
 Install
 .Xr pkg 8
 from a local package instead of fetching from remote.
@@ -83,13 +85,19 @@ If the
 .Fl y
 flag is specified, no confirmation will be asked when bootstrapping
 .Xr pkg 8 .
+.Pp
+If a
+.Ar reponame
+has been specified, then the signature configuration for that repository will be
+used.
 .It Nm Fl N
 Do not bootstrap, just determine if
 .Xr pkg 8
 is actually installed or not.
 Returns 0 and the number of packages installed
 if it is, otherwise 1.
-.It Nm Oo Fl 4 | Fl 6 Oc Li bootstrap Oo Fl f Oc Oo Fl y Oc
+.It Nm Oo Fl 4 | Fl 6 Oc Li bootstrap Oo Fl f Oc \
+Oo Fl r Ar reponame Oc Oo Fl y Oc
 Attempt to bootstrap and do not forward anything to
 .Xr pkg 8
 after it is installed.
@@ -110,14 +118,30 @@ If the
 .Fl y
 flag is specified, no confirmation will be asked when bootstrapping
 .Xr pkg 8 .
+.Pp
+If a
+.Ar reponame
+has been specified, then the configuration for that repository will be used.
 .El
 .Sh CONFIGURATION
 Configuration varies in whether it is in a repository configuration file
 or the global configuration file.
+The default repository configuration for
+.Fx
+is stored in
+.Pa /etc/pkg/FreeBSD.conf ,
+and additional repository configuration files will be searched for in
+.Ev REPOS_DIR ,
+or
+.Pa /usr/local/etc/pkg/repos
+if it is unset.
 .Pp
-Repository configuration can be stored in
-.Pa /etc/pkg/FreeBSD.conf
-in the following format:
+For bootstrapping,
+.Nm
+will process all repositories that it finds and use the last enabled repository
+by default.
+.Pp
+Repository configuration is stored in the following format:
 .Bd -literal -offset indent
 FreeBSD: {
   url: "pkg+http://pkg.FreeBSD.org/${ABI}/latest",
@@ -127,6 +151,7 @@ FreeBSD: {
   enabled: yes
 }
 .Ed
+.Pp
 .Bl -tag -width signature_type -compact
 .It url
 Refer to
diff --git a/usr.sbin/pkg/pkg.c b/usr.sbin/pkg/pkg.c
index aa45a96683ec..59348f7fc478 100644
--- a/usr.sbin/pkg/pkg.c
+++ b/usr.sbin/pkg/pkg.c
@@ -1042,7 +1042,7 @@ int
 main(int argc, char *argv[])
 {
 	char pkgpath[MAXPATHLEN];
-	const char *pkgarg;
+	const char *pkgarg, *repo_name;
 	bool activation_test, add_pkg, bootstrap_only, force, yes;
 	signed char ch;
 	const char *fetchOpts;
@@ -1055,6 +1055,7 @@ main(int argc, char *argv[])
 	fetchOpts = "";
 	force = false;
 	pkgarg = NULL;
+	repo_name = NULL;
 	yes = false;
 
 	struct option longopts[] = {
@@ -1068,7 +1069,7 @@ main(int argc, char *argv[])
 	snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg",
 	    getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE);
 
-	while ((ch = getopt_long(argc, argv, "-:fyN46", longopts, NULL)) != -1) {
+	while ((ch = getopt_long(argc, argv, "-:fr::yN46", longopts, NULL)) != -1) {
 		switch (ch) {
 		case 'f':
 			force = true;
@@ -1085,6 +1086,46 @@ main(int argc, char *argv[])
 		case '6':
 			fetchOpts = "6";
 			break;
+		case 'r':
+			/*
+			 * The repository can only be specified for an explicit
+			 * bootstrap request at this time, so that we don't
+			 * confuse the user if they're trying to use a verb that
+			 * has some other conflicting meaning but we need to
+			 * bootstrap.
+			 *
+			 * For that reason, we specify that -r has an optional
+			 * argument above and process the next index ourselves.
+			 * This is mostly significant because getopt(3) will
+			 * otherwise eat the next argument, which could be
+			 * something we need to try and make sense of.
+			 *
+			 * At worst this gets us false positives that we ignore
+			 * in other contexts, and we have to do a little fudging
+			 * in order to support separating -r from the reponame
+			 * with a space since it's not actually optional in
+			 * the bootstrap/add sense.
+			 */
+			if (add_pkg || bootstrap_only) {
+				if (optarg != NULL) {
+					repo_name = optarg;
+				} else if (optind < argc) {
+					repo_name = argv[optind];
+				}
+
+				if (repo_name == NULL || *repo_name == '\0') {
+					fprintf(stderr,
+					    "Must specify a repository with -r!\n");
+					exit(EXIT_FAILURE);
+				}
+
+				if (optarg == NULL) {
+					/* Advance past repo name. */
+					optreset = 1;
+					optind++;
+				}
+			}
+			break;
 		case 1:
 			// Non-option arguments, first one is the command
 			if (command == NULL) {
@@ -1124,7 +1165,7 @@ main(int argc, char *argv[])
 		if (activation_test)
 			errx(EXIT_FAILURE, "pkg is not installed");
 
-		config_init();
+		config_init(repo_name);
 
 		if (add_pkg) {
 			if (pkgarg == NULL) {



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202105050825.1458PU8Y028833>