New utilities: xbps-{install,pkgdb,query,reconfigure,rindex} (WIP).

This commit is contained in:
Juan RP
2012-11-02 15:04:25 +01:00
parent 7fa8207cf3
commit b05ce9fe57
40 changed files with 5188 additions and 5 deletions

View File

@@ -0,0 +1,9 @@
TOPDIR = ../..
-include $(TOPDIR)/config.mk
BIN = xbps-install
OBJS = main.o transaction.o main.o question.o fetch_cb.o state_cb.o
OBJS += unpack_cb.o util.o
#MAN = $(BIN).8
include $(TOPDIR)/mk/prog.mk

69
bin/xbps-install/defs.h Normal file
View File

@@ -0,0 +1,69 @@
/*-
* Copyright (c) 2009-2012 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.
*/
#ifndef _XBPS_INSTALL_DEFS_H_
#define _XBPS_INSTALL_DEFS_H_
#include <sys/time.h>
#include <xbps_api.h>
struct xferstat {
struct timeval start;
struct timeval last;
};
/* from transaction.c */
int install_new_pkg(struct xbps_handle *, const char *, bool);
int update_pkg(struct xbps_handle *, const char *);
int dist_upgrade(struct xbps_handle *, size_t, bool, bool);
int exec_transaction(struct xbps_handle *, size_t, bool, bool);
/* from question.c */
bool yesno(const char *, ...);
bool noyes(const char *, ...);
/* from fetch_cb.c */
void fetch_file_progress_cb(struct xbps_handle *,
struct xbps_fetch_cb_data *,
void *);
/* from state_cb.c */
void state_cb(struct xbps_handle *,
struct xbps_state_cb_data *,
void *);
/* from unpack_cb.c */
void unpack_progress_cb_verbose(struct xbps_handle *,
struct xbps_unpack_cb_data *,
void *);
void unpack_progress_cb(struct xbps_handle *,
struct xbps_unpack_cb_data *,
void *);
/* From util.c */
void print_package_line(const char *, size_t, bool);
size_t get_maxcols(void);
#endif /* !_XBPS_INSTALL_DEFS_H_ */

176
bin/xbps-install/fetch_cb.c Normal file
View File

