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
This commit is contained in:
Kyle Evans 2021-02-11 18:58:26 -06:00
parent bf80e08ed5
commit 18418e1936
4 changed files with 91 additions and 19 deletions

View file

@ -322,7 +322,7 @@ config_parse(const ucl_object_t *obj, pkg_conf_file_t conftype)
* 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;
@ -337,13 +337,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;
@ -367,7 +371,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);
@ -377,7 +381,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;
@ -401,8 +405,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;
}
@ -416,7 +422,7 @@ load_repositories(const char *repodir)
}
int
config_init(void)
config_init(const char *requested_repo)
{
char *val;
int i;
@ -457,7 +463,7 @@ config_init(void)
localbase = getlocalbase();
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;
@ -475,7 +481,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:

View file

@ -58,7 +58,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 *);

View file

@ -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

View file

@ -1045,7 +1045,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;
@ -1058,6 +1058,7 @@ main(int argc, char *argv[])
fetchOpts = "";
force = false;
pkgarg = NULL;
repo_name = NULL;
yes = false;
struct option longopts[] = {
@ -1070,7 +1071,7 @@ main(int argc, char *argv[])
snprintf(pkgpath, MAXPATHLEN, "%s/sbin/pkg", getlocalbase());
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;
@ -1087,6 +1088,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) {
@ -1126,7 +1167,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) {