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"
28 pool_solvable2str(Pool *pool, Solvable *s)
30 const char *n, *e, *a;
33 n = pool_id2str(pool, s->name);
34 e = pool_id2str(pool, s->evr);
35 /* XXX: may want to skip the epoch here */
36 a = pool_id2str(pool, s->arch);
40 if (pool->havedistepoch)
42 /* strip the distepoch from the evr */
43 const char *de = strrchr(e, '-');
44 if (de && (de = strchr(de, ':')) != 0)
47 p = pool_alloctmpspace(pool, nl + el + al + 3);
52 strncpy(p + nl, e, el);
57 strcpy(p + nl + el + 1, a);
63 solvable_lookup_type(Solvable *s, Id keyname)
67 return repo_lookup_type(s->repo, s - s->repo->pool->solvables, keyname);
71 solvable_lookup_id(Solvable *s, Id keyname)
75 return repo_lookup_id(s->repo, s - s->repo->pool->solvables, keyname);
79 solvable_lookup_idarray(Solvable *s, Id keyname, Queue *q)
86 return repo_lookup_idarray(s->repo, s - s->repo->pool->solvables, keyname, q);
90 solvable_lookup_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
97 return repo_lookup_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
101 solvable_lookup_str(Solvable *s, Id keyname)
105 return repo_lookup_str(s->repo, s - s->repo->pool->solvables, keyname);
109 solvable_lookup_str_base(Solvable *s, Id keyname, Id basekeyname, int usebase)
112 const char *str, *basestr;
119 pool = s->repo->pool;
120 str = solvable_lookup_str(s, keyname);
121 if (str || keyname == basekeyname)
123 basestr = solvable_lookup_str(s, basekeyname);
126 /* search for a solvable with same name and same base that has the
128 if (!pool->whatprovides)
129 return usebase ? basestr : 0;
130 /* we do this in two passes, first same vendor, then all other vendors */
131 for (pass = 0; pass < 2; pass++)
133 FOR_PROVIDES(p, pp, s->name)
135 s2 = pool->solvables + p;
136 if (s2->name != s->name)
138 if ((s->vendor == s2->vendor) != (pass == 0))
140 str = solvable_lookup_str(s2, basekeyname);
141 if (!str || strcmp(str, basestr))
143 str = solvable_lookup_str(s2, keyname);
148 return usebase ? basestr : 0;
152 solvable_lookup_str_poollang(Solvable *s, Id keyname)
161 pool = s->repo->pool;
162 if (!pool->nlanguages)
163 return solvable_lookup_str(s, keyname);
164 cols = pool->nlanguages + 1;
165 if (!pool->languagecache)
167 pool->languagecache = solv_calloc(cols * ID_NUM_INTERNAL, sizeof(Id));
168 pool->languagecacheother = 0;
170 if (keyname >= ID_NUM_INTERNAL)
172 row = pool->languagecache + ID_NUM_INTERNAL * cols;
173 for (i = 0; i < pool->languagecacheother; i++, row += cols)
176 if (i >= pool->languagecacheother)
178 pool->languagecache = solv_realloc2(pool->languagecache, pool->languagecacheother + 1, cols * sizeof(Id));
179 row = pool->languagecache + cols * (ID_NUM_INTERNAL + pool->languagecacheother++);
184 row = pool->languagecache + keyname * cols;
185 row++; /* skip keyname */
186 for (i = 0; i < pool->nlanguages; i++, row++)
189 *row = pool_id2langid(pool, keyname, pool->languages[i], 1);
190 str = solvable_lookup_str_base(s, *row, keyname, 0);
194 return solvable_lookup_str(s, keyname);
198 solvable_lookup_str_lang(Solvable *s, Id keyname, const char *lang, int usebase)
202 Id id = pool_id2langid(s->repo->pool, keyname, lang, 0);
204 return solvable_lookup_str_base(s, id, keyname, usebase);
208 return solvable_lookup_str(s, keyname);
212 solvable_lookup_num(Solvable *s, Id keyname, unsigned long long notfound)
216 return repo_lookup_num(s->repo, s - s->repo->pool->solvables, keyname, notfound);
220 solvable_lookup_sizek(Solvable *s, Id keyname, unsigned int notfound)
222 unsigned long long size;
225 size = solvable_lookup_num(s, keyname, (unsigned long long)notfound << 10);
226 return (unsigned int)((size + 1023) >> 10);
230 solvable_lookup_void(Solvable *s, Id keyname)
234 return repo_lookup_void(s->repo, s - s->repo->pool->solvables, keyname);
238 solvable_lookup_bool(Solvable *s, Id keyname)
242 /* historic nonsense: there are two ways of storing a bool, as num == 1 or void. test both. */
243 if (repo_lookup_type(s->repo, s - s->repo->pool->solvables, keyname) == REPOKEY_TYPE_VOID)
245 return repo_lookup_num(s->repo, s - s->repo->pool->solvables, keyname, 0) == 1;
248 const unsigned char *
249 solvable_lookup_bin_checksum(Solvable *s, Id keyname, Id *typep)
251 Repo *repo = s->repo;
258 return repo_lookup_bin_checksum(repo, s - repo->pool->solvables, keyname, typep);
262 solvable_lookup_checksum(Solvable *s, Id keyname, Id *typep)
264 const unsigned char *chk = solvable_lookup_bin_checksum(s, keyname, typep);
265 return chk ? pool_bin2hex(s->repo->pool, chk, solv_chksum_len(*typep)) : 0;
268 static inline const char *
269 evrid2vrstr(Pool *pool, Id evrid)
271 const char *p, *evr = pool_id2str(pool, evrid);
274 for (p = evr; *p >= '0' && *p <= '9'; p++)
276 return p != evr && *p == ':' && p[1] ? p + 1 : evr;
280 solvable_lookup_location(Solvable *s, unsigned int *medianrp)
285 const char *mediadir, *mediafile;
291 pool = s->repo->pool;
293 *medianrp = solvable_lookup_num(s, SOLVABLE_MEDIANR, 0);
294 if (solvable_lookup_void(s, SOLVABLE_MEDIADIR))
295 mediadir = pool_id2str(pool, s->arch);
297 mediadir = solvable_lookup_str(s, SOLVABLE_MEDIADIR);
299 l = strlen(mediadir) + 1;
300 if (solvable_lookup_void(s, SOLVABLE_MEDIAFILE))
302 const char *name, *evr, *arch;
303 name = pool_id2str(pool, s->name);
304 evr = evrid2vrstr(pool, s->evr);
305 arch = pool_id2str(pool, s->arch);
306 /* name-vr.arch.rpm */
307 loc = pool_alloctmpspace(pool, l + strlen(name) + strlen(evr) + strlen(arch) + 7);
309 sprintf(loc, "%s/%s-%s.%s.rpm", mediadir, name, evr, arch);
311 sprintf(loc, "%s-%s.%s.rpm", name, evr, arch);
315 mediafile = solvable_lookup_str(s, SOLVABLE_MEDIAFILE);
318 loc = pool_alloctmpspace(pool, l + strlen(mediafile) + 1);
320 sprintf(loc, "%s/%s", mediadir, mediafile);
322 strcpy(loc, mediafile);
328 solvable_get_location(Solvable *s, unsigned int *medianrp)
330 const char *loc = solvable_lookup_location(s, medianrp);
331 if (medianrp && *medianrp == 0)
332 *medianrp = 1; /* compat, to be removed */
337 solvable_lookup_sourcepkg(Solvable *s)
340 const char *evr, *name;
345 pool = s->repo->pool;
346 if (solvable_lookup_void(s, SOLVABLE_SOURCENAME))
347 name = pool_id2str(pool, s->name);
349 name = solvable_lookup_str(s, SOLVABLE_SOURCENAME);
352 archid = solvable_lookup_id(s, SOLVABLE_SOURCEARCH);
353 if (solvable_lookup_void(s, SOLVABLE_SOURCEEVR))
354 evr = evrid2vrstr(pool, s->evr);
356 evr = solvable_lookup_str(s, SOLVABLE_SOURCEEVR);
357 if (archid == ARCH_SRC || archid == ARCH_NOSRC)
360 str = pool_tmpjoin(pool, name, evr ? "-" : 0, evr);
361 str = pool_tmpappend(pool, str, ".", pool_id2str(pool, archid));
362 return pool_tmpappend(pool, str, ".rpm", 0);
365 return name; /* FIXME */
369 /*****************************************************************************/
371 static inline Id dep2name(Pool *pool, Id dep)
373 while (ISRELDEP(dep))
375 Reldep *rd = rd = GETRELDEP(pool, dep);
381 static int providedbyinstalled_multiversion(Pool *pool, Map *installed, Id n, Id con)
384 Solvable *sn = pool->solvables + n;
386 FOR_PROVIDES(p, pp, sn->name)
388 Solvable *s = pool->solvables + p;
389 if (s->name != sn->name || s->arch != sn->arch)
391 if (!MAPTST(installed, p))
393 if (pool_match_nevr(pool, pool->solvables + p, con))
395 return 1; /* found installed package that doesn't conflict */
400 static inline int providedbyinstalled(Pool *pool, Map *installed, Id dep, int ispatch, Map *multiversionmap)
403 FOR_PROVIDES(p, pp, dep)
405 if (p == SYSTEMSOLVABLE)
407 if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
409 if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
410 if (providedbyinstalled_multiversion(pool, installed, p, dep))
412 if (MAPTST(installed, p))
419 * solvable_trivial_installable_map - anwers is a solvable is installable
420 * without any other installs/deinstalls.
421 * The packages considered to be installed are provided via the
422 * installedmap bitmap. A additional "conflictsmap" bitmap providing
423 * information about the conflicts of the installed packages can be
424 * used for extra speed up. Provide a NULL pointer if you do not
425 * have this information.
426 * Both maps can be created with pool_create_state_maps() or
427 * solver_create_state_maps().
430 * 1: solvable is installable without any other package changes
431 * 0: solvable is not installable
432 * -1: solvable is installable, but doesn't constrain any installed packages
435 solvable_trivial_installable_map(Solvable *s, Map *installedmap, Map *conflictsmap, Map *multiversionmap)
437 Pool *pool = s->repo->pool;
442 int r, interesting = 0;
444 if (conflictsmap && MAPTST(conflictsmap, s - pool->solvables))
448 reqp = s->repo->idarraydata + s->requires;
449 while ((req = *reqp++) != 0)
451 if (req == SOLVABLE_PREREQMARKER)
453 r = providedbyinstalled(pool, installedmap, req, 0, 0);
464 if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
466 conp = s->repo->idarraydata + s->conflicts;
467 while ((con = *conp++) != 0)
469 if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
471 if (ispatch && solvable_is_irrelevant_patch(s, installedmap))
475 if (!interesting && ISRELDEP(con))
477 con = dep2name(pool, con);
478 if (providedbyinstalled(pool, installedmap, con, ispatch, multiversionmap))
482 if (ispatch && interesting && solvable_is_irrelevant_patch(s, installedmap))
490 if (s->obsoletes && s->repo != installed)
492 obsp = s->repo->idarraydata + s->obsoletes;
493 while ((obs = *obsp++) != 0)
495 if (providedbyinstalled(pool, installedmap, obs, 0, 0))
499 if (s->repo != installed)
502 FOR_PROVIDES(p, pp, s->name)
504 s2 = pool->solvables + p;
505 if (s2->repo == installed && s2->name == s->name)
515 p = s - pool->solvables;
516 for (i = 1; i < pool->nsolvables; i++)
518 if (!MAPTST(installedmap, i))
520 s2 = pool->solvables + i;
523 conp = s2->repo->idarraydata + s2->conflicts;
524 while ((con = *conp++) != 0)
526 dp = pool_whatprovides_ptr(pool, con);
533 return interesting ? 1 : -1;
537 * different interface for solvable_trivial_installable_map, where
538 * the information about the installed packages is provided
542 solvable_trivial_installable_queue(Solvable *s, Queue *installed, Map *multiversionmap)
544 Pool *pool = s->repo->pool;
550 map_init(&installedmap, pool->nsolvables);
551 for (i = 0; i < installed->count; i++)
553 p = installed->elements[i];
554 if (p > 0) /* makes it work with decisionq */
555 MAPSET(&installedmap, p);
557 r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
558 map_free(&installedmap);
563 * different interface for solvable_trivial_installable_map, where
564 * the information about the installed packages is provided
565 * by a repo containing the installed solvables.
568 solvable_trivial_installable_repo(Solvable *s, Repo *installed, Map *multiversionmap)
570 Pool *pool = s->repo->pool;
576 map_init(&installedmap, pool->nsolvables);
577 FOR_REPO_SOLVABLES(installed, p, s2)
578 MAPSET(&installedmap, p);
579 r = solvable_trivial_installable_map(s, &installedmap, 0, multiversionmap);
580 map_free(&installedmap);
584 /* FIXME: this mirrors policy_illegal_vendorchange */
586 pool_illegal_vendorchange(Pool *pool, Solvable *s1, Solvable *s2)
589 Id vendormask1, vendormask2;
591 if (pool->custom_vendorcheck)
592 return pool->custom_vendorcheck(pool, s1, s2);
593 /* treat a missing vendor as empty string */
594 v1 = s1->vendor ? s1->vendor : ID_EMPTY;
595 v2 = s2->vendor ? s2->vendor : ID_EMPTY;
598 vendormask1 = pool_vendor2mask(pool, v1);
600 return 1; /* can't match */
601 vendormask2 = pool_vendor2mask(pool, v2);
602 if ((vendormask1 & vendormask2) != 0)
604 return 1; /* no class matches */
607 /* check if this patch is relevant according to the vendor. To bad that patches
608 * don't have a vendor, so we need to do some careful repo testing. */
610 solvable_is_irrelevant_patch(Solvable *s, Map *installedmap)
612 Pool *pool = s->repo->pool;
614 int hadpatchpackage = 0;
618 conp = s->repo->idarraydata + s->conflicts;
619 while ((con = *conp++) != 0)
625 rd = GETRELDEP(pool, con);
626 if (rd->flags != REL_LT)
628 FOR_PROVIDES(p, pp, con)
631 if (!MAPTST(installedmap, p))
633 si = pool->solvables + p;
634 if (!pool_match_nevr(pool, si, con))
636 FOR_PROVIDES(p2, pp2, rd->name)
638 Solvable *s2 = pool->solvables + p2;
639 if (!pool_match_nevr(pool, s2, rd->name))
641 if (pool_match_nevr(pool, s2, con))
642 continue; /* does not fulfill patch */
643 if (s2->repo == s->repo)
646 /* ok, we have a package from the patch repo that solves the conflict. check vendor */
647 if (si->vendor == s2->vendor)
649 if (!pool_illegal_vendorchange(pool, si, s2))
651 /* vendor change was illegal, ignore conflict */
656 /* if we didn't find a patchpackage don't claim that the patch is irrelevant */
657 if (!hadpatchpackage)
662 /*****************************************************************************/
665 * Create maps containing the state of each solvable. Input is a "installed" queue,
666 * it contains all solvable ids that are considered to be installed.
668 * The created maps can be used for solvable_trivial_installable_map(),
669 * pool_calc_duchanges(), pool_calc_installsizechange().
673 pool_create_state_maps(Pool *pool, Queue *installed, Map *installedmap, Map *conflictsmap)
680 map_init(installedmap, pool->nsolvables);
682 map_init(conflictsmap, pool->nsolvables);
683 for (i = 0; i < installed->count; i++)
685 p = installed->elements[i];
686 if (p <= 0) /* makes it work with decisionq */
688 MAPSET(installedmap, p);
691 s = pool->solvables + p;
694 conp = s->repo->idarraydata + s->conflicts;
695 while ((con = *conp++) != 0)
697 dp = pool_whatprovides_ptr(pool, con);
699 MAPSET(conflictsmap, *dp);
704 /* Tests if two solvables have identical content. Currently
705 * both solvables need to come from the same pool
708 solvable_identical(Solvable *s1, Solvable *s2)
710 unsigned int bt1, bt2;
714 if (s1->name != s2->name)
716 if (s1->arch != s2->arch)
718 if (s1->evr != s2->evr)
720 /* map missing vendor to empty string */
721 if ((s1->vendor ? s1->vendor : 1) != (s2->vendor ? s2->vendor : 1))
724 /* looking good, try some fancier stuff */
725 /* might also look up the package checksum here */
726 bt1 = solvable_lookup_num(s1, SOLVABLE_BUILDTIME, 0);
727 bt2 = solvable_lookup_num(s2, SOLVABLE_BUILDTIME, 0);
735 /* look at requires in a last attempt to find recompiled packages */
738 for (reqp = s1->repo->idarraydata + s1->requires; *reqp; reqp++)
741 for (reqp = s2->repo->idarraydata + s2->requires; *reqp; reqp++)
749 /* return the self provide dependency of a solvable */
751 solvable_selfprovidedep(Solvable *s)
759 pool = s->repo->pool;
762 provp = s->repo->idarraydata + s->provides;
763 while ((prov = *provp++) != 0)
767 rd = GETRELDEP(pool, prov);
768 if (rd->name == s->name && rd->evr == s->evr && rd->flags == REL_EQ)
772 return pool_rel2id(pool, s->name, s->evr, REL_EQ, 1);
775 /* setter functions, simply call the repo variants */
777 solvable_set_id(Solvable *s, Id keyname, Id id)
779 repo_set_id(s->repo, s - s->repo->pool->solvables, keyname, id);
783 solvable_set_num(Solvable *s, Id keyname, unsigned long long num)
785 repo_set_num(s->repo, s - s->repo->pool->solvables, keyname, num);
789 solvable_set_str(Solvable *s, Id keyname, const char *str)
791 repo_set_str(s->repo, s - s->repo->pool->solvables, keyname, str);
795 solvable_set_poolstr(Solvable *s, Id keyname, const char *str)
797 repo_set_poolstr(s->repo, s - s->repo->pool->solvables, keyname, str);
801 solvable_add_poolstr_array(Solvable *s, Id keyname, const char *str)
803 repo_add_poolstr_array(s->repo, s - s->repo->pool->solvables, keyname, str);
807 solvable_add_idarray(Solvable *s, Id keyname, Id id)
809 repo_add_idarray(s->repo, s - s->repo->pool->solvables, keyname, id);
813 solvable_add_deparray(Solvable *s, Id keyname, Id dep, Id marker)
815 repo_add_deparray(s->repo, s - s->repo->pool->solvables, keyname, dep, marker);
819 solvable_set_idarray(Solvable *s, Id keyname, Queue *q)
821 repo_set_idarray(s->repo, s - s->repo->pool->solvables, keyname, q);
825 solvable_set_deparray(Solvable *s, Id keyname, Queue *q, Id marker)
827 repo_set_deparray(s->repo, s - s->repo->pool->solvables, keyname, q, marker);
831 solvable_unset(Solvable *s, Id keyname)
833 repo_unset(s->repo, s - s->repo->pool->solvables, keyname);