@@ -0,0 +1,176 @@
/*-
* Copyright (c) 2009-2012 Juan Romero Pardines
* Copyright (c) 2000-2004 Dag-Erling Coïdan Smørgrav
* 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
* in this position and unchanged.
* 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.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission
*
* 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.
*
* From FreeBSD fetch(8):
* $FreeBSD: src/usr.bin/fetch/fetch.c,v 1.84.2.1 2009/08/03 08:13:06 kensmith Exp $
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <xbps_api.h>
#include "defs.h"
#include "config.h"
static void
get_time(struct timeval *tvp)
{
#ifdef HAVE_CLOCK_GETTIME
struct timespec ts;
(void)clock_gettime(CLOCK_MONOTONIC, &ts);
tvp->tv_sec = ts.tv_sec;
tvp->tv_usec = ts.tv_nsec / 1000;
#else
(void)gettimeofday(tvp, NULL);
#endif
}
/*
* Compute and display ETA
*/
static const char *
stat_eta(struct xbps_fetch_cb_data *xfpd, void *cbdata)
{
struct xferstat *xfer = cbdata;
static char str[16];
long elapsed, eta;
off_t received, expected;
if (xfpd->file_size == -1)
return "unknown";
elapsed = xfer->last.tv_sec - xfer->start.tv_sec;
received = xfpd->file_dloaded - xfpd->file_offset;
expected = xfpd->file_size - xfpd->file_dloaded;
eta = (long)(elapsed * (long)expected / (long)received);
if (eta > 3600)
snprintf(str, sizeof str, "%02ldh%02ldm",
eta / 3600, (eta % 3600) / 60);
else
snprintf(str, sizeof str, "%02ldm%02lds",
eta / 60, eta % 60);
return str;
}
static inline bool
compare_double(const double a, const double b)
{
const double precision = 0.00001;
if ((a - precision) < b && (a + precision) > b)
return true;
else
return false;
}
/*
* Compute and display transfer rate
*/
static const char *
stat_bps(struct xbps_fetch_cb_data *xfpd, void *cbdata)
{
struct xferstat *xfer = cbdata;
static char str[16];
char size[8];
double delta, bps;
delta = (xfer->last.tv_sec + (xfer->last.tv_usec / 1.e6))
- (xfer->start.tv_sec + (xfer->start.tv_usec / 1.e6));
if (compare_double(delta, 0.0001)) {
snprintf(str, sizeof str, "-- stalled --");
} else {
bps =
((double)(xfpd->file_dloaded - xfpd->file_offset) / delta);
(void)xbps_humanize_number(size, (int64_t)bps);
snprintf(str, sizeof str, "%s/s", size);
}
return str;
}
/*
* Update the stats display
*/
static void
stat_display(struct xbps_fetch_cb_data *xfpd, void *cbdata)
{
struct xferstat *xfer = cbdata;
struct timeval now;
char totsize[8], recvsize[8];
int percentage;
get_time(&now);
if (now.tv_sec <= xfer->last.tv_sec)
return;
xfer->last = now;
if (xfpd->file_size == -1) {
percentage = 0;
snprintf(totsize, 3, "0B");
} else {
percentage = (int)((double)(100.0 *
(double)xfpd->file_dloaded) / (double)xfpd->file_size);
(void)xbps_humanize_number(totsize, (int64_t)xfpd->file_size);
}
(void)xbps_humanize_number(recvsize, (int64_t)xfpd->file_dloaded);
fprintf(stderr, "%s: %s [%d%% of %s] %s ETA: %s\033[K\r",
xfpd->file_name, recvsize, percentage, totsize,
stat_bps(xfpd, xfer), stat_eta(xfpd, xfer));
}
void
fetch_file_progress_cb(struct xbps_handle *xhp,
struct xbps_fetch_cb_data *xfpd,
void *cbdata)
{
struct xferstat *xfer = cbdata;
char size[8];
(void)xhp;
if (xfpd->cb_start) {
/* start transfer stats */
get_time(&xfer->start);
xfer->last.tv_sec = xfer->last.tv_usec = 0;
} else if (xfpd->cb_update) {
/* update transfer stats */
stat_display(xfpd, xfer);
} else if (xfpd->cb_end) {
/* end transfer stats */
(void)xbps_humanize_number(size, (int64_t)xfpd->file_dloaded);
fprintf(stderr,"Downloaded %s for %s [avg rate: %s]\033[K\n",
size, xfpd->file_name, stat_bps(xfpd, xfer));
}
}

242
bin/xbps-install/main.c Normal file
View File

