XBPS Library API 20260225
The X Binary Package System
transaction_pkg_deps.c
1/*-
2 * Copyright (c) 2008-2020 Juan Romero Pardines.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <errno.h>
30
31#include "xbps_api_impl.h"
32
33static int
34add_missing_reqdep(struct xbps_handle *xhp, const char *reqpkg)
35{
36 xbps_array_t mdeps;
37 xbps_object_iterator_t iter = NULL;
38 xbps_object_t obj;
39 unsigned int idx = 0;
40 bool add_pkgdep, pkgfound, update_pkgdep;
41 int rv = 0;
42
43 assert(reqpkg != NULL);
44
45 add_pkgdep = update_pkgdep = pkgfound = false;
46 mdeps = xbps_dictionary_get(xhp->transd, "missing_deps");
47
48 iter = xbps_array_iterator(mdeps);
49 if (iter == NULL)
50 goto out;
51
52 while ((obj = xbps_object_iterator_next(iter)) != NULL) {
53 const char *curdep, *curver, *pkgver;
54 char curpkgnamedep[XBPS_NAME_SIZE];
55 char pkgnamedep[XBPS_NAME_SIZE];
56
57 assert(xbps_object_type(obj) == XBPS_TYPE_STRING);
58 curdep = xbps_string_cstring_nocopy(obj);
59 curver = xbps_pkgpattern_version(curdep);
60 pkgver = xbps_pkgpattern_version(reqpkg);
61 if (curver == NULL || pkgver == NULL)
62 goto out;
63 if (!xbps_pkgpattern_name(curpkgnamedep, XBPS_NAME_SIZE, curdep)) {
64 goto out;
65 }
66 if (!xbps_pkgpattern_name(pkgnamedep, XBPS_NAME_SIZE, reqpkg)) {
67 goto out;
68 }
69 if (strcmp(pkgnamedep, curpkgnamedep) == 0) {
70 pkgfound = true;
71 if (strcmp(curver, pkgver) == 0) {
72 rv = EEXIST;
73 goto out;
74 }
75 /*
76 * if new dependency version is greater than current
77 * one, store it.
78 */
79 xbps_dbg_printf("Missing pkgdep name matched, curver: %s newver: %s\n", curver, pkgver);
80 if (xbps_cmpver(curver, pkgver) <= 0) {
81 add_pkgdep = false;
82 rv = EEXIST;
83 goto out;
84 }
85 update_pkgdep = true;
86 }
87 if (pkgfound)
88 break;
89
90 idx++;
91 }
92 add_pkgdep = true;
93out:
94 if (iter)
95 xbps_object_iterator_release(iter);
96 if (update_pkgdep)
97 xbps_array_remove(mdeps, idx);
98 if (add_pkgdep) {
99 char *str;
100
101 str = xbps_xasprintf("MISSING: %s", reqpkg);
102 xbps_array_add_cstring(mdeps, str);
103 free(str);
104 }
105
106 return rv;
107}
108
109#define MAX_DEPTH 512
110
111static int
112repo_deps(struct xbps_handle *xhp,
113 xbps_array_t pkgs, /* array of pkgs */
114 xbps_array_t queued, /* queued packages */
115 xbps_dictionary_t pkg_repod, /* pkg repo dictionary */
116 unsigned short *depth) /* max recursion depth */
117{
118 xbps_array_t pkg_rdeps = NULL, pkg_provides = NULL;
119 xbps_dictionary_t curpkgd = NULL, repopkgd = NULL;
120 xbps_trans_type_t ttype;
121 pkg_state_t state;
122 xbps_object_t obj;
123 xbps_object_iterator_t iter;
124 const char *curpkg = NULL, *reqpkg = NULL, *pkgver_q = NULL;
125 char pkgname[XBPS_NAME_SIZE], reqpkgname[XBPS_NAME_SIZE];
126 int rv = 0;
127
128 assert(xhp);
129 assert(pkgs);
130 assert(pkg_repod);
131
132 if (*depth >= MAX_DEPTH)
133 return ELOOP;
134
135 xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &curpkg);
136 pkg_provides = xbps_dictionary_get(pkg_repod, "provides");
137 /*
138 * Iterate over the list of required run dependencies for
139 * current package.
140 */
141 pkg_rdeps = xbps_dictionary_get(pkg_repod, "run_depends");
142 if (xbps_array_count(pkg_rdeps) == 0)
143 goto out;
144
145 iter = xbps_array_iterator(pkg_rdeps);
146 assert(iter);
147
148 while ((obj = xbps_object_iterator_next(iter))) {
149 bool error = false, foundvpkg = false;
150 bool autoinst = true;
151
152 ttype = XBPS_TRANS_UNKNOWN;
153 reqpkg = xbps_string_cstring_nocopy(obj);
154
155 if (xhp->flags & XBPS_FLAG_DEBUG) {
156 xbps_dbg_printf("%s", "");
157 for (unsigned short x = 0; x < *depth; x++) {
159 }
160 xbps_dbg_printf_append("%s: requires dependency '%s': ", curpkg ? curpkg : " ", reqpkg);
161 }
162 if ((!xbps_pkgpattern_name(pkgname, sizeof(pkgname), reqpkg)) &&
163 (!xbps_pkg_name(pkgname, sizeof(pkgname), reqpkg))) {
164 xbps_dbg_printf("%s: can't guess pkgname for dependency: %s\n", curpkg, reqpkg);
165 xbps_set_cb_state(xhp, XBPS_STATE_INVALID_DEP, ENXIO, NULL,
166 "%s: can't guess pkgname for dependency '%s'", curpkg, reqpkg);
167 rv = ENXIO;
168 break;
169 }
170 /*
171 * Pass 0: check if required dependency is ignored.
172 */
173 if (xbps_pkg_is_ignored(xhp, pkgname)) {
174 xbps_dbg_printf_append("%s ignored.\n", pkgname);
175 continue;
176 }
177 /*
178 * Pass 1: check if required dependency is provided as virtual
179 * package via "provides", if true ignore dependency.
180 */
181 if (pkg_provides && xbps_match_virtual_pkg_in_array(pkg_provides, reqpkg)) {
182 xbps_dbg_printf_append("%s is a vpkg provided by %s, ignored.\n", pkgname, curpkg);
183 continue;
184 }
185 /*
186 * Pass 2: check if required dependency is currently queued or
187 * has been already added in the transaction dictionary.
188 */
189 if ((curpkgd = xbps_find_pkg_in_array(queued, reqpkg, 0)) ||
190 (curpkgd = xbps_find_virtualpkg_in_array(xhp, queued, reqpkg, 0))) {
192 xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q);
193 xbps_dbg_printf_append(" (%s queued %d)\n", pkgver_q, ttype_q);
194 continue;
195 }
196 /*
197 * Pass 3: check if required dependency has been already added
198 * in the transaction dictionary.
199 */
200 if ((curpkgd = xbps_find_pkg_in_array(pkgs, reqpkg, 0)) ||
201 (curpkgd = xbps_find_virtualpkg_in_array(xhp, pkgs, reqpkg, 0))) {
203 xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q);
204 if (ttype_q != XBPS_TRANS_REMOVE && ttype_q != XBPS_TRANS_HOLD) {
205 xbps_dbg_printf_append(" (%s queued %d)\n", pkgver_q, ttype_q);
206 continue;
207 }
208 }
209 /*
210 * Pass 4: check if required dependency is already installed
211 * and its version is fully matched.
212 */
213 if ((curpkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) {
214 if ((curpkgd = xbps_pkgdb_get_virtualpkg(xhp, pkgname))) {
215 foundvpkg = true;
216 }
217 }
218 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
219 /*
220 * if XBPS_FLAG_DOWNLOAD_ONLY always assume
221 * all deps are not installed. This way one can download
222 * the whole set of binary packages to perform an
223 * off-line installation later on.
224 */
225 curpkgd = NULL;
226 }
227
228 if (curpkgd == NULL) {
229 if (errno && errno != ENOENT) {
230 /* error */
231 rv = errno;
232 xbps_dbg_printf("failed to find installed pkg for `%s': %s\n", reqpkg, strerror(rv));
233 break;
234 }
235 /* Required dependency not installed */
236 xbps_dbg_printf_append("not installed.\n");
237 ttype = XBPS_TRANS_INSTALL;
238 state = XBPS_PKG_STATE_NOT_INSTALLED;
239 } else {
240 /*
241 * Required dependency is installed, check if its version can
242 * satisfy the requirements.
243 */
244 xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q);
245
246 /* Check its state */
247 if ((rv = xbps_pkg_state_dictionary(curpkgd, &state)) != 0) {
248 break;
249 }
250
251 if (foundvpkg && xbps_match_virtual_pkg_in_dict(curpkgd, reqpkg)) {
252 /*
253 * Check if required dependency is a virtual package and is satisfied
254 * by an installed package.
255 */
256 xbps_dbg_printf_append("[virtual] satisfied by `%s'.\n", pkgver_q);
257 continue;
258 }
259 rv = xbps_pkgpattern_match(pkgver_q, reqpkg);
260 if (rv == 0) {
261 char curpkgname[XBPS_NAME_SIZE];
262 /*
263 * The version requirement is not satisfied.
264 */
265 if (!xbps_pkg_name(curpkgname, sizeof(curpkgname), pkgver_q)) {
266 abort();
267 }
268
269 if (strcmp(pkgname, curpkgname)) {
270 xbps_dbg_printf_append("not installed `%s (vpkg)'", pkgver_q);
271 if (xbps_dictionary_get(curpkgd, "hold")) {
272 ttype = XBPS_TRANS_HOLD;
273 xbps_dbg_printf_append(" on hold state! ignoring package.\n");
274 rv = ENODEV;
275 } else {
277 ttype = XBPS_TRANS_INSTALL;
278 }
279 } else {
280 xbps_dbg_printf_append("installed `%s', must be updated", pkgver_q);
281 if (xbps_dictionary_get(curpkgd, "hold")) {
282 xbps_dbg_printf_append(" on hold state! ignoring package.\n");
283 ttype = XBPS_TRANS_HOLD;
284 rv = ENODEV;
285 } else {
287 ttype = XBPS_TRANS_UPDATE;
288 }
289 }
290 /*
291 * Not satisfied and package on hold.
292 */
293 if (rv == ENODEV) {
294 rv = add_missing_reqdep(xhp, reqpkg);
295 if (rv != 0 && rv != EEXIST) {
296 xbps_dbg_printf("`%s': add_missing_reqdep failed\n", reqpkg);
297 break;
298 } else if (rv == EEXIST) {
299 xbps_dbg_printf("`%s' missing dep already added.\n", reqpkg);
300 rv = 0;
301 continue;
302 } else {
303 xbps_dbg_printf("`%s' added into the missing deps array.\n", reqpkg);
304 continue;
305 }
306 }
307 } else if (rv == 1) {
308 /*
309 * The version requirement is satisfied.
310 */
311 rv = 0;
312 if (state == XBPS_PKG_STATE_UNPACKED) {
313 /*
314 * Package matches the dependency pattern but was only unpacked,
315 * configure pkg.
316 */
317 xbps_dbg_printf_append("installed `%s', must be configured.\n", pkgver_q);
318 ttype = XBPS_TRANS_CONFIGURE;
319 } else if (state == XBPS_PKG_STATE_INSTALLED) {
320 /*
321 * Package matches the dependency pattern and is fully installed,
322 * skip to next one.
323 */
324 xbps_dbg_printf_append("installed `%s'.\n", pkgver_q);
325 continue;
326 }
327 } else {
328 /* error matching pkgpattern */
329 xbps_dbg_printf("failed to match pattern %s with %s\n", reqpkg, pkgver_q);
330 break;
331 }
332 }
333 if (ttype == XBPS_TRANS_UPDATE || ttype == XBPS_TRANS_CONFIGURE) {
334 /*
335 * If the package is already installed preserve the installation mode,
336 * which is not automatic if automatic-install is not set.
337 */
338 bool pkgd_auto = false;
339 xbps_dictionary_get_bool(curpkgd, "automatic-install", &pkgd_auto);
340 autoinst = pkgd_auto;
341 }
342 if (ttype == XBPS_TRANS_CONFIGURE) {
343 if (!xbps_transaction_pkg_type_set(curpkgd, ttype)) {
344 rv = EINVAL;
345 xbps_dbg_printf("xbps_transaction_pkg_type_set failed for `%s': %s\n", reqpkg, strerror(rv));
346 break;
347 }
348 if (!xbps_transaction_store(xhp, pkgs, curpkgd, autoinst)) {
349 rv = EINVAL;
350 xbps_dbg_printf("xbps_transaction_store failed for `%s': %s\n", reqpkg, strerror(rv));
351 break;
352 }
353 continue;
354 }
355 /*
356 * Pass 5: find required dependency in repository pool.
357 * If dependency does not match add pkg into the missing
358 * deps array and pass to next one.
359 */
360 if (xbps_dictionary_get(curpkgd, "repolock")) {
361 const char *repourl = NULL;
362 struct xbps_repo *repo = NULL;
363 xbps_dbg_printf("`%s' is repolocked, looking at single repository.\n", reqpkg);
364 xbps_dictionary_get_cstring_nocopy(curpkgd, "repository", &repourl);
365 if (repourl && (repo = xbps_regget_repo(xhp, repourl))) {
366 repopkgd = xbps_repo_get_pkg(repo, reqpkg);
367 } else {
368 repopkgd = NULL;
369 }
370 } else {
371 repopkgd = xbps_rpool_get_pkg(xhp, reqpkg);
372 if (!repopkgd) {
373 repopkgd = xbps_rpool_get_virtualpkg(xhp, reqpkg);
374 }
375 }
376 if (repopkgd == NULL) {
377 /* pkg not found, there was some error */
378 if (errno && errno != ENOENT) {
379 xbps_dbg_printf("failed to find pkg for `%s' in rpool: %s\n", reqpkg, strerror(errno));
380 rv = errno;
381 break;
382 }
383 rv = add_missing_reqdep(xhp, reqpkg);
384 if (rv != 0 && rv != EEXIST) {
385 xbps_dbg_printf("`%s': add_missing_reqdep failed\n", reqpkg);
386 break;
387 } else if (rv == EEXIST) {
388 xbps_dbg_printf("`%s' missing dep already added.\n", reqpkg);
389 rv = 0;
390 continue;
391 } else {
392 xbps_dbg_printf("`%s' added into the missing deps array.\n", reqpkg);
393 continue;
394 }
395 }
396
397
398 xbps_dictionary_get_cstring_nocopy(repopkgd, "pkgver", &pkgver_q);
399 if (!xbps_pkg_name(reqpkgname, sizeof(reqpkgname), pkgver_q)) {
400 rv = EINVAL;
401 break;
402 }
403 /*
404 * Check dependency validity.
405 */
406 if (!xbps_pkg_name(pkgname, sizeof(pkgname), curpkg)) {
407 rv = EINVAL;
408 break;
409 }
410 if (strcmp(pkgname, reqpkgname) == 0) {
411 xbps_dbg_printf_append("[ignoring wrong dependency %s (depends on itself)]\n", reqpkg);
412 xbps_remove_string_from_array(pkg_rdeps, reqpkg);
413 continue;
414 }
415 /*
416 * Installed package must be updated, check if dependency is
417 * satisfied.
418 */
419 if (ttype == XBPS_TRANS_UPDATE) {
420 switch (xbps_pkgpattern_match(pkgver_q, reqpkg)) {
421 case 0: /* nomatch */
422 break;
423 case 1: /* match */
424 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver_q)) {
425 abort();
426 }
427 /*
428 * If there's an update in transaction,
429 * it's assumed version is greater.
430 * So dependency pattern matching didn't
431 * succeed... return ENODEV.
432 */
433 if (xbps_find_pkg_in_array(pkgs, pkgname, XBPS_TRANS_UPDATE)) {
434 error = true;
435 rv = ENODEV;
436 }
437 break;
438 default:
439 error = true;
440 rv = EINVAL;
441 break;
442 }
443 if (error)
444 break;
445 }
446
447 if (!xbps_array_add(queued, repopkgd))
448 return -xbps_error_oom();
449
450 pkg_rdeps = xbps_dictionary_get(repopkgd, "run_depends");
451 if (xbps_array_count(pkg_rdeps)) {
452 /*
453 * Process rundeps for current pkg found in rpool.
454 */
455 if (xhp->flags & XBPS_FLAG_DEBUG) {
456 xbps_dbg_printf("%s", "");
457 for (unsigned short x = 0; x < *depth; x++) {
459 }
460 xbps_dbg_printf_append("%s: finding dependencies:\n", pkgver_q);
461 }
462 (*depth)++;
463 rv = repo_deps(xhp, pkgs, queued, repopkgd, depth);
464 if (rv != 0) {
465 xbps_dbg_printf("Error checking %s for rundeps: %s\n", reqpkg, strerror(rv));
466 break;
467 }
468 }
469 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
470 ttype = XBPS_TRANS_DOWNLOAD;
471 } else if (xbps_dictionary_get(curpkgd, "hold")) {
472 ttype = XBPS_TRANS_HOLD;
473 }
474
475 xbps_array_remove(queued, xbps_array_count(queued) - 1);
476
477 /*
478 * All deps were processed, store pkg in transaction.
479 */
480 if (!xbps_transaction_pkg_type_set(repopkgd, ttype)) {
481 rv = EINVAL;
482 xbps_dbg_printf("xbps_transaction_pkg_type_set failed for `%s': %s\n", reqpkg, strerror(rv));
483 break;
484 }
485 if (!xbps_transaction_store(xhp, pkgs, repopkgd, autoinst)) {
486 rv = EINVAL;
487 xbps_dbg_printf("xbps_transaction_store failed for `%s': %s\n", reqpkg, strerror(rv));
488 break;
489 }
490 }
491 xbps_object_iterator_release(iter);
492out:
493 (*depth)--;
494
495 return rv;
496}
497
498int HIDDEN
499xbps_transaction_pkg_deps(struct xbps_handle *xhp,
500 xbps_array_t pkgs,
501 xbps_dictionary_t pkg_repod)
502{
503 const char *pkgver;
504 unsigned short depth = 0;
505 xbps_array_t queued;
506 int rv;
507
508 assert(xhp);
509 assert(pkgs);
510 assert(pkg_repod);
511
512 queued = xbps_array_create();
513 if (!queued)
514 return -xbps_error_oom();
515
516 if (!xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver))
517 return EINVAL;
518
519 xbps_dbg_printf("Finding required dependencies for '%s':\n", pkgver);
520
521 /*
522 * This will find direct and indirect deps, if any of them is not
523 * there it will be added into the missing_deps array.
524 */
525 rv = repo_deps(xhp, pkgs, queued, pkg_repod, &depth);
526 xbps_object_release(queued);
527 return rv;
528}
xbps_dictionary_t transd
Definition xbps.h:597
int flags
Definition xbps.h:693
Generic XBPS structure handler for initialization.
Definition xbps.h:560
#define xbps_error_oom()
Log out of memory condition.
Definition xbps.h:776
void xbps_dbg_printf(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:67
void xbps_dbg_printf_append(const char *fmt,...)
Prints debug messages to stderr.
Definition log.c:54
xbps_dictionary_t xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:403
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:389
pkg_state_t
Definition xbps.h:1831
int xbps_pkg_state_dictionary(xbps_dictionary_t dict, pkg_state_t *state)
bool xbps_match_virtual_pkg_in_dict(xbps_dictionary_t pkgd, const char *str)
Definition plist_match.c:58
bool xbps_match_virtual_pkg_in_array(xbps_array_t array, const char *str)
Definition plist_match.c:43
xbps_dictionary_t xbps_repo_get_pkg(struct xbps_repo *repo, const char *pkg)
Definition repo.c:598
struct xbps_handle * xhp
Definition xbps.h:1501
Repository structure.
Definition xbps.h:1489
xbps_dictionary_t xbps_rpool_get_virtualpkg(struct xbps_handle *xhp, const char *pkg)
Definition rpool.c:323
xbps_dictionary_t xbps_rpool_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition rpool.c:329
xbps_trans_type_t xbps_transaction_pkg_type(xbps_dictionary_t pkg_repod)
xbps_trans_type_t
Definition xbps.h:1400
bool xbps_transaction_pkg_type_set(xbps_dictionary_t pkg_repod, xbps_trans_type_t type)
char * xbps_xasprintf(const char *fmt,...) __attribute__((format(printf
const char * xbps_pkgpattern_version(const char *pattern)
Definition util.c:313
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:249
bool xbps_pkg_is_ignored(struct xbps_handle *xhp, const char *pkg)
Definition util.c:99
bool xbps_remove_string_from_array(xbps_array_t array, const char *str)
int xbps_cmpver(const char *pkg1, const char *pkg2)
Definition dewey.c:273
int xbps_pkgpattern_match(const char *pkgver, const char *pattern)
Definition util.c:526
bool xbps_pkgpattern_name(char *dst, size_t len, const char *pattern)
Definition util.c:285