Mega-commit with improvements and changes done in the past days.

- Introduce package states: unpacked, broken, installed, etc.
  Not yet finished, only unpacked and installed are used for now.

- Move package metadata files in binary packages directly to
  the top directory, this speeds up some ops and makes easier to
  continue working in future changes.

- xbps-bin: -C flag to check the hash of package files has been
  superseded by the 'check' target, which verifies the integrity
  of an installed package.

- Use the 'essential' object when upgrading packages, overwritting
  current files. This is needed for critical packages like sh, libc
  and others.

- Miscellaneous tweaks and improvements thorough the code.

--HG--
extra : convert_revision : 2073fcc123efc24b3e9327b5e22aa91752f20df6
This commit is contained in:
Juan RP
2009-08-08 22:29:48 +02:00
parent 75cac4a637
commit 17404bdb42
24 changed files with 1216 additions and 529 deletions

View File

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

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

@@ -0,0 +1,289 @@
/*-
* 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 <strings.h>
#include <errno.h>
#include <limits.h>
#include <libgen.h>
#include <unistd.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;
}
printf("%s: metadata check PASSED.\n", pkgname);
/*
* 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);
else
printf("%s: files check PASSED.\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 PASSED.\n",
pkgname);
else
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);
else
printf("%s: run-time dependency check PASSED.\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;
}

View File

@@ -28,7 +28,8 @@
void xbps_install_pkg(const char *, bool, bool);
void xbps_autoremove_pkgs(void);
void xbps_remove_pkg(const char *, bool);
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_ */

View File

