2012-11-02 19:34:25 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2009-2013 Juan Romero Pardines.
|
2012-11-02 19:34:25 +05:30
|
|
|
* 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>
|
|
|
|
|
2013-06-20 16:01:02 +05:30
|
|
|
#include <xbps.h>
|
2012-11-02 19:34:25 +05:30
|
|
|
#include "defs.h"
|
|
|
|
|
|
|
|
struct transaction {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_t d;
|
|
|
|
xbps_object_iterator_t iter;
|
2012-11-02 19:34:25 +05:30
|
|
|
uint32_t inst_pkgcnt;
|
|
|
|
uint32_t up_pkgcnt;
|
|
|
|
uint32_t cf_pkgcnt;
|
|
|
|
uint32_t rm_pkgcnt;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
2013-06-20 13:56:12 +05:30
|
|
|
show_missing_deps(xbps_array_t a)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(a); i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(a, i, &str);
|
2013-06-27 20:22:31 +05:30
|
|
|
fprintf(stderr, "%s\n", str);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-20 13:56:12 +05:30
|
|
|
show_conflicts(xbps_array_t a)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
|
|
|
const char *str;
|
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < xbps_array_count(a); i++) {
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_get_cstring_nocopy(a, i, &str);
|
2013-06-27 20:22:31 +05:30
|
|
|
fprintf(stderr, "%s\n", str);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-20 13:56:12 +05:30
|
|
|
show_actions(xbps_object_iterator_t iter)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj;
|
2013-03-05 08:38:42 +05:30
|
|
|
const char *repoloc, *trans, *pkgver, *arch;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-03-05 08:38:42 +05:30
|
|
|
repoloc = trans = pkgver = arch = NULL;
|
2012-11-02 19:34:25 +05:30
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "transaction", &trans);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
2013-03-11 16:42:03 +05:30
|
|
|
printf("%s %s", pkgver, trans);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "architecture", &arch);
|
2013-03-11 16:42:03 +05:30
|
|
|
if (repoloc && arch)
|
|
|
|
printf(" %s %s", arch, repoloc);
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2013-06-20 13:56:12 +05:30
|
|
|
show_package_list(xbps_object_iterator_t iter, const char *match, int cols)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_t obj;
|
2012-11-02 19:34:25 +05:30
|
|
|
const char *pkgver, *tract;
|
|
|
|
|
2013-06-20 13:56:12 +05:30
|
|
|
while ((obj = xbps_object_iterator_next(iter)) != NULL) {
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "pkgver", &pkgver);
|
|
|
|
xbps_dictionary_get_cstring_nocopy(obj, "transaction", &tract);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (strcmp(match, tract))
|
|
|
|
continue;
|
|
|
|
print_package_line(pkgver, cols, false);
|
|
|
|
}
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_reset(iter);
|
2012-11-02 19:34:25 +05:30
|
|
|
print_package_line(NULL, cols, true);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2013-06-12 13:34:10 +05:30
|
|
|
show_transaction_sizes(struct transaction *trans, int cols)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
|
|
|
uint64_t dlsize = 0, instsize = 0, rmsize = 0;
|
|
|
|
char size[8];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Show the list of packages that will be installed.
|
|
|
|
*/
|
2013-12-16 11:54:17 +05:30
|
|
|
xbps_dictionary_get_uint32(trans->d, "total-install-pkgs",
|
|
|
|
&trans->inst_pkgcnt);
|
|
|
|
if (trans->inst_pkgcnt) {
|
2012-11-02 19:34:25 +05:30
|
|
|
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");
|
|
|
|
}
|
2013-12-16 11:54:17 +05:30
|
|
|
xbps_dictionary_get_uint32(trans->d, "total-update-pkgs",
|
|
|
|
&trans->up_pkgcnt);
|
|
|
|
if (trans->up_pkgcnt) {
|
2012-11-02 19:34:25 +05:30
|
|
|
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");
|
|
|
|
}
|
2013-12-16 11:54:17 +05:30
|
|
|
xbps_dictionary_get_uint32(trans->d, "total-configure-pkgs",
|
|
|
|
&trans->cf_pkgcnt);
|
|
|
|
if (trans->cf_pkgcnt) {
|
2012-11-02 19:34:25 +05:30
|
|
|
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");
|
|
|
|
}
|
2013-12-16 11:54:17 +05:30
|
|
|
xbps_dictionary_get_uint32(trans->d, "total-remove-pkgs",
|
|
|
|
&trans->rm_pkgcnt);
|
|
|
|
if (trans->rm_pkgcnt) {
|
2012-11-02 19:34:25 +05:30
|
|
|
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.
|
|
|
|
*/
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_uint64(trans->d, "total-download-size", &dlsize);
|
|
|
|
xbps_dictionary_get_uint64(trans->d, "total-installed-size",
|
2013-03-08 14:31:19 +05:30
|
|
|
&instsize);
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get_uint64(trans->d, "total-removed-size", &rmsize);
|
2013-03-08 14:31:19 +05:30
|
|
|
if (dlsize || instsize || rmsize)
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
if (dlsize) {
|
2012-11-02 19:34:25 +05:30
|
|
|
if (xbps_humanize_number(size, (int64_t)dlsize) == -1) {
|
|
|
|
xbps_error_printf("humanize_number returns "
|
|
|
|
"%s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-16 11:54:17 +05:30
|
|
|
printf("Size to download: %6s\n", size);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-03-08 14:31:19 +05:30
|
|
|
if (instsize) {
|
2013-12-16 11:54:17 +05:30
|
|
|
if (xbps_humanize_number(size, (int64_t)instsize) == -1) {
|
2012-11-02 19:34:25 +05:30
|
|
|
xbps_error_printf("humanize_number2 returns "
|
|
|
|
"%s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-16 11:54:17 +05:30
|
|
|
printf("Size required on disk: %6s\n", size);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
2013-03-08 14:31:19 +05:30
|
|
|
if (rmsize) {
|
2012-11-02 19:34:25 +05:30
|
|
|
if (xbps_humanize_number(size, (int64_t)rmsize) == -1) {
|
|
|
|
xbps_error_printf("humanize_number3 returns "
|
|
|
|
"%s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
2013-12-16 11:54:17 +05:30
|
|
|
printf("Size freed on disk: %6s\n", size);
|
2012-11-02 19:34:25 +05:30
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2013-06-12 13:34:10 +05:30
|
|
|
dist_upgrade(struct xbps_handle *xhp, int cols, bool yes, bool drun)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
|
|
|
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
|
2013-06-12 13:34:10 +05:30
|
|
|
exec_transaction(struct xbps_handle *xhp, int maxcols, bool yes, bool drun)
|
2012-11-02 19:34:25 +05:30
|
|
|
{
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_array_t mdeps, cflicts;
|
2012-11-02 19:34:25 +05:30
|
|
|
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 =
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_get(xhp->transd, "missing_deps");
|
2012-11-02 19:34:25 +05:30
|
|
|
/* missing packages */
|
|
|
|
show_missing_deps(mdeps);
|
2013-06-27 20:22:31 +05:30
|
|
|
fprintf(stderr, "Transaction aborted due to missing/conflicting packages.\n");
|
2012-11-02 19:34:25 +05:30
|
|
|
goto out;
|
|
|
|
} else if (rv == EAGAIN) {
|
|
|
|
/* conflicts */
|
2013-06-20 13:56:12 +05:30
|
|
|
cflicts = xbps_dictionary_get(xhp->transd, "conflicts");
|
2012-11-02 19:34:25 +05:30
|
|
|
show_conflicts(cflicts);
|
2013-06-27 20:22:31 +05:30
|
|
|
fprintf(stderr, "Transaction aborted due to missing/conflicting packages.\n");
|
2012-11-02 19:34:25 +05:30
|
|
|
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",
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_dictionary_externalize(xhp->transd));
|
2012-11-02 19:34:25 +05:30
|
|
|
|
|
|
|
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) {
|
2012-11-16 21:25:35 +05:30
|
|
|
printf("\n%u installed, %u updated, "
|
2012-11-02 19:34:25 +05:30
|
|
|
"%u configured, %u removed.\n", trans->inst_pkgcnt,
|
|
|
|
trans->up_pkgcnt, trans->cf_pkgcnt + trans->inst_pkgcnt,
|
|
|
|
trans->rm_pkgcnt);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (trans->iter)
|
2013-06-20 13:56:12 +05:30
|
|
|
xbps_object_iterator_release(trans->iter);
|
2012-11-02 19:34:25 +05:30
|
|
|
if (trans)
|
|
|
|
free(trans);
|
|
|
|
return rv;
|
|
|
|
}
|