xbps/bin/xbps-install/main.c

278 lines
7.2 KiB
C
Raw Normal View History

/*-
* Licensed under the SPDX BSD-2-Clause identifier.
* Use is subject to license terms, as specified in the LICENSE file.
*/
#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>
2013-06-20 16:01:02 +05:30
#include <xbps.h>
#include "defs.h"
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 <dir> Path to confdir (xbps.d)\n"
" -c, --cachedir <dir> Path to cachedir\n"
" -d, --debug Debug mode shown to stderr\n"
" -D, --download-only Download packages and check integrity, nothing else\n"
" -f, --force Force package re-installation\n"
" If specified twice, all files will be overwritten.\n"
" -h, --help Show usage\n"
" -i, --ignore-conf-repos Ignore repositories defined in xbps.d\n"
" -I, --ignore-file-conflicts Ignore detected file conflicts\n"
" -U, --unpack-only Unpack packages in transaction, do not configure them\n"
" -M, --memory-sync Remote repository data is fetched and stored\n"
" in memory, ignoring on-disk repodata archives\n"
" -n, --dry-run Dry-run mode\n"
" -R, --repository <url> Add repository to the top of the list\n"
" This option can be specified multiple times\n"
" -r, --rootdir <dir> Full path to rootdir\n"
" --reproducible Enable reproducible mode in pkgdb\n"
" -S, --sync Sync remote repository index\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);
}
2013-03-07 16:01:55 +05:30
static void
2018-07-18 07:54:26 +05:30
unpack_progress_cb(const struct xbps_unpack_cb_data *xpd, void *cbdata UNUSED)
2013-03-07 16:01:55 +05:30
{
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);
}
static int
2018-07-18 07:54:26 +05:30
repo_import_key_cb(struct xbps_repo *repo, void *arg UNUSED, bool *done UNUSED)
{
int rv;
2014-09-07 13:04:33 +05:30
if ((rv = xbps_repo_key_import(repo)) != 0)
fprintf(stderr, "Failed to import pubkey from %s: %s\n",
repo->uri, strerror(rv));
2014-09-07 13:04:33 +05:30
return rv;
}
int
main(int argc, char **argv)
{
const char *shortopts = "AC:c:DdfhIiMnR:r:SuUVvy";
const struct option longopts[] = {
{ "automatic", no_argument, NULL, 'A' },
{ "config", required_argument, NULL, 'C' },
{ "cachedir", required_argument, NULL, 'c' },
{ "debug", no_argument, NULL, 'd' },
{ "download-only", no_argument, NULL, 'D' },
{ "force", no_argument, NULL, 'f' },
{ "help", no_argument, NULL, 'h' },
{ "ignore-conf-repos", no_argument, NULL, 'i' },
{ "ignore-file-conflicts", no_argument, NULL, 'I' },
{ "memory-sync", no_argument, NULL, 'M' },
{ "dry-run", no_argument, NULL, 'n' },
{ "repository", required_argument, NULL, 'R' },
{ "rootdir", required_argument, NULL, 'r' },
{ "sync", no_argument, NULL, 'S' },
{ "unpack-only", no_argument, NULL, 'U' },
{ "update", no_argument, NULL, 'u' },
{ "verbose", no_argument, NULL, 'v' },
{ "version", no_argument, NULL, 'V' },
{ "yes", no_argument, NULL, 'y' },
{ "reproducible", no_argument, NULL, 1 },
{ NULL, 0, NULL, 0 }
};
struct xbps_handle xh;
struct xferstat xfer;
const char *rootdir, *cachedir, *confdir;
int i, c, flags, rv, fflag = 0;
bool syncf, yes, force, drun, update;
int maxcols, eexist = 0;
rootdir = cachedir = confdir = NULL;
flags = rv = 0;
syncf = yes = force = drun = update = false;
memset(&xh, 0, sizeof(xh));
while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (c) {
case 1:
flags |= XBPS_FLAG_INSTALL_REPRO;
break;
case 'A':
flags |= XBPS_FLAG_INSTALL_AUTO;
break;
case 'C':
confdir = optarg;
break;
case 'c':
cachedir = optarg;
break;
case 'd':
flags |= XBPS_FLAG_DEBUG;
break;
case 'D':
flags |= XBPS_FLAG_DOWNLOAD_ONLY;
break;
case 'f':
fflag++;
if (fflag > 1)
flags |= XBPS_FLAG_FORCE_UNPACK;
force = true;
break;
case 'h':
usage(false);
/* NOTREACHED */
case 'I':
flags |= XBPS_FLAG_IGNORE_FILE_CONFLICTS;
break;
case 'i':
flags |= XBPS_FLAG_IGNORE_CONF_REPOS;
break;
case 'M':
flags |= XBPS_FLAG_REPOS_MEMSYNC;
break;
case 'n':
drun = true;
break;
case 'R':
xbps_repo_store(&xh, optarg);
break;
case 'r':
rootdir = optarg;
break;
case 'S':
syncf = true;
break;
case 'U':
flags |= XBPS_FLAG_UNPACK_ONLY;
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 && !syncf) && (argc == optind))
usage(true);
/*
* Initialize libxbps.
*/
xh.state_cb = state_cb;
xh.fetch_cb = fetch_file_progress_cb;
xh.fetch_cb_data = &xfer;
if (rootdir)
xbps_strlcpy(xh.rootdir, rootdir, sizeof(xh.rootdir));
if (cachedir)
xbps_strlcpy(xh.cachedir, cachedir, sizeof(xh.cachedir));
if (confdir)
xbps_strlcpy(xh.confdir, confdir, sizeof(xh.confdir));
xh.flags = flags;
if (flags & XBPS_FLAG_VERBOSE)
2013-03-07 16:01:55 +05:30
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);
}
maxcols = get_maxcols();
/* Sync remote repository data and import keys from remote repos */
if (syncf && !drun) {
if ((rv = xbps_rpool_sync(&xh, NULL)) != 0)
exit(rv);
rv = xbps_rpool_foreach(&xh, repo_import_key_cb, NULL);
if (rv != 0)
exit(rv);
}
if (syncf && !update && (argc == optind))
exit(EXIT_SUCCESS);
xbps-install: improved -D,--download-only support. Added support to download all dependencies even if the euid does not have write perms to rootdir. In this mode we only care if cachedir is writable, rootdir access is not necessary. This is really useful to download all binary packages required by any number of packages as any regular user to later perform off-line installations, i.e: ``` $ xbps-install -c $PWD/cachedir -yD xbps ... $ tree cachedir cachedir/ ├── acl-2.2.53_1.x86_64-musl.xbps ├── acl-2.2.53_1.x86_64-musl.xbps.sig ├── attr-2.4.48_1.x86_64-musl.xbps ├── attr-2.4.48_1.x86_64-musl.xbps.sig ├── bzip2-1.0.8_1.x86_64-musl.xbps ├── bzip2-1.0.8_1.x86_64-musl.xbps.sig ├── ca-certificates-20190110_1.noarch.xbps ├── ca-certificates-20190110_1.noarch.xbps.sig ├── libarchive-3.4.1_1.x86_64-musl.xbps ├── libarchive-3.4.1_1.x86_64-musl.xbps.sig ├── libcrypto45-3.0.2_2.x86_64-musl.xbps ├── libcrypto45-3.0.2_2.x86_64-musl.xbps.sig ├── liblz4-1.9.2_1.x86_64-musl.xbps ├── liblz4-1.9.2_1.x86_64-musl.xbps.sig ├── liblzma-5.2.4_2.x86_64-musl.xbps ├── liblzma-5.2.4_2.x86_64-musl.xbps.sig ├── libressl-3.0.2_2.x86_64-musl.xbps ├── libressl-3.0.2_2.x86_64-musl.xbps.sig ├── libssl47-3.0.2_2.x86_64-musl.xbps ├── libssl47-3.0.2_2.x86_64-musl.xbps.sig ├── libtls19-3.0.2_2.x86_64-musl.xbps ├── libtls19-3.0.2_2.x86_64-musl.xbps.sig ├── libxbps-0.57.1_8.x86_64-musl.xbps ├── libxbps-0.57.1_8.x86_64-musl.xbps.sig ├── libzstd-1.4.4_1.x86_64-musl.xbps ├── libzstd-1.4.4_1.x86_64-musl.xbps.sig ├── musl-1.1.24_1.x86_64-musl.xbps ├── musl-1.1.24_1.x86_64-musl.xbps.sig ├── run-parts-4.9.1_1.x86_64-musl.xbps ├── run-parts-4.9.1_1.x86_64-musl.xbps.sig ├── xbps-0.57.1_8.x86_64-musl.xbps ├── xbps-0.57.1_8.x86_64-musl.xbps.sig ├── xbps-triggers-0.113_3.noarch.xbps ├── xbps-triggers-0.113_3.noarch.xbps.sig ├── zlib-1.2.11_3.x86_64-musl.xbps └── zlib-1.2.11_3.x86_64-musl.xbps.sig 0 directories, 36 files $ ``` Inpired by #213 Closes #213
2020-01-25 17:35:46 +05:30
if (!(xh.flags & XBPS_FLAG_DOWNLOAD_ONLY) && !drun) {
if ((rv = xbps_pkgdb_lock(&xh)) != 0) {
fprintf(stderr, "Failed to lock the pkgdb: %s\n", strerror(rv));
exit(rv);
}
}
eexist = optind;
if (update && (argc == optind)) {
/* Update all installed packages */
rv = dist_upgrade(&xh, maxcols, yes, drun);
} else if (update) {
/* Update target packages */
for (i = optind; i < argc; i++) {
rv = update_pkg(&xh, argv[i], force);
if (rv == EEXIST) {
/* pkg already updated, ignore */
rv = 0;
eexist++;
} else if (rv != 0) {
xbps_end(&xh);
exit(rv);
}
}
if (eexist == argc)
goto out;
rv = exec_transaction(&xh, maxcols, yes, drun);
} else if (!update) {
/* Install target packages */
for (i = optind; i < argc; i++) {
rv = install_new_pkg(&xh, argv[i], force);
if (rv == EEXIST) {
/* pkg already installed, ignore */
rv = 0;
eexist++;
} else if (rv != 0) {
xbps_end(&xh);
exit(rv);
}
}
if (eexist == argc)
goto out;
rv = exec_transaction(&xh, maxcols, yes, drun);
}
out:
xbps_end(&xh);
exit(rv);
}