Initial import of xbps with code as August '09.

--HG--
extra : convert_revision : juan%40xbps-20090817170720-amxxac4a2e8bza1j
This commit is contained in:
juan
2009-08-17 19:07:20 +02:00
commit 3f3b6d00dd
52 changed files with 8657 additions and 0 deletions

5
bin/xbps-bin/Makefile Normal file
View File

@@ -0,0 +1,5 @@
BIN = xbps-bin
OBJS = check.o install.o main.o remove.o ../xbps-repo/util.o
TOPDIR = ../..
include $(TOPDIR)/prog.mk

275
bin/xbps-bin/check.c Normal file
View File

@@ -0,0 +1,275 @@
/*-
* Copyright (c) 2009 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 <xbps_api.h>
#include "defs.h"
/*
* Checks package integrity of an installed package. This
* consists in four tasks:
*
* o Check for metadata files (files.plist and props.plist),
* we only check if the file exists and its dictionary can
* be externalized and is not empty.
* o Check for missing installed files.
* o Check the hash for all installed files, except
* configuration files (which is expected if they are modified).
* o Check for missing run time dependencies.
*
* If any of these checks fail, the package will change its
* state to 'broken'.
*/
int
xbps_check_pkg_integrity(const char *pkgname)
{
prop_dictionary_t pkgd, propsd, filesd;
prop_array_t array;
prop_object_t obj;
prop_object_iterator_t iter;
const char *rootdir, *file, *sha256, *reqpkg;
char *path;
int rv = 0;
bool files_broken = false, broken = false;
assert(pkgname != NULL);
rootdir = xbps_get_rootdir();
pkgd = xbps_find_pkg_installed_from_plist(pkgname);
if (pkgd == NULL) {
printf("Package %s is not installed.\n", pkgname);
return 0;
}
/*
* Check for props.plist metadata file.
*/
path = xbps_xasprintf("%s/%s/metadata/%s/%s", rootdir,
XBPS_META_PATH, pkgname, XBPS_PKGPROPS);
if (path == NULL) {
rv = errno;
goto out;
}
propsd = prop_dictionary_internalize_from_file(path);
free(path);
if (propsd == NULL) {
printf("%s: unexistent %s metadata file.\n", pkgname,
XBPS_PKGPROPS);
rv = errno;
broken = true;
goto out;
} else if (prop_object_type(propsd) != PROP_TYPE_DICTIONARY) {
printf("%s: invalid %s metadata file.\n", pkgname,
XBPS_PKGPROPS);
rv = EINVAL;
broken = true;
goto out1;
} else if (prop_dictionary_count(propsd) == 0) {
printf("%s: incomplete %s metadata file.\n", pkgname,
XBPS_PKGPROPS);
rv = EINVAL;
broken = true;
goto out1;
}
/*
* Check for files.plist metadata file.
*/
path = xbps_xasprintf("%s/%s/metadata/%s/%s", rootdir,
XBPS_META_PATH, pkgname, XBPS_PKGFILES);
if (path == NULL) {
rv = errno;
goto out1;
}
filesd = prop_dictionary_internalize_from_file(path);
free(path);
if (filesd == NULL) {
printf("%s: unexistent %s metadata file.\n", pkgname,
XBPS_PKGPROPS);
rv = ENOENT;
broken = true;
goto out1;
} else if (prop_object_type(filesd) != PROP_TYPE_DICTIONARY) {
printf("%s: invalid %s metadata file.\n", pkgname,
XBPS_PKGFILES);
rv = EINVAL;
broken = true;
goto out2;
} else if (prop_dictionary_count(filesd) == 0) {
printf("%s: incomplete %s metadata file.\n", pkgname,
XBPS_PKGFILES);
rv = EINVAL;
broken = true;
goto out2;
} else if (((array = prop_dictionary_get(filesd, "files")) == NULL) ||
((array = prop_dictionary_get(filesd, "links")) == NULL) ||
((array = prop_dictionary_get(filesd, "dirs")) == NULL)) {
printf("%s: incomplete %s metadata file.\n", pkgname,
XBPS_PKGFILES);
rv = EINVAL;
broken = true;
goto out2;
}
/*
* Check for missing files and its hash.
*/
array = prop_dictionary_get(filesd, "files");
if ((prop_object_type(array) == PROP_TYPE_ARRAY) &&
prop_array_count(array) > 0) {
iter = xbps_get_array_iter_from_dict(filesd, "files");
if (iter == NULL) {
rv = ENOMEM;
goto out2;
}
while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj, "file", &file);
path = xbps_xasprintf("%s/%s", rootdir, file);
if (path == NULL) {
prop_object_iterator_release(iter);
rv = errno;
goto out2;
}
prop_dictionary_get_cstring_nocopy(obj,
"sha256", &sha256);
rv = xbps_check_file_hash(path, sha256);
switch (rv) {
case 0:
break;
case ENOENT:
printf("%s: unexistent file %s.\n",
pkgname, file);
files_broken = true;
broken = true;
break;
case ERANGE:
printf("%s: hash mismatch for %s.\n",
pkgname, file);
files_broken = true;
broken = true;
break;
default:
printf("%s: unexpected error for %s (%s)\n",
pkgname, file, strerror(rv));
break;
}
free(path);
}
prop_object_iterator_release(iter);
if (files_broken)
printf("%s: files check FAILED.\n", pkgname);
}
/*
* Check for missing configuration files.
*/
array = prop_dictionary_get(filesd, "conf_files");
if (array && prop_object_type(array) == PROP_TYPE_ARRAY &&
prop_array_count(array) > 0) {
iter = xbps_get_array_iter_from_dict(filesd, "conf_files");
if (iter == NULL) {
rv = ENOMEM;
goto out2;
}
while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj, "file", &file);
path = xbps_xasprintf("%s/%s", rootdir, file);
if (path == NULL) {
prop_object_iterator_release(iter);
rv = ENOMEM;
goto out2;
}
if ((rv = access(path, R_OK)) == -1) {
if (errno == ENOENT) {
printf("%s: unexistent file %s\n",
pkgname, file);
broken = true;
} else
printf("%s: unexpected error for "
"%s (%s)\n", pkgname, file,
strerror(errno));
}
free(path);
}
prop_object_iterator_release(iter);
if (rv != 0)
printf("%s: configuration files check FAILED.\n",
pkgname);
}
/*
* Check for missing run time dependencies.
*/
if (xbps_pkg_has_rundeps(propsd)) {
iter = xbps_get_array_iter_from_dict(propsd, "run_depends");
if (iter == NULL) {
rv = ENOMEM;
goto out2;
}
while ((obj = prop_object_iterator_next(iter))) {
reqpkg = prop_string_cstring_nocopy(obj);
if (xbps_check_is_installed_pkg(reqpkg) < 0) {
rv = ENOENT;
printf("%s: dependency not satisfied: %s\n",
pkgname, reqpkg);
broken = true;
}
}
prop_object_iterator_release(iter);
if (rv == ENOENT)
printf("%s: run-time dependency check FAILED.\n",
pkgname);
}
out2:
prop_object_release(filesd);
out1:
prop_object_release(propsd);
out:
prop_object_release(pkgd);
if (broken) {
rv = xbps_set_pkg_state_installed(pkgname,
XBPS_PKG_STATE_BROKEN);
if (rv == 0)
printf("%s: changed package state to broken.\n",
pkgname);
else
printf("%s: can't change package state (%s).\n",
pkgname, strerror(rv));
}
xbps_release_regpkgdb_dict();
return rv;
}