@@ -0,0 +1,242 @@
/*-
* Copyright (c) 2008-2012 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 <strings.h>
#include <errno.h>
#include <signal.h>
#include <assert.h>
#include <unistd.h>
#include <getopt.h>
#include <xbps_api.h>
#include "defs.h"
static struct xbps_handle xh;
static void __attribute__((noreturn))
usage(bool fail)
{
fprintf(stdout,
"Usage: xbps-install [OPTIONS] [PKGNAME...]\n\n"
"OPTIONS\n"
" -A --automatic Set automatic installation mode\n"
" -C --config <file> Full path to configuration file\n"
" -c --cachedir <dir> Full path to cachedir\n"
" -d --debug Debug mode shown to stderr\n"
" -f --force Force package installation\n"
" -h --help Print help usage\n"
" -n --dry-run Dry-run mode\n"
" -p --print-format <fmt> Print format for dry-run mode\n"
" -R --repository <uri> Default repository to be used if config not set\n"
" -r --rootdir <dir> Full path to rootdir\n"
" -s --skip-sync Skip remote repository index sync\n"
" -u --update Update target package(s)\n"
" -v --verbose Verbose messages\n"
" -y --yes Assume yes to all questions\n"
" -V --version Show XBPS version\n");
exit(fail ? EXIT_FAILURE : EXIT_SUCCESS);
}
static void __attribute__((noreturn))
cleanup_sighandler(int signum)
{
xbps_end(&xh);
_exit(signum);
}
int
main(int argc, char **argv)
{
const char *shortopts = "AC:c:dfhnp:R:r:suVvy";
const struct option longopts[] = {
{ "automatic", no_argument, NULL, 'A' },
{ "config", required_argument, NULL, 'C' },
{ "cachedir", required_argument, NULL, 'c' },
{ "debug", no_argument, NULL, 'd' },
{ "force", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{ "dry-run", no_argument, NULL, 'n' },
{ "print-format", required_argument, NULL, 'p' },
{ "repository", required_argument, NULL, 'R' },
{ "rootdir", required_argument, NULL, 'r' },
{ "skip-sync", no_argument, NULL, 's' },
{ "update", no_argument, NULL, 'u' },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
{ "yes", no_argument, NULL, 'y' },
{ NULL, 0, NULL, 0 }
};
struct xferstat xfer;
struct sigaction sa;
const char *rootdir, *cachedir, *conffile, *defrepo, *pformat;
int i, c, flags, rv;
bool skip_sync, yes, reinstall, drun, update;
size_t maxcols;
rootdir = cachedir = conffile = defrepo = pformat = NULL;
flags = rv = 0;
skip_sync = yes = reinstall = drun = update = false;
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (c) {
case 'A':
flags |= XBPS_FLAG_INSTALL_AUTO;
break;
case 'C':
conffile = optarg;
break;
case 'c':
cachedir = optarg;
break;
case 'd':
flags |= XBPS_FLAG_DEBUG;
break;
case 'f':
reinstall = true;
break;
case 'h':
usage(false);
/* NOTREACHED */
case 'n':
drun = true;
break;
case 'p':
pformat = optarg;
break;
case 'R':
defrepo = optarg;
break;
case 'r':
rootdir = optarg;
break;
case 's':
skip_sync = true;
break;
case 'u':
update = true;
break;
case 'v':
flags |= XBPS_FLAG_VERBOSE;
break;
case 'V':
printf("%s\n", XBPS_RELVER);
exit(EXIT_SUCCESS);
case 'y':
yes = true;
break;
case '?':
default:
usage(true);
/* NOTREACHED */
}
}
if (!update && (argc == optind))
usage(true);
/*
* Initialize libxbps.
*/
memset(&xh, 0, sizeof(xh));
xh.state_cb = state_cb;
xh.fetch_cb = fetch_file_progress_cb;
xh.fetch_cb_data = &xfer;
xh.rootdir = rootdir;
xh.cachedir = cachedir;
xh.conffile = conffile;
xh.flags = flags;
xh.repository = defrepo;
if (flags & XBPS_FLAG_VERBOSE)
xh.unpack_cb = unpack_progress_cb_verbose;
else
xh.unpack_cb = unpack_progress_cb;
if ((rv = xbps_init(&xh)) != 0) {
xbps_error_printf("Failed to initialize libxbps: %s\n",
strerror(rv));
exit(EXIT_FAILURE);
}
/*
* Register a signal handler to clean up resources used by libxbps.
*/
memset(&sa, 0, sizeof(sa));
sa.sa_handler = cleanup_sighandler;
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
maxcols = get_maxcols();
/*
* Check that we have write permission on rootdir, metadir
* and cachedir.
*/
if ((!drun && ((access(xh.rootdir, W_OK) == -1) ||
(access(xh.metadir, W_OK) == -1) ||
(access(xh.cachedir, W_OK) == -1)))) {
if (errno != ENOENT) {
fprintf(stderr, "Not enough permissions on "
"rootdir/cachedir/metadir: %s\n",
strerror(errno));
rv = errno;
goto out;
}
}
/* Sync remote repository index files by default */
if (!skip_sync || !drun) {
rv = xbps_rpool_sync(&xh, NULL);
if (rv != 0)
goto out;
}
if (update && !argc) {
/* Update all installed packages */
rv = dist_upgrade(&xh, maxcols, yes, drun);
} else if (update && argc) {
/* Update target packages */
for (i = optind; i < argc; i++) {
rv = update_pkg(&xh, argv[i]);
if (rv != 0)
goto out;
}
rv = exec_transaction(&xh, maxcols, yes, drun);
} else if (!update && argc) {
/* Install target packages */
for (i = optind; i < argc; i++) {
rv = install_new_pkg(&xh, argv[i], reinstall);
if (rv != 0)
goto out;
}
rv = exec_transaction(&xh, maxcols, yes, drun);
}
out:
xbps_end(&xh);
exit(rv);
}

110
bin/xbps-install/question.c Normal file
View File

