2008-12-23 12:28:21 +01:00
|
|
|
/*-
|
2009-02-07 13:27:24 +01:00
|
|
|
* Copyright (c) 2008-2009 Juan Romero Pardines.
|
2008-12-23 12:28:21 +01:00
|
|
|
* 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 <limits.h>
|
2008-12-28 09:48:18 +01:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
2008-12-23 12:28:21 +01:00
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
2008-12-26 10:13:03 +01:00
|
|
|
int
|
2009-04-02 02:46:36 +02:00
|
|
|
xbps_install_binary_pkg_fini(prop_dictionary_t repo, prop_dictionary_t pkgrd,
|
|
|
|
bool update)
|
2008-12-26 10:13:03 +01:00
|
|
|
{
|
2009-04-02 02:46:36 +02:00
|
|
|
prop_dictionary_t instpkg;
|
|
|
|
const char *pkgname, *version, *instver;
|
2008-12-26 10:13:03 +01:00
|
|
|
int rv = 0;
|
2009-02-15 20:23:23 +01:00
|
|
|
bool automatic = false;
|
2008-12-26 10:13:03 +01:00
|
|
|
|
2009-02-07 13:27:24 +01:00
|
|
|
assert(pkg != NULL);
|
2009-04-02 02:46:36 +02:00
|
|
|
prop_dictionary_get_cstring_nocopy(pkgrd, "pkgname", &pkgname);
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgrd, "version", &version);
|
2008-12-26 10:13:03 +01:00
|
|
|
|
2009-02-16 23:00:50 +01:00
|
|
|
if (repo == false)
|
2009-02-15 20:23:23 +01:00
|
|
|
automatic = true;
|
2009-02-16 23:00:50 +01:00
|
|
|
|
2009-04-02 02:46:36 +02:00
|
|
|
if (update == true) {
|
2009-04-02 03:08:20 +02:00
|
|
|
/*
|
|
|
|
* Update a package, firstly removing current package.
|
|
|
|
*/
|
2009-04-02 02:46:36 +02:00
|
|
|
instpkg = xbps_find_pkg_installed_from_plist(pkgname);
|
|
|
|
if (instpkg == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
prop_dictionary_get_cstring_nocopy(instpkg, "version",
|
|
|
|
&instver);
|
2009-04-02 03:08:20 +02:00
|
|
|
printf("Removing current package %s-%s ...\n", pkgname,
|
|
|
|
instver);
|
2009-04-02 02:46:36 +02:00
|
|
|
prop_object_release(instpkg);
|
2009-04-02 03:08:20 +02:00
|
|
|
rv = xbps_remove_binary_pkg(pkgname, update);
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
printf("Installing new package %s-%s ...\n", pkgname, version);
|
2009-04-02 02:46:36 +02:00
|
|
|
} else {
|
|
|
|
printf("Installing %s%s: found version %s ...\n",
|
|
|
|
automatic ? "dependency " : "", pkgname, version);
|
|
|
|
}
|
2009-02-16 22:48:18 +01:00
|
|
|
(void)fflush(stdout);
|
2009-02-15 20:23:23 +01:00
|
|
|
|
2009-04-02 04:52:29 +02:00
|
|
|
rv = xbps_unpack_binary_pkg(repo, pkgrd);
|
2008-12-26 10:13:03 +01:00
|
|
|
if (rv == 0) {
|
2009-04-02 02:46:36 +02:00
|
|
|
rv = xbps_register_pkg(pkgrd, update, automatic);
|
2009-02-17 00:08:03 +01:00
|
|
|
if (rv != 0) {
|
2009-02-26 05:41:49 +01:00
|
|
|
printf("ERROR: couldn't register %s-%s! (%s)\n",
|
|
|
|
pkgname, version, strerror(rv));
|
2009-02-17 00:08:03 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2008-12-26 10:13:03 +01:00
|
|
|
}
|
|
|
|
|
2009-02-17 00:08:03 +01:00
|
|
|
return 0;
|
2008-12-26 10:13:03 +01:00
|
|
|
}
|
|
|
|
|
2008-12-23 12:28:21 +01:00
|
|
|
int
|
2009-04-02 02:46:36 +02:00
|
|
|
xbps_install_binary_pkg(const char *pkgname, bool update)
|
2008-12-23 12:28:21 +01:00
|
|
|
{
|
2009-04-04 05:24:58 +02:00
|
|
|
prop_dictionary_t repod = NULL, repolistd, pkgrd = NULL;
|
|
|
|
prop_array_t array;
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t repolist_iter;
|
2009-04-04 19:32:39 +02:00
|
|
|
const char *repoloc, *instver, *rootdir;
|
2009-03-17 05:10:21 +01:00
|
|
|
char *plist, *pkg;
|
2009-02-14 01:37:12 +01:00
|
|
|
int rv = 0;
|
|
|
|
|
2009-04-04 05:24:58 +02:00
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
if (rootdir == NULL)
|
|
|
|
rootdir = "";
|
|
|
|
|
|
|
|
plist = xbps_xasprintf("%s/%s/%s", rootdir,
|
|
|
|
XBPS_META_PATH, XBPS_REPOLIST);
|
2009-02-14 01:37:12 +01:00
|
|
|
if (plist == NULL)
|
2008-12-23 12:28:21 +01:00
|
|
|
return EINVAL;
|
2009-02-14 01:37:12 +01:00
|
|
|
|
2009-04-04 05:24:58 +02:00
|
|
|
repolistd = prop_dictionary_internalize_from_file(plist);
|
|
|
|
if (repolistd == NULL) {
|
|
|
|
free(plist);
|
|
|
|
return EINVAL;
|
2008-12-27 22:19:04 +01:00
|
|
|
}
|
2009-02-07 13:27:24 +01:00
|
|
|
free(plist);
|
2009-04-04 05:24:58 +02:00
|
|
|
plist = NULL;
|
|
|
|
|
|
|
|
array = prop_dictionary_get(repolistd, "repository-list");
|
|
|
|
if (array == NULL) {
|
|
|
|
prop_object_release(repolistd);
|
|
|
|
return EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
repolist_iter = prop_array_iterator(array);
|
|
|
|
if (repolist_iter == NULL) {
|
|
|
|
prop_object_release(repolistd);
|
|
|
|
return ENOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((obj = prop_object_iterator_next(repolist_iter)) != NULL) {
|
|
|
|
/*
|
|
|
|
* Iterate over the repository pool and find out if we have
|
|
|
|
* the binary package.
|
|
|
|
*/
|
|
|
|
plist =
|
|
|
|
xbps_get_pkg_index_plist(prop_string_cstring_nocopy(obj));
|
|
|
|
if (plist == NULL)
|
|
|
|
return EINVAL;
|
|
|
|
|
|
|
|
repod = prop_dictionary_internalize_from_file(plist);
|
|
|
|
if (repod == NULL) {
|
|
|
|
free(plist);
|
|
|
|
return errno;
|
|
|
|
}
|
|
|
|
free(plist);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the package dictionary from current repository.
|
|
|
|
* If it's not there, pass to the next repository.
|
|
|
|
*/
|
|
|
|
pkgrd = xbps_find_pkg_in_dict(repod, "packages", pkgname);
|
|
|
|
if (pkgrd == NULL) {
|
|
|
|
prop_object_release(repod);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
prop_object_iterator_reset(repolist_iter);
|
2008-12-23 12:28:21 +01:00
|
|
|
|
2009-02-14 01:37:12 +01:00
|
|
|
if (pkgrd == NULL) {
|
2009-04-04 05:24:58 +02:00
|
|
|
rv = EAGAIN;
|
|
|
|
goto out2;
|
2009-02-14 01:37:12 +01:00
|
|
|
}
|
2009-02-07 13:27:24 +01:00
|
|
|
|
2009-03-17 05:10:21 +01:00
|
|
|
/*
|
2009-04-04 05:24:58 +02:00
|
|
|
* Check if available version in repository is already
|
|
|
|
* installed, and return immediately in that case.
|
2009-03-17 05:10:21 +01:00
|
|
|
*/
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgrd, "version", &instver);
|
2009-04-04 19:32:39 +02:00
|
|
|
pkg = xbps_xasprintf("%s-%s", pkgname, instver);
|
2009-03-17 05:10:21 +01:00
|
|
|
if (pkg == NULL) {
|
2009-04-04 05:24:58 +02:00
|
|
|
rv = errno;
|
2009-03-17 05:10:21 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (xbps_check_is_installed_pkg(pkg) == 0) {
|
|
|
|
free(pkg);
|
|
|
|
rv = EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
free(pkg);
|
|
|
|
|
2009-02-27 16:03:25 +01:00
|
|
|
/*
|
|
|
|
* Check SHA256 hash for binary package before anything else.
|
|
|
|
*/
|
2009-04-04 05:24:58 +02:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(repod,
|
|
|
|
"location-local", &repoloc)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2009-03-02 23:38:24 +01:00
|
|
|
}
|
|
|
|
|
2009-03-16 07:03:25 +01:00
|
|
|
if ((rv = xbps_check_pkg_file_hash(pkgrd, repoloc)) != 0)
|
|
|
|
goto out;
|
2009-02-27 16:03:25 +01:00
|
|
|
|
2009-02-14 01:37:12 +01:00
|
|
|
/*
|
|
|
|
* Check if this package needs dependencies.
|
|
|
|
*/
|
2009-04-04 05:24:58 +02:00
|
|
|
if (xbps_pkg_has_rundeps(pkgrd)) {
|
|
|
|
/*
|
|
|
|
* Construct the dependency chain for this package.
|
|
|
|
*/
|
|
|
|
printf("Finding required dependencies...\n");
|
|
|
|
if ((rv = xbps_find_deps_in_pkg(pkgrd, repolist_iter)) != 0)
|
|
|
|
goto out;
|
2009-02-07 13:27:24 +01:00
|
|
|
|
2009-04-04 05:24:58 +02:00
|
|
|
/*
|
|
|
|
* Install all required dependencies and the package itself.
|
|
|
|
*/
|
|
|
|
rv = xbps_install_pkg_deps(pkgname, update);
|
|
|
|
if (rv != 0)
|
|
|
|
goto out;
|
2009-02-14 01:37:12 +01:00
|
|
|
}
|
2009-02-07 13:27:24 +01:00
|
|
|
|
2009-02-14 01:37:12 +01:00
|
|
|
/*
|
2009-04-04 05:24:58 +02:00
|
|
|
* Finally install the binary package that was requested by
|
|
|
|
* the client.
|
2009-02-14 01:37:12 +01:00
|
|
|
*/
|
2009-04-04 05:24:58 +02:00
|
|
|
rv = xbps_install_binary_pkg_fini(repod, pkgrd, update);
|
2009-03-16 07:03:25 +01:00
|
|
|
out:
|
2009-02-27 16:03:25 +01:00
|
|
|
prop_object_release(repod);
|
2009-04-04 05:24:58 +02:00
|
|
|
out2:
|
|
|
|
prop_object_iterator_release(repolist_iter);
|
|
|
|
prop_object_release(repolistd);
|
2009-02-27 16:03:25 +01:00
|
|
|
|
2008-12-26 10:13:03 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static prop_dictionary_t
|
|
|
|
make_dict_from_pkg(const char *name, const char *ver, const char *desc)
|
|
|
|
{
|
|
|
|
prop_dictionary_t dict;
|
|
|
|
|
|
|
|
dict = prop_dictionary_create();
|
|
|
|
assert(dict != NULL);
|
|
|
|
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "pkgname", name);
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "version", ver);
|
|
|
|
prop_dictionary_set_cstring_nocopy(dict, "short_desc", desc);
|
|
|
|
|
|
|
|
return dict;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-04-02 02:46:36 +02:00
|
|
|
xbps_register_pkg(prop_dictionary_t pkgrd, bool update, bool automatic)
|
2008-12-26 10:13:03 +01:00
|
|
|
{
|
2009-04-02 02:46:36 +02:00
|
|
|
prop_dictionary_t dict, pkgd, newpkgd;
|
2008-12-26 10:13:03 +01:00
|
|
|
prop_array_t array;
|
2009-04-04 19:32:39 +02:00
|
|
|
const char *pkgname, *version, *desc, *rootdir;
|
2008-12-27 22:19:04 +01:00
|
|
|
char *plist;
|
2008-12-26 10:13:03 +01:00
|
|
|
int rv = 0;
|
|
|
|
|
2009-04-04 19:32:39 +02:00
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
plist = xbps_xasprintf("%s/%s/%s", rootdir,
|
|
|
|
XBPS_META_PATH, XBPS_REGPKGDB);
|
2008-12-27 22:19:04 +01:00
|
|
|
if (plist == NULL)
|
2008-12-26 04:36:51 +01:00
|
|
|
return EINVAL;
|
2008-12-24 07:20:19 +01:00
|
|
|
|
2009-04-02 02:46:36 +02:00
|
|
|
prop_dictionary_get_cstring_nocopy(pkgrd, "pkgname", &pkgname);
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgrd, "version", &version);
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgrd, "short_desc", &desc);
|
|
|
|
|
2008-12-26 10:13:03 +01:00
|
|
|
dict = prop_dictionary_internalize_from_file(plist);
|
|
|
|
if (dict == NULL) {
|
2009-04-02 02:46:36 +02:00
|
|
|
/*
|
|
|
|
* No packages registered yet. Register package into
|
|
|
|
* the dictionary.
|
|
|
|
*/
|
2008-12-26 10:13:03 +01:00
|
|
|
dict = prop_dictionary_create();
|
2008-12-27 22:19:04 +01:00
|
|
|
if (dict == NULL) {
|
|
|
|
free(plist);
|
2008-12-26 10:13:03 +01:00
|
|
|
return ENOMEM;
|
2008-12-27 22:19:04 +01:00
|
|
|
}
|
2008-12-23 12:28:21 +01:00
|
|
|
|
2008-12-26 10:13:03 +01:00
|
|
|
array = prop_array_create();
|
|
|
|
if (array == NULL) {
|
2009-02-10 02:52:12 +01:00
|
|
|
rv = ENOMEM;
|
|
|
|
goto out;
|
2008-12-26 04:36:51 +01:00
|
|
|
}
|
2008-12-26 10:13:03 +01:00
|
|
|
|
|
|
|
pkgd = make_dict_from_pkg(pkgname, version, desc);
|
|
|
|
if (!xbps_add_obj_to_array(array, pkgd)) {
|
|
|
|
prop_object_release(array);
|
2009-02-10 02:52:12 +01:00
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2008-12-26 04:36:51 +01:00
|
|
|
}
|
|
|
|
|
2009-02-15 20:23:23 +01:00
|
|
|
prop_dictionary_set_bool(pkgd, "automatic-install",
|
|
|
|
automatic);
|
|
|
|
|
2008-12-26 10:13:03 +01:00
|
|
|
if (!xbps_add_obj_to_dict(dict, array, "packages")) {
|
|
|
|
prop_object_release(array);
|
2009-02-10 02:52:12 +01:00
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
2008-12-26 04:36:51 +01:00
|
|
|
}
|
|
|
|
|
2008-12-26 10:13:03 +01:00
|
|
|
} else {
|
2009-04-02 02:46:36 +02:00
|
|
|
/*
|
|
|
|
* Check if package is already registered and return
|
|
|
|
* an error if not updating.
|
|
|
|
*/
|
2009-02-07 13:27:24 +01:00
|
|
|
pkgd = xbps_find_pkg_in_dict(dict, "packages", pkgname);
|
2009-04-02 02:46:36 +02:00
|
|
|
if (pkgd != NULL && update == false) {
|
2009-02-10 02:52:12 +01:00
|
|
|
rv = EEXIST;
|
|
|
|
goto out;
|
2008-12-26 04:36:51 +01:00
|
|
|
}
|
2008-12-26 10:13:03 +01:00
|
|
|
array = prop_dictionary_get(dict, "packages");
|
2009-02-10 02:52:12 +01:00
|
|
|
if (array == NULL) {
|
|
|
|
rv = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-04-02 02:46:36 +02:00
|
|
|
newpkgd = make_dict_from_pkg(pkgname, version, desc);
|
|
|
|
prop_dictionary_set_bool(newpkgd, "automatic-install",
|
|
|
|
automatic);
|
2009-02-15 20:23:23 +01:00
|
|
|
|
2009-04-02 02:46:36 +02:00
|
|
|
if (update && pkgrd && xbps_pkg_has_rundeps(pkgrd)) {
|
|
|
|
/*
|
|
|
|
* If updating a package, update the requiredby
|
|
|
|
* objects and set new version in pkg dictionary.
|
|
|
|
*/
|
|
|
|
rv = xbps_requiredby_pkg_add(array, pkgrd, true);
|
2009-02-10 02:52:12 +01:00
|
|
|
if (rv != 0) {
|
2009-04-02 02:46:36 +02:00
|
|
|
prop_object_release(newpkgd);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
prop_dictionary_set_cstring_nocopy(pkgd,
|
|
|
|
"version", version);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* If installing a package, update the requiredby
|
|
|
|
* objects and add new pkg dictionary into the
|
|
|
|
* packages array.
|
|
|
|
*/
|
|
|
|
if (pkgrd && xbps_pkg_has_rundeps(pkgrd)) {
|
|
|
|
rv = xbps_requiredby_pkg_add(array, pkgrd,
|
|
|
|
false);
|
|
|
|
if (rv != 0) {
|
|
|
|
prop_object_release(newpkgd);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!xbps_add_obj_to_array(array, newpkgd)) {
|
|
|
|
prop_object_release(newpkgd);
|
|
|
|
rv = EINVAL;
|
2009-02-10 02:52:12 +01:00
|
|
|
goto out;
|
|
|
|
}
|
2008-12-26 04:36:51 +01:00
|
|
|
}
|
2008-12-23 12:28:21 +01:00
|
|
|
}
|
|
|
|
|
2008-12-26 10:13:03 +01:00
|
|
|
if (!prop_dictionary_externalize_to_file(dict, plist))
|
|
|
|
rv = errno;
|
|
|
|
|
2009-02-10 02:52:12 +01:00
|
|
|
out:
|
2008-12-26 10:13:03 +01:00
|
|
|
prop_object_release(dict);
|
2008-12-27 22:19:04 +01:00
|
|
|
free(plist);
|
2008-12-26 04:36:51 +01:00
|
|
|
|
2008-12-23 12:28:21 +01:00
|
|
|
return rv;
|
|
|
|
}
|