2009-04-09 02:29:15 +02:00
|
|
|
/*-
|
|
|
|
* 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 <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <xbps_api.h>
|
|
|
|
|
|
|
|
static prop_dictionary_t pkg_props;
|
2009-05-19 01:42:48 +02:00
|
|
|
static bool pkg_props_initialized;
|
2009-04-09 02:29:15 +02:00
|
|
|
|
|
|
|
static int
|
2009-05-17 04:09:26 +02:00
|
|
|
create_pkg_props_dictionary(void)
|
2009-04-09 02:29:15 +02:00
|
|
|
{
|
|
|
|
prop_array_t unsorted, missing;
|
|
|
|
int rv = 0;
|
|
|
|
|
2009-05-19 01:42:48 +02:00
|
|
|
if (pkg_props_initialized)
|
2009-05-17 04:09:26 +02:00
|
|
|
return 0;
|
|
|
|
|
2009-04-09 02:29:15 +02:00
|
|
|
pkg_props = prop_dictionary_create();
|
|
|
|
if (pkg_props == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
|
|
|
missing = prop_array_create();
|
|
|
|
if (missing == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsorted = prop_array_create();
|
|
|
|
if (unsorted == NULL) {
|
|
|
|
rv = ENOMEM;
|
|
|
|
goto fail2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!xbps_add_obj_to_dict(pkg_props, missing, "missing_deps")) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
if (!xbps_add_obj_to_dict(pkg_props, unsorted, "unsorted_deps")) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto fail3;
|
|
|
|
}
|
|
|
|
|
2009-05-19 01:42:48 +02:00
|
|
|
pkg_props_initialized = true;
|
2009-04-09 02:29:15 +02:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
fail3:
|
|
|
|
prop_object_release(unsorted);
|
|
|
|
fail2:
|
|
|
|
prop_object_release(missing);
|
|
|
|
fail:
|
|
|
|
prop_object_release(pkg_props);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
prop_dictionary_t
|
2009-05-17 04:09:26 +02:00
|
|
|
xbps_get_pkg_props(void)
|
2009-04-09 02:29:15 +02:00
|
|
|
{
|
2009-05-19 01:42:48 +02:00
|
|
|
if (pkg_props_initialized == false)
|
2009-04-09 02:29:15 +02:00
|
|
|
return NULL;
|
|
|
|
|
2009-08-08 22:29:48 +02:00
|
|
|
return pkg_props;
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2009-05-17 04:09:26 +02:00
|
|
|
xbps_prepare_repolist_data(void)
|
2009-04-09 02:29:15 +02:00
|
|
|
{
|
2009-05-17 04:09:26 +02:00
|
|
|
prop_dictionary_t dict = NULL;
|
|
|
|
prop_array_t array;
|
2009-04-09 02:29:15 +02:00
|
|
|
prop_object_t obj;
|
2009-05-17 04:09:26 +02:00
|
|
|
prop_object_iterator_t iter;
|
|
|
|
struct repository_data *rdata;
|
|
|
|
const char *rootdir;
|
2009-04-09 02:29:15 +02:00
|
|
|
char *plist;
|
|
|
|
int rv = 0;
|
2009-05-19 01:42:48 +02:00
|
|
|
static bool repodata_initialized;
|
2009-04-09 02:29:15 +02:00
|
|
|
|
2009-05-19 01:42:48 +02:00
|
|
|
if (repodata_initialized)
|
|
|
|
return 0;
|
2009-05-17 04:09:26 +02:00
|
|
|
|
2009-05-19 01:42:48 +02:00
|
|
|
SIMPLEQ_INIT(&repodata_queue);
|
2009-04-09 02:29:15 +02:00
|
|
|
|
|
|
|
rootdir = xbps_get_rootdir();
|
|
|
|
if (rootdir == NULL)
|
|
|
|
rootdir = "";
|
|
|
|
|
|
|
|
plist = xbps_xasprintf("%s/%s/%s", rootdir,
|
|
|
|
XBPS_META_PATH, XBPS_REPOLIST);
|
2009-05-17 04:09:26 +02:00
|
|
|
if (plist == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-04-09 02:29:15 +02:00
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
dict = prop_dictionary_internalize_from_file(plist);
|
|
|
|
if (dict == NULL) {
|
2009-04-09 02:29:15 +02:00
|
|
|
free(plist);
|
2009-05-17 04:09:26 +02:00
|
|
|
rv = errno;
|
|
|
|
goto out;
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
free(plist);
|
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
array = prop_dictionary_get(dict, "repository-list");
|
2009-04-09 02:29:15 +02:00
|
|
|
if (array == NULL) {
|
2009-05-17 04:09:26 +02:00
|
|
|
rv = EINVAL;
|
2009-08-08 22:29:48 +02:00
|
|
|
goto out1;
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
iter = prop_array_iterator(array);
|
|
|
|
if (iter == NULL) {
|
|
|
|
rv = ENOMEM;
|
2009-08-08 22:29:48 +02:00
|
|
|
goto out1;
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
while ((obj = prop_object_iterator_next(iter)) != NULL) {
|
2009-04-09 02:29:15 +02:00
|
|
|
/*
|
2009-05-17 04:09:26 +02:00
|
|
|
* Iterate over the repository pool and add the dictionary
|
|
|
|
* for current repository into the queue.
|
2009-04-09 02:29:15 +02:00
|
|
|
*/
|
|
|
|
plist =
|
|
|
|
xbps_get_pkg_index_plist(prop_string_cstring_nocopy(obj));
|
2009-05-17 04:09:26 +02:00
|
|
|
if (plist == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out2;
|
|
|
|
}
|
2009-04-09 02:29:15 +02:00
|
|
|
|
2009-05-19 01:42:48 +02:00
|
|
|
rdata = malloc(sizeof(struct repository_data));
|
|
|
|
if (rdata == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out2;
|
|
|
|
}
|
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
rdata->rd_repod = prop_dictionary_internalize_from_file(plist);
|
|
|
|
if (rdata->rd_repod == NULL) {
|
2009-04-09 02:29:15 +02:00
|
|
|
free(plist);
|
2009-05-17 04:09:26 +02:00
|
|
|
rv = errno;
|
|
|
|
goto out2;
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
free(plist);
|
2009-05-17 04:09:26 +02:00
|
|
|
SIMPLEQ_INSERT_TAIL(&repodata_queue, rdata, chain);
|
|
|
|
}
|
|
|
|
|
2009-05-19 01:42:48 +02:00
|
|
|
repodata_initialized = true;
|
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
out2:
|
|
|
|
prop_object_iterator_release(iter);
|
2009-08-08 22:29:48 +02:00
|
|
|
out1:
|
2009-05-17 04:09:26 +02:00
|
|
|
prop_object_release(dict);
|
2009-08-08 22:29:48 +02:00
|
|
|
out:
|
2009-05-17 04:09:26 +02:00
|
|
|
if (rv != 0)
|
|
|
|
xbps_release_repolist_data();
|
|
|
|
|
|
|
|
return rv;
|
2009-04-09 02:29:15 +02:00
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xbps_release_repolist_data(void)
|
|
|
|
{
|
|
|
|
struct repository_data *rdata;
|
|
|
|
|
|
|
|
while ((rdata = SIMPLEQ_FIRST(&repodata_queue)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE(&repodata_queue, rdata, repository_data, chain);
|
|
|
|
prop_object_release(rdata->rd_repod);
|
|
|
|
free(rdata);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_find_new_pkg(const char *pkgname, prop_dictionary_t instpkg)
|
|
|
|
{
|
|
|
|
prop_dictionary_t pkgrd = NULL;
|
|
|
|
prop_array_t unsorted;
|
|
|
|
struct repository_data *rdata;
|
|
|
|
const char *repoloc, *repover, *instver;
|
|
|
|
int rv = 0;
|
2009-08-09 23:34:07 +02:00
|
|
|
pkg_state_t state = 0;
|
2009-05-17 04:09:26 +02:00
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
assert(instpkg != NULL);
|
|
|
|
|
|
|
|
SIMPLEQ_FOREACH(rdata, &repodata_queue, chain) {
|
2009-04-09 02:29:15 +02:00
|
|
|
/*
|
|
|
|
* Get the package dictionary from current repository.
|
|
|
|
* If it's not there, pass to the next repository.
|
|
|
|
*/
|
2009-05-17 04:09:26 +02:00
|
|
|
pkgrd = xbps_find_pkg_in_dict(rdata->rd_repod,
|
|
|
|
"packages", pkgname);
|
|
|
|
if (pkgrd != NULL) {
|
|
|
|
/*
|
|
|
|
* Check if installed version is >= than the
|
|
|
|
* one available in current repository.
|
|
|
|
*/
|
|
|
|
prop_dictionary_get_cstring_nocopy(instpkg,
|
|
|
|
"version", &instver);
|
|
|
|
prop_dictionary_get_cstring_nocopy(pkgrd,
|
|
|
|
"version", &repover);
|
2009-06-07 05:02:58 +02:00
|
|
|
if (xbps_cmpver(instver, repover) >= 0)
|
2009-05-17 04:09:26 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
break;
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
}
|
2009-05-17 04:09:26 +02:00
|
|
|
if (pkgrd == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create master pkg dictionary.
|
|
|
|
*/
|
|
|
|
if ((rv = create_pkg_props_dictionary()) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set repository in pkg dictionary.
|
|
|
|
*/
|
|
|
|
if (!prop_dictionary_get_cstring_nocopy(rdata->rd_repod,
|
|
|
|
"location-local", &repoloc)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
prop_dictionary_set_cstring(pkgrd, "repository", repoloc);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Construct the dependency chain for this package.
|
|
|
|
*/
|
|
|
|
if ((rv = xbps_find_deps_in_pkg(pkg_props, pkgrd)) != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add required package dictionary into the packages
|
|
|
|
* dictionary.
|
|
|
|
*/
|
|
|
|
unsorted = prop_dictionary_get(pkg_props, "unsorted_deps");
|
|
|
|
if (unsorted == NULL) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-08-09 23:34:07 +02:00
|
|
|
/*
|
|
|
|
* Always set "not-installed" package state. Will be overwritten
|
|
|
|
* to its correct state later.
|
|
|
|
*/
|
|
|
|
rv = xbps_set_pkg_state_dictionary(pkgrd, XBPS_PKG_STATE_NOT_INSTALLED);
|
|
|
|
if (rv != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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(pkgrd, state)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
if (!prop_array_add(unsorted, pkgrd))
|
|
|
|
rv = errno;
|
2009-04-09 02:29:15 +02:00
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
out:
|
|
|
|
if (rv != 0)
|
|
|
|
xbps_release_repolist_data();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
xbps_prepare_pkg(const char *pkgname)
|
|
|
|
{
|
|
|
|
prop_dictionary_t pkgrd = NULL;
|
|
|
|
prop_array_t pkgs_array;
|
|
|
|
struct repository_data *rdata;
|
|
|
|
const char *repoloc;
|
|
|
|
int rv = 0;
|
2009-08-08 22:29:48 +02:00
|
|
|
pkg_state_t state = 0;
|
2009-05-17 04:09:26 +02:00
|
|
|
|
|
|
|
assert(pkgname != NULL);
|
|
|
|
|
|
|
|
if ((rv = xbps_prepare_repolist_data()) != 0)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
SIMPLEQ_FOREACH(rdata, &repodata_queue, chain) {
|
|
|
|
/*
|
|
|
|
* Get the package dictionary from current repository.
|
|
|
|
* If it's not there, pass to the next repository.
|
|
|
|
*/
|
|
|
|
pkgrd = xbps_find_pkg_in_dict(rdata->rd_repod,
|
|
|
|
"packages", pkgname);
|
|
|
|
if (pkgrd != NULL)
|
|
|
|
break;
|
|
|
|
}
|
2009-04-09 02:29:15 +02:00
|
|
|
if (pkgrd == NULL) {
|
|
|
|
rv = EAGAIN;
|
2009-05-17 04:09:26 +02:00
|
|
|
goto out;
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create master pkg dictionary.
|
|
|
|
*/
|
2009-05-17 04:09:26 +02:00
|
|
|
if ((rv = create_pkg_props_dictionary()) != 0)
|
2009-04-09 02:29:15 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set repository in pkg dictionary.
|
|
|
|
*/
|
2009-05-17 04:09:26 +02:00
|
|
|
if (!prop_dictionary_get_cstring_nocopy(rdata->rd_repod,
|
2009-04-09 02:29:15 +02:00
|
|
|
"location-local", &repoloc)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
prop_dictionary_set_cstring(pkgrd, "repository", repoloc);
|
2009-05-19 03:33:15 +02:00
|
|
|
prop_dictionary_set_cstring(pkg_props, "origin", pkgname);
|
2009-04-09 02:29:15 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Check if this package needs dependencies.
|
|
|
|
*/
|
|
|
|
if (xbps_pkg_has_rundeps(pkgrd)) {
|
|
|
|
/*
|
|
|
|
* Construct the dependency chain for this package.
|
|
|
|
*/
|
2009-05-17 04:09:26 +02:00
|
|
|
if ((rv = xbps_find_deps_in_pkg(pkg_props, pkgrd)) != 0)
|
2009-04-09 02:29:15 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sort the dependency chain for this package.
|
|
|
|
*/
|
|
|
|
if ((rv = xbps_sort_pkg_deps(pkg_props)) != 0)
|
|
|
|
goto out;
|
2009-04-10 01:36:22 +02:00
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Package has no deps, so we have to create the
|
|
|
|
* "packages" array.
|
|
|
|
*/
|
|
|
|
pkgs_array = prop_array_create();
|
|
|
|
if (pkgs_array == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-05-17 04:09:26 +02:00
|
|
|
if (!prop_dictionary_set(pkg_props, "packages",
|
|
|
|
pkgs_array)) {
|
2009-04-10 01:36:22 +02:00
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
2009-04-09 02:29:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add required package dictionary into the packages
|
|
|
|
* dictionary.
|
|
|
|
*/
|
|
|
|
pkgs_array = prop_dictionary_get(pkg_props, "packages");
|
|
|
|
if (pkgs_array == NULL ||
|
|
|
|
prop_object_type(pkgs_array) != PROP_TYPE_ARRAY) {
|
|
|
|
rv = EINVAL;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-08-08 22:29:48 +02:00
|
|
|
/*
|
|
|
|
* Always set "not-installed" package state. Will be overwritten
|
|
|
|
* to its correct state later.
|
|
|
|
*/
|
|
|
|
rv = xbps_set_pkg_state_dictionary(pkgrd, XBPS_PKG_STATE_NOT_INSTALLED);
|
|
|
|
if (rv != 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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(pkgrd, state)) != 0)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2009-05-17 04:09:26 +02:00
|
|
|
if (!prop_array_add(pkgs_array, pkgrd))
|
2009-04-09 02:29:15 +02:00
|
|
|
rv = errno;
|
|
|
|
|
|
|
|
out:
|
2009-05-17 04:09:26 +02:00
|
|
|
xbps_release_repolist_data();
|
2009-04-09 02:29:15 +02:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|