@@ -0,0 +1,110 @@
/*-
* Copyright (c) 2008-2010 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 <stdarg.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>
#include "defs.h"
static char *
strtrim(char *str)
{
char *pch = str;
if (str == NULL || *str == '\0')
return str;
while (isspace((unsigned char)*pch))
pch++;
if (pch != str)
memmove(str, pch, (strlen(pch) + 1));
if (*str == '\0')
return str;
pch = (str + (strlen(str) - 1));
while (isspace((unsigned char)*pch))
pch--;
*++pch = '\0';
return str;
}
static bool
question(bool preset, const char *fmt, va_list ap)
{
char response[32];
vfprintf(stderr, fmt, ap);
if (preset)
fprintf(stderr, " %s ", "[YES/no]");
else
fprintf(stderr, " %s ", "[yes/NO]");
if (fgets(response, sizeof(response), stdin)) {
(void)strtrim(response);
if (strlen(response) == 0)
return preset;
if (strcasecmp(response, "yes") == 0)
return true;
else if (strcasecmp(response, "no") == 0)
return false;
}
return false;
}
bool
yesno(const char *fmt, ...)
{
va_list ap;
bool res;
va_start(ap, fmt);
res = question(1, fmt, ap);
va_end(ap);
return res;
}
bool
noyes(const char *fmt, ...)
{
va_list ap;
bool res;
va_start(ap, fmt);
res = question(0, fmt, ap);
va_end(ap);
return res;
}

160
bin/xbps-install/state_cb.c Normal file
View File

@@ -0,0 +1,160 @@
/*-
* Copyright (c) 2011-2012 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 <stdlib.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>
#include <xbps_api.h>
#include "defs.h"
void
state_cb(struct xbps_handle *xhp,
struct xbps_state_cb_data *xscd,
void *cbdata)
{
prop_dictionary_t pkgd;
const char *version;
bool syslog_enabled = false;
(void)cbdata;
if (xhp->flags & XBPS_FLAG_SYSLOG) {
syslog_enabled = true;
openlog("xbps-bin", LOG_CONS, LOG_USER);
}
switch (xscd->state) {
/* notifications */
case XBPS_STATE_TRANS_DOWNLOAD:
printf("[*] Downloading binary packages\n");
break;
case XBPS_STATE_TRANS_VERIFY:
printf("[*] Verifying binary package integrity\n");
break;
case XBPS_STATE_TRANS_RUN:
printf("[*] Running transaction tasks\n");
break;
case XBPS_STATE_TRANS_CONFIGURE:
printf("[*] Configuring unpacked packages\n");
break;
case XBPS_STATE_REPOSYNC:
case XBPS_STATE_DOWNLOAD:
case XBPS_STATE_VERIFY:
case XBPS_STATE_CONFIG_FILE:
if (xscd->desc != NULL)
printf("%s\n", xscd->desc);
break;
case XBPS_STATE_REMOVE:
printf("Removing `%s-%s' ...\n", xscd->pkgname, xscd->version);
break;
case XBPS_STATE_CONFIGURE:
printf("Configuring `%s-%s' ...\n", xscd->pkgname,
xscd->version);
break;
case XBPS_STATE_REGISTER:
case XBPS_STATE_UNREGISTER:
/* empty */
break;
case XBPS_STATE_UNPACK:
printf("Unpacking `%s-%s' ...\n", xscd->pkgname, xscd->version);
break;
case XBPS_STATE_INSTALL:
printf("Installing `%s-%s' ...\n",
xscd->pkgname, xscd->version);
break;
case XBPS_STATE_UPDATE:
pkgd = xbps_find_pkg_dict_installed(xhp,
xscd->pkgname, false);
prop_dictionary_get_cstring_nocopy(pkgd, "version", &version);
printf("Updating `%s' (`%s' to `%s') ...\n", xscd->pkgname,
version, xscd->version);
break;
/* success */
case XBPS_STATE_REMOVE_FILE:
case XBPS_STATE_REMOVE_FILE_OBSOLETE:
if (xhp->flags & XBPS_FLAG_VERBOSE)
printf("%s\n", xscd->desc);
else {
printf("%s\n", xscd->desc);
printf("\033[1A\033[K");
}
break;
case XBPS_STATE_INSTALL_DONE:
printf("Installed `%s-%s' successfully.\n",
xscd->pkgname, xscd->version);
if (syslog_enabled)
syslog(LOG_NOTICE, "Installed `%s-%s' successfully "
"(rootdir: %s).", xscd->pkgname, xscd->version,
xhp->rootdir);
break;
case XBPS_STATE_UPDATE_DONE:
printf("Updated `%s' to `%s' successfully.\n",
xscd->pkgname, xscd->version);
if (syslog_enabled)
syslog(LOG_NOTICE, "Updated `%s' to `%s' successfully "
"(rootdir: %s).", xscd->pkgname, xscd->version,
xhp->rootdir);
break;
case XBPS_STATE_REMOVE_DONE:
printf("Removed `%s-%s' successfully.\n",
xscd->pkgname, xscd->version);
if (syslog_enabled)
syslog(LOG_NOTICE, "Removed `%s-%s' successfully "
"(rootdir: %s).", xscd->pkgname, xscd->version,
xhp->rootdir);
break;
/* errors */
case XBPS_STATE_UNPACK_FAIL:
case XBPS_STATE_UPDATE_FAIL:
case XBPS_STATE_CONFIGURE_FAIL:
case XBPS_STATE_REGISTER_FAIL:
case XBPS_STATE_UNREGISTER_FAIL:
case XBPS_STATE_REMOVE_FAIL:
case XBPS_STATE_VERIFY_FAIL:
case XBPS_STATE_DOWNLOAD_FAIL:
case XBPS_STATE_REPOSYNC_FAIL:
case XBPS_STATE_CONFIG_FILE_FAIL:
xbps_error_printf("%s\n", xscd->desc);
if (syslog_enabled)
syslog(LOG_ERR, "%s", xscd->desc);
break;
case XBPS_STATE_REMOVE_FILE_FAIL:
case XBPS_STATE_REMOVE_FILE_HASH_FAIL:
case XBPS_STATE_REMOVE_FILE_OBSOLETE_FAIL:
/* Ignore errors due to not empty directories */
if (xscd->err == ENOTEMPTY)
return;
xbps_error_printf("%s\n", xscd->desc);
if (syslog_enabled)
syslog(LOG_ERR, "%s", xscd->desc);
break;
default:
xbps_dbg_printf(xhp,
"unknown state %d\n", xscd->state);
break;
}
}

