From f92cf55110a8046b26e2cd75feaf510cea7ba9d5 Mon Sep 17 00:00:00 2001 From: Ariel Cohen <74930666+CohenAriel@users.noreply.github.com> Date: Fri, 1 Apr 2022 00:01:53 +0300 Subject: [PATCH] xbps-alternatives: Add error message when group or package is not found --- bin/xbps-alternatives/main.c | 19 ++++++++++++++++--- include/xbps.h.in | 2 +- lib/package_alternatives.c | 14 ++++++++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/bin/xbps-alternatives/main.c b/bin/xbps-alternatives/main.c index e3aec59e3..b0166f52d 100644 --- a/bin/xbps-alternatives/main.c +++ b/bin/xbps-alternatives/main.c @@ -176,7 +176,7 @@ main(int argc, char **argv) }; struct xbps_handle xh; const char *confdir, *rootdir, *group, *pkg; - int c, rv, flags = 0; + int c, rv, set_err = 0, flags = 0; bool list_mode = false, set_mode = false; confdir = rootdir = group = pkg = NULL; @@ -246,8 +246,21 @@ main(int argc, char **argv) fprintf(stderr, "failed to lock pkgdb: %s\n", strerror(rv)); exit(EXIT_FAILURE); } - if ((rv = xbps_alternatives_set(&xh, pkg, group)) == 0) - rv = xbps_pkgdb_update(&xh, true, false); + if ((rv = xbps_alternatives_set(&xh, pkg, group, &set_err)) == 0){ + rv = xbps_pkgdb_update(&xh, true, false);} + else switch (set_err) + { + case 1: + xbps_error_printf("Couldn't find package %s\n", pkg); + break; + case 2: + xbps_error_printf("Package %s has no alternatives\n", pkg); + break; + case 3: + xbps_error_printf("Package %s not in group %s\n", pkg, group); + break; + } + } else if (list_mode) { /* list alternative groups */ rv = list_alternatives(&xh, pkg, group); diff --git a/include/xbps.h.in b/include/xbps.h.in index 7cb1bf59d..1d8527aab 100644 --- a/include/xbps.h.in +++ b/include/xbps.h.in @@ -1009,7 +1009,7 @@ int xbps_pkg_exec_script(struct xbps_handle *xhp, * * @return 0 on success, or an errno value otherwise. */ -int xbps_alternatives_set(struct xbps_handle *xhp, const char *pkg, const char *group); +int xbps_alternatives_set(struct xbps_handle *xhp, const char *pkg, const char *group, int *err); /** * Registers all alternative groups provided by a package. diff --git a/lib/package_alternatives.c b/lib/package_alternatives.c index f23b440d0..927e5f238 100644 --- a/lib/package_alternatives.c +++ b/lib/package_alternatives.c @@ -236,7 +236,7 @@ create_symlinks(struct xbps_handle *xhp, xbps_array_t a, const char *grname) int xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname, - const char *group) + const char *group, int *err) { xbps_array_t allkeys; xbps_dictionary_t alternatives, pkg_alternatives, pkgd, prevpkgd, prevpkg_alts; @@ -251,15 +251,21 @@ xbps_alternatives_set(struct xbps_handle *xhp, const char *pkgname, return ENOENT; pkgd = xbps_pkgdb_get_pkg(xhp, pkgname); - if (pkgd == NULL) + if (pkgd == NULL) { + *err = 1; return ENOENT; + } pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives"); - if (!xbps_dictionary_count(pkg_alternatives)) + if (!xbps_dictionary_count(pkg_alternatives)) { + *err = 2; return ENOENT; + } - if (group && !xbps_dictionary_get(pkg_alternatives, group)) + if (group && !xbps_dictionary_get(pkg_alternatives, group)) { + *err = 3; return ENOENT; + } xbps_dictionary_get_cstring_nocopy(pkgd, "pkgver", &pkgver);