2 * Copyright (c) 2008, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * set/retrieve data from solvables
24 #include "poolvendor.h"
26 #include "linkedpkg.h"
29 pool_solvable2str(Pool *pool, Solvable *s)
31 const char *n, *e, *a;
34 n = pool_id2str(pool, s->name);
35 e = pool_id2str(pool, s->evr);
36 /* XXX: may want to skip the epoch here */
37 a = pool_id2str(pool, s->arch);
41 if (pool->havedistepoch)
43 /* strip the distepoch from the evr */
44 const char *de = strrchr(e, '-');
45 if (de && (de = strchr(de, ':')) != 0)
48 p = pool_alloctmpspace(pool, nl + el + al + 3);
53 strncpy(p + nl, e, el);
57 p[nl + el] = pool->disttype == DISTTYPE_HAIKU ? '-' : '.';
58 strcpy(p + nl + el + 1, a);
64 solvable_lookup_type(Solvable *s, Id keyname)
68 return repo_lookup_type(s->repo, s - s->repo->pool->solvables, keyname);
72 solvable_lookup_id(Solvable *s, Id keyname)
76 return repo_lookup_id(s->repo, s - s->repo->pool->solvables, keyname);
80 solvable_lookup_idarray(Solvable *s, Id keyname, Queue *q)
87 return repo_lookup_idarray(s->repo, s - s->repo->pool->solvables, keyname, q);
91 solvable_lookup_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
98 return repo_lookup_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
102 solvable_lookup_str_joinarray(Solvable *s, Id keyname, const char *joinstr)
108 queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
109 if (solvable_lookup_idarray(s, keyname, &q) && q.count)
111 Pool *pool = s->repo->pool;
113 str = pool_tmpjoin(pool, pool_id2str(pool, q.elements[0]), 0, 0);
114 for (i = 1; i < q.count; i++)
115 str = pool_tmpappend(pool, str, joinstr, pool_id2str(pool, q.elements[i]));
122 solvable_lookup_str(Solvable *s, Id keyname)
127 str = repo_lookup_str(s->repo, s - s->repo->pool->solvables, keyname);
128 if (!str && (keyname == SOLVABLE_LICENSE || keyname == SOLVABLE_GROUP))
129 str = solvable_lookup_str_joinarray(s, keyname, ", ");
134 solvable_lookup_str_base(Solvable *s, Id keyname, Id basekeyname, int usebase)
137 const char *str, *basestr;
144 pool = s->repo->pool;
145 str = solvable_lookup_str(s, keyname);
146 if (str || keyname == basekeyname)
148 basestr = solvable_lookup_str(s, basekeyname);
151 /* search for a solvable with same name and same base that has the
153 if (!pool->whatprovides)
154 return usebase ? basestr : 0;
156 /* we do this in two passes, first same vendor, then all other vendors */
157 for (pass = 0; pass < 2; pass++)
159 FOR_PROVIDES(p, pp, name)
161 s2 = pool->solvables + p;
162 if (s2->name != name)
164 if ((s->vendor == s2->vendor) != (pass == 0))
166 str = solvable_lookup_str(s2, basekeyname);
167 if (!str || strcmp(str, basestr))
169 str = solvable_lookup_str(s2, keyname);
173 #ifdef ENABLE_LINKED_PKGS
174 /* autopattern translation magic */
175 if (pass && !strncmp("pattern:", pool_id2str(pool, name), 8) && (name = find_autopattern_name(pool, s)) != 0)
179 return usebase ? basestr : 0;
183 solvable_lookup_str_poollang(Solvable *s, Id keyname)
192 pool = s->repo->pool;
193 if (!pool->nlanguages)
194 return solvable_lookup_str(s, keyname);
195 cols = pool->nlanguages + 1;
196 if (!pool->languagecache)
198 pool->languagecache = solv_calloc(cols * ID_NUM_INTERNAL, sizeof(Id));
199 pool->languagecacheother = 0;
201 if (keyname >= ID_NUM_INTERNAL)
203 row = pool->languagecache + ID_NUM_INTERNAL * cols;
204 for (i = 0; i < pool->languagecacheother; i++, row += cols)
207 if (i >= pool->languagecacheother)
209 pool->languagecache = solv_realloc2(pool->languagecache, pool->languagecacheother + 1, cols * sizeof(Id));
210 row = pool->languagecache + cols * (ID_NUM_INTERNAL + pool->languagecacheother++);
215 row = pool->languagecache + keyname * cols;
216 row++; /* skip keyname */
217 for (i = 0; i < pool->nlanguages; i++, row++)
220 *row = pool_id2langid(pool, keyname, pool->languages[i], 1);
221 str = solvable_lookup_str_base(s, *row, keyname, 0);
225 return solvable_lookup_str(s, keyname);
229 solvable_lookup_str_lang(Solvable *s, Id keyname, const char *lang, int usebase)
233 Id id = pool_id2langid(s->repo->pool, keyname, lang, 0);
235 return solvable_lookup_str_base(s, id, keyname, usebase);
239 return solvable_lookup_str(s, keyname);
243 solvable_lookup_num(Solvable *s, Id keyname, unsigned long long notfound)
247 return repo_lookup_num(s->repo, s - s->repo->pool->solvables, keyname, notfound);
251 solvable_lookup_sizek(Solvable *s, Id keyname, unsigned int notfound)
253 unsigned long long size;
256 size = solvable_lookup_num(s, keyname, (unsigned long long)notfound << 10);
257 return (unsigned int)((size + 1023) >> 10);
261 solvable_lookup_void(Solvable *s, Id keyname)
265 return repo_lookup_void(s->repo, s - s->repo->pool->solvables, keyname);
269 solvable_lookup_bool(Solvable *s, Id keyname)
273 /* historic nonsense: there are two ways of storing a bool, as num == 1 or void. test both. */
274 if (repo_lookup_type(s->repo, s - s->repo->pool->solvables, keyname) == REPOKEY_TYPE_VOID)
276 return repo_lookup_num(s->repo, s - s->repo->pool->solvables, keyname, 0) == 1;
279 const unsigned char *
280 solvable_lookup_bin_checksum(Solvable *s, Id keyname, Id *typep)
282 Repo *repo = s->repo;
289 return repo_lookup_bin_checksum(repo, s - repo->pool->solvables, keyname, typep);
293 solvable_lookup_checksum(Solvable *s, Id keyname, Id *typep)
295 const unsigned char *chk = solvable_lookup_bin_checksum(s, keyname, typep);
296 return chk ? pool_bin2hex(s->repo->pool, chk, solv_chksum_len(*typep)) : 0;
299 static inline const char *
300 evrid2vrstr(Pool *pool, Id evrid)
302 const char *p, *evr = pool_id2str(pool, evrid);
305 for (p = evr; *p >= '0' && *p <= '9'; p++)
307 return p != evr && *p == ':' && p[1] ? p + 1 : evr;
311 solvable_lookup_location(Solvable *s, unsigned int *medianrp)
316 const char *mediadir, *mediafile;
322 pool = s->repo->pool;
324 *medianrp = solvable_lookup_num(s, SOLVABLE_MEDIANR, 0);
325 if (solvable_lookup_void(s, SOLVABLE_MEDIADIR))
326 mediadir = pool_id2str(pool, s->arch);
328 mediadir = solvable_lookup_str(s, SOLVABLE_MEDIADIR);
330 l = strlen(mediadir) + 1;
331 if (solvable_lookup_void(s, SOLVABLE_MEDIAFILE))
333 const char *name, *evr, *arch;
334 name = pool_id2str(pool, s->name);
335 evr = evrid2vrstr(pool, s->evr);
336 arch = pool_id2str(pool, s->arch);
337 /* name-vr.arch.rpm */
338 loc = pool_alloctmpspace(pool, l + strlen(name) + strlen(evr) + strlen(arch) + 7);
340 sprintf(loc, "%s/%s-%s.%s.rpm", mediadir, name, evr, arch);
342 sprintf(loc, "%s-%s.%s.rpm", name, evr, arch);
346 mediafile = solvable_lookup_str(s, SOLVABLE_MEDIAFILE);
349 loc = pool_alloctmpspace(pool, l + strlen(mediafile) + 1);
351 sprintf(loc, "%s/%s", mediadir, mediafile);
353 strcpy(loc, mediafile);
359 solvable_get_location(Solvable *s, unsigned int *medianrp)
361 const char *loc = solvable_lookup_location(s, medianrp);
362 if (medianrp && *medianrp == 0)
363 *medianrp = 1; /* compat, to be removed */
368 solvable_lookup_sourcepkg(Solvable *s)
371 const char *evr, *name;
376 pool = s->repo->pool;
377 if (solvable_lookup_void(s, SOLVABLE_SOURCENAME))
378 name = pool_id2str(pool, s->name);
380 name = solvable_lookup_str(s, SOLVABLE_SOURCENAME);
383 archid = solvable_lookup_id(s, SOLVABLE_SOURCEARCH);
384 if (solvable_lookup_void(s, SOLVABLE_SOURCEEVR))
385 evr = evrid2vrstr(pool, s->evr);
387 evr = solvable_lookup_str(s, SOLVABLE_SOURCEEVR);
388 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
391 str = pool_tmpjoin(pool, name, evr ? "-" : 0, evr);
392 str = pool_tmpappend(pool, str, ".", pool_id2str(pool, archid));
393 return pool_tmpappend(pool, str, ".rpm", 0);
396 return name; /* FIXME */
400 /*****************************************************************************/
402 static inline Id dep2name(Pool *pool, Id dep)
404 while (ISRELDEP(dep))
406 Reldep *rd = rd = GETRELDEP(pool, dep);
412 static int providedbyinstalled_multiversion(Pool *pool, Map *installed, Id n, Id con)
415 Solvable *sn = pool->solvables + n;
417 FOR_PROVIDES(p, pp, sn->name)
419 Solvable *s = pool->solvables + p;
420 if (s->name != sn->name || s->arch != sn->arch)
422 if (!MAPTST(installed, p))
424 if (pool_match_nevr(pool, pool->solvables + p, con))
426 return 1; /* found installed package that doesn't conflict */
431 static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int ispatch, Map *multiversionmap)
434 FOR_PROVIDES(p, pp, dep)
436 if (p == SYSTEMSOLVABLE)
438 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
440 if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
441 if (providedbyinstalled_multiversion(pool, installed, p, dep))
443 if (MAPTST(installed, p))
450 * solvable_trivial_installable_map - anwers is a solvable is installable
451 * without any other installs/deinstalls.
452 * The packages considered to be installed are provided via the
453 * installedmap bitmap. A additional "conflictsmap" bitmap providing
454 * information about the conflicts of the installed packages can be
455 * used for extra speed up. Provide a NULL pointer if you do not
456 * have this information.
457 * Both maps can be created with pool_create_state_maps() or
458 * solver_create_state_maps().
461 * 1: solvable is installable without any other package changes
462 * 0: solvable is not installable
463 * -1: solvable is installable, but doesn't constrain any installed packages
466 solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap, Map *multiversionmap)
468 Pool *pool = s->repo->pool;
473 int r, interesting = 0;
475 if (conflictsmap && MAPTST(conflictsmap, s - pool->solvables))
479 reqp = s->repo->idarraydata + s->requires;
480 while ((req = *reqp++) != 0)
482 if (req == SOLVABLE_PREREQMARKER)
484 r = providedbyinstalled(pool, installedmap, req, 0, 0);
495 if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
497 conp = s->repo->idarraydata + s->conflicts;
498 while ((con = *conp++) != 0)
500 if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
502 if (ispatch && solvable_is_irrelevant_patch(s, installedmap))
506 if (!interesting && ISRELDEP(con))
508 con = dep2name(pool, con);
509 if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
513 if (ispatch && interesting && solvable_is_irrelevant_patch(s, installedmap))
521 if (s->obsoletes && s->repo != installed)
523 obsp = s->repo->idarraydata + s->obsoletes;
524 while ((obs = *obsp++) != 0)
526 if (providedbyinstalled(pool, installedmap, obs, 0, 0))
530 if (s->repo != installed)
533 FOR_PROVIDES(p, pp, s->name)
535 s2 = pool->solvables + p;
536 if (s2->repo == installed && s2->name == s->name)
546 p = s - pool->solvables;
547 for (i = 1; i < pool->nsolvables; i++)
549 if (!MAPTST(installedmap, i))
551 s2 = pool->solvables + i;
554 conp = s2->repo->idarraydata + s2->conflicts;
555 while ((con = *conp++) != 0)
557 dp = pool_whatprovides_ptr(pool, con);
564 return interesting ? 1 : -1;
568 * different interface for solvable_trivial_installable_map, where
569 * the information about the installed packages is provided
573 solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *multiversionmap)
575 Pool *pool = s->repo->pool;
581 map_init(&installedmap, pool->nsolvables);
582 for (i = 0; i < installed->count; i++)
584 p = installed->elements[i];
585 if (p > 0) /* makes it work with decisionq */
586 MAPSET(&installedmap, p);
588 r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
589 map_free(&installedmap);
594 * different interface for solvable_trivial_installable_map, where
595 * the information about the installed packages is provided
596 * by a repo containing the installed solvables.
599 solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *multiversionmap)
601 Pool *pool = s->repo->pool;
607 map_init(&installedmap, pool->nsolvables);
608 FOR_REPO_SOLVABLES(installed, p, s2)
609 MAPSET(&installedmap, p);
610 r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
611 map_free(&installedmap);
615 /* FIXME: this mirrors policy_illegal_vendorchange */
617 pool_illegal_vendorchange(Pool *pool, Solvable *s1, Solvable *s2)
620 Id vendormask1, vendormask2;
622 if (pool->custom_vendorcheck)
623 return pool->custom_vendorcheck(pool, s1, s2);
624 /* treat a missing vendor as empty string */
625 v1 = s1->vendor ? s1->vendor : ID_EMPTY;
626 v2 = s2->vendor ? s2->vendor : ID_EMPTY;
629 vendormask1 = pool_vendor2mask(pool, v1);
631 return 1; /* can't match */
632 vendormask2 = pool_vendor2mask(pool, v2);
633 if ((vendormask1 & vendormask2) != 0)
635 return 1; /* no class matches */
638 /* check if this patch is relevant according to the vendor. To bad that patches
639 * don't have a vendor, so we need to do some careful repo testing. */
641 solvable_is_irrelevant_patch(Solvable *s, Map *installedmap)
643 Pool *pool = s->repo->pool;
645 int hadpatchpackage = 0;
649 conp = s->repo->idarraydata + s->conflicts;
650 while ((con = *conp++) != 0)
656 rd = GETRELDEP(pool, con);
657 if (rd->flags != REL_LT)
659 FOR_PROVIDES(p, pp, con)
662 if (!MAPTST(installedmap, p))
664 si = pool->solvables + p;
665 if (!pool_match_nevr(pool, si, con))
667 FOR_PROVIDES(p2, pp2, rd->name)
669 Solvable *s2 = pool->solvables + p2;
670 if (!pool_match_nevr(pool, s2, rd->name))
672 if (pool_match_nevr(pool, s2, con))
673 continue; /* does not fulfill patch */
674 if (s2->repo == s->repo)
677 /* ok, we have a package from the patch repo that solves the conflict. check vendor */
678 if (si->vendor == s2->vendor)
680 if (!pool_illegal_vendorchange(pool, si, s2))
682 /* vendor change was illegal, ignore conflict */
687 /* if we didn't find a patchpackage don't claim that the patch is irrelevant */
688 if (!hadpatchpackage)
693 /*****************************************************************************/
696 * Create maps containing the state of each solvable. Input is a "installed" queue,
697 * it contains all solvable ids that are considered to be installed.
699 * The created maps can be used for solvable_trivial_installable_map(),
700 * pool_calc_duchanges(), pool_calc_installsizechange().
704 pool_create_state_maps(Pool *pool, Queue *installed, Map *installedmap, Map *conflictsmap)
711 map_init(installedmap, pool->nsolvables);
713 map_init(conflictsmap, pool->nsolvables);
714 for (i = 0; i < installed->count; i++)
716 p = installed->elements[i];
717 if (p <= 0) /* makes it work with decisionq */
719 MAPSET(installedmap, p);
722 s = pool->solvables + p;
725 conp = s->repo->idarraydata + s->conflicts;
726 while ((con = *conp++) != 0)
728 dp = pool_whatprovides_ptr(pool, con);
730 MAPSET(conflictsmap, *dp);
735 /* Tests if two solvables have identical content. Currently
736 * both solvables need to come from the same pool
739 solvable_identical(Solvable *s1, Solvable *s2)
741 unsigned int bt1, bt2;
745 if (s1->name != s2->name)
747 if (s1->arch != s2->arch)
749 if (s1->evr != s2->evr)
751 /* map missing vendor to empty string */
752 if ((s1->vendor ? s1->vendor : 1) != (s2->vendor ? s2->vendor : 1))
755 /* looking good, try some fancier stuff */
756 /* might also look up the package checksum here */
757 bt1 = solvable_lookup_num(s1, SOLVABLE_BUILDTIME, 0);
758 bt2 = solvable_lookup_num(s2, SOLVABLE_BUILDTIME, 0);
766 /* look at requires in a last attempt to find recompiled packages */
769 for (reqp = s1->repo->idarraydata + s1->requires; *reqp; reqp++)
772 for (reqp = s2->repo->idarraydata + s2->requires; *reqp; reqp++)
780 /* return the self provide dependency of a solvable */
782 solvable_selfprovidedep(Solvable *s)
790 pool = s->repo->pool;
793 provp = s->repo->idarraydata + s->provides;
794 while ((prov = *provp++) != 0)
798 rd = GETRELDEP(pool, prov);
799 if (rd->name == s->name && rd->evr == s->evr && rd->flags == REL_EQ)
803 return pool_rel2id(pool, s->name, s->evr, REL_EQ, 1);
806 /* setter functions, simply call the repo variants */
808 solvable_set_id(Solvable *s, Id keyname, Id id)
810 repo_set_id(s->repo, s - s->repo->pool->solvables, keyname, id);
814 solvable_set_num(Solvable *s, Id keyname, unsigned long long num)
816 repo_set_num(s->repo, s - s->repo->pool->solvables, keyname, num);
820 solvable_set_str(Solvable *s, Id keyname, const char *str)
822 repo_set_str(s->repo, s - s->repo->pool->solvables, keyname, str);
826 solvable_set_poolstr(Solvable *s, Id keyname, const char *str)
828 repo_set_poolstr(s->repo, s - s->repo->pool->solvables, keyname, str);
832 solvable_add_poolstr_array(Solvable *s, Id keyname, const char *str)
834 repo_add_poolstr_array(s->repo, s - s->repo->pool->solvables, keyname, str);
838 solvable_add_idarray(Solvable *s, Id keyname, Id id)
840 repo_add_idarray(s->repo, s - s->repo->pool->solvables, keyname, id);
844 solvable_add_deparray(Solvable *s, Id keyname, Id dep, Id marker)
846 repo_add_deparray(s->repo, s - s->repo->pool->solvables, keyname, dep, marker);
850 solvable_set_idarray(Solvable *s, Id keyname, Queue *q)
852 repo_set_idarray(s->repo, s - s->repo->pool->solvables, keyname, q);
856 solvable_set_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
858 repo_set_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
862 solvable_unset(Solvable *s, Id keyname)
864 repo_unset(s->repo, s - s->repo->pool->solvables, keyname);
867 /* return true if a dependency intersects dep in the keyname array */
869 solvable_matchesdep(Solvable *s, Id keyname, Id dep, int marker)
872 Pool *pool = s->repo->pool;
875 solvable_lookup_deparray(s, keyname, &q, marker);
876 for (i = 0; i < q.count; i++)
877 if (pool_match_dep(pool, q.elements[i], dep))
879 i = i == q.count ? 0 : 1;