2 * Copyright (c) 2016, SUSE LLC.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
8 /* weird SUSE stuff. better not use it for your projects. */
16 #include "solver_private.h"
19 #include "poolvendor.h"
23 repo_fix_supplements(Repo *repo, Offset provides, Offset supplements, Offset freshens)
25 Pool *pool = repo->pool;
27 char buf[1024], *p, *dep;
32 for (i = provides; repo->idarraydata[i]; i++)
34 id = repo->idarraydata[i];
37 dep = (char *)pool_id2str(pool, id);
38 if (!strncmp(dep, "locale(", 7) && strlen(dep) < sizeof(buf) - 2)
43 if ((p = strchr(dep, ':')) != 0 && p != dep)
46 idp = pool_str2id(pool, dep, 1);
50 while ((p = strchr(dep, ';')) != 0)
58 idl = pool_str2id(pool, dep, 1);
59 idl = pool_rel2id(pool, NAMESPACE_LANGUAGE, idl, REL_NAMESPACE, 1);
61 id = pool_rel2id(pool, id, idl, REL_OR, 1);
68 for (p = dep; *p && *p != ')'; p++)
71 idl = pool_str2id(pool, dep, 1);
72 idl = pool_rel2id(pool, NAMESPACE_LANGUAGE, idl, REL_NAMESPACE, 1);
74 id = pool_rel2id(pool, id, idl, REL_OR, 1);
79 id = pool_rel2id(pool, idp, id, REL_AND, 1);
81 supplements = repo_addid_dep(repo, supplements, id, 0);
83 else if ((p = strchr(dep, ':')) != 0 && p != dep && p[1] == '/' && strlen(dep) < sizeof(buf))
88 idp = pool_str2id(pool, buf, 1);
89 /* strip trailing slashes */
91 while (l > 1 && p[l - 1] == '/')
93 id = pool_str2id(pool, p, 1);
94 id = pool_rel2id(pool, idp, id, REL_WITH, 1);
95 id = pool_rel2id(pool, NAMESPACE_SPLITPROVIDES, id, REL_NAMESPACE, 1);
96 supplements = repo_addid_dep(repo, supplements, id, 0);
102 for (i = supplements; repo->idarraydata[i]; i++)
104 id = repo->idarraydata[i];
107 dep = (char *)pool_id2str(pool, id);
108 if (!strncmp(dep, "system:modalias(", 16))
110 if (!strncmp(dep, "modalias(", 9) && dep[9] && dep[10] && strlen(dep) < sizeof(buf))
113 p = strchr(buf + 9, ':');
114 if (p && p != buf + 9 && strchr(p + 1, ':'))
117 idp = pool_str2id(pool, buf + 9, 1);
118 p[strlen(p) - 1] = 0;
119 id = pool_str2id(pool, p, 1);
120 id = pool_rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
121 id = pool_rel2id(pool, idp, id, REL_AND, 1);
126 p[strlen(p) - 1] = 0;
127 id = pool_str2id(pool, p, 1);
128 id = pool_rel2id(pool, NAMESPACE_MODALIAS, id, REL_NAMESPACE, 1);
131 repo->idarraydata[i] = id;
133 else if (!strncmp(dep, "packageand(", 11) && strlen(dep) < sizeof(buf))
138 while ((p = strchr(dep, ':')) != 0)
145 /* argh, allow pattern: prefix. sigh */
146 if (p - dep == 7 && !strncmp(dep, "pattern", 7))
148 p = strchr(p + 1, ':');
153 idp = pool_str2id(pool, dep, 1);
155 id = pool_rel2id(pool, id, idp, REL_AND, 1);
160 if (dep[0] && dep[1])
162 dep[strlen(dep) - 1] = 0;
163 idp = pool_str2id(pool, dep, 1);
165 id = pool_rel2id(pool, id, idp, REL_AND, 1);
170 repo->idarraydata[i] = id;
172 else if (!strncmp(dep, "filesystem(", 11) && strlen(dep) < sizeof(buf))
174 strcpy(buf, dep + 11);
175 if ((p = strrchr(buf, ')')) != 0)
177 id = pool_str2id(pool, buf, 1);
178 id = pool_rel2id(pool, NAMESPACE_FILESYSTEM, id, REL_NAMESPACE, 1);
179 repo->idarraydata[i] = id;
183 if (freshens && repo->idarraydata[freshens])
185 Id idsupp = 0, idfresh = 0;
186 if (!supplements || !repo->idarraydata[supplements])
188 for (i = supplements; repo->idarraydata[i]; i++)
191 idsupp = repo->idarraydata[i];
193 idsupp = pool_rel2id(pool, idsupp, repo->idarraydata[i], REL_OR, 1);
195 for (i = freshens; repo->idarraydata[i]; i++)
198 idfresh = repo->idarraydata[i];
200 idfresh = pool_rel2id(pool, idfresh, repo->idarraydata[i], REL_OR, 1);
205 idsupp = pool_rel2id(pool, idsupp, idfresh, REL_AND, 1);
206 supplements = repo_addid_dep(repo, 0, idsupp, 0);
212 repo_fix_conflicts(Repo *repo, Offset conflicts)
214 char buf[1024], *dep;
215 Pool *pool = repo->pool;
221 for (i = conflicts; repo->idarraydata[i]; i++)
223 id = repo->idarraydata[i];
226 dep = (char *)pool_id2str(pool, id);
227 if (!strncmp(dep, "otherproviders(", 15) && dep[15] && strlen(dep) < sizeof(buf) - 2)
229 strcpy(buf, dep + 15);
230 buf[strlen(buf) - 1] = 0;
231 id = pool_str2id(pool, buf, 1);
232 id = pool_rel2id(pool, NAMESPACE_OTHERPROVIDERS, id, REL_NAMESPACE, 1);
233 repo->idarraydata[i] = id;
240 repo_rewrite_suse_deps(Solvable *s, Offset freshens)
242 s->supplements = repo_fix_supplements(s->repo, s->provides, s->supplements, freshens);
244 s->conflicts = repo_fix_conflicts(s->repo, s->conflicts);
247 /**********************************************************************************/
250 dep2name(Pool *pool, Id dep)
252 while (ISRELDEP(dep))
254 Reldep *rd = GETRELDEP(pool, dep);
261 providedbyinstalled_multiversion(Pool *pool, Map *installed, Id n, Id con)
264 Solvable *sn = pool->solvables + n;
266 FOR_PROVIDES(p, pp, sn->name)
268 Solvable *s = pool->solvables + p;
269 if (s->name != sn->name || s->arch != sn->arch)
271 if (!MAPTST(installed, p))
273 if (pool_match_nevr(pool, pool->solvables + p, con))
275 return 1; /* found installed package that doesn't conflict */
281 providedbyinstalled(Pool *pool, Map *installed, Id dep, int ispatch, Map *multiversionmap)
284 FOR_PROVIDES(p, pp, dep)
286 if (p == SYSTEMSOLVABLE)
288 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
290 if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
291 if (providedbyinstalled_multiversion(pool, installed, p, dep))
293 if (MAPTST(installed, p))
301 * 2: conflicts with installed
302 * 8: interesting (only true if installed)
307 providedbyinstalled_multiversion_xmap(Pool *pool, unsigned char *map, Id n, Id con)
310 Solvable *sn = pool->solvables + n;
312 FOR_PROVIDES(p, pp, sn->name)
314 Solvable *s = pool->solvables + p;
315 if (s->name != sn->name || s->arch != sn->arch)
317 if ((map[p] & 9) != 9)
319 if (pool_match_nevr(pool, pool->solvables + p, con))
321 return 1; /* found installed package that doesn't conflict */
328 providedbyinstalled_xmap(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *multiversionmap)
332 FOR_PROVIDES(p, pp, dep)
334 if (p == SYSTEMSOLVABLE)
335 return 1; /* always boring, as never constraining */
336 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
338 if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
339 if (providedbyinstalled_multiversion_xmap(pool, map, p, dep))
341 if ((map[p] & 9) == 9)
348 /* FIXME: this mirrors policy_illegal_vendorchange */
350 pool_illegal_vendorchange(Pool *pool, Solvable *s1, Solvable *s2)
353 Id vendormask1, vendormask2;
355 if (pool->custom_vendorcheck)
356 return pool->custom_vendorcheck(pool, s1, s2);
357 /* treat a missing vendor as empty string */
358 v1 = s1->vendor ? s1->vendor : ID_EMPTY;
359 v2 = s2->vendor ? s2->vendor : ID_EMPTY;
362 vendormask1 = pool_vendor2mask(pool, v1);
364 return 1; /* can't match */
365 vendormask2 = pool_vendor2mask(pool, v2);
366 if ((vendormask1 & vendormask2) != 0)
368 return 1; /* no class matches */
371 /* check if this patch is relevant according to the vendor. To bad that patches
372 * don't have a vendor, so we need to do some careful repo testing. */
374 solvable_is_irrelevant_patch(Solvable *s, Map *installedmap)
376 Pool *pool = s->repo->pool;
378 int hadpatchpackage = 0;
381 status = solvable_lookup_str(s, UPDATE_STATUS);
382 if (status && !strcmp(status, "retracted"))
386 conp = s->repo->idarraydata + s->conflicts;
387 while ((con = *conp++) != 0)
393 rd = GETRELDEP(pool, con);
394 if (rd->flags != REL_LT)
396 FOR_PROVIDES(p, pp, con)
399 if (!MAPTST(installedmap, p))
401 si = pool->solvables + p;
402 if (!pool_match_nevr(pool, si, con))
404 FOR_PROVIDES(p2, pp2, rd->name)
406 Solvable *s2 = pool->solvables + p2;
407 if (!pool_match_nevr(pool, s2, rd->name))
409 if (pool_match_nevr(pool, s2, con))
410 continue; /* does not fulfill patch */
411 if (s2->repo == s->repo)
414 /* ok, we have a package from the patch repo that solves the conflict. check vendor */
415 if (si->vendor == s2->vendor)
417 if (!pool_illegal_vendorchange(pool, si, s2))
419 /* vendor change was illegal, ignore conflict */
424 /* if we didn't find a patchpackage don't claim that the patch is irrelevant */
425 if (!hadpatchpackage)
431 * solvable_trivial_installable_map - answers if a solvable is installable
432 * without any other installs/deinstalls.
433 * The packages considered to be installed are provided via the
434 * installedmap bitmap. A additional "conflictsmap" bitmap providing
435 * information about the conflicts of the installed packages can be
436 * used for extra speed up. Provide a NULL pointer if you do not
437 * have this information.
438 * Both maps can be created with pool_create_state_maps() or
439 * solver_create_state_maps().
442 * 1: solvable is installable without any other package changes
443 * 0: solvable is not installable
444 * -1: solvable is installable, but doesn't constrain any installed packages
447 solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap, Map *multiversionmap)
449 Pool *pool = s->repo->pool;
454 int r, interesting = 0;
456 if (conflictsmap && MAPTST(conflictsmap, s - pool->solvables))
460 reqp = s->repo->idarraydata + s->requires;
461 while ((req = *reqp++) != 0)
463 if (req == SOLVABLE_PREREQMARKER)
465 r = providedbyinstalled(pool, installedmap, req, 0, 0);
476 if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
478 conp = s->repo->idarraydata + s->conflicts;
479 while ((con = *conp++) != 0)
481 if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
483 if (ispatch && solvable_is_irrelevant_patch(s, installedmap))
487 if (!interesting && ISRELDEP(con))
489 con = dep2name(pool, con);
490 if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
494 if (ispatch && interesting && solvable_is_irrelevant_patch(s, installedmap))
501 p = s - pool->solvables;
502 for (i = 1; i < pool->nsolvables; i++)
504 if (!MAPTST(installedmap, i))
506 s2 = pool->solvables + i;
509 conp = s2->repo->idarraydata + s2->conflicts;
510 while ((con = *conp++) != 0)
512 dp = pool_whatprovides_ptr(pool, con);
519 return interesting ? 1 : -1;
523 * different interface for solvable_trivial_installable_map, where
524 * the information about the installed packages is provided
528 solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *multiversionmap)
530 Pool *pool = s->repo->pool;
536 map_init(&installedmap, pool->nsolvables);
537 for (i = 0; i < installed->count; i++)
539 p = installed->elements[i];
540 if (p > 0) /* makes it work with decisionq */
541 MAPSET(&installedmap, p);
543 r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
544 map_free(&installedmap);
549 * different interface for solvable_trivial_installable_map, where
550 * the information about the installed packages is provided
551 * by a repo containing the installed solvables.
554 solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *multiversionmap)
556 Pool *pool = s->repo->pool;
562 map_init(&installedmap, pool->nsolvables);
563 FOR_REPO_SOLVABLES(installed, p, s2)
564 MAPSET(&installedmap, p);
565 r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
566 map_free(&installedmap);
571 * pool_trivial_installable - calculate if a set of solvables is
572 * trivial installable without any other installs/deinstalls of
573 * packages not belonging to the set.
575 * the state is returned in the result queue:
576 * 1: solvable is installable without any other package changes
577 * 0: solvable is not installable
578 * -1: solvable is installable, but doesn't constrain any installed packages
582 pool_trivial_installable_multiversionmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *multiversionmap)
585 Id p, *dp, con, *conp, req, *reqp;
589 map = solv_calloc(pool->nsolvables, 1);
590 for (p = 1; p < pool->nsolvables; p++)
592 if (!MAPTST(installedmap, p))
595 s = pool->solvables + p;
598 conp = s->repo->idarraydata + s->conflicts;
599 while ((con = *conp++) != 0)
601 dp = pool_whatprovides_ptr(pool, con);
603 map[p] |= 2; /* XXX: self conflict ? */
606 for (i = 0; i < pkgs->count; i++)
607 map[pkgs->elements[i]] = 16;
609 for (i = 0, did = 0; did < pkgs->count; i++, did++)
611 if (i == pkgs->count)
613 p = pkgs->elements[i];
614 if ((map[p] & 16) == 0)
616 if ((map[p] & 2) != 0)
621 s = pool->solvables + p;
625 reqp = s->repo->idarraydata + s->requires;
626 while ((req = *reqp++) != 0)
628 if (req == SOLVABLE_PREREQMARKER)
630 r = providedbyinstalled_xmap(pool, map, req, 0, 0);
633 /* decided and miss */
639 break; /* undecided */
640 m |= r; /* 1 | 9 | 17 */
649 int ispatch = 0; /* see solver.c patch handling */
651 if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
653 conp = s->repo->idarraydata + s->conflicts;
654 while ((con = *conp++) != 0)
656 if ((providedbyinstalled_xmap(pool, map, con, ispatch, multiversionmap) & 1) != 0)
662 if ((m == 1 || m == 17) && ISRELDEP(con))
664 con = dep2name(pool, con);
665 if ((providedbyinstalled_xmap(pool, map, con, ispatch, multiversionmap) & 1) != 0)
670 continue; /* found a conflict */
679 queue_init_clone(res, pkgs);
680 for (i = 0; i < pkgs->count; i++)
682 m = map[pkgs->elements[i]];
689 res->elements[i] = r;
695 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
697 pool_trivial_installable_multiversionmap(pool, installedmap, pkgs, res, 0);
701 solver_trivial_installable(Solver *solv, Queue *pkgs, Queue *res)
703 Pool *pool = solv->pool;
706 pool_create_state_maps(pool, &solv->decisionq, &installedmap, 0);
707 pool_trivial_installable_multiversionmap(pool, &installedmap, pkgs, res, solv->multiversion.size ? &solv->multiversion : 0);
708 for (i = 0; i < res->count; i++)
709 if (res->elements[i] != -1)
711 Solvable *s = pool->solvables + pkgs->elements[i];
712 if (!strncmp("patch:", pool_id2str(pool, s->name), 6) && solvable_is_irrelevant_patch(s, &installedmap))
713 res->elements[i] = -1;
715 map_free(&installedmap);
719 solver_printtrivial(Solver *solv)
721 Pool *pool = solv->pool;
729 for (p = 1, s = pool->solvables + p; p < solv->pool->nsolvables; p++, s++)
731 n = pool_id2str(pool, s->name);
732 if (strncmp(n, "patch:", 6) != 0 && strncmp(n, "pattern:", 8) != 0)
742 solver_trivial_installable(solv, &in, &out);
743 POOL_DEBUG(SOLV_DEBUG_RESULT, "trivial installable status:\n");
744 for (i = 0; i < in.count; i++)
745 POOL_DEBUG(SOLV_DEBUG_RESULT, " %s: %d\n", pool_solvid2str(pool, in.elements[i]), out.elements[i]);
746 POOL_DEBUG(SOLV_DEBUG_RESULT, "\n");