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