@@ -27,6 +27,7 @@
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <errno.h>
#include <limits.h>
#include <prop/proplib.h>
@@ -34,8 +35,25 @@
#include <xbps_api.h>
#include "defs.h"
enum {
TRANS_ONE,
TRANS_ALL
};
struct transaction {
prop_dictionary_t dict;
prop_object_iterator_t iter;
const char *originpkgname;
const char *curpkgname;
int type;
bool force;
bool update;
};
static void show_missing_deps(prop_dictionary_t, const char *);
static int show_missing_dep_cb(prop_object_t, void *, bool *);
static int exec_transaction(struct transaction *);
static void cleanup(int);
static void
show_missing_deps(prop_dictionary_t d, const char *pkgname)
@@ -65,36 +83,44 @@ show_missing_dep_cb(prop_object_t obj, void *arg, bool *loop_done)
return EINVAL;
}
static void
check_pkg_hashes(prop_dictionary_t props, prop_object_iterator_t iter)
static int
check_pkg_hashes(prop_object_iterator_t iter)
{
prop_object_t obj;
const char *repoloc, *filename;
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("error: checking hash for %s (%s)\n",
filename, strerror(rv));
prop_object_release(props);
exit(EXIT_FAILURE);
printf("Unexpected error while checking hash for "
"%s (%s)\n", filename, strerror(rv));
return -1;
} else if (rv != 0 && rv == ERANGE) {
printf("Hash doesn't match for %s!\n", filename);
prop_object_release(props);
exit(EXIT_FAILURE);
printf("Hash mismatch for %s, exiting.\n",
filename);
return -1;
}
}
prop_object_iterator_reset(iter);
printf("\n");
return 0;
}
static void
show_transaction_sizes(prop_dictionary_t props, prop_object_iterator_t iter,
const char *descr)
static int
show_transaction_sizes(prop_object_iterator_t iter, const char *descr)
{
prop_object_t obj;
uint64_t tsize = 0, dlsize = 0, instsize = 0;
@@ -145,301 +171,344 @@ show_transaction_sizes(prop_dictionary_t props, prop_object_iterator_t iter,
*/
if (xbps_humanize_number(size, 5, (int64_t)dlsize,
"", HN_AUTOSCALE, HN_NOSPACE) == -1) {
printf("error: humanize_number %s\n", strerror(errno));
prop_object_release(props);
exit(EXIT_FAILURE);
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 %s\n", strerror(errno));
prop_object_release(props);
exit(EXIT_FAILURE);
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)
{
prop_dictionary_t props, instpkg;
struct transaction *trans;
prop_dictionary_t pkgd;
prop_array_t array;
prop_object_t obj;
prop_object_iterator_t iter;
const char *instver, *origin, *pkgname, *version;
int rv = 0;
bool pkg_is_dep = false, doup = false;
/*
* Find and sort all required package dictionaries.
* Find all required pkgs and sort the package transaction.
*/
printf("Finding/sorting required binary packages...\n");
rv = xbps_prepare_pkg(pkg);
if (rv != 0 && rv == EAGAIN) {
printf("Unable to locate %s in repository pool.\n", pkg);
exit(EXIT_FAILURE);
} else if (rv != 0 && rv != ENOENT) {
printf("Unexpected error: %s\n", strerror(rv));
exit(EXIT_FAILURE);
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);
}
}
props = xbps_get_pkg_props();
if (props == NULL) {
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");
exit(EXIT_FAILURE);
goto out1;
}
/*
* Bail out if there are unresolved deps.
*/
array = prop_dictionary_get(props, "missing_deps");
array = prop_dictionary_get(trans->dict, "missing_deps");
if (prop_array_count(array) > 0) {
show_missing_deps(props, pkg);
goto out;
show_missing_deps(trans->dict, pkg);
goto out2;
}
prop_dictionary_get_cstring_nocopy(props, "origin", &origin);
prop_dictionary_get_cstring_nocopy(trans->dict,
"origin", &trans->originpkgname);
array = prop_dictionary_get(props, "packages");
if (array == NULL || prop_array_count(array) == 0) {
printf("error: empty packages array!\n");
goto out;
}
iter = prop_array_iterator(array);
if (iter == NULL) {
printf("error: allocating array mem! (%s)\n", strerror(errno));
goto out;
/*
* 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;
trans->update = update;
trans->type = TRANS_ONE;
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;
int rv = 0;
bool essential, isdep;
pkg_state_t state = 0;
assert(trans != NULL);
assert(trans->dict != NULL);
assert(trans->iter != NULL);
essential = isdep = false;
/*
* Show download/installed size for the transaction.
*/
show_transaction_sizes(props, iter, "installed");
rv = show_transaction_sizes(trans->iter,
trans->type == TRANS_ALL ? "updated" : "installed");
if (rv != 0)
return rv;
/*
* Ask interactively (if -f not set).
*/
if (force == false) {
if (trans->force == false) {
if (xbps_noyes("Do you want to continue?") == false) {
printf("Aborting!\n");
goto out2;
return 0;
}
}
/*
* Check the SHA256 hash for all required packages.
*/
check_pkg_hashes(props, iter);
if ((rv = check_pkg_hashes(trans->iter)) != 0)
return rv;
/*
* Install all packages, the list is already sorted.
* Iterate over the transaction dictionary.
*/
while ((obj = prop_object_iterator_next(iter)) != NULL) {
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);
if (strcmp(origin, pkgname))
pkg_is_dep = true;
prop_dictionary_get_bool(obj, "essential", &essential);
prop_dictionary_get_cstring_nocopy(obj, "filename", &filename);
if (update && strcmp(pkg, pkgname) == 0) {
/*
* Update a package, firstly removing current package.
*/
instpkg = xbps_find_pkg_installed_from_plist(pkgname);
if (instpkg == NULL) {
if ((trans->type == TRANS_ONE) &&
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 ((trans->type == TRANS_ALL) ||
(trans->update &&
strcmp(trans->curpkgname, pkgname) == 0)) {
instpkgd = xbps_find_pkg_installed_from_plist(pkgname);
if (instpkgd == NULL) {
printf("error: unable to find %s installed "
"dict!\n", pkgname);
goto out2;
return EINVAL;
}
prop_dictionary_get_cstring_nocopy(instpkg,
prop_dictionary_get_cstring_nocopy(instpkgd,
"version", &instver);
printf("Updating package %s-%s to %s...\n", pkgname,
instver, version);
prop_object_release(instpkg);
rv = xbps_remove_binary_pkg(pkgname, update);
if (rv != 0) {
printf("error: removing %s-%s (%s)\n",
pkgname, instver, strerror(rv));
goto out2;
}
prop_object_release(instpkgd);
} else {
printf("Installing %s%s-%s ...\n",
pkg_is_dep ? "dependency " : "", pkgname, version);
/*
* 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.
*/
if ((rv = xbps_unpack_binary_pkg(obj)) != 0) {
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));
goto out2;
return rv;
}
/*
* Register binary package.
*/
if (update && !pkg_is_dep)
doup = true;
if ((rv = xbps_register_pkg(obj, doup, pkg_is_dep)) != 0) {
if ((rv = xbps_register_pkg(obj, isdep)) != 0) {
printf("error: registering %s-%s! (%s)\n",
pkgname, version, strerror(rv));
goto out2;
return rv;
}
pkg_is_dep = false;
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;
}
out2:
prop_object_iterator_release(iter);
out:
prop_object_release(props);
xbps_release_repolist_data();
xbps_release_regpkgdb_dict();
if (rv != 0)
exit(EXIT_FAILURE);
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);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
printf("Configuring package %s-%s ...\n", pkgname, version);
exit(EXIT_SUCCESS);
if ((rv = xbps_configure_pkg(pkgname, version)) != 0) {
printf("Error configuring package %s-%s\n",
pkgname, version);
return rv;
}
}
return 0;
}
void
xbps_autoupdate_pkgs(bool force)
{
prop_dictionary_t dict, props, instpkg;
prop_array_t array;
struct transaction *trans;
prop_dictionary_t dict;
prop_object_t obj;
prop_object_iterator_t iter;
const char *pkgname, *version, *instver;
const char *pkgname;
int rv = 0;
/*
* Prepare dictionary with all registered packages.
*/
dict = xbps_prepare_regpkgdb_dict();
if (dict == NULL) {
printf("No packages currently installed (%s).\n",
strerror(errno));
exit(EXIT_SUCCESS);
cleanup(rv);
}
iter = xbps_get_array_iter_from_dict(dict, "packages");
if (iter == NULL) {
rv = EINVAL;
goto out;
}
/*
* Prepare dictionary with all registered repositories.
*/
if ((rv = xbps_prepare_repolist_data()) != 0)
goto out;
while ((obj = prop_object_iterator_next(iter)) != NULL) {
/*
* Prepare transaction data.
*/
trans = calloc(1, sizeof(struct transaction));
if (trans == NULL)
goto out;
trans->iter = xbps_get_array_iter_from_dict(dict, "packages");
if (trans->iter == NULL) {
rv = EINVAL;
goto out1;
}
/*
* Find out if there is a newer version for all currently
* installed packages.
*/
while ((obj = prop_object_iterator_next(trans->iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
rv = xbps_find_new_pkg(pkgname, obj);
if (rv != 0)
break;
if (rv != 0) {
prop_object_iterator_release(trans->iter);
goto out1;
}
}
prop_object_iterator_release(iter);
prop_object_iterator_release(trans->iter);
/* Sort the list of packages */
props = xbps_get_pkg_props();
if (props == NULL) {
/*
* 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");
exit(EXIT_SUCCESS);
goto out;
}
printf("Error while checking for new pkgs: %s\n",
strerror(errno));
goto out;
goto out1;
}
if ((rv = xbps_sort_pkg_deps(props)) != 0) {
/*
* Sort the package transaction dictionary.
*/
if ((rv = xbps_sort_pkg_deps(trans->dict)) != 0) {
printf("Error while sorting packages: %s\n",
strerror(rv));
goto out;
goto out2;
}
/* Update all packages now */
array = prop_dictionary_get(props, "packages");
if (array == NULL || prop_array_count(array) == 0) {
printf("error: empty packages array!\n");
prop_object_release(props);
goto out;
}
iter = prop_array_iterator(array);
if (iter == NULL) {
/*
* 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));
prop_object_release(props);
goto out;
goto out2;
}
/*
* Show download/installed size for the transaction.
*/
show_transaction_sizes(props, iter, "upgraded");
/*
* Ask interactively (if -f not set).
*/
if (force == false) {
if (xbps_noyes("Do you want to continue?") == false) {
printf("Aborting!\n");
prop_object_release(props);
goto out2;
}
}
/*
* Check the SHA256 hash for all required packages.
*/
check_pkg_hashes(props, iter);
while ((obj = prop_object_iterator_next(iter)) != NULL) {
prop_dictionary_get_cstring_nocopy(obj, "pkgname", &pkgname);
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
/*
* Update a package, firstly removing current package.
*/
instpkg = xbps_find_pkg_installed_from_plist(pkgname);
if (instpkg == NULL) {
printf("error: unable to find %s installed "
"dict!\n", pkgname);
prop_object_release(props);
goto out2;
}
prop_dictionary_get_cstring_nocopy(instpkg,
"version", &instver);
printf("Updating package %s-%s to %s...\n", pkgname,
instver, version);
prop_object_release(instpkg);
rv = xbps_remove_binary_pkg(pkgname, true);
if (rv != 0) {
printf("error: removing %s-%s (%s)\n",
pkgname, instver, strerror(rv));
prop_object_release(props);
goto out2;
}
/*
* Unpack binary package.
*/
if ((rv = xbps_unpack_binary_pkg(obj)) != 0) {
printf("error: unpacking %s-%s (%s)\n", pkgname,
version, strerror(rv));
prop_object_release(props);
goto out2;
}
/*
* Register binary package.
*/
if ((rv = xbps_register_pkg(obj, true, false)) != 0) {
printf("error: registering %s-%s! (%s)\n",
pkgname, version, strerror(rv));
prop_object_release(props);
goto out2;
}
}
trans->force = force;
trans->update = true;
trans->type = TRANS_ALL;
rv = exec_transaction(trans);
prop_object_iterator_release(trans->iter);
out2:
prop_object_iterator_release(iter);
prop_object_release(trans->dict);
out1:
free(trans);
out:
cleanup(rv);
}
static void
cleanup(int rv)
{
xbps_release_repolist_data();
xbps_release_regpkgdb_dict();
if (rv != 0)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
exit(rv == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
}

View File

@@ -45,9 +45,10 @@ usage(void)
{
printf("Usage: xbps-bin [options] [target] [arguments]\n\n"
" Available targets:\n"
" autoremove, autoupdate, files, install, list, remove\n"
" show, update\n"
" autoremove, autoupdate, check, files, install, list\n"
" remove, show, update\n"
" Targets with arguments:\n"
" check\t<pkgname>\n"
" files\t<pkgname>\n"
" install\t<pkgname>\n"
" remove\t<pkgname>\n"
@@ -56,8 +57,6 @@ usage(void)
" Options shared by all targets:\n"
" -r\t\t<rootdir>\n"
" -v\t\t<verbose>\n"
" Options used by the files target:\n"
" -C\t\tTo check the SHA256 hash for any listed file.\n"
" Options used by the (auto)remove target:\n"
" -f\t\tForce installation or removal of packages.\n"
" \t\tBeware with this option if you use autoremove!\n"
@@ -65,7 +64,7 @@ usage(void)
" Examples:\n"
" $ xbps-bin autoremove\n"
" $ xbps-bin autoupdate\n"
" $ xbps-bin -C files klibc\n"
" $ xbps-bin files klibc\n"
" $ xbps-bin install klibc\n"
" $ xbps-bin -r /path/to/root install klibc\n"
" $ xbps-bin list\n"
@@ -101,13 +100,10 @@ main(int argc, char **argv)
{
prop_dictionary_t dict;
int c, flags = 0, rv = 0;
bool chkhash = false, force = false, verbose = false;
bool force = false, verbose = false;
while ((c = getopt(argc, argv, "Cfr:v")) != -1) {
switch (c) {
case 'C':
chkhash = true;
break;
case 'f':
force = true;
break;
@@ -178,7 +174,7 @@ main(int argc, char **argv)
if (argc != 2)
usage();
xbps_remove_pkg(argv[1], force);
xbps_remove_installed_pkg(argv[1], force);
} else if (strcasecmp(argv[0], "show") == 0) {
/* Shows info about an installed binary package. */
@@ -196,12 +192,19 @@ main(int argc, char **argv)
if (argc != 2)
usage();
rv = show_pkg_files_from_metadir(argv[1], chkhash);
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.

View File

@@ -96,7 +96,7 @@ xbps_autoremove_pkgs(void)
prop_dictionary_get_cstring_nocopy(obj, "version", &version);
printf("Removing package %s-%s ...\n", pkgname, version);
if ((rv = xbps_remove_binary_pkg(pkgname, false)) != 0)
if ((rv = xbps_remove_pkg(pkgname, version, false)) != 0)
goto out2;
}
out2:
@@ -110,7 +110,7 @@ out:
}
void
xbps_remove_pkg(const char *pkgname, bool force)
xbps_remove_installed_pkg(const char *pkgname, bool force)
{
prop_array_t reqby;
prop_dictionary_t dict;
@@ -151,7 +151,7 @@ xbps_remove_pkg(const char *pkgname, bool force)
}
printf("Removing package %s-%s ...\n", pkgname, version);
if ((rv = xbps_remove_binary_pkg(pkgname, false)) != 0) {
if ((rv = xbps_remove_pkg(pkgname, version, false)) != 0) {
printf("Unable to remove %s-%s (%s).\n",
pkgname, version, strerror(errno));
goto out;

View File

@@ -139,7 +139,12 @@ main(int argc, char **argv)
prop_dictionary_set_cstring_nocopy(dict, "version", argv[2]);
prop_dictionary_set_cstring_nocopy(dict, "short_desc", argv[3]);
rv = xbps_register_pkg(dict, false, automatic);
rv = xbps_set_pkg_state_installed(argv[1],
XBPS_PKG_STATE_INSTALLED);
if (rv != 0)
exit(EXIT_FAILURE);
rv = xbps_register_pkg(dict, automatic);
if (rv == EEXIST) {
printf("%s=> %s-%s already registered.\n",
in_chroot ? "[chroot] " : "", argv[1], argv[2]);

View File

@@ -36,7 +36,7 @@
#include "index.h"
/* Array of valid architectures */
const char *archdirs[] = { "i686", "x86_64", "noarch", NULL };
static const char *archdirs[] = { "i686", "x86_64", "noarch", NULL };
static prop_dictionary_t
repoidx_getdict(const char *pkgdir)
@@ -52,16 +52,13 @@ repoidx_getdict(const char *pkgdir)
dict = prop_dictionary_internalize_from_file(plist);
if (dict == NULL) {
dict = prop_dictionary_create();
if (dict == NULL) {
free(plist);
return NULL;
}
if (dict == NULL)
goto out;
array = prop_array_create();
if (array == NULL) {
free(plist);
prop_object_release(dict);
return NULL;
goto out;
}
prop_dictionary_set(dict, "packages", array);
@@ -71,6 +68,7 @@ repoidx_getdict(const char *pkgdir)
prop_dictionary_set_cstring_nocopy(dict,
"pkgindex-version", XBPS_PKGINDEX_VERSION);
}
out:
free(plist);
return dict;
@@ -84,38 +82,36 @@ repoidx_addpkg(const char *file, const char *filename, const char *pkgdir)
struct archive *ar;
struct archive_entry *entry;
struct stat st;
ssize_t nbytes = -1;
const char *pkgname, *version, *regver;
char *props, *sha256, *plist;
size_t propslen = 0;
char *sha256, *plist;
int rv = 0;
ar = archive_read_new();
if (ar == NULL)
return errno;
if (ar == NULL) {
rv = errno;
goto out;
}
/* Enable support for tar format and all compression methods */
archive_read_support_compression_all(ar);
archive_read_support_format_tar(ar);
if ((rv = archive_read_open_filename(ar, file,
ARCHIVE_READ_BLOCKSIZE)) == -1) {
archive_read_finish(ar);
return errno;
rv = errno;
goto out1;
}
/* Get existing or create repo index dictionary */
idxdict = repoidx_getdict(pkgdir);
if (idxdict == NULL) {
archive_read_finish(ar);
return errno;
rv = errno;
goto out1;
}
plist = xbps_get_pkg_index_plist(pkgdir);
if (plist == NULL) {
prop_dictionary_remove(idxdict, "packages");
prop_object_release(idxdict);
archive_read_finish(ar);
return ENOMEM;
rv = ENOMEM;
goto out2;
}
/*
@@ -123,32 +119,15 @@ repoidx_addpkg(const char *file, const char *filename, const char *pkgdir)
* into a buffer.
*/
while (archive_read_next_header(ar, &entry) == ARCHIVE_OK) {
if (strstr(archive_entry_pathname(entry), "props.plist") == 0) {
if (strstr(archive_entry_pathname(entry), XBPS_PKGPROPS) == 0) {
archive_read_data_skip(ar);
continue;
}
propslen = (size_t)archive_entry_size(entry);
props = malloc(propslen);
if (props == NULL) {
rv = errno;
break;
}
nbytes = archive_read_data(ar, props, propslen);
if ((size_t)nbytes != propslen) {
rv = EINVAL;
break;
}
newpkgd = prop_dictionary_internalize(props);
free(props);
propslen = 0;
newpkgd = xbps_read_dict_from_archive_entry(ar, entry);
if (newpkgd == NULL) {
archive_read_data_skip(ar);
continue;
} else if (prop_object_type(newpkgd) != PROP_TYPE_DICTIONARY) {
prop_object_release(newpkgd);
archive_read_data_skip(ar);
continue;
printf("%s: can't read %s metadata file, skipping!\n",
file, XBPS_PKGPROPS);
break;
}
prop_dictionary_get_cstring_nocopy(newpkgd, "pkgname",
@@ -233,10 +212,13 @@ repoidx_addpkg(const char *file, const char *filename, const char *pkgdir)
pkgname, version);
break;
}
archive_read_finish(ar);
free(plist);
prop_object_release(idxdict);
free(plist);
out2:
prop_object_release(idxdict);
out1:
archive_read_finish(ar);
out:
return rv;
}
@@ -253,7 +235,6 @@ xbps_repo_genindex(const char *pkgdir)
if (uname(&un) == -1)
return errno;
/*
* Iterate over the known architecture directories to find
* binary packages.
@@ -296,7 +277,6 @@ xbps_repo_genindex(const char *pkgdir)
free(path);
return rv;
}
}
(void)closedir(dirp);
free(path);

View File

@@ -197,14 +197,14 @@ show_pkg_info_from_metadir(const char *pkgname)
}
int
show_pkg_files_from_metadir(const char *pkgname, bool hash)
show_pkg_files_from_metadir(const char *pkgname)
{
prop_dictionary_t pkgd;
prop_array_t array;
prop_object_iterator_t iter = NULL;
prop_object_t obj;
const char *destdir, *file, *sha256;
char *plist, *path = NULL, *array_str = "files";
const char *destdir, *file;
char *plist, *array_str = "files";
int i, rv = 0;
destdir = xbps_get_rootdir();
@@ -233,7 +233,6 @@ show_pkg_files_from_metadir(const char *pkgname, bool hash)
printf("%s\n", file);
}
prop_object_iterator_release(iter);
iter = NULL;
}
/* Files and configuration files. */
@@ -254,41 +253,10 @@ show_pkg_files_from_metadir(const char *pkgname, bool hash)
}
while ((obj = prop_object_iterator_next(iter))) {
prop_dictionary_get_cstring_nocopy(obj, "file", &file);
if (hash == false) {
printf("%s\n", file);
continue;
}
printf("%s", file);
if (destdir) {
path = xbps_xasprintf("%s/%s", destdir, file);
if (path == NULL) {
rv = EINVAL;
goto out2;
}
}
prop_dictionary_get_cstring_nocopy(obj,
"sha256", &sha256);
if (destdir)
rv = xbps_check_file_hash(path, sha256);
else
rv = xbps_check_file_hash(file, sha256);
if (rv != 0 && rv != ERANGE)
printf(" (can't check: %s)", strerror(rv));
else if (rv == ERANGE)
printf(" WARNING! SHA256 HASH MISMATCH!");
printf("\n");
if (destdir)
free(path);
printf("%s\n", file);
}
prop_object_iterator_release(iter);
iter = NULL;
}
out2:
if (iter != NULL)
prop_object_iterator_release(iter);
out:
prop_object_release(pkgd);

View File

@@ -28,7 +28,7 @@
int search_string_in_pkgs(prop_object_t, void *, bool *);
int show_pkg_info_from_metadir(const char *);
int show_pkg_files_from_metadir(const char *, bool);
int show_pkg_files_from_metadir(const char *);
int show_pkg_info_from_repolist(prop_object_t, void *, bool *);
int list_strings_in_array(prop_object_t, void *, bool *);
int list_strings_sep_in_array(prop_object_t, void *, bool *);