2009-08-17 22:37:20 +05:30
|
|
|
/*-
|
|
|
|
* 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>
|
|
|
|
|
2009-11-30 16:38:46 +05:30
|
|
|
static prop_dictionary_t trans_dict;
|
|
|
|
static bool trans_dict_initialized;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
static int set_pkg_state(prop_dictionary_t, const char *);
|
|
|
|
|
|
|
|
static int
|
2009-11-30 16:38:46 +05:30
|
|
|
create_transaction_dictionary(void)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
|
|
|
prop_array_t unsorted, missing;
|
|
|
|
int rv = 0;
|
|
|
|
|
2009-11-30 16:38:46 +05:30
|
|
|
if (trans_dict_initialized)
|
2009-08-17 22:37:20 +05:30
|
|
|
return 0;
|
|
|
|
|
2009-11-30 16:38:46 +05:30
|
|
|
trans_dict = prop_dictionary_create();
|
|
|
|
if (trans_dict == NULL)
|
2009-08-17 22:37:20 +05:30
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
missing = prop_array_create();
|
|
|
|
if (missing == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsorted = prop_array_create();
|
|
|
|
if (unsorted == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
2009-11-30 16:38:46 +05:30
|
|
|
if (!xbps_add_obj_to_dict(trans_dict, missing, "missing_deps")) {
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto fail3;
|
|
|
|
}
|
2009-11-30 16:38:46 +05:30
|
|
|
if (!xbps_add_obj_to_dict(trans_dict, unsorted, "unsorted_deps")) {
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
2009-11-30 16:38:46 +05:30
|
|
|
trans_dict_initialized = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
fail3:
|
|
|
|
prop_object_release(unsorted);
|
|
|
|
fail2:
|
|
|
|
prop_object_release(missing);
|
|
|
|
fail:
|
2009-11-30 16:38:46 +05:30
|
|
|
prop_object_release(trans_dict);
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-10-27 06:16:00 +05:30
|
|
|
prop_dictionary_t SYMEXPORT
|
2009-11-30 16:38:46 +05:30
|
|
|
xbps_repository_get_transaction_dict(void)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2009-11-30 16:38:46 +05:30
|
|
|
if (trans_dict_initialized == false)
|
2009-08-17 22:37:20 +05:30
|
|
|
return NULL;
|
|
|
|
|
2009-11-30 16:38:46 +05:30
|
|
|
return trans_dict;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
|
2009-10-27 06:16:00 +05:30
|
|
|
int SYMEXPORT
|
2009-11-30 16:38:46 +05:30
|
|
|
xbps_repository_update_allpkgs(void)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
|
|
|
prop_dictionary_t dict;
|
|
|
|
prop_object_t obj;
|
|
|
|
prop_object_iterator_t iter;
|
|
|
|
const char *pkgname;
|
|
|
|
int rv = 0;
|
2009-10-17 09:48:20 +05:30
|
|
|
bool newpkg_found = false;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Prepare dictionary with all registered packages.
|
|
|
|
*/
|
2009-11-26 07:52:50 +05:30
|
|
|
dict = xbps_regpkgs_dictionary_init();
|
2009-08-17 22:37:20 +05:30
|
|
|
if (dict == NULL)
|
|
|
|
return ENOENT;
|
|
|
|
|
|
|
|
/*
|
2009-11-26 07:52:50 +05:30
|
|
|
* Prepare simpleq with all registered repositories.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2009-11-26 07:52:50 +05:30
|
|
|
if ((rv = xbps_repository_pool_init()) != 0)
|
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
iter = xbps_get_array_iter_from_dict(dict, "packages");
|
2009-11-26 07:52:50 +05:30
|
|
|
if (iter == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out if there is a newer version for all currently
|
|
|
|
* installed packages.
|
|
|
|
*/
|
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(obj,
|
|
|
|
"pkgname", &pkgname)) {
|
|
|
|
rv = errno;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-30 16:38:46 +05:30
|
|
|
rv = xbps_repository_update_pkg(pkgname, obj);
|
2009-10-17 09:48:20 +05:30
|
|
|
if (rv == ENOENT)
|
|
|
|
continue;
|
|
|
|
else if (rv == EEXIST) {
|
2009-10-16 19:16:54 +05:30
|
|
|
rv = 0;
|
2009-10-16 19:01:57 +05:30
|
|
|
continue;
|
2009-11-21 11:42:42 +05:30
|
|
|
} else if (rv != 0)
|
|
|
|
break;
|
|
|
|
|
2009-10-17 09:48:20 +05:30
|
|
|
newpkg_found = true;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
prop_object_iterator_release(iter);
|
|
|
|
|
2009-10-17 09:48:20 +05:30
|
|
|
if (rv != ENOENT && !newpkg_found)
|
|
|
|
rv = ENOPKG;
|
2009-11-26 07:52:50 +05:30
|
|
|
out:
|
|
|
|
xbps_repository_pool_release();
|
|
|
|
xbps_regpkgs_dictionary_release();
|
2009-10-17 09:48:20 +05:30
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-10-27 06:16:00 +05:30
|
|
|
int SYMEXPORT
|
2009-11-30 16:38:46 +05:30
|
|
|
xbps_repository_update_pkg(const char *pkgname, prop_dictionary_t instpkg)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
|
|
|
prop_dictionary_t pkgrd = NULL;
|
|
|
|
prop_array_t unsorted;
|
2009-11-30 16:54:04 +05:30
|
|
|
struct repository_pool *rpool;
|
2009-10-27 06:16:00 +05:30
|
|
|
const char *repover, *instver;
|
2009-08-17 22:37:20 +05:30
|
|
|
int rv = 0;
|
2009-10-16 19:01:57 +05:30
|
|
|
bool newpkg_found = false;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
assert(instpkg != NULL);
|
|
|
|
|
2009-10-16 20:34:35 +05:30
|
|
|
/*
|
2009-11-26 07:52:50 +05:30
|
|
|
* Prepare repository pool queue.
|
2009-10-16 20:34:35 +05:30
|
|
|
*/
|
2009-11-26 07:52:50 +05:30
|
|
|
if ((rv = xbps_repository_pool_init()) != 0)
|
2009-10-16 20:34:35 +05:30
|
|
|
return rv;
|
|
|
|
|
2009-11-30 16:54:04 +05:30
|
|
|
SIMPLEQ_FOREACH(rpool, &repopool_queue, chain) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Get the package dictionary from current repository.
|
|
|
|
* If it's not there, pass to the next repository.
|
|
|
|
*/
|
2009-11-30 16:54:04 +05:30
|
|
|
pkgrd = xbps_find_pkg_in_dict(rpool->rp_repod,
|
2009-08-17 22:37:20 +05:30
|
|
|
"packages", pkgname);
|
2009-11-26 07:52:50 +05:30
|
|
|
if (pkgrd == NULL) {
|
|
|
|
if (errno && errno != ENOENT) {
|
|
|
|
rv = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DPRINTF(("Package %s not found in repo %s.\n",
|
2009-11-30 16:54:04 +05:30
|
|
|
pkgname, rpool->rp_uri));
|
2009-11-26 07:52:50 +05:30
|
|
|
} else if (pkgrd != NULL) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
2009-10-16 19:01:57 +05:30
|
|
|
* Check if version in repository is greater than
|
|
|
|
* the version currently installed.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_get_cstring_nocopy(instpkg,
|
|
|
|
"version", &instver)) {
|
|
|
|
rv = errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(pkgrd,
|
|
|
|
"version", &repover)) {
|
|
|
|
rv = errno;
|
|
|
|
break;
|
|
|
|
}
|
2009-10-16 19:01:57 +05:30
|
|
|
if (xbps_cmpver(repover, instver) > 0) {
|
2009-11-21 11:42:42 +05:30
|
|
|
DPRINTF(("Found %s-%s in repo %s.\n",
|
2009-11-30 16:54:04 +05:30
|
|
|
pkgname, repover, rpool->rp_uri));
|
2009-10-16 19:01:57 +05:30
|
|
|
newpkg_found = true;
|
|
|
|
break;
|
|
|
|
}
|
2009-11-21 11:42:42 +05:30
|
|
|
DPRINTF(("Skipping %s-%s in repo %s.\n",
|
2009-11-30 16:54:04 +05:30
|
|
|
pkgname, repover, rpool->rp_uri));
|
2009-11-23 02:22:15 +05:30
|
|
|
continue;
|
2009-08-17 22:37:20 +05:30
|
|
|
}
|
|
|
|
}
|
2009-11-26 07:52:50 +05:30
|
|
|
if (!newpkg_found) {
|
|
|
|
rv = EEXIST;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
|
2009-11-26 07:52:50 +05:30
|
|
|
if (pkgrd == NULL) {
|
|
|
|
rv = ENOENT;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
2009-11-30 16:38:46 +05:30
|
|
|
* Create the transaction dictionary.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2009-11-30 16:38:46 +05:30
|
|
|
if ((rv = create_transaction_dictionary()) != 0)
|
2009-08-17 22:37:20 +05:30
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set repository in pkg dictionary.
|
|
|
|
*/
|
2009-11-30 16:54:04 +05:30
|
|
|
if (!prop_dictionary_set_cstring(pkgrd, "repository", rpool->rp_uri)) {
|
2009-11-23 15:16:51 +05:30
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct the dependency chain for this package.
|
|
|
|
*/
|
2009-11-30 16:38:46 +05:30
|
|
|
if ((rv = xbps_repository_find_pkg_deps(trans_dict, pkgrd)) != 0)
|
2009-08-17 22:37:20 +05:30
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add required package dictionary into the packages
|
|
|
|
* dictionary.
|
|
|
|
*/
|
2009-11-30 16:38:46 +05:30
|
|
|
unsorted = prop_dictionary_get(trans_dict, "unsorted_deps");
|
2009-08-17 22:37:20 +05:30
|
|
|
if (unsorted == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Always set "not-installed" package state. Will be overwritten
|
|
|
|
* to its correct state later.
|
|
|
|
*/
|
|
|
|
if ((rv = set_pkg_state(pkgrd, pkgname)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
2009-11-23 15:16:51 +05:30
|
|
|
if (!prop_dictionary_set_cstring_nocopy(pkgrd,
|
|
|
|
"trans-action", "update")) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
if (!prop_array_add(unsorted, pkgrd))
|
|
|
|
rv = errno;
|
|
|
|
|
|
|
|
out:
|
2009-11-26 07:52:50 +05:30
|
|
|
xbps_repository_pool_release();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
set_pkg_state(prop_dictionary_t pkgd, const char *pkgname)
|
|
|
|
{
|
|
|
|
pkg_state_t state = 0;
|
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
rv = xbps_set_pkg_state_dictionary(pkgd, XBPS_PKG_STATE_NOT_INSTALLED);
|
|
|
|
if (rv != 0)
|
|
|
|
return rv;
|
|
|
|
/*
|
|
|
|
* Overwrite package state in dictionary if it was unpacked
|
|
|
|
* previously.
|
|
|
|
*/
|
|
|
|
rv = xbps_get_pkg_state_installed(pkgname, &state);
|
|
|
|
if (rv == 0) {
|
|
|
|
if ((rv = xbps_set_pkg_state_dictionary(pkgd, state)) != 0)
|
|
|
|
return rv;
|
|
|
|
} else if (rv == ENOENT)
|
|
|
|
rv = 0;
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2009-10-27 06:16:00 +05:30
|
|
|
int SYMEXPORT
|
2009-11-30 16:38:46 +05:30
|
|
|
xbps_repository_install_pkg(const char *pkgname)
|
2009-08-17 22:37:20 +05:30
|
|
|
{
|
2009-11-26 07:52:50 +05:30
|
|
|
prop_dictionary_t origin_pkgrd = NULL, pkgrd = NULL;
|
2009-12-22 17:07:36 +05:30
|
|
|
prop_array_t unsorted;
|
2009-11-30 16:54:04 +05:30
|
|
|
struct repository_pool *rpool;
|
2009-08-17 22:37:20 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
2009-11-26 07:52:50 +05:30
|
|
|
if ((rv = xbps_repository_pool_init()) != 0)
|
2009-08-17 22:37:20 +05:30
|
|
|
return rv;
|
|
|
|
|
2009-11-30 16:54:04 +05:30
|
|
|
SIMPLEQ_FOREACH(rpool, &repopool_queue, chain) {
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
|
|
|
* Get the package dictionary from current repository.
|
|
|
|
* If it's not there, pass to the next repository.
|
|
|
|
*/
|
2009-11-30 16:54:04 +05:30
|
|
|
pkgrd = xbps_find_pkg_in_dict(rpool->rp_repod,
|
2009-08-17 22:37:20 +05:30
|
|
|
"packages", pkgname);
|
2009-11-26 07:52:50 +05:30
|
|
|
if (pkgrd == NULL) {
|
|
|
|
if (errno && errno != ENOENT) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
} else if (pkgrd != NULL)
|
2009-08-17 22:37:20 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (pkgrd == NULL) {
|
|
|
|
rv = EAGAIN;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2009-11-30 16:38:46 +05:30
|
|
|
* Create the transaction dictionary.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2009-11-30 16:38:46 +05:30
|
|
|
if ((rv = create_transaction_dictionary()) != 0)
|
2009-08-17 22:37:20 +05:30
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set repository in pkg dictionary.
|
|
|
|
*/
|
2009-11-30 16:54:04 +05:30
|
|
|
if (!prop_dictionary_set_cstring(pkgrd, "repository", rpool->rp_uri)) {
|
2009-11-23 15:16:51 +05:30
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-11-26 07:52:50 +05:30
|
|
|
origin_pkgrd = prop_dictionary_copy(pkgrd);
|
|
|
|
|
2009-08-17 22:37:20 +05:30
|
|
|
/*
|
2009-12-22 17:07:36 +05:30
|
|
|
* Prepare required package dependencies.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2009-12-22 17:07:36 +05:30
|
|
|
if ((rv = xbps_repository_find_pkg_deps(trans_dict, pkgrd)) != 0)
|
|
|
|
goto out;
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
/*
|
2009-12-22 17:07:36 +05:30
|
|
|
* Add required package dictionary into the unsorted deps dictionary,
|
|
|
|
* set package state as not yet installed.
|
2009-08-17 22:37:20 +05:30
|
|
|
*/
|
2009-12-22 17:07:36 +05:30
|
|
|
unsorted = prop_dictionary_get(trans_dict, "unsorted_deps");
|
|
|
|
if (unsorted == NULL) {
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-11-26 07:52:50 +05:30
|
|
|
if ((rv = set_pkg_state(origin_pkgrd, pkgname)) != 0)
|
2009-08-17 22:37:20 +05:30
|
|
|
goto out;
|
|
|
|
|
2009-11-26 07:52:50 +05:30
|
|
|
if (!prop_dictionary_set_cstring_nocopy(origin_pkgrd,
|
2009-11-23 15:16:51 +05:30
|
|
|
"trans-action", "install")) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-12-22 17:07:36 +05:30
|
|
|
if (!prop_array_add(unsorted, origin_pkgrd))
|
2009-08-17 22:37:20 +05:30
|
|
|
rv = errno;
|
|
|
|
|
|
|
|
out:
|
2009-11-26 07:52:50 +05:30
|
|
|
if (origin_pkgrd)
|
|
|
|
prop_object_release(origin_pkgrd);
|
|
|
|
|
|
|
|
xbps_repository_pool_release();
|
2009-08-17 22:37:20 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|