2009-11-26 07:52:50 +05:30
|
|
|
/*-
|
2013-03-05 08:38:42 +05:30
|
|
|
* Copyright (c) 2009-2013 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
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
struct rpool {
|
|
|
|
SIMPLEQ_ENTRY(rpool) entries;
|
|
|
|
struct xbps_repo *repo;
|
|
|
|
};
|
|
|
|
|
|
|
|
static SIMPLEQ_HEAD(rpool_head, rpool) rpool_queue =
|
|
|
|
SIMPLEQ_HEAD_INITIALIZER(rpool_queue);
|
|
|
|
|
2010-01-21 07:40:19 +05:30
|
|
|
/**
|
2012-12-19 05:31:27 +05:30
|
|
|
* @file lib/rpool.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-06-04 17:07:53 +05:30
|
|
|
int HIDDEN
|
2012-05-30 14:26:32 +05:30
|
|
|
xbps_rpool_init(struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2013-06-10 13:58:39 +05:30
|
|
|
struct rpool *rp;
|
2011-06-04 17:07:53 +05:30
|
|
|
const char *repouri;
|
2013-06-10 13:58:39 +05:30
|
|
|
bool foundrepo = false;
|
2009-11-26 07:52:50 +05:30
|
|
|
int rv = 0;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
assert(xhp);
|
|
|
|
|
|
|
|
if (xhp->rpool_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
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
|
2013-06-10 13:58:39 +05:30
|
|
|
rp = malloc(sizeof(struct rpool));
|
|
|
|
assert(rp);
|
2011-12-15 15:49:20 +05:30
|
|
|
repouri = cfg_getnstr(xhp->cfg, "repositories", i);
|
2013-06-10 13:58:39 +05:30
|
|
|
if ((rp->repo = xbps_repo_open(xhp, repouri)) == NULL) {
|
2013-07-26 15:12:52 +05:30
|
|
|
rp->repo = calloc(1, sizeof(struct xbps_repo));
|
|
|
|
assert(rp->repo);
|
2011-11-11 04:37:26 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
rp->repo->idx = xbps_repo_get_plist(rp->repo, XBPS_PKGINDEX);
|
|
|
|
rp->repo->uri = repouri;
|
|
|
|
rp->repo->xhp = xhp;
|
|
|
|
SIMPLEQ_INSERT_TAIL(&rpool_queue, rp, entries);
|
|
|
|
foundrepo = true;
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "[rpool] `%s' registered.\n", repouri);
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
if (!foundrepo) {
|
2011-07-29 14:47:34 +05:30
|
|
|
/* 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
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
xhp->rpool_initialized = true;
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "[rpool] initialized ok.\n");
|
2009-11-26 07:52:50 +05:30
|
|
|
out:
|
2012-11-30 11:41:51 +05:30
|
|
|
if (rv != 0)
|
2012-05-30 14:26:32 +05:30
|
|
|
xbps_rpool_release(xhp);
|
2009-11-26 07:52:50 +05:30
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-06-04 17:07:53 +05:30
|
|
|
void HIDDEN
|
2012-05-30 14:26:32 +05:30
|
|
|
xbps_rpool_release(struct xbps_handle *xhp)
|
2009-11-26 07:52:50 +05:30
|
|
|
{
|
2013-06-10 13:58:39 +05:30
|
|
|
struct rpool *rp;
|
2009-11-26 07:52:50 +05:30
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
if (!xhp->rpool_initialized)
|
2009-11-26 07:52:50 +05:30
|
|
|
return;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
while ((rp = SIMPLEQ_FIRST(&rpool_queue))) {
|
|
|
|
SIMPLEQ_REMOVE(&rpool_queue, rp, rpool, entries);
|
|
|
|
xbps_repo_close(rp->repo);
|
|
|
|
free(rp);
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|
2013-06-10 13:58:39 +05:30
|
|
|
xhp->rpool_initialized = false;
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp, "[rpool] released ok.\n");
|
2010-11-19 18:10:13 +05:30
|
|
|
}
|
|
|
|
|
2011-11-11 03:44:50 +05:30
|
|
|
int
|
2013-06-10 13:58:39 +05:30
|
|
|
xbps_rpool_sync(struct xbps_handle *xhp, const char *uri)
|
2011-11-11 03:44:50 +05:30
|
|
|
{
|
|
|
|
const char *repouri;
|
|
|
|
|
2011-12-15 15:49:20 +05:30
|
|
|
if (xhp->cfg == NULL)
|
2011-11-11 03:44:50 +05:30
|
|
|
return ENOTSUP;
|
|
|
|
|
2013-09-15 13:36:49 +05:30
|
|
|
for (unsigned int i = 0; i < cfg_size(xhp->cfg, "repositories"); i++) {
|
2011-12-15 15:49:20 +05:30
|
|
|
repouri = cfg_getnstr(xhp->cfg, "repositories", i);
|
2012-06-01 18:32:06 +05:30
|
|
|
/* If argument was set just process that repository */
|
|
|
|
if (uri && strcmp(repouri, uri))
|
2012-01-15 18:54:44 +05:30
|
|
|
continue;
|
2012-11-07 15:07:58 +05:30
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
if (xbps_repo_sync(xhp, repouri) == -1) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
2013-06-10 13:58:39 +05:30
|
|
|
"[rpool] `%s' failed to fetch repository data: %s\n",
|
|
|
|
repouri, fetchLastErrCode == 0 ? strerror(errno) :
|
2012-06-01 18:32:06 +05:30
|
|
|
xbps_fetch_error_string());
|
2011-11-11 03:44:50 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2012-06-01 19:34:47 +05:30
|
|
|
return 0;
|
2011-11-11 03:44:50 +05:30
|
|
|
}
|
|
|
|
|
2010-11-19 18:10:13 +05:30
|
|
|
int
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_rpool_foreach(struct xbps_handle *xhp,
|
2013-06-10 13:58:39 +05:30
|
|
|
int (*fn)(struct xbps_repo *, void *, bool *),
|
2012-06-14 11:52:11 +05:30
|
|
|
void *arg)
|
2010-11-19 18:10:13 +05:30
|
|
|
{
|
2013-06-10 13:58:39 +05:30
|
|
|
struct rpool *rp;
|
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 */
|
2012-05-30 14:26:32 +05:30
|
|
|
if ((rv = xbps_rpool_init(xhp)) != 0) {
|
2011-06-22 15:25:02 +05:30
|
|
|
if (rv == ENOTSUP) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"[rpool] empty repository list.\n");
|
2011-06-22 15:25:02 +05:30
|
|
|
} else if (rv != ENOENT && rv != ENOTSUP) {
|
2012-06-14 11:52:11 +05:30
|
|
|
xbps_dbg_printf(xhp,
|
|
|
|
"[rpool] couldn't initialize: %s\n",
|
2011-07-29 14:47:34 +05:30
|
|
|
strerror(rv));
|
2011-06-22 15:25:02 +05:30
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2011-12-22 21:08:05 +05:30
|
|
|
/* Iterate over repository pool */
|
2013-06-10 13:58:39 +05:30
|
|
|
SIMPLEQ_FOREACH(rp, &rpool_queue, entries) {
|
2013-09-17 19:54:19 +05:30
|
|
|
/* ignore invalid repos */
|
|
|
|
if (rp->repo->idx == NULL)
|
|
|
|
continue;
|
|
|
|
|
2013-06-10 13:58:39 +05:30
|
|
|
rv = (*fn)(rp->repo, arg, &done);
|
2012-02-01 07:00:23 +05:30
|
|
|
if (rv != 0 || done)
|
2010-11-19 18:10:13 +05:30
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2009-11-26 07:52:50 +05:30
|
|
|
}
|