From be7e532c3cf86006ef5f4b6bd7334087202ede9b Mon Sep 17 00:00:00 2001 From: classabbyamp Date: Sat, 23 Apr 2022 18:49:25 -0400 Subject: [PATCH] xbps-reconfigure: add ability to reconfigure dependencies adds `-x/--deps` and `--fulldeptree`, that behave similar to the xbps-query modes fixes #464 --- bin/xbps-reconfigure/Makefile | 1 + bin/xbps-reconfigure/defs.h | 34 +++++++++++++++ bin/xbps-reconfigure/find-deps.c | 49 +++++++++++++++++++++ bin/xbps-reconfigure/main.c | 58 ++++++++++++++++++++++--- bin/xbps-reconfigure/xbps-reconfigure.1 | 10 +++++ 5 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 bin/xbps-reconfigure/defs.h create mode 100644 bin/xbps-reconfigure/find-deps.c diff --git a/bin/xbps-reconfigure/Makefile b/bin/xbps-reconfigure/Makefile index 3cfcde4c..dfe161ca 100644 --- a/bin/xbps-reconfigure/Makefile +++ b/bin/xbps-reconfigure/Makefile @@ -2,5 +2,6 @@ TOPDIR = ../.. -include $(TOPDIR)/config.mk BIN = xbps-reconfigure +OBJS = main.o find-deps.o include $(TOPDIR)/mk/prog.mk diff --git a/bin/xbps-reconfigure/defs.h b/bin/xbps-reconfigure/defs.h new file mode 100644 index 00000000..4e6bed34 --- /dev/null +++ b/bin/xbps-reconfigure/defs.h @@ -0,0 +1,34 @@ +/*- + * Copyright (c) 2022 classabbyamp. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#ifndef _XBPS_RECONFIGURE_DEFS_H_ +#define _XBPS_RECONFIGURE_DEFS_H_ + +#include + +/* from find-deps.c */ +int find_pkg_deps(struct xbps_handle *, const char *, bool, xbps_array_t *); + +#endif /* !_XBPS_RECONFIGURE_DEFS_H_ */ diff --git a/bin/xbps-reconfigure/find-deps.c b/bin/xbps-reconfigure/find-deps.c new file mode 100644 index 00000000..ceab4081 --- /dev/null +++ b/bin/xbps-reconfigure/find-deps.c @@ -0,0 +1,49 @@ +/*- + * Copyright (c) 2022 classabbyamp. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include + +#include +#include "defs.h" + +int +find_pkg_deps(struct xbps_handle *xhp, const char *pkgname, bool full, xbps_array_t *rdeps) +{ + xbps_dictionary_t pkgd; + + if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) { + return errno; + } + + if (full) { + *rdeps = xbps_pkgdb_get_pkg_fulldeptree(xhp, pkgname); + + if (*rdeps == NULL) + return errno; + } else { + *rdeps = xbps_dictionary_get(pkgd, "run_depends"); + } + return 0; +} diff --git a/bin/xbps-reconfigure/main.c b/bin/xbps-reconfigure/main.c index 2cd41d58..c1f4e2fc 100644 --- a/bin/xbps-reconfigure/main.c +++ b/bin/xbps-reconfigure/main.c @@ -32,6 +32,7 @@ #include #include +#include "defs.h" static void __attribute__((noreturn)) usage(bool fail) @@ -43,9 +44,11 @@ usage(bool fail) " -C, --config Path to confdir (xbps.d)\n" " -d, --debug Debug mode shown to stderr\n" " -f, --force Force reconfiguration\n" + " --fulldeptree Full dependency tree for -x/--deps\n" " -h, --help Show usage\n" " -i, --ignore PKG Ignore PKG with -a/--all\n" " -r, --rootdir Full path to rootdir\n" + " -x, --deps Also process dependencies for each package\n" " -v, --verbose Verbose messages\n" " -V, --version Show XBPS version\n"); exit(fail ? EXIT_FAILURE : EXIT_SUCCESS); @@ -90,7 +93,7 @@ state_cb(const struct xbps_state_cb_data *xscd, void *cbd UNUSED) int main(int argc, char **argv) { - const char *shortopts = "aC:dfhi:r:Vv"; + const char *shortopts = "aC:dfhi:r:xVv"; const struct option longopts[] = { { "all", no_argument, NULL, 'a' }, { "config", required_argument, NULL, 'C' }, @@ -99,15 +102,17 @@ main(int argc, char **argv) { "help", no_argument, NULL, 'h' }, { "ignore", required_argument, NULL, 'i' }, { "rootdir", required_argument, NULL, 'r' }, + { "deps", no_argument, NULL, 'x' }, { "verbose", no_argument, NULL, 'v' }, { "version", no_argument, NULL, 'V' }, + { "fulldeptree", no_argument, NULL, 1 }, { NULL, 0, NULL, 0 } }; struct xbps_handle xh; const char *confdir = NULL, *rootdir = NULL; int c, i, rv, flags = 0; - bool all = false; - xbps_array_t ignpkgs = NULL; + bool all = false, rdeps = false, fulldeptree = false; + xbps_array_t ignpkgs = NULL, deps = NULL; while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { switch (c) { @@ -135,12 +140,18 @@ main(int argc, char **argv) case 'r': rootdir = optarg; break; + case 'x': + rdeps = true; + break; case 'v': flags |= XBPS_FLAG_VERBOSE; break; case 'V': printf("%s\n", XBPS_RELVER); exit(EXIT_SUCCESS); + case 1: + fulldeptree = true; + break; case '?': default: usage(true); @@ -168,7 +179,7 @@ main(int argc, char **argv) } if ((rv = xbps_pkgdb_lock(&xh)) != 0) { - fprintf(stderr, "failed to lock pkgdb: %s\n", strerror(rv)); + xbps_error_printf("failed to lock pkgdb: %s\n", strerror(rv)); exit(EXIT_FAILURE); } @@ -176,10 +187,43 @@ main(int argc, char **argv) rv = xbps_configure_packages(&xh, ignpkgs); } else { for (i = optind; i < argc; i++) { - rv = xbps_configure_pkg(&xh, argv[i], true, false); + const char* pkg = argv[i]; + if (rdeps) { + rv = find_pkg_deps(&xh, pkg, fulldeptree, &deps); + if (rv != 0) { + xbps_error_printf("failed to collect dependencies for " + "`%s': %s\n", pkg, strerror(rv)); + } + for (unsigned int j = 0; j < xbps_array_count(deps); j++) { + const char *pkgdep = NULL; + char pkgname[XBPS_NAME_SIZE]; + xbps_array_get_cstring_nocopy(deps, j, &pkgdep); + + if (fulldeptree) { + if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgdep)) { + xbps_error_printf( + "unable to get package name for dependency `%s'\n", pkgdep); + exit(EXIT_FAILURE); + } + } else { + if (!xbps_pkgpattern_name(pkgname, sizeof(pkgname), pkgdep)) { + xbps_error_printf( + "unable to get package name for dependency `%s'\n", pkgdep); + exit(EXIT_FAILURE); + } + } + + rv = xbps_configure_pkg(&xh, pkgname, true, false); + if (rv != 0) { + xbps_error_printf("failed to reconfigure " + "`%s': %s\n", pkgname, strerror(rv)); + } + } + } + rv = xbps_configure_pkg(&xh, pkg, true, false); if (rv != 0) { - fprintf(stderr, "Failed to reconfigure " - "`%s': %s\n", argv[i], strerror(rv)); + xbps_error_printf("failed to reconfigure " + "`%s': %s\n", pkg, strerror(rv)); } } } diff --git a/bin/xbps-reconfigure/xbps-reconfigure.1 b/bin/xbps-reconfigure/xbps-reconfigure.1 index 65fcb9ae..b3052d86 100644 --- a/bin/xbps-reconfigure/xbps-reconfigure.1 +++ b/bin/xbps-reconfigure/xbps-reconfigure.1 @@ -56,6 +56,16 @@ argument can be a package name or a package name with version. This option can be specified multiple times. .It Fl r, Fl -rootdir Ar dir Specifies a path for the target root directory. +.It Fl x, Fl -deps +Configure +.Ar PKGNAME... +and its direct dependencies. +.It Fl -fulldeptree +Configure the full dependency tree of +.Ar PKGNAME... +when used with +.Fl x, Fl -deps . +. .It Fl v, Fl -verbose Enables verbose messages. .It Fl V, Fl -version