35
bin/xbps-bin/defs.h Normal file
View File

@@ -0,0 +1,35 @@
/*-
* Copyright (c) 2009 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_BIN_DEFS_H_
#define _XBPS_BIN_DEFS_H_
void xbps_install_pkg(const char *, bool, bool);
void xbps_autoremove_pkgs(void);
void xbps_remove_installed_pkg(const char *, bool);
void xbps_autoupdate_pkgs(bool);
int xbps_check_pkg_integrity(const char *);
#endif /* !_XBPS_BIN_DEFS_H_ */

501
bin/xbps-bin/install.c Normal file
View File

@@ -0,0 +1,501 @@
/*-
* Copyright (c) 2009 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 <xbps_api.h>
#include "defs.h"
struct transaction {
prop_dictionary_t dict;
prop_object_iterator_t iter;
const char *originpkgname;
bool force;
};
static void cleanup(int);
static int exec_transaction(struct transaction *);
static void show_missing_deps(prop_dictionary_t, const char *);
static int show_missing_dep_cb(prop_object_t, void *, bool *);
static void show_package_list(prop_object_iterator_t, const char *);
static void
show_missing_deps(prop_dictionary_t d, const char *pkgname)
{
printf("Unable to locate some required packages for %s:\n",
pkgname);
(void)xbps_callback_array_iter_in_dict(d, "missing_deps",
show_missing_dep_cb, NULL);
}
static int
show_missing_dep_cb(prop_object_t obj, void *arg, bool *loop_done)
{
const char *pkgname, *version;
(void)arg;
(void)loop_done;
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
if (pkgname && version) {
printf(" * Missing binary package for: %s >= %s\n",
pkgname, version);
return 0;
}
return EINVAL;
}
static int
check_pkg_hashes(prop_object_iterator_t iter)
{
prop_object_t obj;
const char *pkgname, *repoloc, *filename;
int rv = 0;
pkg_state_t state = 0;
printf("Checking binary package file(s) integrity...\n");
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
state = 0;
if (xbps_get_pkg_state_dictionary(obj, &state) != 0)
return EINVAL;
if (state == XBPS_PKG_STATE_UNPACKED)
continue;
prop_dictionary_get_cstring_nocopy(obj, "repository", &repoloc);
prop_dictionary_get_cstring_nocopy(obj, "filename", &filename);
rv = xbps_check_pkg_file_hash(obj, repoloc);
if (rv != 0 && rv != ERANGE) {
printf("Unexpected error while checking hash for "
"%s (%s)\n", filename, strerror(rv));
return -1;
} else if (rv != 0 && rv == ERANGE) {
printf("Hash mismatch for %s, exiting.\n",
filename);
return -1;
}
}
prop_object_iterator_reset(iter);
return 0;
}
static void
show_package_list(prop_object_iterator_t iter, const char *match)
{
prop_object_t obj;
size_t cols = 0;
const char *pkgname, *version, *tract;
bool first = false;
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
prop_dictionary_get_cstring_nocopy(obj, "trans-action", &tract);
if (strcmp(match, tract))
continue;
cols += strlen(pkgname) + strlen(version) + 4;
if (cols <= 80) {
if (first == false) {
printf(" ");
first = true;
}
} else {
printf("\n ");
cols = strlen(pkgname) + strlen(version) + 4;
}
printf("%s-%s ", pkgname, version);
}
prop_object_iterator_reset(iter);
}
static int
show_transaction_sizes(prop_object_iterator_t iter)
{
prop_object_t obj;
uint64_t tsize = 0, dlsize = 0, instsize = 0;
const char *tract;
char size[64];
bool trans_inst = false, trans_up = false;
/*
* Iterate over the list of packages that are going to be
* installed and check the file hash.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_uint64(obj, "filename-size", &tsize);
dlsize += tsize;
tsize = 0;
prop_dictionary_get_uint64(obj, "installed_size", &tsize);
instsize += tsize;
tsize = 0;
}
prop_object_iterator_reset(iter);
while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj, "trans-action", &tract);
if (strcmp(tract, "install") == 0)
trans_inst = true;
else if (strcmp(tract, "update") == 0)
trans_up = true;
}
prop_object_iterator_reset(iter);
/*
* Show the list of packages that will be installed.
*/
if (trans_inst) {
printf("The following packages will be installed:\n\n");
show_package_list(iter, "install");
printf("\n\n");
}
if (trans_up) {
printf("The following packages will be updated:\n\n");
show_package_list(iter, "update");
printf("\n\n");
}
/*
* Show total download/installed size for all required packages.
*/
if (xbps_humanize_number(size, 5, (int64_t)dlsize,
"", HN_AUTOSCALE, HN_NOSPACE) == -1) {
printf("error: humanize_number returns %s\n",
strerror(errno));
return -1;
}
printf("Total download size: %s\n", size);
if (xbps_humanize_number(size, 5, (int64_t)instsize,
"", HN_AUTOSCALE, HN_NOSPACE) == -1) {
printf("error: humanize_number2 returns %s\n",
strerror(errno));
return -1;
}
printf("Total installed size: %s\n\n", size);
return 0;
}
void
xbps_install_pkg(const char *pkg, bool force, bool update)
{
struct transaction *trans;
prop_dictionary_t pkgd;
prop_array_t array;
int rv = 0;
/*
* Find all required pkgs and sort the package transaction.
*/
pkgd = xbps_find_pkg_installed_from_plist(pkg);
if (update) {
if (pkgd) {
if ((rv = xbps_find_new_pkg(pkg, pkgd)) == 0) {
printf("Package '%s' is up to date.\n", pkg);
prop_object_release(pkgd);
cleanup(rv);
}
prop_object_release(pkgd);
} else {
printf("Package '%s' not installed.\n", pkg);
cleanup(rv);
}
} else {
if (pkgd) {
printf("Package '%s' is already installed.\n", pkg);
prop_object_release(pkgd);
cleanup(rv);
}
rv = xbps_prepare_pkg(pkg);
if (rv != 0 && rv == EAGAIN) {
printf("unable to locate %s in repository pool.", pkg);
cleanup(rv);
} else if (rv != 0 && rv != ENOENT) {
printf("unexpected error: %s", strerror(rv));
cleanup(rv);
}
}
trans = calloc(1, sizeof(struct transaction));
if (trans == NULL)
goto out;
trans->dict = xbps_get_pkg_props();
if (trans->dict == NULL) {
printf("error: unexistent props dictionary!\n");
goto out1;
}
/*
* Bail out if there are unresolved deps.
*/
array = prop_dictionary_get(trans->dict, "missing_deps");
if (prop_array_count(array) > 0) {
show_missing_deps(trans->dict, pkg);
goto out2;
}
prop_dictionary_get_cstring_nocopy(trans->dict,
"origin", &trans->originpkgname);
/*
* It's time to run the transaction!
*/
trans->iter = xbps_get_array_iter_from_dict(trans->dict, "packages");
if (trans->iter == NULL) {
printf("error: allocating array mem! (%s)",
strerror(errno));
goto out2;
}
trans->force = force;
rv = exec_transaction(trans);
prop_object_iterator_release(trans->iter);
out2:
prop_object_release(trans->dict);
out1:
free(trans);
out:
cleanup(rv);
}
static int
exec_transaction(struct transaction *trans)
{
prop_dictionary_t instpkgd;
prop_object_t obj;
const char *pkgname, *version, *instver, *filename, *tract;
int rv = 0;
bool essential, isdep, autoinst;
pkg_state_t state = 0;
assert(trans != NULL);
assert(trans->dict != NULL);
assert(trans->iter != NULL);
essential = isdep = autoinst = false;
/*
* Show download/installed size for the transaction.
*/
rv = show_transaction_sizes(trans->iter);
if (rv != 0)
return rv;
/*
* Ask interactively (if -f not set).
*/
if (trans->force == false) {
if (xbps_noyes("Do you want to continue?") == false) {
printf("Aborting!\n");
return 0;
}
}
/*
* Check the SHA256 hash for all required packages.
*/
if ((rv = check_pkg_hashes(trans->iter)) != 0)
return rv;
/*
* Iterate over the transaction dictionary.
*/
while ((obj = prop_object_iterator_next(trans->iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
prop_dictionary_get_bool(obj, "essential", &essential);
prop_dictionary_get_cstring_nocopy(obj, "filename", &filename);
prop_dictionary_get_cstring_nocopy(obj, "trans-action", &tract);
if (trans->originpkgname &&
strcmp(trans->originpkgname, pkgname))
isdep = true;
/*
* If dependency is already unpacked skip this phase.
*/
state = 0;
if (xbps_get_pkg_state_dictionary(obj, &state) != 0)
return EINVAL;
if (state == XBPS_PKG_STATE_UNPACKED)
continue;
if (strcmp(tract, "update") == 0) {
instpkgd = xbps_find_pkg_installed_from_plist(pkgname);
if (instpkgd == NULL) {
printf("error: unable to find %s installed "
"dict!\n", pkgname);
return EINVAL;
}
prop_dictionary_get_cstring_nocopy(instpkgd,
"version", &instver);
autoinst = false;
prop_dictionary_get_bool(instpkgd, "automatic-install",
&autoinst);
isdep = autoinst;
prop_object_release(instpkgd);
/*
* If this package is not 'essential', just remove
* the old package and install the new one. Otherwise
* we just overwrite the files.
*/
if (essential == false) {
rv = xbps_remove_pkg(pkgname, version, true);
if (rv != 0) {
printf("error: removing %s-%s (%s)\n",
pkgname, instver, strerror(rv));
return rv;
}
}
}
/*
* Unpack binary package.
*/
printf("Unpacking %s-%s (from .../%s) ...\n", pkgname, version,
filename);
if ((rv = xbps_unpack_binary_pkg(obj, essential)) != 0) {
printf("error: unpacking %s-%s (%s)\n", pkgname,
version, strerror(rv));
return rv;
}
/*
* Register binary package.
*/
if ((rv = xbps_register_pkg(obj, isdep)) != 0) {
printf("error: registering %s-%s! (%s)\n",
pkgname, version, strerror(rv));
return rv;
}
isdep = false;
/*
* Set package state to unpacked in the transaction
* dictionary.
*/
if ((rv = xbps_set_pkg_state_dictionary(obj,
XBPS_PKG_STATE_UNPACKED)) != 0)
return rv;
}
prop_object_iterator_reset(trans->iter);
/*
* Configure all unpacked packages.
*/
while ((obj = prop_object_iterator_next(trans->iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
if ((rv = xbps_configure_pkg(pkgname)) != 0) {
printf("Error configuring package %s (%s)\n",
pkgname, strerror(rv));
return rv;
}
}
return 0;
}
void
xbps_autoupdate_pkgs(bool force)
{
struct transaction *trans;
int rv = 0;
/*
* Find new package versions.
*/
if ((rv = xbps_find_new_packages()) != 0) {
if (rv == ENOENT) {
printf("No packages currently registered.\n");
cleanup(0);
}
goto out;
}
/*
* Prepare transaction data.
*/
trans = calloc(1, sizeof(struct transaction));
if (trans == NULL)
goto out;
/*
* Get package transaction dictionary.
*/
trans->dict = xbps_get_pkg_props();
if (trans->dict == NULL) {
if (errno == 0) {
printf("All packages are up-to-date.\n");
goto out;
}
printf("Error while checking for new pkgs: %s\n",
strerror(errno));
goto out1;
}
/*
* Sort the package transaction dictionary.
*/
if ((rv = xbps_sort_pkg_deps(trans->dict)) != 0) {
printf("Error while sorting packages: %s\n",
strerror(rv));
goto out2;
}
/*
* It's time to run the transaction!
*/
trans->iter = xbps_get_array_iter_from_dict(trans->dict, "packages");
if (trans->iter == NULL) {
printf("error: allocating array mem! (%s)\n", strerror(errno));
goto out2;
}
trans->force = force;
rv = exec_transaction(trans);
prop_object_iterator_release(trans->iter);
out2:
prop_object_release(trans->dict);
out1:
free(trans);
out:
cleanup(rv);
}
static void
cleanup(int rv)
{
xbps_release_repolist_data();
xbps_release_regpkgdb_dict();
exit(rv == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

276
bin/xbps-bin/main.c Normal file
View File

@@ -0,0 +1,276 @@
/*-
* Copyright (c) 2008-2009 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 <xbps_api.h>
#include "defs.h"
#include "../xbps-repo/util.h"
static void usage(void);
static int list_pkgs_in_dict(prop_object_t, void *, bool *);
static void
usage(void)
{
printf("Usage: xbps-bin [options] [target] [arguments]\n\n"
" Available targets:\n"
" autoremove, autoupdate, check, files, install, list\n"
" purge, remove, show, update\n"
" Targets with arguments:\n"
" check\t<pkgname>\n"
" files\t<pkgname>\n"
" install\t<pkgname>\n"
" purge\t[<pkgname>|<all>]\n"
" reconfigure\t[<pkgname>|<all>]\n"
" remove\t<pkgname>\n"
" show\t<pkgname>\n"
" update\t<pkgname>\n"
" Options shared by all targets:\n"
" -r\t\t<rootdir>\n"
" -v\t\t<verbose>\n"
" Options used by the (auto)remove and install target:\n"
" -f\t\tForce installation or removal of packages.\n"
" \t\tBeware with this option if you use autoremove!\n"
"\n");
exit(EXIT_FAILURE);
}
static int
list_pkgs_in_dict(prop_object_t obj, void *arg, bool *loop_done)
{
const char *pkgname, *version, *short_desc;
(void)arg;
(void)loop_done;
assert(prop_object_type(obj) == PROP_TYPE_DICTIONARY);
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
prop_dictionary_get_cstring_nocopy(obj, "short_desc", &short_desc);
if (pkgname && version && short_desc) {
printf("%s-%s\t%s\n", pkgname, version, short_desc);
return 0;
}
return EINVAL;
}
int
main(int argc, char **argv)
{
prop_dictionary_t dict;
prop_object_t obj;
prop_object_iterator_t iter;
const char *curpkgname;
int c, flags = 0, rv = 0;
bool force = false, verbose = false;
while ((c = getopt(argc, argv, "Cfr:v")) != -1) {
switch (c) {
case 'f':
flags |= XBPS_FLAG_FORCE;
force = true;
break;
case 'r':
/* To specify the root directory */
xbps_set_rootdir(optarg);
break;
case 'v':
verbose = true;
flags |= XBPS_FLAG_VERBOSE;
break;
case '?':
default:
usage();
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
if (flags != 0)
xbps_set_flags(flags);
if ((dict = xbps_prepare_regpkgdb_dict()) == NULL) {
if (errno != ENOENT) {
rv = errno;
printf("Couldn't initialized regpkgdb dict: %s\n",
strerror(errno));
goto out;
}
}
if (strcasecmp(argv[0], "list") == 0) {
/* Lists packages currently registered in database. */
if (argc != 1)
usage();
if (dict == NULL) {
printf("No packages currently installed.\n");
goto out;
}
if (!xbps_callback_array_iter_in_dict(dict, "packages",
list_pkgs_in_dict, NULL)) {
rv = errno;
goto out;
}
} else if (strcasecmp(argv[0], "install") == 0) {
/* Installs a binary package and required deps. */
if (argc != 2)
usage();
xbps_install_pkg(argv[1], force, false);
} else if (strcasecmp(argv[0], "update") == 0) {
/* Update an installed package. */
if (argc != 2)
usage();
xbps_install_pkg(argv[1], force, true);
} else if (strcasecmp(argv[0], "remove") == 0) {
/* Removes a binary package. */
if (argc != 2)
usage();
xbps_remove_installed_pkg(argv[1], force);
} else if (strcasecmp(argv[0], "show") == 0) {
/* Shows info about an installed binary package. */
if (argc != 2)
usage();
rv = show_pkg_info_from_metadir(argv[1]);
if (rv != 0) {
printf("Package %s not installed.\n", argv[1]);
exit(EXIT_FAILURE);
}
} else if (strcasecmp(argv[0], "files") == 0) {
/* Shows files installed by a binary package. */
if (argc != 2)
usage();
rv = show_pkg_files_from_metadir(argv[1]);
if (rv != 0) {
printf("Package %s not installed.\n", argv[1]);
exit(EXIT_FAILURE);
}
} else if (strcasecmp(argv[0], "check") == 0) {
/* Checks the integrity of an installed package. */
if (argc != 2)
usage();
rv = xbps_check_pkg_integrity(argv[1]);
} else if (strcasecmp(argv[0], "autoupdate") == 0) {
/*
* To update all packages currently installed.
*/
if (argc != 1)
usage();
xbps_autoupdate_pkgs(force);
} else if (strcasecmp(argv[0], "autoremove") == 0) {
/*
* Removes orphan pkgs. These packages were installed
* as dependency and any installed package does not depend
* on it currently.
*/
if (argc != 1)
usage();
xbps_autoremove_pkgs();
} else if (strcasecmp(argv[0], "purge") == 0) {
/*
* Purge a package completely.
*/
if (argc != 2)
usage();
if (strcasecmp(argv[1], "all") == 0) {
iter = xbps_get_array_iter_from_dict(dict, "packages");
if (iter == NULL)
goto out;
while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj,
"pkgname", &curpkgname);
if ((rv = xbps_purge_pkg(curpkgname)) != 0)
break;
}
prop_object_iterator_release(iter);
} else {
rv = xbps_purge_pkg(argv[1]);
}
} else if (strcasecmp(argv[0], "reconfigure") == 0) {
/*
* Reconfigure a package.
*/
if (argc != 2)
usage();
if (strcasecmp(argv[1], "all") == 0) {
iter = xbps_get_array_iter_from_dict(dict, "packages");
if (iter == NULL)
goto out;
while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj,
"pkgname", &curpkgname);
if ((rv = xbps_configure_pkg(curpkgname)) != 0)
break;
}
prop_object_iterator_release(iter);
} else {
rv = xbps_configure_pkg(argv[1]);
}
} else {
usage();
}
out:
xbps_release_regpkgdb_dict();
if (rv != 0)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}

162
bin/xbps-bin/remove.c Normal file
View File

@@ -0,0 +1,162 @@
/*-
* Copyright (c) 2008-2009 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 <xbps_api.h>
#include "defs.h"
#include "../xbps-repo/util.h"
void
xbps_autoremove_pkgs(void)
{
prop_array_t orphans;
prop_object_t obj;
prop_object_iterator_t iter;
const char *pkgname, *version;
size_t cols = 0;
int rv = 0;
bool first = false;
/*
* Removes orphan pkgs. These packages were installed
* as dependency and any installed package does not depend
* on it currently.
*/
orphans = xbps_find_orphan_packages();
if (orphans == NULL)
exit(EXIT_FAILURE);
if (orphans != NULL && prop_array_count(orphans) == 0) {
printf("There are not orphaned packages currently.\n");
exit(EXIT_SUCCESS);
}
iter = prop_array_iterator(orphans);
if (iter == NULL)
goto out;
printf("The following packages were installed automatically\n"
"(as dependencies) and aren't needed anymore:\n\n");
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
cols += strlen(pkgname) + strlen(version) + 4;
if (cols <= 80) {
if (first == false) {
printf(" ");
first = true;
}
} else {
printf("\n ");
cols = strlen(pkgname) + strlen(version) + 4;
}
printf("%s-%s ", pkgname, version);
}
prop_object_iterator_reset(iter);
printf("\n\n");
if (xbps_noyes("Do you want to remove them?") == false) {
printf("Cancelled!\n");
goto out2;
}
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
printf("Removing package %s-%s ...\n", pkgname, version);
if ((rv = xbps_remove_pkg(pkgname, version, false)) != 0)
goto out2;
}
out2:
prop_object_iterator_release(iter);
out:
prop_object_release(orphans);
if (rv != 0)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}
void
xbps_remove_installed_pkg(const char *pkgname, bool force)
{
prop_array_t reqby;
prop_dictionary_t dict;
const char *version;
int rv = 0;
/*
* First check if package is required by other packages.
*/
dict = xbps_find_pkg_installed_from_plist(pkgname);
if (dict == NULL) {
printf("Package %s is not installed.\n", pkgname);
goto out;
}
prop_dictionary_get_cstring_nocopy(dict, "version", &version);
reqby = prop_dictionary_get(dict, "requiredby");
if (reqby != NULL && prop_array_count(reqby) > 0) {
printf("WARNING! %s-%s is required by the following "
"packages:\n\n", pkgname, version);
(void)xbps_callback_array_iter_in_dict(dict,
"requiredby", list_strings_in_array, NULL);
printf("\n\n");
if (!force) {
if (!xbps_noyes("Do you want to remove %s?", pkgname)) {
printf("Cancelling!\n");
goto out;
}
}
printf("Forcing %s-%s for deletion!\n", pkgname, version);
} else {
if (!force) {
if (!xbps_noyes("Do you want to remove %s?", pkgname)) {
printf("Cancelling!\n");
goto out;
}
}
}
printf("Removing package %s-%s ...\n", pkgname, version);
if ((rv = xbps_remove_pkg(pkgname, version, false)) != 0) {
printf("Unable to remove %s-%s (%s).\n",
pkgname, version, strerror(errno));
goto out;
}
out:
xbps_release_regpkgdb_dict();
if (rv != 0)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}