2009-11-26 07:52:50 +05:30
|
|
|
/*-
|
2012-01-19 16:56:40 +05:30
|
|
|
* Copyright (c) 2009-2012 Juan Romero Pardines.
|
2009-11-26 07:52:50 +05:30
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2011-11-08 00:58:35 +05:30
|
|
|
#include <sys/utsname.h>
|
2009-11-26 07:52:50 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdlib.h>
|
2012-01-15 21:53:50 +05:30
|
|
|
#include <libgen.h>
|
2009-11-26 07:52:50 +05:30
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-11-13 07:48:58 +05:30
|
|
|
#include "xbps_api_impl.h"
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
|
|
|
* @file lib/repository_pool.c
|
2011-01-18 19:14:39 +05:30
|
|
|
* @brief Repository pool routines
|
|
|
|
* @defgroup repopool Repository pool functions
|
2010-01-21 07:40:19 +05:30
|
|
|
*/
|
|
|
|
|
2011-11-08 00:58:35 +05:30
|
|
|
/*
|
|
|
|
* Returns true if repository URI contains "noarch" or matching architecture
|
|
|
|
* in last component, false otherwise.
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
check_repo_arch(const char *uri)
|
|
|
|
{
|
|
|
|
struct utsname un;
|
2012-01-15 21:53:50 +05:30
|
|
|
char *p, *b;
|
2011-11-08 00:58:35 +05:30
|
|
|
|
2012-01-15 21:53:50 +05:30
|
|
|
if ((p = strdup(uri)) == NULL)
|
2011-11-08 00:58:35 +05:30
|
|
|
return false;
|
2012-01-15 21:53:50 +05:30
|
|
|
|
|
|
|
uname(&un);
|
|
|
|
b = basename(p);
|
2012-01-20 15:40:52 +05:30
|
|
|
if ((strcmp(b, "noarch")) && (strcmp(b, un.machine))) {
|
|
|
|
free(p);
|
2011-11-08 00:58:35 +05:30
|
|
|
return false;
|
2012-01-20 15:40:52 +05:30
|
|
|
}
|
|
|
|
free(p);
|
2012-01-15 21:53:50 +05:30
|
|
|
return true;
|
2011-11-08 00:58:35 +05:30
|
|
|
}
|
2011-07-14 03:43:25 +05:30
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
int HIDDEN
|
2011-12-22 21:08:05 +05:30
|
|
|
xbps_repository_pool_init(struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2011-12-22 21:08:05 +05:30
|
|
|
prop_dictionary_t d = NULL;
|
2012-01-21 14:50:45 +05:30
|
|
|
prop_array_t array;
|
2011-12-22 21:08:05 +05:30
|
|
|
size_t i, ntotal = 0, nmissing = 0;
|
2011-06-04 17:07:53 +05:30
|
|
|
const char *repouri;
|
2009-11-26 07:52:50 +05:30
|
|
|
char *plist;
|
|
|
|
int rv = 0;
|
|
|
|
|
2011-12-22 21:08:05 +05:30
|
|
|
if (prop_object_type(xhp->repo_pool) == PROP_TYPE_ARRAY)
|
2009-11-26 07:52:50 +05:30
|
|
|
return 0;
|
2011-12-15 15:49:20 +05:30
|
|
|
else if (xhp->cfg == NULL)
|
2011-11-11 04:37:26 +05:30
|
|
|
return ENOTSUP;
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2011-12-22 21:08:05 +05:30
|
|
|
xhp->repo_pool = prop_array_create();
|
|
|
|
if (xhp->repo_pool == NULL)
|
|
|
|
return ENOMEM;
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
|
|
|
|
repouri = cfg_getnstr(xhp->cfg, "repositories", i);
|
2011-11-08 00:58:35 +05:30
|
|
|
ntotal++;
|
2011-12-22 17:26:49 +05:30
|
|
|
/*
|
|
|
|
* Check if repository doesn't match our architecture.
|
|
|
|
*/
|
|
|
|
if (!check_repo_arch(repouri)) {
|
|
|
|
xbps_dbg_printf("[rpool] `%s' arch not matched, "
|
|
|
|
"ignoring.\n", repouri);
|
|
|
|
nmissing++;
|
|
|
|
continue;
|
|
|
|
}
|
2011-11-11 04:37:26 +05:30
|
|
|
/*
|
|
|
|
* If index file is not there, skip.
|
|
|
|
*/
|
|
|
|
plist = xbps_pkg_index_plist(repouri);
|
|
|
|
if (plist == NULL) {
|
|
|
|
rv = errno;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
if (access(plist, R_OK) == -1) {
|
|
|
|
xbps_dbg_printf("[rpool] `%s' missing index "
|
|
|
|
"file, ignoring.\n", repouri);
|
|
|
|
free(plist);
|
|
|
|
nmissing++;
|
|
|
|
continue;
|
|
|
|
}
|
2011-06-04 17:07:53 +05:30
|
|
|
/*
|
2011-12-22 21:08:05 +05:30
|
|
|
* Register repository into the array.
|
2011-06-04 17:07:53 +05:30
|
|
|
*/
|
2011-12-22 21:08:05 +05:30
|
|
|
d = prop_dictionary_create();
|
|
|
|
if (d == NULL) {
|
|
|
|
rv = ENOMEM;
|
2011-11-11 04:37:26 +05:30
|
|
|
free(plist);
|
2009-11-27 02:11:46 +05:30
|
|
|
goto out;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2011-12-22 21:08:05 +05:30
|
|
|
if (!prop_dictionary_set_cstring_nocopy(d, "uri", repouri)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
prop_object_release(d);
|
2011-11-11 04:37:26 +05:30
|
|
|
free(plist);
|
2010-11-19 18:10:13 +05:30
|
|
|
goto out;
|
|
|
|
}
|
2012-01-21 14:50:45 +05:30
|
|
|
array = prop_array_internalize_from_zfile(plist);
|
|
|
|
if (array == NULL) {
|
2011-12-22 21:08:05 +05:30
|
|
|
rv = EINVAL;
|
|
|
|
prop_object_release(d);
|
2009-11-26 07:52:50 +05:30
|
|
|
free(plist);
|
2009-11-27 02:11:46 +05:30
|
|
|
goto out;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
|
|
|
free(plist);
|
2012-01-21 14:50:45 +05:30
|
|
|
prop_array_make_immutable(array);
|
|
|
|
if (!xbps_add_obj_to_dict(d, array, "index")) {
|
|
|
|
rv = EINVAL;
|
|
|
|
prop_object_release(d);
|
|
|
|
goto out;
|
|
|
|
}
|
2011-12-22 21:08:05 +05:30
|
|
|
if (!prop_array_add(xhp->repo_pool, d)) {
|
|
|
|
rv = EINVAL;
|
|
|
|
prop_object_release(d);
|
|
|
|
goto out;
|
|
|
|
}
|
2011-11-11 04:37:26 +05:30
|
|
|
xbps_dbg_printf("[rpool] `%s' registered.\n", repouri);
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2011-07-29 14:47:34 +05:30
|
|
|
if (ntotal - nmissing == 0) {
|
|
|
|
/* no repositories available, error out */
|
|
|
|
rv = ENOTSUP;
|
2009-11-27 02:11:46 +05:30
|
|
|
goto out;
|
2011-07-29 14:47:34 +05:30
|
|
|
}
|
2009-11-27 02:11:46 +05:30
|
|
|
|
2011-07-29 14:47:34 +05:30
|
|
|
xbps_dbg_printf("[rpool] initialized ok.\n");
|
2009-11-26 07:52:50 +05:30
|
|
|
out:
|
2010-11-19 18:10:13 +05:30
|
|
|
if (rv != 0)
|
2011-12-22 21:25:35 +05:30
|
|
|
xbps_repository_pool_release(xhp);
|
2009-11-26 07:52:50 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
void HIDDEN
|
2011-12-22 21:25:35 +05:30
|
|
|
xbps_repository_pool_release(struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2011-12-22 21:08:05 +05:30
|
|
|
prop_dictionary_t d;
|
|
|
|
size_t i;
|
|
|
|
const char *uri;
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2011-12-22 21:08:05 +05:30
|
|
|
if (xhp->repo_pool == NULL)
|
2009-11-26 07:52:50 +05:30
|
|
|
return;
|
|
|
|
|
2011-12-22 21:08:05 +05:30
|
|
|
for (i = 0; i < prop_array_count(xhp->repo_pool); i++) {
|
|
|
|
d = prop_array_get(xhp->repo_pool, i);
|
|
|
|
prop_dictionary_get_cstring_nocopy(d, "uri", &uri);
|
|
|
|
xbps_dbg_printf("[rpool] unregistered repository '%s'\n", uri);
|
|
|
|
prop_object_release(d);
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2011-12-22 21:08:05 +05:30
|
|
|
prop_object_release(xhp->repo_pool);
|
|
|
|
xhp->repo_pool = NULL;
|
2011-07-29 14:47:34 +05:30
|
|
|
xbps_dbg_printf("[rpool] released ok.\n");
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
|
|
|
|
2011-11-11 03:44:50 +05:30
|
|
|
int
|
2012-01-22 14:30:46 +05:30
|
|
|
xbps_repository_pool_sync(void)
|
2011-11-11 03:44:50 +05:30
|
|
|
{
|
2012-01-22 14:30:46 +05:30
|
|
|
const struct xbps_handle *xhp = xbps_handle_get();
|
2011-11-11 03:44:50 +05:30
|
|
|
const char *repouri;
|
|
|
|
size_t i;
|
|
|
|
int rv;
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
if (xhp->cfg == NULL)
|
2011-11-11 03:44:50 +05:30
|
|
|
return ENOTSUP;
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
|
|
|
|
repouri = cfg_getnstr(xhp->cfg, "repositories", i);
|
2011-11-11 03:44:50 +05:30
|
|
|
/*
|
|
|
|
* Check if repository doesn't match our architecture.
|
|
|
|
*/
|
|
|
|
if (!check_repo_arch(repouri)) {
|
|
|
|
xbps_dbg_printf("[rpool] `%s' arch not matched, "
|
|
|
|
"ignoring.\n", repouri);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
2012-01-19 16:56:40 +05:30
|
|
|
* Fetch repository plist index.
|
2011-11-11 03:44:50 +05:30
|
|
|
*/
|
2012-01-15 18:54:44 +05:30
|
|
|
rv = xbps_repository_sync_pkg_index(repouri, XBPS_PKGINDEX);
|
|
|
|
if (rv == -1) {
|
|
|
|
xbps_dbg_printf("[rpool] `%s' failed to fetch: %s\n",
|
|
|
|
repouri, fetchLastErrCode == 0 ?
|
|
|
|
strerror(errno) : xbps_fetch_error_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/*
|
2012-01-19 16:56:40 +05:30
|
|
|
* Fetch repository plist files index.
|
2012-01-15 18:54:44 +05:30
|
|
|
*/
|
|
|
|
rv = xbps_repository_sync_pkg_index(repouri,
|
|
|
|
XBPS_PKGINDEX_FILES);
|
2011-11-11 03:44:50 +05:30
|
|
|
if (rv == -1) {
|
|
|
|
xbps_dbg_printf("[rpool] `%s' failed to fetch: %s\n",
|
2011-11-11 05:01:56 +05:30
|
|
|
repouri, fetchLastErrCode == 0 ?
|
|
|
|
strerror(errno) : xbps_fetch_error_string());
|
2011-11-11 03:44:50 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
int
|
|
|
|
xbps_repository_pool_foreach(
|
|
|
|
int (*fn)(struct repository_pool_index *, void *, bool *),
|
|
|
|
void *arg)
|
|
|
|
{
|
2011-12-22 21:08:05 +05:30
|
|
|
prop_dictionary_t d;
|
|
|
|
struct xbps_handle *xhp = xbps_handle_get();
|
|
|
|
struct repository_pool_index *rpi;
|
|
|
|
size_t i;
|
2010-11-19 18:10:13 +05:30
|
|
|
int rv = 0;
|
|
|
|
bool done = false;
|
|
|
|
|
2011-01-25 08:44:33 +05:30
|
|
|
assert(fn != NULL);
|
2011-12-22 21:08:05 +05:30
|
|
|
/* Initialize repository pool */
|
2011-12-15 15:49:20 +05:30
|
|
|
if ((rv = xbps_repository_pool_init(xhp)) != 0) {
|
2011-06-22 15:25:02 +05:30
|
|
|
if (rv == ENOTSUP) {
|
2011-07-29 14:47:34 +05:30
|
|
|
xbps_dbg_printf("[rpool] empty repository list.\n");
|
2011-06-22 15:25:02 +05:30
|
|
|
} else if (rv != ENOENT && rv != ENOTSUP) {
|
2011-07-29 14:47:34 +05:30
|
|
|
xbps_dbg_printf("[rpool] couldn't initialize: %s\n",
|
|
|
|
strerror(rv));
|
2011-06-22 15:25:02 +05:30
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2011-12-22 21:08:05 +05:30
|
|
|
/* Iterate over repository pool */
|
|
|
|
for (i = 0; i < prop_array_count(xhp->repo_pool); i++) {
|
|
|
|
rpi = malloc(sizeof(*rpi));
|
|
|
|
if (rpi == NULL)
|
|
|
|
return ENOMEM;
|
2010-11-19 18:10:13 +05:30
|
|
|
|
2011-12-22 21:08:05 +05:30
|
|
|
d = prop_array_get(xhp->repo_pool, i);
|
|
|
|
prop_dictionary_get_cstring_nocopy(d, "uri", &rpi->rpi_uri);
|
2012-01-19 16:56:40 +05:30
|
|
|
rpi->rpi_repo = prop_dictionary_get(d, "index");
|
2011-12-22 21:08:05 +05:30
|
|
|
rpi->rpi_index = i;
|
|
|
|
|
|
|
|
rv = (*fn)(rpi, arg, &done);
|
|
|
|
if (rv != 0 || done) {
|
|
|
|
free(rpi);
|
2010-11-19 18:10:13 +05:30
|
|
|
break;
|
2011-12-22 21:08:05 +05:30
|
|
|
}
|
|
|
|
free(rpi);
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|