View File

@@ -0,0 +1,331 @@
/*-
* Copyright (c) 2009-2012 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 <errno.h>
#include <ctype.h>
#include <assert.h>
#include <unistd.h>
#include <limits.h>
#include <xbps_api.h>
#include "compat.h"
#include "defs.h"
#include "../xbps-repo/defs.h"
struct transaction {
prop_dictionary_t d;
prop_object_iterator_t iter;
uint32_t inst_pkgcnt;
uint32_t up_pkgcnt;
uint32_t cf_pkgcnt;
uint32_t rm_pkgcnt;
};
static void
show_missing_deps(prop_array_t a)
{
size_t i;
const char *str;
fprintf(stderr, "Unable to locate some required packages:\n");
for (i = 0; i < prop_array_count(a); i++) {
prop_array_get_cstring_nocopy(a, i, &str);
fprintf(stderr, " %s\n", str);
}
}
static void
show_conflicts(prop_array_t a)
{
size_t i;
const char *str;
fprintf(stderr, "Conflicting packages were found:\n");
for (i = 0; i < prop_array_count(a); i++) {
prop_array_get_cstring_nocopy(a, i, &str);
fprintf(stderr, " %s\n", str);
}
}
static void
show_actions(prop_object_iterator_t iter)
{
prop_object_t obj;
const char *repoloc, *trans, *pkgname, *version, *fname, *arch;
repoloc = trans = pkgname = version = fname = arch = NULL;
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
printf("%s %s %s", pkgname, trans, version);
prop_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
prop_dictionary_get_cstring_nocopy(obj, "filename", &fname);
prop_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
if (repoloc && fname && arch)
printf(" %s %s %s", repoloc, fname, arch);
printf("\n");
}
}
static void
show_package_list(prop_object_iterator_t iter, const char *match, size_t cols)
{
prop_object_t obj;
const char *pkgver, *tract;
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
prop_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
if (strcmp(match, tract))
continue;
print_package_line(pkgver, cols, false);
}
prop_object_iterator_reset(iter);
print_package_line(NULL, cols, true);
}
static int
show_transaction_sizes(struct transaction *trans, size_t cols)
{
uint64_t dlsize = 0, instsize = 0, rmsize = 0;
char size[8];
/*
* Show the list of packages that will be installed.
*/
if (prop_dictionary_get_uint32(trans->d, "total-install-pkgs",
&trans->inst_pkgcnt)) {
printf("%u package%s will be installed:\n",
trans->inst_pkgcnt, trans->inst_pkgcnt == 1 ? "" : "s");
show_package_list(trans->iter, "install", cols);
printf("\n");
}
if (prop_dictionary_get_uint32(trans->d, "total-update-pkgs",
&trans->up_pkgcnt)) {
printf("%u package%s will be updated:\n",
trans->up_pkgcnt, trans->up_pkgcnt == 1 ? "" : "s");
show_package_list(trans->iter, "update", cols);
printf("\n");
}
if (prop_dictionary_get_uint32(trans->d, "total-configure-pkgs",
&trans->cf_pkgcnt)) {
printf("%u package%s will be configured:\n",
trans->cf_pkgcnt, trans->cf_pkgcnt == 1 ? "" : "s");
show_package_list(trans->iter, "configure", cols);
printf("\n");
}
if (prop_dictionary_get_uint32(trans->d, "total-remove-pkgs",
&trans->rm_pkgcnt)) {
printf("%u package%s will be removed:\n",
trans->rm_pkgcnt, trans->rm_pkgcnt == 1 ? "" : "s");
show_package_list(trans->iter, "remove", cols);
printf("\n");
}
/*
* Show total download/installed/removed size for all required packages.
*/
printf("\n");
prop_dictionary_get_uint64(trans->d, "total-download-size", &dlsize);
if (dlsize > 0) {
if (xbps_humanize_number(size, (int64_t)dlsize) == -1) {
xbps_error_printf("humanize_number returns "
"%s\n", strerror(errno));
return -1;
}
printf("Total download size:\t%6s\n", size);
}
prop_dictionary_get_uint64(trans->d, "total-installed-size",
&instsize);
if (instsize > 0) {
if (xbps_humanize_number(size, (int64_t)instsize) == -1) {
xbps_error_printf("humanize_number2 returns "
"%s\n", strerror(errno));
return -1;
}
printf("Total installed size:\t%6s\n", size);
}
prop_dictionary_get_uint64(trans->d, "total-removed-size", &rmsize);
if (rmsize > 0) {
if (xbps_humanize_number(size, (int64_t)rmsize) == -1) {
xbps_error_printf("humanize_number3 returns "
"%s\n", strerror(errno));
return -1;
}
printf("Total freed size:\t%6s\n", size);
}
printf("\n");
return 0;
}
int
dist_upgrade(struct xbps_handle *xhp, size_t cols, bool yes, bool drun)
{
int rv = 0;
if ((rv = xbps_transaction_update_packages(xhp)) != 0) {
if (rv == ENOENT) {
printf("No packages currently registered.\n");
return 0;
} else if (rv == EEXIST) {
return 0;
} else if (rv == ENOTSUP) {
fprintf(stderr, "No repositories currently "
"registered!\n");
return -1;
} else {
fprintf(stderr, "Unexpected error %s\n",
strerror(rv));
return -1;
}
}
return exec_transaction(xhp, cols, yes, drun);
}
int
install_new_pkg(struct xbps_handle *xhp, const char *pkg, bool reinstall)
{
int rv;
if ((rv = xbps_transaction_install_pkg(xhp, pkg, reinstall)) != 0) {
if (rv == EEXIST) {
printf("Package `%s' already installed.\n", pkg);
} else if (rv == ENOENT) {
fprintf(stderr, "Unable to locate '%s' in "
"repository pool.\n", pkg);
} else if (rv == ENOTSUP) {
fprintf(stderr, "No repositories "
"currently registered!\n");
} else {
fprintf(stderr, "Unexpected error: %s\n",
strerror(rv));
rv = -1;
}
}
return rv;
}
int
update_pkg(struct xbps_handle *xhp, const char *pkgname)
{
int rv;
rv = xbps_transaction_update_pkg(xhp, pkgname);
if (rv == EEXIST)
printf("Package '%s' is up to date.\n", pkgname);
else if (rv == ENOENT)
fprintf(stderr, "Package '%s' not found in "
"repository pool.\n", pkgname);
else if (rv == ENODEV)
printf("Package '%s' not installed.\n", pkgname);
else if (rv == ENOTSUP)
fprintf(stderr, "No repositories currently registered!\n");
else if (rv != 0) {
fprintf(stderr, "Unexpected error: %s\n", strerror(rv));
return -1;
}
return rv;
}
int
exec_transaction(struct xbps_handle *xhp, size_t maxcols, bool yes, bool drun)
{
prop_array_t mdeps, cflicts;
struct transaction *trans;
int rv = 0;
trans = calloc(1, sizeof(*trans));
if (trans == NULL)
return ENOMEM;
if ((rv = xbps_transaction_prepare(xhp)) != 0) {
if (rv == ENODEV) {
mdeps =
prop_dictionary_get(xhp->transd, "missing_deps");
/* missing packages */
show_missing_deps(mdeps);
goto out;
} else if (rv == EAGAIN) {
/* conflicts */
cflicts = prop_dictionary_get(xhp->transd, "conflicts");
show_conflicts(cflicts);
goto out;
}
xbps_dbg_printf(xhp, "Empty transaction dictionary: %s\n",
strerror(errno));
return rv;
}
xbps_dbg_printf(xhp, "Dictionary before transaction happens:\n");
xbps_dbg_printf_append(xhp, "%s",
prop_dictionary_externalize(xhp->transd));
trans->d = xhp->transd;
trans->iter = xbps_array_iter_from_dict(xhp->transd, "packages");
assert(trans->iter);
/*
* dry-run mode, show what would be done but don't run anything.
*/
if (drun) {
show_actions(trans->iter);
goto out;
}
/*
* Show download/installed size for the transaction.
*/
if ((rv = show_transaction_sizes(trans, maxcols)) != 0)
goto out;
/*
* Ask interactively (if -y not set).
*/
if (!yes && !yesno("Do you want to continue?")) {
printf("Aborting!\n");
goto out;
}
/*
* It's time to run the transaction!
*/
if ((rv = xbps_transaction_commit(xhp)) == 0) {
printf("\n %u installed, %u updated, "
"%u configured, %u removed.\n", trans->inst_pkgcnt,
trans->up_pkgcnt, trans->cf_pkgcnt + trans->inst_pkgcnt,
trans->rm_pkgcnt);
}
out:
if (trans->iter)
prop_object_iterator_release(trans->iter);
if (trans)
free(trans);
return rv;
}

