Alternatives framework for xbps (2/2).
See xbps-alternatives(1) for more information. Thanks to all who helped to design this and for fixing grammar in the manual page.
This commit is contained in:
6
bin/xbps-alternatives/Makefile
Normal file
6
bin/xbps-alternatives/Makefile
Normal file
@@ -0,0 +1,6 @@
|
||||
TOPDIR = ../..
|
||||
-include $(TOPDIR)/config.mk
|
||||
|
||||
BIN = xbps-alternatives
|
||||
|
||||
include $(TOPDIR)/mk/prog.mk
|
252
bin/xbps-alternatives/main.c
Normal file
252
bin/xbps-alternatives/main.c
Normal file
@@ -0,0 +1,252 @@
|
||||
/*-
|
||||
* Copyright (c) 2015 Juan Romero Pardines.
|
||||
* 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 <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <getopt.h>
|
||||
#include <errno.h>
|
||||
#include <assert.h>
|
||||
#include <syslog.h>
|
||||
|
||||
#include <xbps.h>
|
||||
|
||||
static void __attribute__((noreturn))
|
||||
usage(bool fail)
|
||||
{
|
||||
fprintf(stdout,
|
||||
"Usage: xbps-alternatives [OPTIONS] MODE\n\n"
|
||||
"OPTIONS\n"
|
||||
" -C --config <dir> Path to confdir (xbps.d)\n"
|
||||
" -d --debug Debug mode shown to stderr\n"
|
||||
" -g --group <name> Group of alternatives to match\n"
|
||||
" -h --help Print usage help\n"
|
||||
" -r --rootdir <dir> Full path to rootdir\n"
|
||||
" -v --verbose Verbose messages\n"
|
||||
" -V --version Show XBPS version\n"
|
||||
"MODE\n"
|
||||
" -l --list [PKG] List all alternatives or from PKG\n"
|
||||
" -s --set PKG Set alternatives for PKG\n\n");
|
||||
exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
static int
|
||||
state_cb(const struct xbps_state_cb_data *xscd, void *cbd _unused)
|
||||
{
|
||||
bool slog = false;
|
||||
|
||||
if ((xscd->xhp->flags & XBPS_FLAG_DISABLE_SYSLOG) == 0) {
|
||||
slog = true;
|
||||
openlog("xbps-alternatives", 0, LOG_USER);
|
||||
}
|
||||
if (xscd->desc) {
|
||||
printf("%s\n", xscd->desc);
|
||||
if (slog)
|
||||
syslog(LOG_NOTICE, "%s", xscd->desc);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void
|
||||
list_pkg_alternatives(xbps_dictionary_t pkgd, bool print_key)
|
||||
{
|
||||
xbps_dictionary_t pkg_alternatives;
|
||||
xbps_array_t allkeys;
|
||||
|
||||
pkg_alternatives = xbps_dictionary_get(pkgd, "alternatives");
|
||||
if (pkg_alternatives == NULL)
|
||||
return;
|
||||
|
||||
allkeys = xbps_dictionary_all_keys(pkg_alternatives);
|
||||
for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
|
||||
xbps_object_t keysym;
|
||||
xbps_array_t array;
|
||||
const char *keyname;
|
||||
|
||||
keysym = xbps_array_get(allkeys, i);
|
||||
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
|
||||
array = xbps_dictionary_get_keysym(pkg_alternatives, keysym);
|
||||
|
||||
if (print_key)
|
||||
printf("%s\n", keyname);
|
||||
|
||||
for (unsigned int x = 0; x < xbps_array_count(array); x++) {
|
||||
const char *str;
|
||||
|
||||
xbps_array_get_cstring_nocopy(array, x, &str);
|
||||
printf(" - %s\n", str);
|
||||
}
|
||||
}
|
||||
xbps_object_release(allkeys);
|
||||
}
|
||||
|
||||
static int
|
||||
list_alternatives(struct xbps_handle *xhp, const char *pkgname)
|
||||
{
|
||||
xbps_dictionary_t alternatives, pkgd;
|
||||
xbps_array_t allkeys;
|
||||
|
||||
(void)xbps_pkgdb_get_pkg(xhp, "foo");
|
||||
|
||||
if (pkgname) {
|
||||
/* list alternatives for pkgname */
|
||||
if ((pkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL)
|
||||
return ENOENT;
|
||||
|
||||
list_pkg_alternatives(pkgd, true);
|
||||
return 0;
|
||||
}
|
||||
assert(xhp->pkgdb);
|
||||
|
||||
alternatives = xbps_dictionary_get(xhp->pkgdb, "_XBPS_ALTERNATIVES_");
|
||||
if (alternatives == NULL)
|
||||
return ENOENT;
|
||||
|
||||
allkeys = xbps_dictionary_all_keys(alternatives);
|
||||
for (unsigned int i = 0; i < xbps_array_count(allkeys); i++) {
|
||||
xbps_array_t array;
|
||||
xbps_object_t keysym;
|
||||
const char *keyname;
|
||||
|
||||
keysym = xbps_array_get(allkeys, i);
|
||||
keyname = xbps_dictionary_keysym_cstring_nocopy(keysym);
|
||||
array = xbps_dictionary_get_keysym(alternatives, keysym);
|
||||
|
||||
printf("%s\n", keyname);
|
||||
for (unsigned int x = 0; x < xbps_array_count(array); x++) {
|
||||
const char *str;
|
||||
|
||||
xbps_array_get_cstring_nocopy(array, x, &str);
|
||||
printf(" - %s%s\n", str, x == 0 ? " (current)" : "");
|
||||
pkgd = xbps_pkgdb_get_pkg(xhp, str);
|
||||
assert(pkgd);
|
||||
list_pkg_alternatives(pkgd, false);
|
||||
}
|
||||
}
|
||||
xbps_object_release(allkeys);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
const char *shortopts = "C:dg:hls:r:Vv";
|
||||
const struct option longopts[] = {
|
||||
{ "config", required_argument, NULL, 'C' },
|
||||
{ "debug", no_argument, NULL, 'd' },
|
||||
{ "group", required_argument, NULL, 'g' },
|
||||
{ "help", no_argument, NULL, 'h' },
|
||||
{ "list", no_argument, NULL, 'l' },
|
||||
{ "set", required_argument, NULL, 's' },
|
||||
{ "rootdir", required_argument, NULL, 'r' },
|
||||
{ "verbose", no_argument, NULL, 'v' },
|
||||
{ "version", no_argument, NULL, 'V' },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
};
|
||||
struct xbps_handle xh;
|
||||
const char *confdir, *rootdir, *group, *pkg;
|
||||
int c, rv, flags = 0;
|
||||
bool list_mode = false, set_mode = false;
|
||||
|
||||
confdir = rootdir = group = pkg = NULL;
|
||||
|
||||
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
||||
switch (c) {
|
||||
case 'C':
|
||||
confdir = optarg;
|
||||
break;
|
||||
case 'd':
|
||||
flags |= XBPS_FLAG_DEBUG;
|
||||
break;
|
||||
case 'g':
|
||||
group = optarg;
|
||||
break;
|
||||
case 'h':
|
||||
usage(false);
|
||||
/* NOTREACHED */
|
||||
case 'l':
|
||||
list_mode = true;
|
||||
break;
|
||||
case 's':
|
||||
set_mode = true;
|
||||
pkg = optarg;
|
||||
break;
|
||||
case 'r':
|
||||
rootdir = optarg;
|
||||
break;
|
||||
case 'v':
|
||||
flags |= XBPS_FLAG_VERBOSE;
|
||||
break;
|
||||
case 'V':
|
||||
printf("%s\n", XBPS_RELVER);
|
||||
exit(EXIT_SUCCESS);
|
||||
case '?':
|
||||
default:
|
||||
usage(true);
|
||||
/* NOTREACHED */
|
||||
}
|
||||
}
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
if (!list_mode && !set_mode)
|
||||
usage(true);
|
||||
else if (argc && list_mode)
|
||||
pkg = *argv;
|
||||
|
||||
memset(&xh, 0, sizeof(xh));
|
||||
xh.state_cb = state_cb;
|
||||
if (rootdir)
|
||||
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
|
||||
if (confdir)
|
||||
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
|
||||
|
||||
xh.flags = flags;
|
||||
|
||||
/* initialize xbps */
|
||||
if ((rv = xbps_init(&xh)) != 0) {
|
||||
xbps_error_printf("Failed to initialize libxbps: %s\n",
|
||||
strerror(rv));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
if (set_mode) {
|
||||
/* in set mode pkgdb must be locked and flushed on success */
|
||||
if ((rv = xbps_pkgdb_lock(&xh)) != 0) {
|
||||
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, true);
|
||||
} else if (list_mode) {
|
||||
/* list alternative groups */
|
||||
rv = list_alternatives(&xh, pkg);
|
||||
}
|
||||
|
||||
xbps_end(&xh);
|
||||
exit(rv ? EXIT_FAILURE : EXIT_SUCCESS);
|
||||
}
|
89
bin/xbps-alternatives/xbps-alternatives.1
Normal file
89
bin/xbps-alternatives/xbps-alternatives.1
Normal file
@@ -0,0 +1,89 @@
|
||||
.Dd October 30, 2015
|
||||
.Dt XBPS-ALTERNATIVES 1
|
||||
.Sh NAME
|
||||
.Nm xbps-alternatives
|
||||
.Nd XBPS utility to handle alternatives
|
||||
.Sh SYNOPSIS
|
||||
.Nm xbps-alternatives
|
||||
.Op OPTIONS
|
||||
.Ar MODE
|
||||
.Sh DESCRIPTION
|
||||
The
|
||||
.Nm
|
||||
utility lists or sets the alternatives provided by installed packages.
|
||||
Alternatives are classified by groups, and a group contains a number
|
||||
of symbolic links which are applied when the group is set.
|
||||
.Pp
|
||||
When a package is installed its alternative groups are registered in the package database (pkgdb).
|
||||
.Pp
|
||||
When a package is removed its alternative groups are unregistered from the package database (pkgdb).
|
||||
If there are no more alternative groups, the package database removes the references. If there were
|
||||
alternative groups registered previously, the previous package is set as default provider.
|
||||
.Sh OPTIONS
|
||||
.Bl -tag -width -x
|
||||
.It Fl C, Fl -config Ar dir
|
||||
Specifies a path to the XBPS configuration directory.
|
||||
If the first character is not '\/' then it's a relative path of
|
||||
.Ar rootdir .
|
||||
.It Fl d, Fl -debug
|
||||
Enables extra debugging shown to stderr.
|
||||
.It Fl g, Fl -group
|
||||
Alternative group name to match. To be used with the
|
||||
.Ar set
|
||||
mode.
|
||||
.It Fl h, Fl -help
|
||||
Show the help message.
|
||||
.It Fl r, Fl -rootdir Ar dir
|
||||
Specifies a full path for the target root directory.
|
||||
.It Fl v, Fl -verbose
|
||||
Enables verbose messages.
|
||||
.It Fl V, Fl -version
|
||||
Show the version information.
|
||||
.El
|
||||
.Sh MODE
|
||||
Only one of the following modes can be used at a time.
|
||||
.Bl -tag -width -x
|
||||
.It Fl l, Fl -list Op PKG
|
||||
Lists all current alternatives or only from
|
||||
.Ar PKG .
|
||||
.It Fl s, Fl -set Ar PKG Op -g Ar group
|
||||
Set alternative groups specified by
|
||||
.Ar PKG
|
||||
or just a specific group, if the
|
||||
.Fl g Fl -group
|
||||
option is set.
|
||||
.El
|
||||
.Sh FILES
|
||||
.Bl -tag -width /var/db/xbps/.<pkgname>-files.plist
|
||||
.It Ar /etc/xbps.d
|
||||
Default configuration directory.
|
||||
.It Ar /usr/share/xbps.d
|
||||
Default system configuration directory.
|
||||
.It Ar /var/db/xbps/.<pkgname>-files.plist
|
||||
Package files metadata.
|
||||
.It Ar /var/db/xbps/pkgdb-0.38.plist
|
||||
Default package database (0.38 format). Keeps track of installed packages and properties.
|
||||
.It Ar /var/cache/xbps
|
||||
Default cache directory to store downloaded binary packages.
|
||||
.El
|
||||
.Sh SEE ALSO
|
||||
.Xr xbps-checkvers 1 ,
|
||||
.Xr xbps-create 1 ,
|
||||
.Xr xbps-dgraph 1 ,
|
||||
.Xr xbps-fbulk 1 ,
|
||||
.Xr xbps-install 1 ,
|
||||
.Xr xbps-pkgdb 1 ,
|
||||
.Xr xbps-query 1 ,
|
||||
.Xr xbps-reconfigure 1 ,
|
||||
.Xr xbps-remove 1 ,
|
||||
.Xr xbps-rindex 1 ,
|
||||
.Xr xbps-uchroot 1 ,
|
||||
.Xr xbps-uunshare 1 ,
|
||||
.Xr xbps.d 5
|
||||
.Sh AUTHORS
|
||||
.An Juan Romero Pardines <xtraeme@gmail.com>
|
||||
.Sh BUGS
|
||||
Probably, but I try to make this not happen. Use it under your own
|
||||
responsibility and enjoy your life.
|
||||
.Pp
|
||||
Report bugs at https://github.com/voidlinux/xbps/issues
|
Reference in New Issue
Block a user