XBPS Library API 20240111
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_dictionary_t pkg_repod, /* pkg repo dictionary */
115 unsigned short *depth) /* max recursion depth */
116{
117 xbps_array_t pkg_rdeps = NULL, pkg_provides = NULL;
118 xbps_dictionary_t curpkgd = NULL, repopkgd = NULL;
119 xbps_trans_type_t ttype;
120 pkg_state_t state;
121 xbps_object_t obj;
122 xbps_object_iterator_t iter;
123 const char *curpkg = NULL, *reqpkg = NULL, *pkgver_q = NULL;
124 char pkgname[XBPS_NAME_SIZE], reqpkgname[XBPS_NAME_SIZE];
125 int rv = 0;
126
127 assert(xhp);
128 assert(pkgs);
129 assert(pkg_repod);
130
131 if (*depth >= MAX_DEPTH)
132 return ELOOP;
133
134 xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &curpkg);
135 pkg_provides = xbps_dictionary_get(pkg_repod, "provides");
136 /*
137 * Iterate over the list of required run dependencies for
138 * current package.
139 */
140 pkg_rdeps = xbps_dictionary_get(pkg_repod, "run_depends");
141 if (xbps_array_count(pkg_rdeps) == 0)
142 goto out;
143
144 iter = xbps_array_iterator(pkg_rdeps);
145 assert(iter);
146
147 while ((obj = xbps_object_iterator_next(iter))) {
148 bool error = false, foundvpkg = false;
149 bool autoinst = true;
150
151 ttype = XBPS_TRANS_UNKNOWN;
152 reqpkg = xbps_string_cstring_nocopy(obj);
153
154 if (xhp->flags & XBPS_FLAG_DEBUG) {
155 xbps_dbg_printf("%s", "");
156 for (unsigned short x = 0; x < *depth; x++) {
157 xbps_dbg_printf_append(" ");
158 }
159 xbps_dbg_printf_append("%s: requires dependency '%s': ", curpkg ? curpkg : " ", reqpkg);
160 }
161 if ((!xbps_pkgpattern_name(pkgname, sizeof(pkgname), reqpkg)) &&
162 (!xbps_pkg_name(pkgname, sizeof(pkgname), reqpkg))) {
163 xbps_dbg_printf("%s: can't guess pkgname for dependency: %s\n", curpkg, reqpkg);
164 xbps_set_cb_state(xhp, XBPS_STATE_INVALID_DEP, ENXIO, NULL,
165 "%s: can't guess pkgname for dependency '%s'", curpkg, reqpkg);
166 rv = ENXIO;
167 break;
168 }
169 /*
170 * Pass 0: check if required dependency is ignored.
171 */
172 if (xbps_pkg_is_ignored(xhp, pkgname)) {
173 xbps_dbg_printf_append("%s ignored.\n", pkgname);
174 continue;
175 }
176 /*
177 * Pass 1: check if required dependency is provided as virtual
178 * package via "provides", if true ignore dependency.
179 */
180 if (pkg_provides && xbps_match_virtual_pkg_in_array(pkg_provides, reqpkg)) {
181 xbps_dbg_printf_append("%s is a vpkg provided by %s, ignored.\n", pkgname, curpkg);
182 continue;
183 }
184 /*
185 * Pass 2: check if required dependency has been already
186 * added in the transaction dictionary.
187 */
188 if ((curpkgd = xbps_find_pkg_in_array(pkgs, reqpkg, 0)) ||
189 (curpkgd = xbps_find_virtualpkg_in_array(xhp, pkgs, reqpkg, 0))) {
191 xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q);
192 if (ttype_q != XBPS_TRANS_REMOVE && ttype_q != XBPS_TRANS_HOLD) {
193 xbps_dbg_printf_append(" (%s queued %d)\n", pkgver_q, ttype_q);
194 continue;
195 }
196 }
197 /*
198 * Pass 3: check if required dependency is already installed
199 * and its version is fully matched.
200 */
201 if ((curpkgd = xbps_pkgdb_get_pkg(xhp, pkgname)) == NULL) {
202 if ((curpkgd = xbps_pkgdb_get_virtualpkg(xhp, pkgname))) {
203 foundvpkg = true;
204 }
205 }
206 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
207 /*
208 * if XBPS_FLAG_DOWNLOAD_ONLY always assume
209 * all deps are not installed. This way one can download
210 * the whole set of binary packages to perform an
211 * off-line installation later on.
212 */
213 curpkgd = NULL;
214 }
215
216 if (curpkgd == NULL) {
217 if (errno && errno != ENOENT) {
218 /* error */
219 rv = errno;
220 xbps_dbg_printf("failed to find installed pkg for `%s': %s\n", reqpkg, strerror(rv));
221 break;
222 }
223 /* Required dependency not installed */
224 xbps_dbg_printf_append("not installed.\n");
225 ttype = XBPS_TRANS_INSTALL;
226 state = XBPS_PKG_STATE_NOT_INSTALLED;
227 } else {
228 /*
229 * Required dependency is installed, check if its version can
230 * satisfy the requirements.
231 */
232 xbps_dictionary_get_cstring_nocopy(curpkgd, "pkgver", &pkgver_q);
233
234 /* Check its state */
235 if ((rv = xbps_pkg_state_dictionary(curpkgd, &state)) != 0) {
236 break;
237 }
238
239 if (foundvpkg && xbps_match_virtual_pkg_in_dict(curpkgd, reqpkg)) {
240 /*
241 * Check if required dependency is a virtual package and is satisfied
242 * by an installed package.
243 */
244 xbps_dbg_printf_append("[virtual] satisfied by `%s'.\n", pkgver_q);
245 continue;
246 }
247 rv = xbps_pkgpattern_match(pkgver_q, reqpkg);
248 if (rv == 0) {
249 char curpkgname[XBPS_NAME_SIZE];
250 /*
251 * The version requirement is not satisfied.
252 */
253 if (!xbps_pkg_name(curpkgname, sizeof(curpkgname), pkgver_q)) {
254 abort();
255 }
256
257 if (strcmp(pkgname, curpkgname)) {
258 xbps_dbg_printf_append("not installed `%s (vpkg)'", pkgver_q);
259 if (xbps_dictionary_get(curpkgd, "hold")) {
260 ttype = XBPS_TRANS_HOLD;
261 xbps_dbg_printf_append(" on hold state! ignoring package.\n");
262 rv = ENODEV;
263 } else {
264 xbps_dbg_printf_append("\n");
265 ttype = XBPS_TRANS_INSTALL;
266 }
267 } else {
268 xbps_dbg_printf_append("installed `%s', must be updated", pkgver_q);
269 if (xbps_dictionary_get(curpkgd, "hold")) {
270 xbps_dbg_printf_append(" on hold state! ignoring package.\n");
271 ttype = XBPS_TRANS_HOLD;
272 rv = ENODEV;
273 } else {
274 xbps_dbg_printf_append("\n");
275 ttype = XBPS_TRANS_UPDATE;
276 }
277 }
278 /*
279 * Not satisfied and package on hold.
280 */
281 if (rv == ENODEV) {
282 rv = add_missing_reqdep(xhp, reqpkg);
283 if (rv != 0 && rv != EEXIST) {
284 xbps_dbg_printf("`%s': add_missing_reqdep failed\n", reqpkg);
285 break;
286 } else if (rv == EEXIST) {
287 xbps_dbg_printf("`%s' missing dep already added.\n", reqpkg);
288 rv = 0;
289 continue;
290 } else {
291 xbps_dbg_printf("`%s' added into the missing deps array.\n", reqpkg);
292 continue;
293 }
294 }
295 } else if (rv == 1) {
296 /*
297 * The version requirement is satisfied.
298 */
299 rv = 0;
300 if (state == XBPS_PKG_STATE_UNPACKED) {
301 /*
302 * Package matches the dependency pattern but was only unpacked,
303 * configure pkg.
304 */
305 xbps_dbg_printf_append("installed `%s', must be configured.\n", pkgver_q);
306 ttype = XBPS_TRANS_CONFIGURE;
307 } else if (state == XBPS_PKG_STATE_INSTALLED) {
308 /*
309 * Package matches the dependency pattern and is fully installed,
310 * skip to next one.
311 */
312 xbps_dbg_printf_append("installed `%s'.\n", pkgver_q);
313 continue;
314 }
315 } else {
316 /* error matching pkgpattern */
317 xbps_dbg_printf("failed to match pattern %s with %s\n", reqpkg, pkgver_q);
318 break;
319 }
320 }
321 /*
322 * Pass 4: find required dependency in repository pool.
323 * If dependency does not match add pkg into the missing
324 * deps array and pass to next one.
325 */
326 if (xbps_dictionary_get(curpkgd, "repolock")) {
327 const char *repourl = NULL;
328 struct xbps_repo *repo = NULL;
329 xbps_dbg_printf("`%s' is repolocked, looking at single repository.\n", reqpkg);
330 xbps_dictionary_get_cstring_nocopy(curpkgd, "repository", &repourl);
331 if (repourl && (repo = xbps_regget_repo(xhp, repourl))) {
333 } else {
334 repopkgd = NULL;
335 }
336 } else {
338 if (!repopkgd) {
340 }
341 }
342 if (repopkgd == NULL) {
343 /* pkg not found, there was some error */
344 if (errno && errno != ENOENT) {
345 xbps_dbg_printf("failed to find pkg for `%s' in rpool: %s\n", reqpkg, strerror(errno));
346 rv = errno;
347 break;
348 }
349 rv = add_missing_reqdep(xhp, reqpkg);
350 if (rv != 0 && rv != EEXIST) {
351 xbps_dbg_printf("`%s': add_missing_reqdep failed\n", reqpkg);
352 break;
353 } else if (rv == EEXIST) {
354 xbps_dbg_printf("`%s' missing dep already added.\n", reqpkg);
355 rv = 0;
356 continue;
357 } else {
358 xbps_dbg_printf("`%s' added into the missing deps array.\n", reqpkg);
359 continue;
360 }
361 }
362
363
364 xbps_dictionary_get_cstring_nocopy(repopkgd, "pkgver", &pkgver_q);
366 rv = EINVAL;
367 break;
368 }
369 /*
370 * Check dependency validity.
371 */
372 if (!xbps_pkg_name(pkgname, sizeof(pkgname), curpkg)) {
373 rv = EINVAL;
374 break;
375 }
376 if (strcmp(pkgname, reqpkgname) == 0) {
377 xbps_dbg_printf_append("[ignoring wrong dependency %s (depends on itself)]\n", reqpkg);
379 continue;
380 }
381 /*
382 * Installed package must be updated, check if dependency is
383 * satisfied.
384 */
385 if (ttype == XBPS_TRANS_UPDATE) {
387 case 0: /* nomatch */
388 break;
389 case 1: /* match */
390 if (!xbps_pkg_name(pkgname, sizeof(pkgname), pkgver_q)) {
391 abort();
392 }
393 /*
394 * If there's an update in transaction,
395 * it's assumed version is greater.
396 * So dependency pattern matching didn't
397 * succeed... return ENODEV.
398 */
399 if (xbps_find_pkg_in_array(pkgs, pkgname, XBPS_TRANS_UPDATE)) {
400 error = true;
401 rv = ENODEV;
402 }
403 break;
404 default:
405 error = true;
406 rv = EINVAL;
407 break;
408 }
409 if (error)
410 break;
411 }
412 pkg_rdeps = xbps_dictionary_get(repopkgd, "run_depends");
413 if (xbps_array_count(pkg_rdeps)) {
414 /*
415 * Process rundeps for current pkg found in rpool.
416 */
417 if (xhp->flags & XBPS_FLAG_DEBUG) {
418 xbps_dbg_printf("%s", "");
419 for (unsigned short x = 0; x < *depth; x++) {
420 xbps_dbg_printf_append(" ");
421 }
422 xbps_dbg_printf_append("%s: finding dependencies:\n", pkgver_q);
423 }
424 (*depth)++;
425 rv = repo_deps(xhp, pkgs, repopkgd, depth);
426 if (rv != 0) {
427 xbps_dbg_printf("Error checking %s for rundeps: %s\n", reqpkg, strerror(rv));
428 break;
429 }
430 }
431 if (xhp->flags & XBPS_FLAG_DOWNLOAD_ONLY) {
432 ttype = XBPS_TRANS_DOWNLOAD;
433 } else if (xbps_dictionary_get(curpkgd, "hold")) {
434 ttype = XBPS_TRANS_HOLD;
435 }
436 if (ttype == XBPS_TRANS_UPDATE || ttype == XBPS_TRANS_CONFIGURE) {
437 /*
438 * If the package is already installed preserve the installation mode,
439 * which is not automatic if automatic-install is not set.
440 */
441 bool pkgd_auto = false;
442 xbps_dictionary_get_bool(curpkgd, "automatic-install", &pkgd_auto);
444 }
445 /*
446 * All deps were processed, store pkg in transaction.
447 */
449 rv = EINVAL;
450 xbps_dbg_printf("xbps_transaction_pkg_type_set failed for `%s': %s\n", reqpkg, strerror(rv));
451 break;
452 }
453 if (!xbps_transaction_store(xhp, pkgs, repopkgd, autoinst)) {
454 rv = EINVAL;
455 xbps_dbg_printf("xbps_transaction_store failed for `%s': %s\n", reqpkg, strerror(rv));
456 break;
457 }
458 }
459 xbps_object_iterator_release(iter);
460out:
461 (*depth)--;
462
463 return rv;
464}
465
466int HIDDEN
467xbps_transaction_pkg_deps(struct xbps_handle *xhp,
468 xbps_array_t pkgs,
469 xbps_dictionary_t pkg_repod)
470{
471 const char *pkgver;
472 unsigned short depth = 0;
473
474 assert(xhp);
475 assert(pkgs);
477
478 xbps_dictionary_get_cstring_nocopy(pkg_repod, "pkgver", &pkgver);
479 xbps_dbg_printf("Finding required dependencies for '%s':\n", pkgver);
480 /*
481 * This will find direct and indirect deps, if any of them is not
482 * there it will be added into the missing_deps array.
483 */
484 return repo_deps(xhp, pkgs, pkg_repod, &depth);
485}
int flags
Definition xbps.h:679
xbps_dictionary_t transd
Definition xbps.h:583
Generic XBPS structure handler for initialization.
Definition xbps.h:550
xbps_dictionary_t xbps_pkgdb_get_virtualpkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:388
xbps_dictionary_t xbps_pkgdb_get_pkg(struct xbps_handle *xhp, const char *pkg)
Definition pkgdb.c:379
pkg_state_t
Definition xbps.h:1768
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:441
struct xbps_handle * xhp
Definition xbps.h:1422
xbps_dictionary_t idx
Definition xbps.h:1428
Repository structure.
Definition xbps.h:1409
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:1320
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:317
bool xbps_pkg_name(char *dst, size_t len, const char *pkg)
Definition util.c:253
bool xbps_pkg_is_ignored(struct xbps_handle *xhp, const char *pkg)
Definition util.c:103
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:530
bool xbps_pkgpattern_name(char *dst, size_t len, const char *pattern)
Definition util.c:289