-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathrpool.c
347 lines (305 loc) · 8.18 KB
/
rpool.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*-
* Copyright (c) 2009-2015 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 <sys/utsname.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <libgen.h>
#include <string.h>
#include <errno.h>
#include "xbps_api_impl.h"
#include "fetch.h"
struct rpool_fpkg {
xbps_array_t revdeps;
xbps_dictionary_t pkgd;
const char *pattern;
const char *bestpkgver;
bool best;
};
typedef enum {
BEST_PKG = 1,
VIRTUAL_PKG,
REAL_PKG,
REVDEPS_PKG
} pkg_repo_type_t;
static SIMPLEQ_HEAD(rpool_head, xbps_repo) rpool_queue =
SIMPLEQ_HEAD_INITIALIZER(rpool_queue);
/**
* @file lib/rpool.c
* @brief Repository pool routines
* @defgroup repopool Repository pool functions
*/
int
xbps_rpool_sync(struct xbps_handle *xhp, const char *uri)
{
const char *repouri = NULL;
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
/* If argument was set just process that repository */
if (uri && strcmp(repouri, uri))
continue;
if (xbps_repo_sync(xhp, repouri) == -1) {
xbps_dbg_printf(
"[rpool] `%s' failed to fetch repository data: %s\n",
repouri, fetchLastErrCode == 0 ? strerror(errno) :
xbps_fetch_error_string());
continue;
}
}
return 0;
}
struct xbps_repo HIDDEN *
xbps_regget_repo(struct xbps_handle *xhp, const char *url)
{
struct xbps_repo *repo;
const char *repouri = NULL;
if (SIMPLEQ_EMPTY(&rpool_queue)) {
/* iterate until we have a match */
for (unsigned int i = 0; i < xbps_array_count(xhp->repositories); i++) {
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
if (strcmp(repouri, url))
continue;
repo = xbps_repo_open(xhp, repouri);
if (!repo)
return NULL;
SIMPLEQ_INSERT_TAIL(&rpool_queue, repo, entries);
xbps_dbg_printf("[rpool] `%s' registered.\n", repouri);
}
}
SIMPLEQ_FOREACH(repo, &rpool_queue, entries)
if (strcmp(url, repo->uri) == 0)
return repo;
return NULL;
}
struct xbps_repo *
xbps_rpool_get_repo(const char *url)
{
struct xbps_repo *repo;
SIMPLEQ_FOREACH(repo, &rpool_queue, entries)
if (strcmp(url, repo->uri) == 0)
return repo;
return NULL;
}
void
xbps_rpool_release(struct xbps_handle *xhp)
{
struct xbps_repo *repo;
while ((repo = SIMPLEQ_FIRST(&rpool_queue))) {
SIMPLEQ_REMOVE(&rpool_queue, repo, xbps_repo, entries);
xbps_repo_release(repo);
}
if (xhp && xhp->repositories) {
xbps_object_release(xhp->repositories);
xhp->repositories = NULL;
}
}
int
xbps_rpool_foreach(struct xbps_handle *xhp,
int (*fn)(struct xbps_repo *, void *, bool *),
void *arg)
{
struct xbps_repo *repo = NULL;
const char *repouri = NULL;
int rv = 0;
bool foundrepo = false, done = false;
unsigned int n = 0;
assert(fn != NULL);
again:
for (unsigned int i = n; i < xbps_array_count(xhp->repositories); i++, n++) {
xbps_array_get_cstring_nocopy(xhp->repositories, i, &repouri);
xbps_dbg_printf("[rpool] checking `%s' at index %u\n", repouri, n);
if ((repo = xbps_rpool_get_repo(repouri)) == NULL) {
repo = xbps_repo_open(xhp, repouri);
if (!repo) {
xbps_repo_remove(xhp, repouri);
goto again;
}
SIMPLEQ_INSERT_TAIL(&rpool_queue, repo, entries);
xbps_dbg_printf("[rpool] `%s' registered.\n", repouri);
}
foundrepo = true;
rv = (*fn)(repo, arg, &done);
if (rv != 0 || done)
break;
}
if (!foundrepo)
rv = ENOTSUP;
return rv;
}
static int
find_virtualpkg_cb(struct xbps_repo *repo, void *arg, bool *done)
{
struct rpool_fpkg *rpf = arg;
rpf->pkgd = xbps_repo_get_virtualpkg(repo, rpf->pattern);
if (rpf->pkgd) {
/* found */
*done = true;
return 0;
}
/* not found */
return 0;
}
static int
find_pkg_cb(struct xbps_repo *repo, void *arg, bool *done)
{
struct rpool_fpkg *rpf = arg;
rpf->pkgd = xbps_repo_get_pkg(repo, rpf->pattern);
if (rpf->pkgd) {
/* found */
*done = true;
return 0;
}
/* Not found */
return 0;
}
static int
find_pkg_revdeps_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
{
struct rpool_fpkg *rpf = arg;
xbps_array_t revdeps = NULL;
const char *pkgver = NULL;
revdeps = xbps_repo_get_pkg_revdeps(repo, rpf->pattern);
if (xbps_array_count(revdeps)) {
/* found */
if (rpf->revdeps == NULL)
rpf->revdeps = xbps_array_create();
for (unsigned int i = 0; i < xbps_array_count(revdeps); i++) {
xbps_array_get_cstring_nocopy(revdeps, i, &pkgver);
xbps_array_add_cstring_nocopy(rpf->revdeps, pkgver);
}
xbps_object_release(revdeps);
}
return 0;
}
static int
find_best_pkg_cb(struct xbps_repo *repo, void *arg, bool *done UNUSED)
{
struct rpool_fpkg *rpf = arg;
xbps_dictionary_t pkgd;
const char *repopkgver = NULL;
pkgd = xbps_repo_get_pkg(repo, rpf->pattern);
if (pkgd == NULL) {
if (errno && errno != ENOENT)
return errno;
xbps_dbg_printf("[rpool] Package '%s' not found in repository"
" '%s'.\n", rpf->pattern, repo->uri);
return 0;
}
xbps_dictionary_get_cstring_nocopy(pkgd,
"pkgver", &repopkgver);
if (rpf->bestpkgver == NULL) {
xbps_dbg_printf("[rpool] Found match '%s' (%s).\n",
repopkgver, repo->uri);
rpf->pkgd = pkgd;
rpf->bestpkgver = repopkgver;
return 0;
}
/*
* Compare current stored version against new
* version from current package in repository.
*/
if (xbps_cmpver(repopkgver, rpf->bestpkgver) == 1) {
xbps_dbg_printf("[rpool] Found best match '%s' (%s).\n",
repopkgver, repo->uri);
rpf->pkgd = pkgd;
rpf->bestpkgver = repopkgver;
}
return 0;
}
static xbps_object_t
repo_find_pkg(struct xbps_handle *xhp,
const char *pkg,
pkg_repo_type_t type)
{
struct rpool_fpkg rpf;
int rv = 0;
assert(xhp);
assert(pkg);
rpf.pattern = pkg;
rpf.pkgd = NULL;
rpf.revdeps = NULL;
rpf.bestpkgver = NULL;
switch (type) {
case BEST_PKG:
/*
* Find best pkg version.
*/
rv = xbps_rpool_foreach(xhp, find_best_pkg_cb, &rpf);
break;
case VIRTUAL_PKG:
/*
* Find virtual pkg.
*/
rv = xbps_rpool_foreach(xhp, find_virtualpkg_cb, &rpf);
break;
case REAL_PKG:
/*
* Find real pkg.
*/
rv = xbps_rpool_foreach(xhp, find_pkg_cb, &rpf);
break;
case REVDEPS_PKG:
/*
* Find revdeps for pkg.
*/
rv = xbps_rpool_foreach(xhp, find_pkg_revdeps_cb, &rpf);
break;
}
if (rv != 0) {
errno = rv;
return NULL;
}
if (type == REVDEPS_PKG) {
if (rpf.revdeps == NULL)
errno = ENOENT;
return rpf.revdeps;
} else {
if (rpf.pkgd == NULL)
errno = ENOENT;
}
return rpf.pkgd;
}
xbps_dictionary_t
xbps_rpool_get_virtualpkg(struct xbps_handle *xhp, const char *pkg)
{
return repo_find_pkg(xhp, pkg, VIRTUAL_PKG);
}
xbps_dictionary_t
xbps_rpool_get_pkg(struct xbps_handle *xhp, const char *pkg)
{
if (xhp->flags & XBPS_FLAG_BESTMATCH)
return repo_find_pkg(xhp, pkg, BEST_PKG);
return repo_find_pkg(xhp, pkg, REAL_PKG);
}
xbps_array_t
xbps_rpool_get_pkg_revdeps(struct xbps_handle *xhp, const char *pkg)
{
return repo_find_pkg(xhp, pkg, REVDEPS_PKG);
}
xbps_array_t
xbps_rpool_get_pkg_fulldeptree(struct xbps_handle *xhp, const char *pkg)
{
return xbps_get_pkg_fulldeptree(xhp, pkg, true);
}