bring libsolv in line with currrent rpm's multiversion handling
[platform/upstream/libsolv.git] / src / pool.c
1 /*
2  * Copyright (c) 2007-2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 /*
9  * pool.c
10  * 
11  * The pool contains information about solvables
12  * stored optimized for memory consumption and fast retrieval.
13  */
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <stdarg.h>
18 #include <unistd.h>
19 #include <string.h>
20
21 #include "pool.h"
22 #include "repo.h"
23 #include "poolid.h"
24 #include "poolid_private.h"
25 #include "poolarch.h"
26 #include "util.h"
27 #include "bitmap.h"
28 #include "evr.h"
29
30 #define SOLVABLE_BLOCK  255
31
32 #define KNOWNID_INITIALIZE
33 #include "knownid.h"
34 #undef KNOWNID_INITIALIZE
35
36 /* create pool */
37 Pool *
38 pool_create(void)
39 {
40   Pool *pool;
41   Solvable *s;
42
43   pool = (Pool *)solv_calloc(1, sizeof(*pool));
44
45   stringpool_init (&pool->ss, initpool_data);
46
47   /* alloc space for RelDep 0 */
48   pool->rels = solv_extend_resize(0, 1, sizeof(Reldep), REL_BLOCK);
49   pool->nrels = 1;
50   memset(pool->rels, 0, sizeof(Reldep));
51
52   /* alloc space for Solvable 0 and system solvable */
53   pool->solvables = solv_extend_resize(0, 2, sizeof(Solvable), SOLVABLE_BLOCK);
54   pool->nsolvables = 2;
55   memset(pool->solvables, 0, 2 * sizeof(Solvable));
56
57   queue_init(&pool->vendormap);
58
59 #if defined(DEBIAN)
60   pool->disttype = DISTTYPE_DEB;
61   pool->noarchid = ARCH_ALL;
62 #elif defined(ARCHLINUX)
63   pool->disttype = DISTTYPE_ARCH;
64   pool->noarchid = ARCH_ANY;
65 #elif defined(HAIKU)
66   pool->disttype = DISTTYPE_HAIKU;
67   pool->noarchid = ARCH_ANY;
68   pool->obsoleteusesprovides = 1;
69 #else
70   pool->disttype = DISTTYPE_RPM;
71   pool->noarchid = ARCH_NOARCH;
72 #endif
73
74   /* initialize the system solvable */
75   s = pool->solvables + SYSTEMSOLVABLE;
76   s->name = SYSTEM_SYSTEM;
77   s->arch = pool->noarchid;
78   s->evr = ID_EMPTY;
79
80   pool->debugmask = SOLV_DEBUG_RESULT;  /* FIXME */
81 #ifdef FEDORA
82   pool->obsoleteusescolors = 1;
83 #endif
84 #ifdef RPM5
85   pool->noobsoletesmultiversion = 1;
86   pool->forbidselfconflicts = 1;
87   pool->obsoleteusesprovides = 1;
88   pool->implicitobsoleteusesprovides = 1;
89   pool->havedistepoch = 1;
90 #endif
91   return pool;
92 }
93
94
95 /* free all the resources of our pool */
96 void
97 pool_free(Pool *pool)
98 {
99   int i;
100
101   pool_freewhatprovides(pool);
102   pool_freeidhashes(pool);
103   pool_freeallrepos(pool, 1);
104   solv_free(pool->id2arch);
105   solv_free(pool->id2color);
106   solv_free(pool->solvables);
107   stringpool_free(&pool->ss);
108   solv_free(pool->rels);
109   pool_setvendorclasses(pool, 0);
110   queue_free(&pool->vendormap);
111   queue_free(&pool->pooljobs);
112   for (i = 0; i < POOL_TMPSPACEBUF; i++)
113     solv_free(pool->tmpspace.buf[i]);
114   for (i = 0; i < pool->nlanguages; i++)
115     free((char *)pool->languages[i]);
116   solv_free(pool->languages);
117   solv_free(pool->languagecache);
118   solv_free(pool->errstr);
119   solv_free(pool->rootdir);
120   solv_free(pool);
121 }
122
123 void
124 pool_freeallrepos(Pool *pool, int reuseids)
125 {
126   int i;
127
128   pool_freewhatprovides(pool);
129   for (i = 1; i < pool->nrepos; i++) 
130     if (pool->repos[i])
131       repo_freedata(pool->repos[i]);
132   pool->repos = solv_free(pool->repos);
133   pool->nrepos = 0; 
134   pool->urepos = 0; 
135   /* the first two solvables don't belong to a repo */
136   pool_free_solvable_block(pool, 2, pool->nsolvables - 2, reuseids);
137 }
138
139 #ifdef MULTI_SEMANTICS
140 void
141 pool_setdisttype(Pool *pool, int disttype)
142 {
143   pool->disttype = disttype;
144   if (disttype == DISTTYPE_RPM)
145     pool->noarchid = ARCH_NOARCH;
146   if (disttype == DISTTYPE_DEB)
147     pool->noarchid = ARCH_ALL;
148   if (disttype == DISTTYPE_ARCH)
149     pool->noarchid = ARCH_ANY;
150   if (disttype == DISTTYPE_HAIKU)
151     pool->noarchid = ARCH_ANY;
152   pool->solvables[SYSTEMSOLVABLE].arch = pool->noarchid;
153 }
154 #endif
155
156 int
157 pool_get_flag(Pool *pool, int flag)
158 {
159   switch (flag)
160     {
161     case POOL_FLAG_PROMOTEEPOCH:
162       return pool->promoteepoch;
163     case POOL_FLAG_FORBIDSELFCONFLICTS:
164       return pool->forbidselfconflicts;
165     case POOL_FLAG_OBSOLETEUSESPROVIDES:
166       return pool->obsoleteusesprovides;
167     case POOL_FLAG_IMPLICITOBSOLETEUSESPROVIDES:
168       return pool->implicitobsoleteusesprovides;
169     case POOL_FLAG_OBSOLETEUSESCOLORS:
170       return pool->obsoleteusescolors;
171     case POOL_FLAG_NOINSTALLEDOBSOLETES:
172       return pool->noinstalledobsoletes;
173     case POOL_FLAG_HAVEDISTEPOCH:
174       return pool->havedistepoch;
175     case POOL_FLAG_NOOBSOLETESMULTIVERSION:
176       return pool->noobsoletesmultiversion;
177     default:
178       break;
179     }
180   return -1;
181 }
182
183 int
184 pool_set_flag(Pool *pool, int flag, int value)
185 {
186   int old = pool_get_flag(pool, flag);
187   switch (flag)
188     {
189     case POOL_FLAG_PROMOTEEPOCH:
190       pool->promoteepoch = value;
191       break;
192     case POOL_FLAG_FORBIDSELFCONFLICTS:
193       pool->forbidselfconflicts = value;
194       break;
195     case POOL_FLAG_OBSOLETEUSESPROVIDES:
196       pool->obsoleteusesprovides = value;
197       break;
198     case POOL_FLAG_IMPLICITOBSOLETEUSESPROVIDES:
199       pool->implicitobsoleteusesprovides = value;
200       break;
201     case POOL_FLAG_OBSOLETEUSESCOLORS:
202       pool->obsoleteusescolors = value;
203       break;
204     case POOL_FLAG_NOINSTALLEDOBSOLETES:
205       pool->noinstalledobsoletes = value;
206       break;
207     case POOL_FLAG_HAVEDISTEPOCH:
208       pool->havedistepoch = value;
209       break;
210     case POOL_FLAG_NOOBSOLETESMULTIVERSION:
211       pool->noobsoletesmultiversion = value;
212       break;
213     default:
214       break;
215     }
216   return old;
217 }
218
219
220 Id
221 pool_add_solvable(Pool *pool)
222 {
223   pool->solvables = solv_extend(pool->solvables, pool->nsolvables, 1, sizeof(Solvable), SOLVABLE_BLOCK);
224   memset(pool->solvables + pool->nsolvables, 0, sizeof(Solvable));
225   return pool->nsolvables++;
226 }
227
228 Id
229 pool_add_solvable_block(Pool *pool, int count)
230 {
231   Id nsolvables = pool->nsolvables;
232   if (!count)
233     return nsolvables;
234   pool->solvables = solv_extend(pool->solvables, pool->nsolvables, count, sizeof(Solvable), SOLVABLE_BLOCK);
235   memset(pool->solvables + nsolvables, 0, sizeof(Solvable) * count);
236   pool->nsolvables += count;
237   return nsolvables;
238 }
239
240 void
241 pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids)
242 {
243   if (!count)
244     return;
245   if (reuseids && start + count == pool->nsolvables)
246     {
247       /* might want to shrink solvable array */
248       pool->nsolvables = start;
249       return;
250     }
251   memset(pool->solvables + start, 0, sizeof(Solvable) * count);
252 }
253
254
255 void
256 pool_set_installed(Pool *pool, Repo *installed)
257 {
258   if (pool->installed == installed)
259     return;
260   pool->installed = installed;
261   pool_freewhatprovides(pool);
262 }
263
264 static int
265 pool_shrink_whatprovides_sortcmp(const void *ap, const void *bp, void *dp)
266 {
267   int r;
268   Pool *pool = dp;
269   Id oa, ob, *da, *db;
270   oa = pool->whatprovides[*(Id *)ap];
271   ob = pool->whatprovides[*(Id *)bp];
272   if (oa == ob)
273     return *(Id *)ap - *(Id *)bp;
274   da = pool->whatprovidesdata + oa;
275   db = pool->whatprovidesdata + ob;
276   while (*db)
277     if ((r = (*da++ - *db++)) != 0)
278       return r;
279   if (*da)
280     return *da;
281   return *(Id *)ap - *(Id *)bp;
282 }
283
284 /*
285  * pool_shrink_whatprovides  - unify whatprovides data
286  *
287  * whatprovides_rel must be empty for this to work!
288  *
289  */
290 static void
291 pool_shrink_whatprovides(Pool *pool)
292 {
293   Id i, n, id;
294   Id *sorted;
295   Id lastid, *last, *dp, *lp;
296   Offset o;
297   int r;
298
299   if (pool->ss.nstrings < 3)
300     return;
301   sorted = solv_malloc2(pool->ss.nstrings, sizeof(Id));
302   for (i = id = 0; id < pool->ss.nstrings; id++)
303     if (pool->whatprovides[id] >= 4)
304       sorted[i++] = id;
305   n = i;
306   solv_sort(sorted, n, sizeof(Id), pool_shrink_whatprovides_sortcmp, pool);
307   last = 0;
308   lastid = 0;
309   for (i = 0; i < n; i++)
310     {
311       id = sorted[i];
312       o = pool->whatprovides[id];
313       dp = pool->whatprovidesdata + o;
314       if (last)
315         {
316           lp = last;
317           while (*dp)   
318             if (*dp++ != *lp++)
319               {
320                 last = 0;
321                 break;
322               }
323           if (last && *lp)
324             last = 0;
325           if (last)
326             {
327               pool->whatprovides[id] = -lastid;
328               continue;
329             }
330         }
331       last = pool->whatprovidesdata + o;
332       lastid = id;
333     }
334   solv_free(sorted);
335   dp = pool->whatprovidesdata + 4;
336   for (id = 1; id < pool->ss.nstrings; id++)
337     {
338       o = pool->whatprovides[id];
339       if (!o)
340         continue;
341       if ((Id)o < 0)
342         {
343           i = -(Id)o;
344           if (i >= id)
345             abort();
346           pool->whatprovides[id] = pool->whatprovides[i];
347           continue;
348         }
349       if (o < 4)
350         continue;
351       lp = pool->whatprovidesdata + o;
352       if (lp < dp)
353         abort();
354       pool->whatprovides[id] = dp - pool->whatprovidesdata;
355       while ((*dp++ = *lp++) != 0)
356         ;
357     }
358   o = dp - pool->whatprovidesdata;
359   POOL_DEBUG(SOLV_DEBUG_STATS, "shrunk whatprovidesdata from %d to %d\n", pool->whatprovidesdataoff, o);
360   if (pool->whatprovidesdataoff == o)
361     return;
362   r = pool->whatprovidesdataoff - o;
363   pool->whatprovidesdataoff = o;
364   pool->whatprovidesdata = solv_realloc(pool->whatprovidesdata, (o + pool->whatprovidesdataleft) * sizeof(Id));
365   if (r > pool->whatprovidesdataleft)
366     r = pool->whatprovidesdataleft;
367   memset(pool->whatprovidesdata + o, 0, r * sizeof(Id));
368 }
369
370
371 /*
372  * pool_createwhatprovides()
373  * 
374  * create hashes over pool of solvables to ease provide lookups
375  * 
376  */
377 void
378 pool_createwhatprovides(Pool *pool)
379 {
380   int i, num, np, extra;
381   Offset off;
382   Solvable *s;
383   Id id;
384   Offset *idp, n;
385   Offset *whatprovides;
386   Id *whatprovidesdata, *d;
387   Repo *installed = pool->installed;
388   unsigned int now;
389
390   now = solv_timems(0);
391   POOL_DEBUG(SOLV_DEBUG_STATS, "number of solvables: %d, memory used: %d K\n", pool->nsolvables, pool->nsolvables * (int)sizeof(Solvable) / 1024);
392   POOL_DEBUG(SOLV_DEBUG_STATS, "number of ids: %d + %d\n", pool->ss.nstrings, pool->nrels);
393   POOL_DEBUG(SOLV_DEBUG_STATS, "string memory used: %d K array + %d K data,  rel memory used: %d K array\n", pool->ss.nstrings / (1024 / (int)sizeof(Id)), pool->ss.sstrings / 1024, pool->nrels * (int)sizeof(Reldep) / 1024);
394   if (pool->ss.stringhashmask || pool->relhashmask)
395     POOL_DEBUG(SOLV_DEBUG_STATS, "string hash memory: %d K, rel hash memory : %d K\n", (pool->ss.stringhashmask + 1) / (int)(1024/sizeof(Id)), (pool->relhashmask + 1) / (int)(1024/sizeof(Id)));
396
397   pool_freeidhashes(pool);      /* XXX: should not be here! */
398   pool_freewhatprovides(pool);
399   num = pool->ss.nstrings;
400   pool->whatprovides = whatprovides = solv_calloc_block(num, sizeof(Offset), WHATPROVIDES_BLOCK);
401   pool->whatprovides_rel = solv_calloc_block(pool->nrels, sizeof(Offset), WHATPROVIDES_BLOCK);
402
403   /* count providers for each name */
404   for (i = pool->nsolvables - 1; i > 0; i--)
405     {
406       Id *pp;
407       s = pool->solvables + i;
408       if (!s->provides || !s->repo || s->repo->disabled)
409         continue;
410       /* we always need the installed solvable in the whatprovides data,
411          otherwise obsoletes/conflicts on them won't work */
412       if (s->repo != installed && !pool_installable(pool, s))
413         continue;
414       pp = s->repo->idarraydata + s->provides;
415       while ((id = *pp++) != 0)
416         {
417           while (ISRELDEP(id))
418             {
419               Reldep *rd = GETRELDEP(pool, id);
420               id = rd->name;
421             }
422           whatprovides[id]++;          /* inc count of providers */
423         }
424     }
425
426   off = 4;      /* first entry is undef, second is empty list, third is system solvable  */
427   np = 0;                              /* number of names provided */
428   for (i = 0, idp = whatprovides; i < num; i++, idp++)
429     {
430       n = *idp;
431       if (!n)                          /* no providers */
432         continue;
433       off += n;                        /* make space for all providers */
434       *idp = off++;                    /* now idp points to terminating zero */
435       np++;                            /* inc # of provider 'slots' for stats */
436     }
437
438   POOL_DEBUG(SOLV_DEBUG_STATS, "provide ids: %d\n", np);
439
440   /* reserve some space for relation data */
441   extra = 2 * pool->nrels;
442   if (extra < 256)
443     extra = 256;
444
445   POOL_DEBUG(SOLV_DEBUG_STATS, "provide space needed: %d + %d\n", off, extra);
446
447   /* alloc space for all providers + extra */
448   whatprovidesdata = solv_calloc(off + extra, sizeof(Id));
449   whatprovidesdata[2] = SYSTEMSOLVABLE;
450
451   /* now fill data for all provides */
452   for (i = pool->nsolvables - 1; i > 0; i--)
453     {
454       Id *pp;
455       s = pool->solvables + i;
456       if (!s->provides || !s->repo || s->repo->disabled)
457         continue;
458       if (s->repo != installed && !pool_installable(pool, s))
459         continue;
460
461       /* for all provides of this solvable */
462       pp = s->repo->idarraydata + s->provides;
463       while ((id = *pp++) != 0)
464         {
465           while (ISRELDEP(id))
466             {
467               Reldep *rd = GETRELDEP(pool, id);
468               id = rd->name;
469             }
470           d = whatprovidesdata + whatprovides[id];   /* offset into whatprovidesdata */
471           if (*d != i)          /* don't add same solvable twice */
472             {
473               d[-1] = i;
474               whatprovides[id]--;
475             }
476         }
477     }
478   pool->whatprovidesdata = whatprovidesdata;
479   pool->whatprovidesdataoff = off;
480   pool->whatprovidesdataleft = extra;
481   pool_shrink_whatprovides(pool);
482   POOL_DEBUG(SOLV_DEBUG_STATS, "whatprovides memory used: %d K id array, %d K data\n", (pool->ss.nstrings + pool->nrels + WHATPROVIDES_BLOCK) / (int)(1024/sizeof(Id)), (pool->whatprovidesdataoff + pool->whatprovidesdataleft) / (int)(1024/sizeof(Id)));
483   POOL_DEBUG(SOLV_DEBUG_STATS, "createwhatprovides took %d ms\n", solv_timems(now));
484 }
485
486 /*
487  * free all of our whatprovides data
488  * be careful, everything internalized with pool_queuetowhatprovides is
489  * gone, too
490  */
491 void
492 pool_freewhatprovides(Pool *pool)
493 {
494   pool->whatprovides = solv_free(pool->whatprovides);
495   pool->whatprovides_rel = solv_free(pool->whatprovides_rel);
496   pool->whatprovidesdata = solv_free(pool->whatprovidesdata);
497   pool->whatprovidesdataoff = 0;
498   pool->whatprovidesdataleft = 0;
499 }
500
501
502 /******************************************************************************/
503
504 /*
505  * pool_queuetowhatprovides  - add queue contents to whatprovidesdata
506  * 
507  * used for whatprovides, jobs, learnt rules, selections
508  * input: q: queue of Ids
509  * returns: Offset into whatprovidesdata
510  *
511  */
512 Id
513 pool_queuetowhatprovides(Pool *pool, Queue *q)
514 {
515   Offset off;
516   int count = q->count;
517
518   if (count == 0)                      /* queue empty -> 1 */
519     return 1;
520   if (count == 1 && q->elements[0] == SYSTEMSOLVABLE)
521     return 2;
522
523   /* extend whatprovidesdata if needed, +1 for 0-termination */
524   if (pool->whatprovidesdataleft < count + 1)
525     {
526       POOL_DEBUG(SOLV_DEBUG_STATS, "growing provides hash data...\n");
527       pool->whatprovidesdata = solv_realloc(pool->whatprovidesdata, (pool->whatprovidesdataoff + count + 4096) * sizeof(Id));
528       pool->whatprovidesdataleft = count + 4096;
529     }
530
531   /* copy queue to next free slot */
532   off = pool->whatprovidesdataoff;
533   memcpy(pool->whatprovidesdata + pool->whatprovidesdataoff, q->elements, count * sizeof(Id));
534
535   /* adapt count and 0-terminate */
536   pool->whatprovidesdataoff += count;
537   pool->whatprovidesdata[pool->whatprovidesdataoff++] = 0;
538   pool->whatprovidesdataleft -= count + 1;
539
540   return (Id)off;
541 }
542
543
544 /*************************************************************************/
545
546 #if defined(MULTI_SEMANTICS)
547 # define EVRCMP_DEPCMP (pool->disttype == DISTTYPE_DEB ? EVRCMP_COMPARE : EVRCMP_MATCH_RELEASE)
548 #elif defined(DEBIAN)
549 # define EVRCMP_DEPCMP EVRCMP_COMPARE
550 #else
551 # define EVRCMP_DEPCMP EVRCMP_MATCH_RELEASE
552 #endif
553
554 /* check if a package's nevr matches a dependency */
555
556 int
557 pool_match_nevr_rel(Pool *pool, Solvable *s, Id d)
558 {
559   Reldep *rd = GETRELDEP(pool, d);
560   Id name = rd->name;
561   Id evr = rd->evr;
562   int flags = rd->flags;
563
564   if (flags > 7)
565     {
566       switch (flags)
567         {
568         case REL_ARCH:
569           if (s->arch != evr)
570             {
571               if (evr != ARCH_SRC || s->arch != ARCH_NOSRC)
572                 return 0;
573             }
574           return pool_match_nevr(pool, s, name);
575         case REL_OR:
576           if (pool_match_nevr(pool, s, name))
577             return 1;
578           return pool_match_nevr(pool, s, evr);
579         case REL_AND:
580         case REL_WITH:
581           if (!pool_match_nevr(pool, s, name))
582             return 0;
583           return pool_match_nevr(pool, s, evr);
584         default:
585           return 0;
586         }
587     }
588   if (!pool_match_nevr(pool, s, name))
589     return 0;
590   if (evr == s->evr)
591     return (flags & REL_EQ) ? 1 : 0;
592   if (!flags)
593     return 0;
594   if (flags == 7)
595     return 1;
596   switch (pool_evrcmp(pool, s->evr, evr, EVRCMP_DEPCMP))
597     {
598     case -2:
599       return 1;
600     case -1:
601       return (flags & REL_LT) ? 1 : 0;
602     case 0:
603       return (flags & REL_EQ) ? 1 : 0;
604     case 1:
605       return (flags & REL_GT) ? 1 : 0;
606     case 2:
607       return (flags & REL_EQ) ? 1 : 0;
608     default:
609       break;
610     }
611   return 0;
612 }
613
614 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
615 /* forward declaration */
616 static int pool_match_flags_evr_rel_compat(Pool *pool, Reldep *range, int flags, int evr);
617 #endif
618
619 /* match (flags, evr) against provider (pflags, pevr) */
620 static inline int
621 pool_match_flags_evr(Pool *pool, int pflags, Id pevr, int flags, int evr)
622 {
623   if (!pflags || !flags || pflags >= 8 || flags >= 8)
624     return 0;
625   if (flags == 7 || pflags == 7)
626     return 1;           /* rel provides every version */
627   if ((pflags & flags & (REL_LT | REL_GT)) != 0)
628     return 1;           /* both rels show in the same direction */
629   if (pevr == evr)
630     return (flags & pflags & REL_EQ) ? 1 : 0;
631 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
632   if (ISRELDEP(pevr))
633     {
634       Reldep *rd = GETRELDEP(pool, pevr);
635       if (rd->flags == REL_COMPAT)
636         return pool_match_flags_evr_rel_compat(pool, rd, flags, evr);
637     }
638 #endif
639   switch (pool_evrcmp(pool, pevr, evr, EVRCMP_DEPCMP))
640     {
641     case -2:
642       return (pflags & REL_EQ) ? 1 : 0;
643     case -1:
644       return (flags & REL_LT) || (pflags & REL_GT) ? 1 : 0;
645     case 0:
646       return (flags & pflags & REL_EQ) ? 1 : 0;
647     case 1:
648       return (flags & REL_GT) || (pflags & REL_LT) ? 1 : 0;
649     case 2:
650       return (flags & REL_EQ) ? 1 : 0;
651     default:
652       break;
653     }
654   return 0;
655 }
656
657 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
658 static int
659 pool_match_flags_evr_rel_compat(Pool *pool, Reldep *range, int flags, int evr)
660 {
661   /* range->name is the actual version, range->evr the backwards compatibility
662      version. If flags are '>=' or '>', we match the compatibility version
663      as well, otherwise only the actual version. */
664   if (!(flags & REL_GT) || (flags & REL_LT))
665     return pool_match_flags_evr(pool, REL_EQ, range->name, flags, evr);
666   return pool_match_flags_evr(pool, REL_LT | REL_EQ, range->name, flags, evr) &&
667          pool_match_flags_evr(pool, REL_GT | REL_EQ, range->evr, REL_EQ, evr);
668 }
669 #endif
670
671 /* match two dependencies (d1 = provider) */
672
673 int
674 pool_match_dep(Pool *pool, Id d1, Id d2)
675 {
676   Reldep *rd1, *rd2;
677
678   if (d1 == d2)
679     return 1;
680   if (!ISRELDEP(d1))
681     {
682       if (!ISRELDEP(d2))
683         return 0;
684       rd2 = GETRELDEP(pool, d2);
685       return pool_match_dep(pool, d1, rd2->name);
686     }
687   rd1 = GETRELDEP(pool, d1);
688   if (!ISRELDEP(d2))
689     {
690       return pool_match_dep(pool, rd1->name, d2);
691     }
692   rd2 = GETRELDEP(pool, d2);
693   /* first match name */
694   if (!pool_match_dep(pool, rd1->name, rd2->name))
695     return 0;
696   /* name matches, check flags and evr */
697   return pool_match_flags_evr(pool, rd1->flags, rd1->evr, rd2->flags, rd2->evr);
698 }
699
700 /*
701  * addrelproviders
702  * 
703  * add packages fulfilling the relation to whatprovides array
704  * no exact providers, do range match
705  * 
706  */
707
708 Id
709 pool_addrelproviders(Pool *pool, Id d)
710 {
711   Reldep *rd = GETRELDEP(pool, d);
712   Reldep *prd;
713   Queue plist;
714   Id buf[16];
715   Id name = rd->name;
716   Id evr = rd->evr;
717   int flags = rd->flags;
718   Id pid, *pidp;
719   Id p, *pp;
720
721   d = GETRELID(d);
722   queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
723
724   if (flags >= 8)
725     {
726       /* special relation */
727       Id wp = 0;
728       Id *pp2, *pp3;
729
730       switch (flags)
731         {
732         case REL_AND:
733         case REL_WITH:
734           wp = pool_whatprovides(pool, name);
735           pp2 = pool_whatprovides_ptr(pool, evr);
736           pp = pool->whatprovidesdata + wp;
737           while ((p = *pp++) != 0)
738             {
739               for (pp3 = pp2; *pp3; pp3++)
740                 if (*pp3 == p)
741                   break;
742               if (*pp3)
743                 queue_push(&plist, p);  /* found it */
744               else
745                 wp = 0;
746             }
747           break;
748         case REL_OR:
749           wp = pool_whatprovides(pool, name);
750           pp = pool->whatprovidesdata + wp;
751           if (!*pp)
752             wp = pool_whatprovides(pool, evr);
753           else
754             {
755               int cnt;
756               while ((p = *pp++) != 0)
757                 queue_push(&plist, p);
758               cnt = plist.count;
759               pp = pool_whatprovides_ptr(pool, evr);
760               while ((p = *pp++) != 0)
761                 queue_pushunique(&plist, p);
762               if (plist.count != cnt)
763                 wp = 0;
764             }
765           break;
766         case REL_NAMESPACE:
767           if (name == NAMESPACE_OTHERPROVIDERS)
768             {
769               wp = pool_whatprovides(pool, evr);
770               break;
771             }
772           if (pool->nscallback)
773             {
774               /* ask callback which packages provide the dependency
775                * 0:  none
776                * 1:  the system (aka SYSTEMSOLVABLE)
777                * >1: set of packages, stored as offset on whatprovidesdata
778                */
779               p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
780               if (p > 1)
781                 wp = p;
782               if (p == 1)
783                 queue_push(&plist, SYSTEMSOLVABLE);
784             }
785           break;
786         case REL_ARCH:
787           /* small hack: make it possible to match <pkg>.src
788            * we have to iterate over the solvables as src packages do not
789            * provide anything, thus they are not indexed in our
790            * whatprovides hash */
791           if (evr == ARCH_SRC || evr == ARCH_NOSRC)
792             {
793               Solvable *s;
794               for (p = 1, s = pool->solvables + p; p < pool->nsolvables; p++, s++)
795                 {
796                   if (!s->repo)
797                     continue;
798                   if (s->arch != evr && s->arch != ARCH_NOSRC)
799                     continue;
800                   if (pool_disabled_solvable(pool, s))
801                     continue;
802                   if (pool_match_nevr(pool, s, name))
803                     queue_push(&plist, p);
804                 }
805               break;
806             }
807           wp = pool_whatprovides(pool, name);
808           pp = pool->whatprovidesdata + wp;
809           while ((p = *pp++) != 0)
810             {
811               Solvable *s = pool->solvables + p;
812               if (s->arch == evr)
813                 queue_push(&plist, p);
814               else
815                 wp = 0;
816             }
817           break;
818         case REL_FILECONFLICT:
819           pp = pool_whatprovides_ptr(pool, name);
820           while ((p = *pp++) != 0)
821             {
822               Id origd = MAKERELDEP(d);
823               Solvable *s = pool->solvables + p;
824               if (!s->provides)
825                 continue;
826               pidp = s->repo->idarraydata + s->provides;
827               while ((pid = *pidp++) != 0)
828                 if (pid == origd)
829                   break;
830               if (pid)
831                 queue_push(&plist, p);
832             }
833           break;
834         default:
835           break;
836         }
837       if (wp)
838         {
839           /* we can reuse an existing entry */
840           queue_free(&plist);
841           pool->whatprovides_rel[d] = wp;
842           return wp;
843         }
844     }
845   else if (flags)
846     {
847       /* simple version comparison relation */
848 #if 0
849       POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: what provides %s?\n", pool_dep2str(pool, name));
850 #endif
851       pp = pool_whatprovides_ptr(pool, name);
852       while (ISRELDEP(name))
853         {
854           rd = GETRELDEP(pool, name);
855           name = rd->name;
856         }
857       while ((p = *pp++) != 0)
858         {
859           Solvable *s = pool->solvables + p;
860           if (!s->provides)
861             {
862               /* no provides - check nevr */
863               if (pool_match_nevr_rel(pool, s, MAKERELDEP(d)))
864                 queue_push(&plist, p);
865               continue;
866             }
867           /* solvable p provides name in some rels */
868           pidp = s->repo->idarraydata + s->provides;
869           while ((pid = *pidp++) != 0)
870             {
871               if (!ISRELDEP(pid))
872                 {
873                   if (pid != name)
874                     continue;           /* wrong provides name */
875                   if (pool->disttype == DISTTYPE_DEB)
876                     continue;           /* unversioned provides can never match versioned deps */
877                   break;
878                 }
879               prd = GETRELDEP(pool, pid);
880               if (prd->name != name)
881                 continue;               /* wrong provides name */
882               /* right package, both deps are rels. check flags/evr */
883               if (pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
884                 break;  /* matches */
885             }
886           if (!pid)
887             continue;   /* none of the providers matched */
888           queue_push(&plist, p);
889         }
890       /* make our system solvable provide all unknown rpmlib() stuff */
891       if (plist.count == 0 && !strncmp(pool_id2str(pool, name), "rpmlib(", 7))
892         queue_push(&plist, SYSTEMSOLVABLE);
893     }
894   /* add providers to whatprovides */
895 #if 0
896   POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
897 #endif
898   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
899   queue_free(&plist);
900
901   return pool->whatprovides_rel[d];
902 }
903
904 void
905 pool_flush_namespaceproviders(Pool *pool, Id ns, Id evr)
906 {
907   int nrels = pool->nrels;
908   Id d;
909   Reldep *rd;
910
911   if (!pool->whatprovides_rel)
912     return;
913   for (d = 1, rd = pool->rels + d; d < nrels; d++, rd++)
914     {
915       if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
916         continue;
917       if (ns && rd->name != ns)
918         continue;
919       if (evr && rd->evr != evr)
920         continue;
921       pool->whatprovides_rel[d] = 0;
922     }
923 }
924
925 /*************************************************************************/
926
927 void
928 pool_debug(Pool *pool, int type, const char *format, ...)
929 {
930   va_list args;
931   char buf[1024];
932
933   if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0)
934     {
935       if ((pool->debugmask & type) == 0)
936         return;
937     }
938   va_start(args, format);
939   if (!pool->debugcallback)
940     {
941       if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0 && !(pool->debugmask & SOLV_DEBUG_TO_STDERR))
942         vprintf(format, args);
943       else
944         vfprintf(stderr, format, args);
945       return;
946     }
947   vsnprintf(buf, sizeof(buf), format, args);
948   va_end(args);
949   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
950 }
951
952 int
953 pool_error(Pool *pool, int ret, const char *format, ...)
954 {
955   va_list args;
956   int l;
957   va_start(args, format);
958   if (!pool->errstr)
959     {
960       pool->errstra = 1024;
961       pool->errstr = solv_malloc(pool->errstra);
962     }
963   if (!*format)
964     {
965       *pool->errstr = 0;
966       l = 0;
967     }
968   else
969     l = vsnprintf(pool->errstr, pool->errstra, format, args);
970   va_end(args);
971   if (l >= 0 && l + 1 > pool->errstra)
972     {
973       pool->errstra = l + 256;
974       pool->errstr = solv_realloc(pool->errstr, pool->errstra);
975       va_start(args, format);
976       l = vsnprintf(pool->errstr, pool->errstra, format, args);
977       va_end(args);
978     }
979   if (l < 0)
980     strcpy(pool->errstr, "unknown error");
981   if (pool->debugmask & SOLV_ERROR)
982     pool_debug(pool, SOLV_ERROR, "%s\n", pool->errstr);
983   return ret;
984 }
985
986 char *
987 pool_errstr(Pool *pool)
988 {
989   return pool->errstr ? pool->errstr : "no error";
990 }
991
992 void
993 pool_setdebuglevel(Pool *pool, int level)
994 {
995   int mask = SOLV_DEBUG_RESULT;
996   if (level > 0)
997     mask |= SOLV_DEBUG_STATS|SOLV_DEBUG_ANALYZE|SOLV_DEBUG_UNSOLVABLE|SOLV_DEBUG_SOLVER|SOLV_DEBUG_TRANSACTION|SOLV_ERROR;
998   if (level > 1)
999     mask |= SOLV_DEBUG_JOB|SOLV_DEBUG_SOLUTIONS|SOLV_DEBUG_POLICY;
1000   if (level > 2)
1001     mask |= SOLV_DEBUG_PROPAGATE;
1002   if (level > 3)
1003     mask |= SOLV_DEBUG_RULE_CREATION;
1004   mask |= pool->debugmask & SOLV_DEBUG_TO_STDERR;       /* keep bit */
1005   pool->debugmask = mask;
1006 }
1007
1008 void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
1009 {
1010   pool->debugcallback = debugcallback;
1011   pool->debugcallbackdata = debugcallbackdata;
1012 }
1013
1014 void pool_setdebugmask(Pool *pool, int mask)
1015 {
1016   pool->debugmask = mask;
1017 }
1018
1019 void pool_setloadcallback(Pool *pool, int (*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
1020 {
1021   pool->loadcallback = cb;
1022   pool->loadcallbackdata = loadcbdata;
1023 }
1024
1025 void pool_setnamespacecallback(Pool *pool, Id (*cb)(struct _Pool *, void *, Id, Id), void *nscbdata)
1026 {
1027   pool->nscallback = cb;
1028   pool->nscallbackdata = nscbdata;
1029 }
1030
1031 /*************************************************************************/
1032
1033 struct searchfiles {
1034   Id *ids;
1035   char **dirs;
1036   char **names;
1037   int nfiles;
1038   Map seen;
1039 };
1040
1041 #define SEARCHFILES_BLOCK 127
1042
1043 static void
1044 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
1045 {
1046   Id dep, sid;
1047   const char *s, *sr;
1048   struct searchfiles *csf;
1049
1050   while ((dep = *ida++) != 0)
1051     {
1052       csf = sf;
1053       while (ISRELDEP(dep))
1054         {
1055           Reldep *rd;
1056           sid = pool->ss.nstrings + GETRELID(dep);
1057           if (MAPTST(&csf->seen, sid))
1058             {
1059               dep = 0;
1060               break;
1061             }
1062           MAPSET(&csf->seen, sid);
1063           rd = GETRELDEP(pool, dep);
1064           if (rd->flags < 8)
1065             dep = rd->name;
1066           else if (rd->flags == REL_NAMESPACE)
1067             {
1068               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
1069                 {
1070                   csf = isf;
1071                   if (!csf || MAPTST(&csf->seen, sid))
1072                     {
1073                       dep = 0;
1074                       break;
1075                     }
1076                   MAPSET(&csf->seen, sid);
1077                 }
1078               dep = rd->evr;
1079             }
1080           else if (rd->flags == REL_FILECONFLICT)
1081             {
1082               dep = 0;
1083               break;
1084             }
1085           else
1086             {
1087               Id ids[2];
1088               ids[0] = rd->name;
1089               ids[1] = 0;
1090               pool_addfileprovides_dep(pool, ids, csf, isf);
1091               dep = rd->evr;
1092             }
1093         }
1094       if (!dep)
1095         continue;
1096       if (MAPTST(&csf->seen, dep))
1097         continue;
1098       MAPSET(&csf->seen, dep);
1099       s = pool_id2str(pool, dep);
1100       if (*s != '/')
1101         continue;
1102       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1103       csf->dirs = solv_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1104       csf->names = solv_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1105       csf->ids[csf->nfiles] = dep;
1106       sr = strrchr(s, '/');
1107       csf->names[csf->nfiles] = solv_strdup(sr + 1);
1108       csf->dirs[csf->nfiles] = solv_malloc(sr - s + 1);
1109       if (sr != s)
1110         strncpy(csf->dirs[csf->nfiles], s, sr - s);
1111       csf->dirs[csf->nfiles][sr - s] = 0;
1112       csf->nfiles++;
1113     }
1114 }
1115
1116 struct addfileprovides_cbdata {
1117   int nfiles;
1118   Id *ids;
1119   char **dirs;
1120   char **names;
1121
1122   Id *dids;
1123
1124   Map providedids;
1125
1126   Map useddirs;
1127 };
1128
1129 static int
1130 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1131 {
1132   struct addfileprovides_cbdata *cbd = cbdata;
1133   int i;
1134
1135   if (!cbd->useddirs.size)
1136     {
1137       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
1138       for (i = 0; i < cbd->nfiles; i++)
1139         {
1140           Id did;
1141           if (MAPTST(&cbd->providedids, cbd->ids[i]))
1142             {
1143               cbd->dids[i] = 0;
1144               continue;
1145             }
1146           did = repodata_str2dir(data, cbd->dirs[i], 0);
1147           cbd->dids[i] = did;
1148           if (did)
1149             MAPSET(&cbd->useddirs, did);
1150         }
1151       repodata_free_dircache(data);
1152     }
1153   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
1154     return 0;
1155   for (i = 0; i < cbd->nfiles; i++)
1156     {
1157       if (cbd->dids[i] != value->id)
1158         continue;
1159       if (!strcmp(cbd->names[i], value->str))
1160         break;
1161     }
1162   if (i == cbd->nfiles)
1163     return 0;
1164   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
1165   return 0;
1166 }
1167
1168 static void
1169 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
1170 {
1171   Id p;
1172   Repodata *data;
1173   Repo *repo;
1174   Queue fileprovidesq;
1175   int i, j, repoid, repodataid;
1176   int provstart, provend;
1177   Map donemap;
1178   int ndone, incomplete;
1179
1180   if (!pool->urepos)
1181     return;
1182
1183   cbd->nfiles = sf->nfiles;
1184   cbd->ids = sf->ids;
1185   cbd->dirs = sf->dirs;
1186   cbd->names = sf->names;
1187   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
1188   map_init(&cbd->providedids, pool->ss.nstrings);
1189
1190   repoid = 1;
1191   repo = repoonly ? repoonly : pool->repos[repoid];
1192   map_init(&donemap, pool->nsolvables);
1193   queue_init(&fileprovidesq);
1194   provstart = provend = 0;
1195   for (;;)
1196     {
1197       if (!repo || repo->disabled)
1198         {
1199           if (repoonly || ++repoid == pool->nrepos)
1200             break;
1201           repo = pool->repos[repoid];
1202           continue;
1203         }
1204       ndone = 0;
1205       FOR_REPODATAS(repo, repodataid, data)
1206         {
1207           if (ndone >= repo->nsolvables)
1208             break;
1209
1210           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1211             {
1212               map_empty(&cbd->providedids);
1213               for (i = 0; i < fileprovidesq.count; i++)
1214                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1215               provstart = data->start;
1216               provend = data->end;
1217               for (i = 0; i < cbd->nfiles; i++)
1218                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1219                   break;
1220               if (i == cbd->nfiles)
1221                 {
1222                   /* great! no need to search files */
1223                   for (p = data->start; p < data->end; p++)
1224                     if (pool->solvables[p].repo == repo)
1225                       {
1226                         if (MAPTST(&donemap, p))
1227                           continue;
1228                         MAPSET(&donemap, p);
1229                         ndone++;
1230                       }
1231                   continue;
1232                 }
1233             }
1234
1235           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1236             continue;
1237
1238           if (data->start < provstart || data->end > provend)
1239             {
1240               map_empty(&cbd->providedids);
1241               provstart = provend = 0;
1242             }
1243
1244           /* check if the data is incomplete */
1245           incomplete = 0;
1246           if (data->state == REPODATA_AVAILABLE)
1247             {
1248               for (j = 1; j < data->nkeys; j++)
1249                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1250                   break;
1251               if (j < data->nkeys)
1252                 {
1253 #if 0
1254                   for (i = 0; i < cbd->nfiles; i++)
1255                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1256                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1257 #endif
1258                   for (i = 0; i < cbd->nfiles; i++)
1259                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1260                       break;
1261                   if (i < cbd->nfiles)
1262                     incomplete = 1;
1263                 }
1264             }
1265
1266           /* do the search */
1267           map_init(&cbd->useddirs, 0);
1268           for (p = data->start; p < data->end; p++)
1269             if (pool->solvables[p].repo == repo)
1270               {
1271                 if (MAPTST(&donemap, p))
1272                   continue;
1273                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1274                 if (!incomplete)
1275                   {
1276                     MAPSET(&donemap, p);
1277                     ndone++;
1278                   }
1279               }
1280           map_free(&cbd->useddirs);
1281         }
1282
1283       if (repoonly || ++repoid == pool->nrepos)
1284         break;
1285       repo = pool->repos[repoid];
1286     }
1287   map_free(&donemap);
1288   queue_free(&fileprovidesq);
1289   map_free(&cbd->providedids);
1290 }
1291
1292 void
1293 pool_addfileprovides_queue(Pool *pool, Queue *idq, Queue *idqinst)
1294 {
1295   Solvable *s;
1296   Repo *installed, *repo;
1297   struct searchfiles sf, isf, *isfp;
1298   struct addfileprovides_cbdata cbd;
1299   int i;
1300   unsigned int now;
1301
1302   installed = pool->installed;
1303   now = solv_timems(0);
1304   memset(&sf, 0, sizeof(sf));
1305   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1306   memset(&isf, 0, sizeof(isf));
1307   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1308
1309   if (idq)
1310     queue_empty(idq);
1311   if (idqinst)
1312     queue_empty(idqinst);
1313   isfp = installed ? &isf : 0;
1314   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1315     {
1316       repo = s->repo;
1317       if (!repo)
1318         continue;
1319       if (s->obsoletes)
1320         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1321       if (s->conflicts)
1322         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1323       if (s->requires)
1324         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1325       if (s->recommends)
1326         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1327       if (s->suggests)
1328         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1329       if (s->supplements)
1330         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1331       if (s->enhances)
1332         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1333     }
1334   map_free(&sf.seen);
1335   map_free(&isf.seen);
1336   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1337   cbd.dids = 0;
1338   if (sf.nfiles)
1339     {
1340 #if 0
1341       for (i = 0; i < sf.nfiles; i++)
1342         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1343 #endif
1344       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1345       if (idq)
1346         for (i = 0; i < sf.nfiles; i++)
1347           queue_push(idq, sf.ids[i]);
1348       if (idqinst)
1349         for (i = 0; i < sf.nfiles; i++)
1350           queue_push(idqinst, sf.ids[i]);
1351       solv_free(sf.ids);
1352       for (i = 0; i < sf.nfiles; i++)
1353         {
1354           solv_free(sf.dirs[i]);
1355           solv_free(sf.names[i]);
1356         }
1357       solv_free(sf.dirs);
1358       solv_free(sf.names);
1359     }
1360   if (isf.nfiles)
1361     {
1362 #if 0
1363       for (i = 0; i < isf.nfiles; i++)
1364         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1365 #endif
1366       if (installed)
1367         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1368       if (installed && idqinst)
1369         for (i = 0; i < isf.nfiles; i++)
1370           queue_pushunique(idqinst, isf.ids[i]);
1371       solv_free(isf.ids);
1372       for (i = 0; i < isf.nfiles; i++)
1373         {
1374           solv_free(isf.dirs[i]);
1375           solv_free(isf.names[i]);
1376         }
1377       solv_free(isf.dirs);
1378       solv_free(isf.names);
1379     }
1380   solv_free(cbd.dids);
1381   pool_freewhatprovides(pool);  /* as we have added provides */
1382   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1383 }
1384
1385 void
1386 pool_addfileprovides(Pool *pool)
1387 {
1388   pool_addfileprovides_queue(pool, 0, 0);
1389 }
1390
1391 void
1392 pool_search(Pool *pool, Id p, Id key, const char *match, int flags, int (*callback)(void *cbdata, Solvable *s, struct _Repodata *data, struct _Repokey *key, struct _KeyValue *kv), void *cbdata)
1393 {
1394   if (p)
1395     {
1396       if (pool->solvables[p].repo)
1397         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1398       return;
1399     }
1400   /* FIXME: obey callback return value! */
1401   for (p = 1; p < pool->nsolvables; p++)
1402     if (pool->solvables[p].repo)
1403       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1404 }
1405
1406 void
1407 pool_clear_pos(Pool *pool)
1408 {
1409   memset(&pool->pos, 0, sizeof(pool->pos));
1410 }
1411
1412
1413 void
1414 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1415 {
1416   int i;
1417
1418   pool->languagecache = solv_free(pool->languagecache);
1419   pool->languagecacheother = 0;
1420   if (pool->nlanguages)
1421     {
1422       for (i = 0; i < pool->nlanguages; i++)
1423         free((char *)pool->languages[i]);
1424       free(pool->languages);
1425     }
1426   pool->nlanguages = nlanguages;
1427   if (!nlanguages)
1428     return;
1429   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1430   for (i = 0; i < pool->nlanguages; i++)
1431     pool->languages[i] = solv_strdup(languages[i]);
1432 }
1433
1434 Id
1435 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1436 {
1437   const char *n;
1438   char buf[256], *p;
1439   int l;
1440
1441   if (!lang || !*lang)
1442     return id;
1443   n = pool_id2str(pool, id);
1444   l = strlen(n) + strlen(lang) + 2;
1445   if (l > sizeof(buf))
1446     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1447   else
1448     p = buf;
1449   sprintf(p, "%s:%s", n, lang);
1450   id = pool_str2id(pool, p, create);
1451   if (p != buf)
1452     free(p);
1453   return id;
1454 }
1455
1456 char *
1457 pool_alloctmpspace(Pool *pool, int len)
1458 {
1459   int n = pool->tmpspace.n;
1460   if (!len)
1461     return 0;
1462   if (len > pool->tmpspace.len[n])
1463     {
1464       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1465       pool->tmpspace.len[n] = len + 32;
1466     }
1467   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1468   return pool->tmpspace.buf[n];
1469 }
1470
1471 static char *
1472 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1473 {
1474   if (space)
1475     {
1476       int n, oldn;
1477       n = oldn = pool->tmpspace.n;
1478       for (;;)
1479         {
1480           if (!n--)
1481             n = POOL_TMPSPACEBUF - 1;
1482           if (n == oldn)
1483             break;
1484           if (pool->tmpspace.buf[n] != space)
1485             continue;
1486           if (len > pool->tmpspace.len[n])
1487             {
1488               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1489               pool->tmpspace.len[n] = len + 32;
1490             }
1491           return pool->tmpspace.buf[n];
1492         }
1493     }
1494   return 0;
1495 }
1496
1497 void
1498 pool_freetmpspace(Pool *pool, const char *space)
1499 {
1500   int n = pool->tmpspace.n;
1501   if (!space)
1502     return;
1503   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1504   if (pool->tmpspace.buf[n] == space)
1505     pool->tmpspace.n = n;
1506 }
1507
1508 char *
1509 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1510 {
1511   int l1, l2, l3;
1512   char *s, *str;
1513   l1 = str1 ? strlen(str1) : 0;
1514   l2 = str2 ? strlen(str2) : 0;
1515   l3 = str3 ? strlen(str3) : 0;
1516   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1517   if (l1)
1518     {
1519       strcpy(s, str1);
1520       s += l1;
1521     }
1522   if (l2)
1523     {
1524       strcpy(s, str2);
1525       s += l2;
1526     }
1527   if (l3)
1528     {
1529       strcpy(s, str3);
1530       s += l3;
1531     }
1532   *s = 0;
1533   return str;
1534 }
1535
1536 char *
1537 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1538 {
1539   int l1, l2, l3;
1540   char *s, *str;
1541
1542   l1 = str1 ? strlen(str1) : 0;
1543   l2 = str2 ? strlen(str2) : 0;
1544   l3 = str3 ? strlen(str3) : 0;
1545   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1546   if (str)
1547     str1 = str;
1548   else
1549     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1550   s = str;
1551   if (l1)
1552     {
1553       if (s != str1)
1554         strcpy(s, str1);
1555       s += l1;
1556     }
1557   if (l2)
1558     {
1559       strcpy(s, str2);
1560       s += l2;
1561     }
1562   if (l3)
1563     {
1564       strcpy(s, str3);
1565       s += l3;
1566     }
1567   *s = 0;
1568   return str;
1569 }
1570
1571 const char *
1572 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1573 {
1574   char *s;
1575   if (!len)
1576     return "";
1577   s = pool_alloctmpspace(pool, 2 * len + 1);
1578   solv_bin2hex(buf, len, s);
1579   return s;
1580 }
1581
1582 /*******************************************************************/
1583
1584 struct mptree {
1585   Id sibling;
1586   Id child;
1587   const char *comp;
1588   int compl;
1589   Id mountpoint;
1590 };
1591
1592 struct ducbdata {
1593   DUChanges *mps;
1594   struct mptree *mptree;
1595   int addsub;
1596   int hasdu;
1597
1598   Id *dirmap;
1599   int nmap;
1600   Repodata *olddata;
1601 };
1602
1603
1604 static int
1605 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1606 {
1607   struct ducbdata *cbd = cbdata;
1608   Id mp;
1609
1610   if (data != cbd->olddata)
1611     {
1612       Id dn, mp, comp, *dirmap, *dirs;
1613       int i, compl;
1614       const char *compstr;
1615       struct mptree *mptree;
1616
1617       /* create map from dir to mptree */
1618       cbd->dirmap = solv_free(cbd->dirmap);
1619       cbd->nmap = 0;
1620       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1621       mptree = cbd->mptree;
1622       mp = 0;
1623       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1624         {
1625           comp = *dirs++;
1626           if (comp <= 0)
1627             {
1628               mp = dirmap[-comp];
1629               continue;
1630             }
1631           if (mp < 0)
1632             {
1633               /* unconnected */
1634               dirmap[dn] = mp;
1635               continue;
1636             }
1637           if (!mptree[mp].child)
1638             {
1639               dirmap[dn] = -mp;
1640               continue;
1641             }
1642           if (data->localpool)
1643             compstr = stringpool_id2str(&data->spool, comp);
1644           else
1645             compstr = pool_id2str(data->repo->pool, comp);
1646           compl = strlen(compstr);
1647           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1648             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1649               break;
1650           dirmap[dn] = i ? i : -mp;
1651         }
1652       /* change dirmap to point to mountpoint instead of mptree */
1653       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1654         {
1655           mp = dirmap[dn];
1656           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1657         }
1658       cbd->dirmap = dirmap;
1659       cbd->nmap = data->dirpool.ndirs;
1660       cbd->olddata = data;
1661     }
1662   cbd->hasdu = 1;
1663   if (value->id < 0 || value->id >= cbd->nmap)
1664     return 0;
1665   mp = cbd->dirmap[value->id];
1666   if (mp < 0)
1667     return 0;
1668   if (cbd->addsub > 0)
1669     {
1670       cbd->mps[mp].kbytes += value->num;
1671       cbd->mps[mp].files += value->num2;
1672     }
1673   else
1674     {
1675       cbd->mps[mp].kbytes -= value->num;
1676       cbd->mps[mp].files -= value->num2;
1677     }
1678   return 0;
1679 }
1680
1681 static void
1682 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1683 {
1684   int i;
1685   if (mptree[pos].mountpoint == -1)
1686     mptree[pos].mountpoint = mountpoint;
1687   else
1688     mountpoint = mptree[pos].mountpoint;
1689   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1690     propagate_mountpoints(mptree, i, mountpoint);
1691 }
1692
1693 #define MPTREE_BLOCK 15
1694
1695 void
1696 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1697 {
1698   char *p;
1699   const char *path, *compstr;
1700   struct mptree *mptree;
1701   int i, nmptree;
1702   int pos, compl;
1703   int mp;
1704   struct ducbdata cbd;
1705   Solvable *s;
1706   Id sp;
1707   Map ignoredu;
1708   Repo *oldinstalled = pool->installed;
1709
1710   memset(&ignoredu, 0, sizeof(ignoredu));
1711   cbd.mps = mps;
1712   cbd.addsub = 0;
1713   cbd.dirmap = 0;
1714   cbd.nmap = 0;
1715   cbd.olddata = 0;
1716
1717   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1718
1719   /* our root node */
1720   mptree[0].sibling = 0;
1721   mptree[0].child = 0;
1722   mptree[0].comp = 0;
1723   mptree[0].compl = 0;
1724   mptree[0].mountpoint = -1;
1725   nmptree = 1;
1726   
1727   /* create component tree */
1728   for (mp = 0; mp < nmps; mp++)
1729     {
1730       mps[mp].kbytes = 0;
1731       mps[mp].files = 0;
1732       pos = 0;
1733       path = mps[mp].path;
1734       while(*path == '/')
1735         path++;
1736       while (*path)
1737         {
1738           if ((p = strchr(path, '/')) == 0)
1739             {
1740               compstr = path;
1741               compl = strlen(compstr);
1742               path += compl;
1743             }
1744           else
1745             {
1746               compstr = path;
1747               compl = p - path;
1748               path = p + 1;
1749               while(*path == '/')
1750                 path++;
1751             }
1752           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1753             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1754               break;
1755           if (!i)
1756             {
1757               /* create new node */
1758               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1759               i = nmptree++;
1760               mptree[i].sibling = mptree[pos].child;
1761               mptree[i].child = 0;
1762               mptree[i].comp = compstr;
1763               mptree[i].compl = compl;
1764               mptree[i].mountpoint = -1;
1765               mptree[pos].child = i;
1766             }
1767           pos = i;
1768         }
1769       mptree[pos].mountpoint = mp;
1770     }
1771
1772   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1773
1774 #if 0
1775   for (i = 0; i < nmptree; i++)
1776     {
1777       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1778       printf("#%d child: %d\n", i, mptree[i].child);
1779       printf("#%d comp: %s\n", i, mptree[i].comp);
1780       printf("#%d compl: %d\n", i, mptree[i].compl);
1781       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1782     }
1783 #endif
1784
1785   cbd.mptree = mptree;
1786   cbd.addsub = 1;
1787   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1788     {
1789       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1790         continue;
1791       if (!MAPTST(installedmap, sp))
1792         continue;
1793       cbd.hasdu = 0;
1794       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1795       if (!cbd.hasdu && oldinstalled)
1796         {
1797           Id op, opp;
1798           /* no du data available, ignore data of all installed solvables we obsolete */
1799           if (!ignoredu.map)
1800             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1801           if (s->obsoletes)
1802             {
1803               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1804               while ((obs = *obsp++) != 0)
1805                 FOR_PROVIDES(op, opp, obs)
1806                   if (op >= oldinstalled->start && op < oldinstalled->end)
1807                     MAPSET(&ignoredu, op - oldinstalled->start);
1808             }
1809           FOR_PROVIDES(op, opp, s->name)
1810             if (pool->solvables[op].name == s->name)
1811               if (op >= oldinstalled->start && op < oldinstalled->end)
1812                 MAPSET(&ignoredu, op - oldinstalled->start);
1813         }
1814     }
1815   cbd.addsub = -1;
1816   if (oldinstalled)
1817     {
1818       /* assumes we allways have du data for installed solvables */
1819       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1820         {
1821           if (MAPTST(installedmap, sp))
1822             continue;
1823           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1824             continue;
1825           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1826         }
1827     }
1828   if (ignoredu.map)
1829     map_free(&ignoredu);
1830   solv_free(cbd.dirmap);
1831   solv_free(mptree);
1832 }
1833
1834 int
1835 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1836 {
1837   Id sp;
1838   Solvable *s;
1839   int change = 0;
1840   Repo *oldinstalled = pool->installed;
1841
1842   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1843     {
1844       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1845         continue;
1846       if (!MAPTST(installedmap, sp))
1847         continue;
1848       change += solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
1849     }
1850   if (oldinstalled)
1851     {
1852       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1853         {
1854           if (MAPTST(installedmap, sp))
1855             continue;
1856           change -= solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
1857         }
1858     }
1859   return change;
1860 }
1861
1862 /* map:
1863  *  1: installed
1864  *  2: conflicts with installed
1865  *  8: interesting (only true if installed)
1866  * 16: undecided
1867  */
1868  
1869 static inline Id dep2name(Pool *pool, Id dep)
1870 {
1871   while (ISRELDEP(dep))
1872     {
1873       Reldep *rd = rd = GETRELDEP(pool, dep);
1874       dep = rd->name;
1875     }
1876   return dep;
1877 }
1878
1879 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
1880 {
1881   Id p, pp;
1882   Solvable *sn = pool->solvables + n; 
1883
1884   FOR_PROVIDES(p, pp, sn->name)
1885     {    
1886       Solvable *s = pool->solvables + p; 
1887       if (s->name != sn->name || s->arch != sn->arch)
1888         continue;
1889       if ((map[p] & 9) != 9)
1890         continue;
1891       if (pool_match_nevr(pool, pool->solvables + p, con))
1892         continue;
1893       return 1;         /* found installed package that doesn't conflict */
1894     }
1895   return 0;
1896 }
1897
1898 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *multiversionmap)
1899 {
1900   Id p, pp;
1901   int r = 0;
1902   FOR_PROVIDES(p, pp, dep)
1903     {
1904       if (p == SYSTEMSOLVABLE)
1905         return 1;       /* always boring, as never constraining */
1906       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
1907         continue;
1908       if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
1909         if (providedbyinstalled_multiversion(pool, map, p, dep))
1910           continue;
1911       if ((map[p] & 9) == 9)
1912         return 9;
1913       r |= map[p] & 17;
1914     }
1915   return r;
1916 }
1917
1918 /*
1919  * pool_trivial_installable - calculate if a set of solvables is
1920  * trivial installable without any other installs/deinstalls of
1921  * packages not belonging to the set.
1922  *
1923  * the state is returned in the result queue:
1924  * 1:  solvable is installable without any other package changes
1925  * 0:  solvable is not installable
1926  * -1: solvable is installable, but doesn't constrain any installed packages
1927  */
1928
1929 void
1930 pool_trivial_installable_multiversionmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *multiversionmap)
1931 {
1932   int i, r, m, did;
1933   Id p, *dp, con, *conp, req, *reqp;
1934   unsigned char *map;
1935   Solvable *s;
1936
1937   map = solv_calloc(pool->nsolvables, 1);
1938   for (p = 1; p < pool->nsolvables; p++)
1939     {
1940       if (!MAPTST(installedmap, p))
1941         continue;
1942       map[p] |= 9;
1943       s = pool->solvables + p;
1944       if (!s->conflicts)
1945         continue;
1946       conp = s->repo->idarraydata + s->conflicts;
1947       while ((con = *conp++) != 0)
1948         {
1949           dp = pool_whatprovides_ptr(pool, con);
1950           for (; *dp; dp++)
1951             map[p] |= 2;        /* XXX: self conflict ? */
1952         }
1953     }
1954   for (i = 0; i < pkgs->count; i++)
1955     map[pkgs->elements[i]] = 16;
1956
1957   for (i = 0, did = 0; did < pkgs->count; i++, did++)
1958     {
1959       if (i == pkgs->count)
1960         i = 0;
1961       p = pkgs->elements[i];
1962       if ((map[p] & 16) == 0)
1963         continue;
1964       if ((map[p] & 2) != 0)
1965         {
1966           map[p] = 2;
1967           continue;
1968         }
1969       s = pool->solvables + p;
1970       m = 1;
1971       if (s->requires)
1972         {
1973           reqp = s->repo->idarraydata + s->requires;
1974           while ((req = *reqp++) != 0)
1975             {
1976               if (req == SOLVABLE_PREREQMARKER)
1977                 continue;
1978               r = providedbyinstalled(pool, map, req, 0, 0);
1979               if (!r)
1980                 {
1981                   /* decided and miss */
1982                   map[p] = 2;
1983                   did = 0;
1984                   break;
1985                 }
1986               if (r == 16)
1987                 break;  /* undecided */
1988               m |= r;   /* 1 | 9 | 17 */
1989             }
1990           if (req)
1991             continue;
1992           if ((m & 9) == 9)
1993             m = 9;
1994         }
1995       if (s->conflicts)
1996         {
1997           int ispatch = 0;      /* see solver.c patch handling */
1998
1999           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
2000             ispatch = 1;
2001           conp = s->repo->idarraydata + s->conflicts;
2002           while ((con = *conp++) != 0)
2003             {
2004               if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2005                 {
2006                   map[p] = 2;
2007                   did = 0;
2008                   break;
2009                 }
2010               if ((m == 1 || m == 17) && ISRELDEP(con))
2011                 {
2012                   con = dep2name(pool, con);
2013                   if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2014                     m = 9;
2015                 }
2016             }
2017           if (con)
2018             continue;   /* found a conflict */
2019         }
2020 #if 0
2021       if (s->repo && s->repo != oldinstalled)
2022         {
2023           Id p2, obs, *obsp, *pp;
2024           Solvable *s2;
2025           if (s->obsoletes)
2026             {
2027               obsp = s->repo->idarraydata + s->obsoletes;
2028               while ((obs = *obsp++) != 0)
2029                 {
2030                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
2031                     {
2032                       map[p] = 2;
2033                       break;
2034                     }
2035                 }
2036               if (obs)
2037                 continue;
2038             }
2039           FOR_PROVIDES(p2, pp, s->name)
2040             {
2041               s2 = pool->solvables + p2;
2042               if (s2->name == s->name && (map[p2] & 1) != 0)
2043                 {
2044                   map[p] = 2;
2045                   break;
2046                 }
2047             }
2048           if (p2)
2049             continue;
2050         }
2051 #endif
2052       if (m != map[p])
2053         {
2054           map[p] = m;
2055           did = 0;
2056         }
2057     }
2058   queue_free(res);
2059   queue_init_clone(res, pkgs);
2060   for (i = 0; i < pkgs->count; i++)
2061     {
2062       m = map[pkgs->elements[i]];
2063       if ((m & 9) == 9)
2064         r = 1;
2065       else if (m & 1)
2066         r = -1;
2067       else
2068         r = 0;
2069       res->elements[i] = r;
2070     }
2071   free(map);
2072 }
2073
2074 void
2075 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
2076 {
2077   pool_trivial_installable_multiversionmap(pool, installedmap, pkgs, res, 0);
2078 }
2079
2080 const char *
2081 pool_lookup_str(Pool *pool, Id entry, Id keyname)
2082 {
2083   if (entry == SOLVID_POS && pool->pos.repo)
2084     return repodata_lookup_str(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
2085   if (entry <= 0)
2086     return 0;
2087   return solvable_lookup_str(pool->solvables + entry, keyname);
2088 }
2089
2090 Id
2091 pool_lookup_id(Pool *pool, Id entry, Id keyname)
2092 {
2093   if (entry == SOLVID_POS && pool->pos.repo)
2094     {
2095       Repodata *data = pool->pos.repo->repodata + pool->pos.repodataid;
2096       Id id = repodata_lookup_id(data, SOLVID_POS, keyname);
2097       return data->localpool ? repodata_globalize_id(data, id, 1) : id;
2098     }
2099   if (entry <= 0)
2100     return 0;
2101   return solvable_lookup_id(pool->solvables + entry, keyname);
2102 }
2103
2104 unsigned long long
2105 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned long long notfound)
2106 {
2107   if (entry == SOLVID_POS && pool->pos.repo)
2108     {
2109       unsigned long long value;
2110       if (repodata_lookup_num(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, &value))
2111         return value;
2112       return notfound;
2113     }
2114   if (entry <= 0)
2115     return notfound;
2116   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
2117 }
2118
2119 int
2120 pool_lookup_void(Pool *pool, Id entry, Id keyname)
2121 {
2122   if (entry == SOLVID_POS && pool->pos.repo)
2123     return repodata_lookup_void(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
2124   if (entry <= 0)
2125     return 0;
2126   return solvable_lookup_void(pool->solvables + entry, keyname);
2127 }
2128
2129 const unsigned char *
2130 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2131 {
2132   if (entry == SOLVID_POS && pool->pos.repo)
2133     return repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
2134   if (entry <= 0)
2135     return 0;
2136   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
2137 }
2138
2139 const char *
2140 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2141 {
2142   if (entry == SOLVID_POS && pool->pos.repo)
2143     {
2144       const unsigned char *chk = repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
2145       return chk ? repodata_chk2str(pool->pos.repo->repodata + pool->pos.repodataid, *typep, chk) : 0;
2146     }
2147   if (entry <= 0)
2148     return 0;
2149   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2150 }
2151
2152 const char *
2153 pool_lookup_deltalocation(Pool *pool, Id entry, unsigned int *medianrp)
2154 {
2155   const char *loc;
2156   if (medianrp)
2157     *medianrp = 0;
2158   if (entry != SOLVID_POS)
2159     return 0;
2160   loc = pool_lookup_str(pool, entry, DELTA_LOCATION_DIR);
2161   loc = pool_tmpjoin(pool, loc, loc ? "/" : 0, pool_lookup_str(pool, entry, DELTA_LOCATION_NAME));
2162   loc = pool_tmpappend(pool, loc, "-", pool_lookup_str(pool, entry, DELTA_LOCATION_EVR));
2163   loc = pool_tmpappend(pool, loc, ".", pool_lookup_str(pool, entry, DELTA_LOCATION_SUFFIX));
2164   return loc;
2165 }
2166
2167 static void
2168 add_new_provider(Pool *pool, Id id, Id p)
2169 {
2170   Queue q;
2171   Id *pp;
2172
2173   while (ISRELDEP(id))
2174     {
2175       Reldep *rd = GETRELDEP(pool, id);
2176       id = rd->name;
2177     }
2178
2179   queue_init(&q);
2180   for (pp = pool->whatprovidesdata + pool->whatprovides[id]; *pp; pp++)
2181     {
2182       if (*pp == p)
2183         {
2184           queue_free(&q);
2185           return;
2186         }
2187       if (*pp > p)
2188         {
2189           queue_push(&q, p);
2190           p = 0;
2191         }
2192       queue_push(&q, *pp);
2193     }
2194   if (p)
2195     queue_push(&q, p);
2196   pool->whatprovides[id] = pool_queuetowhatprovides(pool, &q);
2197   queue_free(&q);
2198 }
2199
2200 void
2201 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2202 {
2203   int hadhashes = pool->relhashtbl ? 1 : 0;
2204   Solvable *s;
2205   Id fn, p, q, md5;
2206   Id id;
2207   int i;
2208
2209   if (!conflicts->count)
2210     return;
2211   for (i = 0; i < conflicts->count; i += 6)
2212     {
2213       fn = conflicts->elements[i];
2214       p = conflicts->elements[i + 1];
2215       md5 = conflicts->elements[i + 2];
2216       q = conflicts->elements[i + 4];
2217       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2218       s = pool->solvables + p;
2219       if (!s->repo)
2220         continue;
2221       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2222       if (pool->whatprovides)
2223         add_new_provider(pool, fn, p);
2224       if (pool->whatprovides_rel)
2225         pool->whatprovides_rel[GETRELID(id)] = 0;       /* clear cache */
2226       s = pool->solvables + q;
2227       if (!s->repo)
2228         continue;
2229       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2230     }
2231   if (!hadhashes)
2232     pool_freeidhashes(pool);
2233 }
2234
2235 char *
2236 pool_prepend_rootdir(Pool *pool, const char *path)
2237 {
2238   if (!path)
2239     return 0;
2240   if (!pool->rootdir)
2241     return solv_strdup(path);
2242   return solv_dupjoin(pool->rootdir, "/", *path == '/' ? path + 1 : path);
2243 }
2244
2245 const char *
2246 pool_prepend_rootdir_tmp(Pool *pool, const char *path)
2247 {
2248   if (!path)
2249     return 0;
2250   if (!pool->rootdir)
2251     return path;
2252   return pool_tmpjoin(pool, pool->rootdir, "/", *path == '/' ? path + 1 : path);
2253 }
2254
2255 void
2256 pool_set_rootdir(Pool *pool, const char *rootdir)
2257 {
2258   solv_free(pool->rootdir);
2259   pool->rootdir = solv_strdup(rootdir);
2260 }
2261
2262 const char *
2263 pool_get_rootdir(Pool *pool)
2264 {
2265   return pool->rootdir;
2266 }
2267
2268 /* only used in libzypp */
2269 void
2270 pool_set_custom_vendorcheck(Pool *pool, int (*vendorcheck)(Pool *, Solvable *, Solvable *))
2271 {
2272   pool->custom_vendorcheck = vendorcheck;
2273 }
2274
2275 /* EOF */