View File

@@ -0,0 +1,63 @@
/*-
* Copyright (c) 2008-2011 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 <stdlib.h>
#include <errno.h>
#include "defs.h"
void
unpack_progress_cb_verbose(struct xbps_handle *xhp,
struct xbps_unpack_cb_data *xpd,
void *cbdata)
{
(void)xhp;
(void)cbdata;
if (xpd->entry == NULL || xpd->entry_total_count <= 0)
return;
printf("%s: unpacked %sfile `%s' (%" PRIi64 " bytes)\n",
xpd->pkgver,
xpd->entry_is_conf ? "configuration " : "", xpd->entry,
xpd->entry_size);
}
void
unpack_progress_cb(struct xbps_handle *xhp,
struct xbps_unpack_cb_data *xpd,
void *cbdata)
{
(void)xhp;
(void)cbdata;
if (xpd->entry_total_count <= 0)
return;
printf("%s: unpacked %zd of %zd files...\n",
xpd->pkgver, xpd->entry_extract_count, xpd->entry_total_count);
printf("\033[1A\033[K");
}

71
bin/xbps-install/util.c Normal file
View File

@@ -0,0 +1,71 @@
/*-
* Copyright (c) 2008-2012 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 <errno.h>
#include <fnmatch.h>
#include <string.h>
#include <strings.h>
#include <sys/ioctl.h>
#include <xbps_api.h>
#include "defs.h"
size_t
get_maxcols(void)
{
struct winsize ws;
if (ioctl(fileno(stdin), TIOCGWINSZ, &ws) == 0)
return ws.ws_col;
return 80;
}
void
print_package_line(const char *str, size_t maxcols, bool reset)
{
static size_t cols;
static bool first;
if (reset) {
cols = 0;
first = false;
return;
}
cols += strlen(str) + 4;
if (cols <= maxcols) {
if (first == false) {
printf(" ");
first = true;
}
} else {
printf("\n ");
cols = strlen(str) + 4;
}
printf("%s ", str);
}