Skip to content

xbps-alternatives: Add error message when group or package is not found #481

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions bin/xbps-alternatives/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion include/xbps.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 10 additions & 4 deletions lib/package_alternatives.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);

Expand Down