2009-11-26 07:52:50 +05:30
|
|
|
/*-
|
2011-01-18 19:14:39 +05:30
|
|
|
* Copyright (c) 2009-2011 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>
|
|
|
|
#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
|
|
|
*/
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
struct repository_pool {
|
|
|
|
SIMPLEQ_ENTRY(repository_pool) rp_entries;
|
|
|
|
struct repository_pool_index *rpi;
|
|
|
|
};
|
|
|
|
|
|
|
|
static SIMPLEQ_HEAD(rpool_head, repository_pool) rpool_queue =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(rpool_queue);
|
|
|
|
|
2009-11-26 07:52:50 +05:30
|
|
|
static bool repolist_initialized;
|
|
|
|
|
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;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
uname(&un);
|
|
|
|
p = strrchr(uri, '/');
|
|
|
|
if (p == NULL)
|
|
|
|
return false;
|
|
|
|
p++;
|
|
|
|
if (*p == '\0')
|
|
|
|
return false;
|
|
|
|
else if (strcmp(p, "noarch") == 0)
|
|
|
|
return true;
|
|
|
|
else if (strcmp(p, un.machine) == 0)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-07-14 03:43:25 +05:30
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
int HIDDEN
|
2011-12-15 15:49:20 +05:30
|
|
|
xbps_repository_pool_init(const struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2009-11-30 16:54:04 +05:30
|
|
|
struct repository_pool *rpool;
|
2011-12-03 15:07:31 +05:30
|
|
|
size_t i, ntotal = 0, nmissing = 0, repocnt = 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-06-04 17:07:53 +05:30
|
|
|
bool duprepo;
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
if (repolist_initialized)
|
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-15 15:49:20 +05:30
|
|
|
for (i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
|
|
|
|
repouri = cfg_getnstr(xhp->cfg, "repositories", i);
|
2009-11-26 07:52:50 +05:30
|
|
|
/*
|
2011-06-04 17:07:53 +05:30
|
|
|
* Check that we do not register duplicate repositories.
|
2009-11-26 07:52:50 +05:30
|
|
|
*/
|
2011-06-04 17:07:53 +05:30
|
|
|
duprepo = false;
|
|
|
|
SIMPLEQ_FOREACH(rpool, &rpool_queue, rp_entries) {
|
|
|
|
if (strcmp(rpool->rpi->rpi_uri, repouri) == 0) {
|
|
|
|
duprepo = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (duprepo)
|
|
|
|
continue;
|
|
|
|
|
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-11-08 00:58:35 +05:30
|
|
|
* Internalize repository's index dictionary and add it
|
|
|
|
* into the queue.
|
2011-06-04 17:07:53 +05:30
|
|
|
*/
|
2009-11-30 16:54:04 +05:30
|
|
|
rpool = malloc(sizeof(struct repository_pool));
|
|
|
|
if (rpool == NULL) {
|
2009-11-26 07:52:50 +05:30
|
|
|
rv = errno;
|
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
|
|
|
}
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
rpool->rpi = malloc(sizeof(struct repository_pool_index));
|
|
|
|
if (rpool->rpi == NULL) {
|
|
|
|
rv = errno;
|
2010-01-18 04:56:50 +05:30
|
|
|
free(rpool);
|
2011-11-11 04:37:26 +05:30
|
|
|
free(plist);
|
2010-11-19 18:10:13 +05:30
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
rpool->rpi->rpi_uri = repouri;
|
2010-11-19 18:10:13 +05:30
|
|
|
rpool->rpi->rpi_repod =
|
|
|
|
prop_dictionary_internalize_from_zfile(plist);
|
|
|
|
if (rpool->rpi->rpi_repod == NULL) {
|
2011-11-11 04:37:26 +05:30
|
|
|
rv = errno;
|
2010-11-19 18:10:13 +05:30
|
|
|
free(rpool->rpi);
|
2010-01-18 04:56:50 +05:30
|
|
|
free(rpool);
|
2009-11-26 07:52:50 +05:30
|
|
|
free(plist);
|
2011-07-29 14:47:34 +05:30
|
|
|
xbps_dbg_printf("[rpool] cannot internalize plist %s: %s\n",
|
|
|
|
plist, strerror(rv));
|
2009-11-27 02:11:46 +05:30
|
|
|
goto out;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
|
|
|
free(plist);
|
2011-11-11 04:37:26 +05:30
|
|
|
xbps_dbg_printf("[rpool] `%s' registered.\n", repouri);
|
2011-12-03 15:07:31 +05:30
|
|
|
rpool->rpi->rpi_index = repocnt;
|
2010-11-19 18:10:13 +05:30
|
|
|
SIMPLEQ_INSERT_TAIL(&rpool_queue, rpool, rp_entries);
|
2011-12-03 15:07:31 +05:30
|
|
|
repocnt++;
|
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
|
|
|
|
2009-11-26 07:52:50 +05:30
|
|
|
repolist_initialized = true;
|
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)
|
2009-11-26 07:52:50 +05:30
|
|
|
xbps_repository_pool_release();
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
void HIDDEN
|
2009-11-26 07:52:50 +05:30
|
|
|
xbps_repository_pool_release(void)
|
|
|
|
{
|
2009-11-30 16:54:04 +05:30
|
|
|
struct repository_pool *rpool;
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
if (!repolist_initialized)
|
2009-11-26 07:52:50 +05:30
|
|
|
return;
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
while ((rpool = SIMPLEQ_FIRST(&rpool_queue)) != NULL) {
|
|
|
|
SIMPLEQ_REMOVE(&rpool_queue, rpool, repository_pool, rp_entries);
|
2011-10-29 13:15:51 +05:30
|
|
|
xbps_dbg_printf("[rpool] unregistered repository '%s'\n",
|
2010-11-19 18:10:13 +05:30
|
|
|
rpool->rpi->rpi_uri);
|
|
|
|
prop_object_release(rpool->rpi->rpi_repod);
|
|
|
|
free(rpool->rpi);
|
2009-11-30 16:54:04 +05:30
|
|
|
free(rpool);
|
2011-01-18 19:14:39 +05:30
|
|
|
rpool = NULL;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
|
|
|
repolist_initialized = false;
|
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
|
2011-12-15 15:49:20 +05:30
|
|
|
xbps_repository_pool_sync(const struct xbps_handle *xhp)
|
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;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Fetch repository index file.
|
|
|
|
*/
|
|
|
|
rv = xbps_repository_sync_pkg_index(repouri);
|
|
|
|
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-15 15:49:20 +05:30
|
|
|
const struct xbps_handle *xhp = xbps_handle_get();
|
2011-01-30 19:39:18 +05:30
|
|
|
struct repository_pool *rpool, *rpool_new;
|
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-06-22 15:25:02 +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;
|
|
|
|
}
|
2010-11-19 18:10:13 +05:30
|
|
|
|
2011-01-30 19:39:18 +05:30
|
|
|
SIMPLEQ_FOREACH_SAFE(rpool, &rpool_queue, rp_entries, rpool_new) {
|
2010-11-19 18:10:13 +05:30
|
|
|
rv = (*fn)(rpool->rpi, arg, &done);
|
|
|
|
if (rv != 0 || done)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|