2 * Copyright (c) 2007, Novell Inc.
4 * This program is licensed under the BSD license, read LICENSE.BSD
5 * for further information
11 * The pool contains information about solvables
12 * stored optimized for memory consumption and fast retrieval.
24 #include "poolid_private.h"
30 #define SOLVABLE_BLOCK 255
33 * solvable_kind -> string prefix
37 static const char *kindprefix_data[] = {
38 NULL, NULL, NULL, NULL, NULL,
47 kind_prefix( solvable_kind kind )
49 if (kind >= _KIND_MAX)
51 return kindprefix_data[kind];
56 * list of string constants, so we can do pointer/Id instead of string comparison
57 * index into array matches ID_xxx constants in pool.h
60 static const char *initpool_data[] = {
71 "solvable:recommends",
73 "solvable:supplements",
76 "rpm:dbid", /* direct key into rpmdb */
77 "solvable:prereqmarker",
78 "solvable:filemarker",
79 "namespace:installed",
81 "namespace:splitprovides",
100 pool = (Pool *)sat_calloc(1, sizeof(*pool));
102 stringpool_init (&pool->ss, initpool_data);
104 /* alloc space for ReDep 0 */
105 pool->rels = sat_extend_resize(0, 1, sizeof(Reldep), REL_BLOCK);
107 memset(pool->rels, 0, sizeof(Reldep));
109 /* alloc space for Solvable 0 and system solvable */
110 pool->solvables = sat_extend_resize(0, 2, sizeof(Solvable), SOLVABLE_BLOCK);
111 pool->nsolvables = 2;
112 memset(pool->solvables, 0, 2 * sizeof(Solvable));
113 s = pool->solvables + SYSTEMSOLVABLE;
114 s->name = SYSTEM_SYSTEM;
115 s->arch = ARCH_NOARCH;
118 queue_init(&pool->vendormap);
120 pool->debugmask = SAT_DEBUG_RESULT; /* FIXME */
125 /* free all the resources of our pool */
127 pool_free(Pool *pool)
131 pool_freewhatprovides(pool);
132 pool_freeidhashes(pool);
133 repo_freeallrepos(pool, 1);
134 sat_free(pool->id2arch);
135 sat_free(pool->solvables);
136 sat_free(pool->ss.stringspace);
137 sat_free(pool->ss.strings);
138 sat_free(pool->rels);
139 queue_free(&pool->vendormap);
140 for (i = 0; i < DEP2STRBUF; i++)
141 sat_free(pool->dep2strbuf[i]);
146 pool_add_solvable(Pool *pool)
148 pool->solvables = sat_extend(pool->solvables, pool->nsolvables, 1, sizeof(Solvable), SOLVABLE_BLOCK);
149 memset(pool->solvables + pool->nsolvables, 0, sizeof(Solvable));
150 return pool->nsolvables++;
154 pool_add_solvable_block(Pool *pool, int count)
156 Id nsolvables = pool->nsolvables;
159 pool->solvables = sat_extend(pool->solvables, pool->nsolvables, count, sizeof(Solvable), SOLVABLE_BLOCK);
160 memset(pool->solvables + nsolvables, 0, sizeof(Solvable) * count);
161 pool->nsolvables += count;
166 pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids)
170 if (reuseids && start + count == pool->nsolvables)
172 /* might want to shrink solvable array */
173 pool->nsolvables = start;
176 memset(pool->solvables + start, 0, sizeof(Solvable) * count);
181 solvable2str(Pool *pool, Solvable *s)
183 int l, nn = pool->dep2strn;
184 const char *n, *e, *a;
185 n = id2str(pool, s->name);
186 e = id2str(pool, s->evr);
187 a = id2str(pool, s->arch);
188 l = strlen(n) + strlen(e) + strlen(a) + 3;
189 if (l > pool->dep2strlen[nn])
191 pool->dep2strbuf[nn] = sat_realloc(pool->dep2strbuf[nn], l + 32);
192 pool->dep2strlen[nn] = l + 32;
194 sprintf(pool->dep2strbuf[nn], "%s-%s.%s", n, e, a);
195 pool->dep2strn = (nn + 1) % DEP2STRBUF;
196 return pool->dep2strbuf[nn];
199 static Pool *pool_shrink_whatprovides_sortcmp_data;
202 pool_shrink_whatprovides_sortcmp(const void *ap, const void *bp)
205 Pool *pool = pool_shrink_whatprovides_sortcmp_data;
207 oa = pool->whatprovides[*(Id *)ap];
208 ob = pool->whatprovides[*(Id *)bp];
210 return *(Id *)ap - *(Id *)bp;
215 da = pool->whatprovidesdata + oa;
216 db = pool->whatprovidesdata + ob;
218 if ((r = (*da++ - *db++)) != 0)
222 return *(Id *)ap - *(Id *)bp;
226 * pool_shrink_whatprovides - unify whatprovides data
228 * whatprovides_rel must be empty for this to work!
232 pool_shrink_whatprovides(Pool *pool)
236 Id lastid, *last, *dp, *lp;
240 if (pool->ss.nstrings < 3)
242 sorted = sat_malloc2(pool->ss.nstrings, sizeof(Id));
243 for (id = 0; id < pool->ss.nstrings; id++)
245 pool_shrink_whatprovides_sortcmp_data = pool;
246 qsort(sorted + 1, pool->ss.nstrings - 1, sizeof(Id), pool_shrink_whatprovides_sortcmp);
249 for (i = 1; i < pool->ss.nstrings; i++)
252 o = pool->whatprovides[id];
253 if (o == 0 || o == 1)
255 dp = pool->whatprovidesdata + o;
269 pool->whatprovides[id] = -lastid;
273 last = pool->whatprovidesdata + o;
277 dp = pool->whatprovidesdata + 2;
278 for (id = 1; id < pool->ss.nstrings; id++)
280 o = pool->whatprovides[id];
281 if (o == 0 || o == 1)
288 pool->whatprovides[id] = pool->whatprovides[i];
291 lp = pool->whatprovidesdata + o;
294 pool->whatprovides[id] = dp - pool->whatprovidesdata;
295 while ((*dp++ = *lp++) != 0)
298 o = dp - pool->whatprovidesdata;
299 POOL_DEBUG(SAT_DEBUG_STATS, "shrunk whatprovidesdata from %d to %d\n", pool->whatprovidesdataoff, o);
300 if (pool->whatprovidesdataoff == o)
302 r = pool->whatprovidesdataoff - o;
303 pool->whatprovidesdataoff = o;
304 pool->whatprovidesdata = sat_realloc(pool->whatprovidesdata, (o + pool->whatprovidesdataleft) * sizeof(Id));
305 if (r > pool->whatprovidesdataleft)
306 r = pool->whatprovidesdataleft;
307 memset(pool->whatprovidesdata + o, 0, r * sizeof(Id));
312 * pool_createwhatprovides()
314 * create hashes over pool of solvables to ease provide lookups
318 pool_createwhatprovides(Pool *pool)
320 int i, num, np, extra;
325 Offset *whatprovides;
326 Id *whatprovidesdata, *d;
328 POOL_DEBUG(SAT_DEBUG_STATS, "number of solvables: %d\n", pool->nsolvables);
329 POOL_DEBUG(SAT_DEBUG_STATS, "number of ids: %d + %d\n", pool->ss.nstrings, pool->nrels);
331 pool_freeidhashes(pool); /* XXX: should not be here! */
332 pool_freewhatprovides(pool);
333 num = pool->ss.nstrings;
334 pool->whatprovides = whatprovides = sat_extend_resize(0, num, sizeof(Offset), WHATPROVIDES_BLOCK);
335 memset(whatprovides, 0, num * sizeof(Offset));
336 pool->whatprovides_rel = sat_extend_resize(0, pool->nrels, sizeof(Offset), WHATPROVIDES_BLOCK);
337 memset(pool->whatprovides_rel, 0, pool->nrels * sizeof(Offset));
339 /* count providers for each name */
340 for (i = 1; i < pool->nsolvables; i++)
343 s = pool->solvables + i;
346 if (!pool_installable(pool, s))
348 pp = s->repo->idarraydata + s->provides;
349 while ((id = *pp++) != ID_NULL)
353 Reldep *rd = GETRELDEP(pool, id);
356 whatprovides[id]++; /* inc count of providers */
360 off = 2; /* first entry is undef, second is empty list */
362 np = 0; /* number of names provided */
363 for (i = 0; i < num; i++, idp++)
366 if (!n) /* no providers */
368 *idp = off; /* move from counts to offsets into whatprovidesdata */
369 off += n + 1; /* make space for all providers + terminating ID_NULL */
370 np++; /* inc # of provider 'slots' */
373 POOL_DEBUG(SAT_DEBUG_STATS, "provide ids: %d\n", np);
375 /* reserve some space for relation data */
376 extra = 2 * pool->nrels;
380 POOL_DEBUG(SAT_DEBUG_STATS, "provide space needed: %d + %d\n", off, extra);
382 /* alloc space for all providers + extra */
383 whatprovidesdata = sat_calloc(off + extra, sizeof(Id));
385 /* now fill data for all provides */
386 for (i = 1; i < pool->nsolvables; i++)
389 s = pool->solvables + i;
392 if (!pool_installable(pool, s))
395 /* for all provides of this solvable */
396 pp = s->repo->idarraydata + s->provides;
397 while ((id = *pp++) != 0)
401 Reldep *rd = GETRELDEP(pool, id);
404 d = whatprovidesdata + whatprovides[id]; /* offset into whatprovidesdata */
408 while (*d) /* find free slot */
413 *d = i; /* put solvable Id into data */
416 pool->whatprovidesdata = whatprovidesdata;
417 pool->whatprovidesdataoff = off;
418 pool->whatprovidesdataleft = extra;
419 pool_shrink_whatprovides(pool);
423 * free all of our whatprovides data
424 * be careful, everything internalized with pool_queuetowhatprovides is gone, too
427 pool_freewhatprovides(Pool *pool)
429 pool->whatprovides = sat_free(pool->whatprovides);
430 pool->whatprovides_rel = sat_free(pool->whatprovides_rel);
431 pool->whatprovidesdata = sat_free(pool->whatprovidesdata);
432 pool->whatprovidesdataoff = 0;
433 pool->whatprovidesdataleft = 0;
437 /******************************************************************************/
440 * pool_queuetowhatprovides - add queue contents to whatprovidesdata
442 * on-demand filling of provider information
443 * move queue data into whatprovidesdata
445 * returns: Offset into whatprovides
449 pool_queuetowhatprovides(Pool *pool, Queue *q)
452 int count = q->count;
454 if (count == 0) /* queue empty -> ID_EMPTY */
457 /* extend whatprovidesdata if needed, +1 for ID_NULL-termination */
458 if (pool->whatprovidesdataleft < count + 1)
460 POOL_DEBUG(SAT_DEBUG_STATS, "growing provides hash data...\n");
461 pool->whatprovidesdata = sat_realloc(pool->whatprovidesdata, (pool->whatprovidesdataoff + count + 4096) * sizeof(Id));
462 pool->whatprovidesdataleft = count + 4096;
465 /* copy queue to next free slot */
466 off = pool->whatprovidesdataoff;
467 memcpy(pool->whatprovidesdata + pool->whatprovidesdataoff, q->elements, count * sizeof(Id));
469 /* adapt count and ID_NULL-terminate */
470 pool->whatprovidesdataoff += count;
471 pool->whatprovidesdata[pool->whatprovidesdataoff++] = ID_NULL;
472 pool->whatprovidesdataleft -= count + 1;
478 /*************************************************************************/
483 * add packages fulfilling the relation to whatprovides array
484 * no exact providers, do range match
489 pool_addrelproviders(Pool *pool, Id d)
491 Reldep *rd = GETRELDEP(pool, d);
497 int flags = rd->flags;
499 Id p, *pp, *pp2, *pp3;
502 queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
507 pp = pool_whatprovides(pool, name);
508 pp2 = pool_whatprovides(pool, evr);
509 while ((p = *pp++) != 0)
511 for (pp3 = pp2; *pp3;)
514 queue_push(&plist, p);
520 pp = pool_whatprovides(pool, name);
521 while ((p = *pp++) != 0)
522 queue_push(&plist, p);
523 pp = pool_whatprovides(pool, evr);
524 while ((p = *pp++) != 0)
525 queue_pushunique(&plist, p);
528 if (pool->nscallback)
530 p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
534 pool->whatprovides_rel[d] = p;
535 return pool->whatprovidesdata + p;
538 queue_push(&plist, SYSTEMSOLVABLE);
545 /* convert to whatprovides id */
547 POOL_DEBUG(DEBUG_1, "addrelproviders: what provides %s?\n", id2str(pool, name));
549 if (flags && flags < 8)
551 FOR_PROVIDES(p, pp, name)
554 POOL_DEBUG(DEBUG_1, "addrelproviders: checking package %s\n", id2str(pool, pool->p[p].name));
556 /* solvable p provides name in some rels */
557 pidp = pool->solvables[p].repo->idarraydata + pool->solvables[p].provides;
558 while ((pid = *pidp++) != 0)
565 #ifdef DEBIAN_SEMANTICS
566 continue; /* unversioned provides can
567 * never match versioned deps */
569 break; /* yes, provides all versions */
573 continue; /* wrong provides name */
574 prd = GETRELDEP(pool, pid);
575 if (prd->name != name)
576 continue; /* wrong provides name */
577 /* right package, both deps are rels */
581 if (flags == 7 || pflags == 7)
582 break; /* included */
583 if ((pflags & flags & 5) != 0)
584 break; /* same direction, match */
588 if ((pflags & flags & 2) != 0)
589 break; /* both have =, match */
593 int f = flags == 5 ? 5 : flags == 2 ? pflags : (flags ^ 5) & (pflags | 5);
594 if ((f & (1 << (1 + evrcmp(pool, pevr, evr, EVRCMP_MATCH_RELEASE)))) != 0)
599 continue; /* no rel match */
600 queue_push(&plist, p);
602 /* make our system solvable provide all unknown rpmlib() stuff */
603 if (plist.count == 0 && !strncmp(id2str(pool, name), "rpmlib(", 7))
604 queue_push(&plist, SYSTEMSOLVABLE);
606 /* add providers to whatprovides */
608 POOL_DEBUG(DEBUG_1, "addrelproviders: adding %d packages to %d\n", plist.count, d);
610 pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
613 return pool->whatprovidesdata + pool->whatprovides_rel[d];
616 /*************************************************************************/
619 pool_debug(Pool *pool, int type, const char *format, ...)
624 if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
626 if ((pool->debugmask & type) == 0)
629 va_start(args, format);
630 if (!pool->debugcallback)
632 if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
633 vprintf(format, args);
635 vfprintf(stderr, format, args);
638 vsnprintf(buf, sizeof(buf), format, args);
639 pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
643 pool_setdebuglevel(Pool *pool, int level)
645 int mask = SAT_DEBUG_RESULT;
647 mask |= SAT_DEBUG_STATS|SAT_DEBUG_ANALYZE|SAT_DEBUG_UNSOLVABLE;
649 mask |= SAT_DEBUG_JOB|SAT_DEBUG_SOLUTIONS|SAT_DEBUG_POLICY;
651 mask |= SAT_DEBUG_PROPAGATE;
653 mask |= SAT_DEBUG_RULE_CREATION;
655 mask |= SAT_DEBUG_SCHUBI;
656 pool->debugmask = mask;
659 /*************************************************************************/
667 #define SEARCHFILES_BLOCK 127
670 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
675 while ((dep = *ida++) != 0)
677 while (ISRELDEP(dep))
680 sid = pool->ss.nstrings + GETRELID(dep);
681 if (MAPTST(&sf->seen, sid))
686 MAPSET(&sf->seen, sid);
687 rd = GETRELDEP(pool, dep);
690 else if (rd->flags == REL_NAMESPACE)
692 if (isf && (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES))
696 if (MAPTST(&sf->seen, sid))
701 MAPSET(&sf->seen, sid);
710 pool_addfileprovides_dep(pool, ids, sf, isf);
716 if (MAPTST(&sf->seen, dep))
718 MAPSET(&sf->seen, dep);
719 s = id2str(pool, dep);
722 sf->files = sat_extend(sf->files, sf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
723 sf->files[sf->nfiles++] = strdup(s);
729 addfileprovides_cb(void *data, Solvable *s, Id key, const char *str)
731 Pool *pool = s->repo->pool;
733 id = str2id(pool, str, 0);
735 return 0; /* can't happen */
736 s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
742 pool_addfileprovides(Pool *pool, Repo *installed)
746 struct searchfiles sf, isf;
749 memset(&sf, 0, sizeof(sf));
750 map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
751 memset(&isf, 0, sizeof(isf));
752 map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
754 for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
760 pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, &isf);
762 pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, &isf);
764 pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, &isf);
766 pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, &isf);
768 pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, &isf);
770 pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, &isf);
772 pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, &isf);
774 pool_addfileprovides_dep(pool, repo->idarraydata + s->freshens, &sf, &isf);
778 POOL_DEBUG(SAT_DEBUG_STATS, "found %d file dependencies\n", sf.nfiles);
779 POOL_DEBUG(SAT_DEBUG_STATS, "found %d installed file dependencies\n", isf.nfiles);
783 for (i = 0; i < sf.nfiles; i++)
784 POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in filelist\n", sf.files[i]);
786 sf.files = sat_extend(sf.files, sf.nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
787 sf.files[sf.nfiles++] = 0;
789 pool_search(0, SOLVABLE_FILELIST, (const char *)sf.files, SEARCH_STRING|SEARCH_MULTIPLE, addfileprovides_cb, 0);
793 if (isf.nfiles && installed)
796 for (i = 0; i < isf.nfiles; i++)
797 POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in installed filelist\n", isf.files[i]);
799 isf.files = sat_extend(isf.files, isf.nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
800 isf.files[isf.nfiles++] = 0;
802 repo_search(installed, 0, SOLVABLE_FILELIST, (const char *)isf.files, SEARCH_STRING|SEARCH_MULTIPLE, addfileprovides_cb, 0);
806 pool_freewhatprovides(pool); /* as we have added provides */
826 struct mountpoint *mps;
832 pool_fill_DU_add_cb(void *data, Solvable *s, Id key, const char *str)
834 struct cbdata *cbdata = data;
835 Id mp, dirnum, kbytes, files;
837 dp = data_read_id(dp, &dirnum);
838 dp = data_read_id(dp, &kbytes);
839 data_read_id(dp, &files);
840 if (dirnum < 0 || dirnum > cbdata->nmap)
842 mp = cbdata->dirmap[dirnum];
845 cbdata->mps[mp].kbytes += kbytes;
846 cbdata->mps[mp].files += files;
852 pool_fill_DU_sub_cb(void *data, Solvable *s, Id key, const char *str)
854 struct cbdata *cbdata = data;
855 Id mp, dirnum, kbytes, files;
857 dp = data_read_id(dp, &dirnum);
858 dp = data_read_id(dp, &kbytes);
859 data_read_id(dp, &files);
860 if (dirnum < 0 || dirnum > cbdata->nmap)
862 mp = cbdata->dirmap[dirnum];
865 cbdata->mps[mp].kbytes -= kbytes;
866 cbdata->mps[mp].files -= files;
872 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
875 if (mptree[pos].mountpoint == -1)
876 mptree[pos].mountpoint = mountpoint;
878 mountpoint = mptree[pos].mountpoint;
879 for (i = mptree[pos].child; i; i = mptree[i].sibling)
880 propagate_mountpoints(mptree, i, mountpoint);
884 pool_fill_DU(Pool *pool, struct mountpoint *mps, int nmps)
888 struct mptree *mptree;
894 struct cbdata cbdata;
896 memset(&md, 0, sizeof(md));
901 md.callback_data = &cbdata
907 mptree = sat_malloc2(16, sizeof(mptree));
910 mptree[0].sibling = 0;
914 mptree[0].mountpoint = -1;
917 /* create component tree */
918 for (mp = 0; mp < nmps; mp++)
926 if ((p = strchr('/', path)) == 0)
929 compl = strlen(comp);
940 for (i = mptree[pos].child; i; i = mptree[i].sibling)
941 if (mptree[i].compl == compl && !strncmp(mptree[i].comp, comp, compl))
945 /* create new node */
946 if ((nmptree & 15) == 0)
947 mptree = sat_realloc2(mptree, nmptree + 16, sizeof(mptree));
949 mptree[i].sibling = mptree[pos].child;
951 mptree[i].comp = comp;
952 mptree[i].compl = compl;
953 mptree[i].mountpoint = -1;
954 mptree[pos].child = i;
958 mptree[pos].mountpoint = mp;
960 propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
964 for_all_repodatas_containing_DU
966 /* create map from dir to mptree */
967 dirmap = xcalloc2(pool->ndirs, sizeof(Id));
969 for (dn = 2, dirs = pool->dirs + dn; dn < pool->ndirs; dn++)
983 if (!mptree[mp].child)
988 comp = id2str(pool, id);
989 compl = strlen(comp);
990 for (i = mptree[mp].child; i; i = mptree[i].sibling)
991 if (mptree[i].compl == compl && !strncmp(mptree[i].comp, comp, compl))
993 dirmap[dn] = i ? i : -mp;
995 /* change dirmap to point to mountpoint instead of mptree */
996 for (dn = 0; dn < pool->ndirs; dn++)
999 dirmap[i] = mptree[mp > 0 ? mp : -mp].mountpoint;
1002 cbdata.nmap = pool->ndirs;
1003 cbdata.dirmap = dirmap;
1005 md.callback = pool_fill_DU_add_cb;
1006 for_solvables_to_be_installed()
1008 if (p < data->start || p >= data->end)
1010 repodata_search(data, p - data->start, SOLVABLE_DUDATA, &md);
1012 md.callback = pool_fill_DU_sub_cb;
1013 for_solvables_to_be_erased()
1015 if (p < data->start || p >= data->end)
1017 repodata_search(data, p - data->start, SOLVABLE_DUDATA, &md);