get rid of the ugly policy callbacks while we're breaking the ABI, add new pool_set_c...
[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   switch (pool_evrcmp(pool, pevr, evr, EVRCMP_DEPCMP))
615     {
616     case -2:
617       return (pflags & REL_EQ) ? 1 : 0;
618     case -1:
619       return (flags & REL_LT) || (pflags & REL_GT) ? 1 : 0;
620     case 0:
621       return (flags & pflags & REL_EQ) ? 1 : 0;
622     case 1:
623       return (flags & REL_GT) || (pflags & REL_LT) ? 1 : 0;
624     case 2:
625       return (flags & REL_EQ) ? 1 : 0;
626     default:
627       break;
628     }
629   return 0;
630 }
631
632 /* match two dependencies (d1 = provider) */
633
634 int
635 pool_match_dep(Pool *pool, Id d1, Id d2)
636 {
637   Reldep *rd1, *rd2;
638
639   if (d1 == d2)
640     return 1;
641   if (!ISRELDEP(d1))
642     {
643       if (!ISRELDEP(d2))
644         return 0;
645       rd2 = GETRELDEP(pool, d2);
646       return pool_match_dep(pool, d1, rd2->name);
647     }
648   rd1 = GETRELDEP(pool, d1);
649   if (!ISRELDEP(d2))
650     {
651       return pool_match_dep(pool, rd1->name, d2);
652     }
653   rd2 = GETRELDEP(pool, d2);
654   /* first match name */
655   if (!pool_match_dep(pool, rd1->name, rd2->name))
656     return 0;
657   /* name matches, check flags and evr */
658   return pool_match_flags_evr(pool, rd1->flags, rd1->evr, rd2->flags, rd2->evr);
659 }
660
661 /*
662  * addrelproviders
663  * 
664  * add packages fulfilling the relation to whatprovides array
665  * no exact providers, do range match
666  * 
667  */
668
669 Id
670 pool_addrelproviders(Pool *pool, Id d)
671 {
672   Reldep *rd = GETRELDEP(pool, d);
673   Reldep *prd;
674   Queue plist;
675   Id buf[16];
676   Id name = rd->name;
677   Id evr = rd->evr;
678   int flags = rd->flags;
679   Id pid, *pidp;
680   Id p, *pp;
681
682   d = GETRELID(d);
683   queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
684
685   if (flags >= 8)
686     {
687       /* special relation */
688       Id wp = 0;
689       Id *pp2, *pp3;
690
691       switch (flags)
692         {
693         case REL_AND:
694         case REL_WITH:
695           wp = pool_whatprovides(pool, name);
696           pp2 = pool_whatprovides_ptr(pool, evr);
697           pp = pool->whatprovidesdata + wp;
698           while ((p = *pp++) != 0)
699             {
700               for (pp3 = pp2; *pp3; pp3++)
701                 if (*pp3 == p)
702                   break;
703               if (*pp3)
704                 queue_push(&plist, p);  /* found it */
705               else
706                 wp = 0;
707             }
708           break;
709         case REL_OR:
710           wp = pool_whatprovides(pool, name);
711           pp = pool->whatprovidesdata + wp;
712           if (!*pp)
713             wp = pool_whatprovides(pool, evr);
714           else
715             {
716               int cnt;
717               while ((p = *pp++) != 0)
718                 queue_push(&plist, p);
719               cnt = plist.count;
720               pp = pool_whatprovides_ptr(pool, evr);
721               while ((p = *pp++) != 0)
722                 queue_pushunique(&plist, p);
723               if (plist.count != cnt)
724                 wp = 0;
725             }
726           break;
727         case REL_NAMESPACE:
728           if (name == NAMESPACE_OTHERPROVIDERS)
729             {
730               wp = pool_whatprovides(pool, evr);
731               break;
732             }
733           if (pool->nscallback)
734             {
735               /* ask callback which packages provide the dependency
736                * 0:  none
737                * 1:  the system (aka SYSTEMSOLVABLE)
738                * >1: set of packages, stored as offset on whatprovidesdata
739                */
740               p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
741               if (p > 1)
742                 wp = p;
743               if (p == 1)
744                 queue_push(&plist, SYSTEMSOLVABLE);
745             }
746           break;
747         case REL_ARCH:
748           /* small hack: make it possible to match <pkg>.src
749            * we have to iterate over the solvables as src packages do not
750            * provide anything, thus they are not indexed in our
751            * whatprovides hash */
752           if (evr == ARCH_SRC || evr == ARCH_NOSRC)
753             {
754               Solvable *s;
755               for (p = 1, s = pool->solvables + p; p < pool->nsolvables; p++, s++)
756                 {
757                   if (!s->repo)
758                     continue;
759                   if (s->arch != evr && s->arch != ARCH_NOSRC)
760                     continue;
761                   if (pool_disabled_solvable(pool, s))
762                     continue;
763                   if (pool_match_nevr(pool, s, name))
764                     queue_push(&plist, p);
765                 }
766               break;
767             }
768           wp = pool_whatprovides(pool, name);
769           pp = pool->whatprovidesdata + wp;
770           while ((p = *pp++) != 0)
771             {
772               Solvable *s = pool->solvables + p;
773               if (s->arch == evr)
774                 queue_push(&plist, p);
775               else
776                 wp = 0;
777             }
778           break;
779         case REL_FILECONFLICT:
780           pp = pool_whatprovides_ptr(pool, name);
781           while ((p = *pp++) != 0)
782             {
783               Id origd = MAKERELDEP(d);
784               Solvable *s = pool->solvables + p;
785               if (!s->provides)
786                 continue;
787               pidp = s->repo->idarraydata + s->provides;
788               while ((pid = *pidp++) != 0)
789                 if (pid == origd)
790                   break;
791               if (pid)
792                 queue_push(&plist, p);
793             }
794           break;
795         default:
796           break;
797         }
798       if (wp)
799         {
800           /* we can reuse an existing entry */
801           queue_free(&plist);
802           pool->whatprovides_rel[d] = wp;
803           return wp;
804         }
805     }
806   else if (flags)
807     {
808       /* simple version comparison relation */
809 #if 0
810       POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: what provides %s?\n", pool_dep2str(pool, name));
811 #endif
812       pp = pool_whatprovides_ptr(pool, name);
813       while (ISRELDEP(name))
814         {
815           rd = GETRELDEP(pool, name);
816           name = rd->name;
817         }
818       while ((p = *pp++) != 0)
819         {
820           Solvable *s = pool->solvables + p;
821           if (!s->provides)
822             {
823               /* no provides - check nevr */
824               if (pool_match_nevr_rel(pool, s, MAKERELDEP(d)))
825                 queue_push(&plist, p);
826               continue;
827             }
828           /* solvable p provides name in some rels */
829           pidp = s->repo->idarraydata + s->provides;
830           while ((pid = *pidp++) != 0)
831             {
832               if (!ISRELDEP(pid))
833                 {
834                   if (pid != name)
835                     continue;           /* wrong provides name */
836                   if (pool->disttype == DISTTYPE_DEB)
837                     continue;           /* unversioned provides can never match versioned deps */
838                   break;
839                 }
840               prd = GETRELDEP(pool, pid);
841               if (prd->name != name)
842                 continue;               /* wrong provides name */
843               /* right package, both deps are rels. check flags/evr */
844               if (pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
845                 break;  /* matches */
846             }
847           if (!pid)
848             continue;   /* none of the providers matched */
849           queue_push(&plist, p);
850         }
851       /* make our system solvable provide all unknown rpmlib() stuff */
852       if (plist.count == 0 && !strncmp(pool_id2str(pool, name), "rpmlib(", 7))
853         queue_push(&plist, SYSTEMSOLVABLE);
854     }
855   /* add providers to whatprovides */
856 #if 0
857   POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
858 #endif
859   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
860   queue_free(&plist);
861
862   return pool->whatprovides_rel[d];
863 }
864
865 /*************************************************************************/
866
867 void
868 pool_debug(Pool *pool, int type, const char *format, ...)
869 {
870   va_list args;
871   char buf[1024];
872
873   if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0)
874     {
875       if ((pool->debugmask & type) == 0)
876         return;
877     }
878   va_start(args, format);
879   if (!pool->debugcallback)
880     {
881       if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0 && !(pool->debugmask & SOLV_DEBUG_TO_STDERR))
882         vprintf(format, args);
883       else
884         vfprintf(stderr, format, args);
885       return;
886     }
887   vsnprintf(buf, sizeof(buf), format, args);
888   va_end(args);
889   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
890 }
891
892 int
893 pool_error(Pool *pool, int ret, const char *format, ...)
894 {
895   va_list args;
896   int l;
897   va_start(args, format);
898   if (!pool->errstr)
899     {
900       pool->errstra = 1024;
901       pool->errstr = solv_malloc(pool->errstra);
902     }
903   if (!*format)
904     {
905       *pool->errstr = 0;
906       l = 0;
907     }
908   else
909     l = vsnprintf(pool->errstr, pool->errstra, format, args);
910   va_end(args);
911   if (l >= 0 && l + 1 > pool->errstra)
912     {
913       pool->errstra = l + 256;
914       pool->errstr = solv_realloc(pool->errstr, pool->errstra);
915       va_start(args, format);
916       l = vsnprintf(pool->errstr, pool->errstra, format, args);
917       va_end(args);
918     }
919   if (l < 0)
920     strcpy(pool->errstr, "unknown error");
921   if (pool->debugmask & SOLV_ERROR)
922     pool_debug(pool, SOLV_ERROR, "%s\n", pool->errstr);
923   return ret;
924 }
925
926 char *
927 pool_errstr(Pool *pool)
928 {
929   return pool->errstr ? pool->errstr : "no error";
930 }
931
932 void
933 pool_setdebuglevel(Pool *pool, int level)
934 {
935   int mask = SOLV_DEBUG_RESULT;
936   if (level > 0)
937     mask |= SOLV_DEBUG_STATS|SOLV_DEBUG_ANALYZE|SOLV_DEBUG_UNSOLVABLE|SOLV_DEBUG_SOLVER|SOLV_DEBUG_TRANSACTION|SOLV_ERROR;
938   if (level > 1)
939     mask |= SOLV_DEBUG_JOB|SOLV_DEBUG_SOLUTIONS|SOLV_DEBUG_POLICY;
940   if (level > 2)
941     mask |= SOLV_DEBUG_PROPAGATE;
942   if (level > 3)
943     mask |= SOLV_DEBUG_RULE_CREATION;
944   mask |= pool->debugmask & SOLV_DEBUG_TO_STDERR;       /* keep bit */
945   pool->debugmask = mask;
946 }
947
948 void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
949 {
950   pool->debugcallback = debugcallback;
951   pool->debugcallbackdata = debugcallbackdata;
952 }
953
954 void pool_setdebugmask(Pool *pool, int mask)
955 {
956   pool->debugmask = mask;
957 }
958
959 void pool_setloadcallback(Pool *pool, int (*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
960 {
961   pool->loadcallback = cb;
962   pool->loadcallbackdata = loadcbdata;
963 }
964
965 /*************************************************************************/
966
967 struct searchfiles {
968   Id *ids;
969   char **dirs;
970   char **names;
971   int nfiles;
972   Map seen;
973 };
974
975 #define SEARCHFILES_BLOCK 127
976
977 static void
978 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
979 {
980   Id dep, sid;
981   const char *s, *sr;
982   struct searchfiles *csf;
983
984   while ((dep = *ida++) != 0)
985     {
986       csf = sf;
987       while (ISRELDEP(dep))
988         {
989           Reldep *rd;
990           sid = pool->ss.nstrings + GETRELID(dep);
991           if (MAPTST(&csf->seen, sid))
992             {
993               dep = 0;
994               break;
995             }
996           MAPSET(&csf->seen, sid);
997           rd = GETRELDEP(pool, dep);
998           if (rd->flags < 8)
999             dep = rd->name;
1000           else if (rd->flags == REL_NAMESPACE)
1001             {
1002               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
1003                 {
1004                   csf = isf;
1005                   if (!csf || MAPTST(&csf->seen, sid))
1006                     {
1007                       dep = 0;
1008                       break;
1009                     }
1010                   MAPSET(&csf->seen, sid);
1011                 }
1012               dep = rd->evr;
1013             }
1014           else if (rd->flags == REL_FILECONFLICT)
1015             {
1016               dep = 0;
1017               break;
1018             }
1019           else
1020             {
1021               Id ids[2];
1022               ids[0] = rd->name;
1023               ids[1] = 0;
1024               pool_addfileprovides_dep(pool, ids, csf, isf);
1025               dep = rd->evr;
1026             }
1027         }
1028       if (!dep)
1029         continue;
1030       if (MAPTST(&csf->seen, dep))
1031         continue;
1032       MAPSET(&csf->seen, dep);
1033       s = pool_id2str(pool, dep);
1034       if (*s != '/')
1035         continue;
1036       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1037       csf->dirs = solv_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1038       csf->names = solv_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1039       csf->ids[csf->nfiles] = dep;
1040       sr = strrchr(s, '/');
1041       csf->names[csf->nfiles] = solv_strdup(sr + 1);
1042       csf->dirs[csf->nfiles] = solv_malloc(sr - s + 1);
1043       if (sr != s)
1044         strncpy(csf->dirs[csf->nfiles], s, sr - s);
1045       csf->dirs[csf->nfiles][sr - s] = 0;
1046       csf->nfiles++;
1047     }
1048 }
1049
1050 struct addfileprovides_cbdata {
1051   int nfiles;
1052   Id *ids;
1053   char **dirs;
1054   char **names;
1055
1056   Id *dids;
1057
1058   Map providedids;
1059
1060   Map useddirs;
1061 };
1062
1063 static int
1064 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1065 {
1066   struct addfileprovides_cbdata *cbd = cbdata;
1067   int i;
1068
1069   if (!cbd->useddirs.size)
1070     {
1071       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
1072       for (i = 0; i < cbd->nfiles; i++)
1073         {
1074           Id did;
1075           if (MAPTST(&cbd->providedids, cbd->ids[i]))
1076             {
1077               cbd->dids[i] = 0;
1078               continue;
1079             }
1080           did = repodata_str2dir(data, cbd->dirs[i], 0);
1081           cbd->dids[i] = did;
1082           if (did)
1083             MAPSET(&cbd->useddirs, did);
1084         }
1085       repodata_free_dircache(data);
1086     }
1087   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
1088     return 0;
1089   for (i = 0; i < cbd->nfiles; i++)
1090     {
1091       if (cbd->dids[i] != value->id)
1092         continue;
1093       if (!strcmp(cbd->names[i], value->str))
1094         break;
1095     }
1096   if (i == cbd->nfiles)
1097     return 0;
1098   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
1099   return 0;
1100 }
1101
1102 static void
1103 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
1104 {
1105   Id p;
1106   Repodata *data;
1107   Repo *repo;
1108   Queue fileprovidesq;
1109   int i, j, repoid, repodataid;
1110   int provstart, provend;
1111   Map donemap;
1112   int ndone, incomplete;
1113
1114   if (!pool->urepos)
1115     return;
1116
1117   cbd->nfiles = sf->nfiles;
1118   cbd->ids = sf->ids;
1119   cbd->dirs = sf->dirs;
1120   cbd->names = sf->names;
1121   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
1122   map_init(&cbd->providedids, pool->ss.nstrings);
1123
1124   repoid = 1;
1125   repo = repoonly ? repoonly : pool->repos[repoid];
1126   map_init(&donemap, pool->nsolvables);
1127   queue_init(&fileprovidesq);
1128   provstart = provend = 0;
1129   for (;;)
1130     {
1131       if (!repo || repo->disabled)
1132         {
1133           if (repoonly || ++repoid == pool->nrepos)
1134             break;
1135           repo = pool->repos[repoid];
1136           continue;
1137         }
1138       ndone = 0;
1139       FOR_REPODATAS(repo, repodataid, data)
1140         {
1141           if (ndone >= repo->nsolvables)
1142             break;
1143
1144           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1145             {
1146               map_empty(&cbd->providedids);
1147               for (i = 0; i < fileprovidesq.count; i++)
1148                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1149               provstart = data->start;
1150               provend = data->end;
1151               for (i = 0; i < cbd->nfiles; i++)
1152                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1153                   break;
1154               if (i == cbd->nfiles)
1155                 {
1156                   /* great! no need to search files */
1157                   for (p = data->start; p < data->end; p++)
1158                     if (pool->solvables[p].repo == repo)
1159                       {
1160                         if (MAPTST(&donemap, p))
1161                           continue;
1162                         MAPSET(&donemap, p);
1163                         ndone++;
1164                       }
1165                   continue;
1166                 }
1167             }
1168
1169           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1170             continue;
1171
1172           if (data->start < provstart || data->end > provend)
1173             {
1174               map_empty(&cbd->providedids);
1175               provstart = provend = 0;
1176             }
1177
1178           /* check if the data is incomplete */
1179           incomplete = 0;
1180           if (data->state == REPODATA_AVAILABLE)
1181             {
1182               for (j = 1; j < data->nkeys; j++)
1183                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1184                   break;
1185               if (j < data->nkeys)
1186                 {
1187 #if 0
1188                   for (i = 0; i < cbd->nfiles; i++)
1189                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1190                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1191 #endif
1192                   for (i = 0; i < cbd->nfiles; i++)
1193                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1194                       break;
1195                   if (i < cbd->nfiles)
1196                     incomplete = 1;
1197                 }
1198             }
1199
1200           /* do the search */
1201           map_init(&cbd->useddirs, 0);
1202           for (p = data->start; p < data->end; p++)
1203             if (pool->solvables[p].repo == repo)
1204               {
1205                 if (MAPTST(&donemap, p))
1206                   continue;
1207                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1208                 if (!incomplete)
1209                   {
1210                     MAPSET(&donemap, p);
1211                     ndone++;
1212                   }
1213               }
1214           map_free(&cbd->useddirs);
1215         }
1216
1217       if (repoonly || ++repoid == pool->nrepos)
1218         break;
1219       repo = pool->repos[repoid];
1220     }
1221   map_free(&donemap);
1222   queue_free(&fileprovidesq);
1223   map_free(&cbd->providedids);
1224 }
1225
1226 void
1227 pool_addfileprovides_queue(Pool *pool, Queue *idq, Queue *idqinst)
1228 {
1229   Solvable *s;
1230   Repo *installed, *repo;
1231   struct searchfiles sf, isf, *isfp;
1232   struct addfileprovides_cbdata cbd;
1233   int i;
1234   unsigned int now;
1235
1236   installed = pool->installed;
1237   now = solv_timems(0);
1238   memset(&sf, 0, sizeof(sf));
1239   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1240   memset(&isf, 0, sizeof(isf));
1241   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1242
1243   if (idq)
1244     queue_empty(idq);
1245   if (idqinst)
1246     queue_empty(idqinst);
1247   isfp = installed ? &isf : 0;
1248   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1249     {
1250       repo = s->repo;
1251       if (!repo)
1252         continue;
1253       if (s->obsoletes)
1254         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1255       if (s->conflicts)
1256         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1257       if (s->requires)
1258         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1259       if (s->recommends)
1260         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1261       if (s->suggests)
1262         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1263       if (s->supplements)
1264         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1265       if (s->enhances)
1266         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1267     }
1268   map_free(&sf.seen);
1269   map_free(&isf.seen);
1270   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1271   cbd.dids = 0;
1272   if (sf.nfiles)
1273     {
1274 #if 0
1275       for (i = 0; i < sf.nfiles; i++)
1276         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1277 #endif
1278       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1279       if (idq)
1280         for (i = 0; i < sf.nfiles; i++)
1281           queue_push(idq, sf.ids[i]);
1282       if (idqinst)
1283         for (i = 0; i < sf.nfiles; i++)
1284           queue_push(idqinst, sf.ids[i]);
1285       solv_free(sf.ids);
1286       for (i = 0; i < sf.nfiles; i++)
1287         {
1288           solv_free(sf.dirs[i]);
1289           solv_free(sf.names[i]);
1290         }
1291       solv_free(sf.dirs);
1292       solv_free(sf.names);
1293     }
1294   if (isf.nfiles)
1295     {
1296 #if 0
1297       for (i = 0; i < isf.nfiles; i++)
1298         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1299 #endif
1300       if (installed)
1301         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1302       if (installed && idqinst)
1303         for (i = 0; i < isf.nfiles; i++)
1304           queue_pushunique(idqinst, isf.ids[i]);
1305       solv_free(isf.ids);
1306       for (i = 0; i < isf.nfiles; i++)
1307         {
1308           solv_free(isf.dirs[i]);
1309           solv_free(isf.names[i]);
1310         }
1311       solv_free(isf.dirs);
1312       solv_free(isf.names);
1313     }
1314   solv_free(cbd.dids);
1315   pool_freewhatprovides(pool);  /* as we have added provides */
1316   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1317 }
1318
1319 void
1320 pool_addfileprovides(Pool *pool)
1321 {
1322   pool_addfileprovides_queue(pool, 0, 0);
1323 }
1324
1325 void
1326 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)
1327 {
1328   if (p)
1329     {
1330       if (pool->solvables[p].repo)
1331         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1332       return;
1333     }
1334   /* FIXME: obey callback return value! */
1335   for (p = 1; p < pool->nsolvables; p++)
1336     if (pool->solvables[p].repo)
1337       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1338 }
1339
1340 void
1341 pool_clear_pos(Pool *pool)
1342 {
1343   memset(&pool->pos, 0, sizeof(pool->pos));
1344 }
1345
1346
1347 void
1348 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1349 {
1350   int i;
1351
1352   pool->languagecache = solv_free(pool->languagecache);
1353   pool->languagecacheother = 0;
1354   if (pool->nlanguages)
1355     {
1356       for (i = 0; i < pool->nlanguages; i++)
1357         free((char *)pool->languages[i]);
1358       free(pool->languages);
1359     }
1360   pool->nlanguages = nlanguages;
1361   if (!nlanguages)
1362     return;
1363   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1364   for (i = 0; i < pool->nlanguages; i++)
1365     pool->languages[i] = solv_strdup(languages[i]);
1366 }
1367
1368 Id
1369 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1370 {
1371   const char *n;
1372   char buf[256], *p;
1373   int l;
1374
1375   if (!lang || !*lang)
1376     return id;
1377   n = pool_id2str(pool, id);
1378   l = strlen(n) + strlen(lang) + 2;
1379   if (l > sizeof(buf))
1380     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1381   else
1382     p = buf;
1383   sprintf(p, "%s:%s", n, lang);
1384   id = pool_str2id(pool, p, create);
1385   if (p != buf)
1386     free(p);
1387   return id;
1388 }
1389
1390 char *
1391 pool_alloctmpspace(Pool *pool, int len)
1392 {
1393   int n = pool->tmpspace.n;
1394   if (!len)
1395     return 0;
1396   if (len > pool->tmpspace.len[n])
1397     {
1398       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1399       pool->tmpspace.len[n] = len + 32;
1400     }
1401   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1402   return pool->tmpspace.buf[n];
1403 }
1404
1405 static char *
1406 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1407 {
1408   if (space)
1409     {
1410       int n, oldn;
1411       n = oldn = pool->tmpspace.n;
1412       for (;;)
1413         {
1414           if (!n--)
1415             n = POOL_TMPSPACEBUF - 1;
1416           if (n == oldn)
1417             break;
1418           if (pool->tmpspace.buf[n] != space)
1419             continue;
1420           if (len > pool->tmpspace.len[n])
1421             {
1422               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1423               pool->tmpspace.len[n] = len + 32;
1424             }
1425           return pool->tmpspace.buf[n];
1426         }
1427     }
1428   return 0;
1429 }
1430
1431 void
1432 pool_freetmpspace(Pool *pool, const char *space)
1433 {
1434   int n = pool->tmpspace.n;
1435   if (!space)
1436     return;
1437   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1438   if (pool->tmpspace.buf[n] == space)
1439     pool->tmpspace.n = n;
1440 }
1441
1442 char *
1443 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1444 {
1445   int l1, l2, l3;
1446   char *s, *str;
1447   l1 = str1 ? strlen(str1) : 0;
1448   l2 = str2 ? strlen(str2) : 0;
1449   l3 = str3 ? strlen(str3) : 0;
1450   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1451   if (l1)
1452     {
1453       strcpy(s, str1);
1454       s += l1;
1455     }
1456   if (l2)
1457     {
1458       strcpy(s, str2);
1459       s += l2;
1460     }
1461   if (l3)
1462     {
1463       strcpy(s, str3);
1464       s += l3;
1465     }
1466   *s = 0;
1467   return str;
1468 }
1469
1470 char *
1471 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1472 {
1473   int l1, l2, l3;
1474   char *s, *str;
1475
1476   l1 = str1 ? strlen(str1) : 0;
1477   l2 = str2 ? strlen(str2) : 0;
1478   l3 = str3 ? strlen(str3) : 0;
1479   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1480   if (str)
1481     str1 = str;
1482   else
1483     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1484   s = str;
1485   if (l1)
1486     {
1487       if (s != str1)
1488         strcpy(s, str1);
1489       s += l1;
1490     }
1491   if (l2)
1492     {
1493       strcpy(s, str2);
1494       s += l2;
1495     }
1496   if (l3)
1497     {
1498       strcpy(s, str3);
1499       s += l3;
1500     }
1501   *s = 0;
1502   return str;
1503 }
1504
1505 const char *
1506 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1507 {
1508   char *s;
1509   if (!len)
1510     return "";
1511   s = pool_alloctmpspace(pool, 2 * len + 1);
1512   solv_bin2hex(buf, len, s);
1513   return s;
1514 }
1515
1516 /*******************************************************************/
1517
1518 struct mptree {
1519   Id sibling;
1520   Id child;
1521   const char *comp;
1522   int compl;
1523   Id mountpoint;
1524 };
1525
1526 struct ducbdata {
1527   DUChanges *mps;
1528   struct mptree *mptree;
1529   int addsub;
1530   int hasdu;
1531
1532   Id *dirmap;
1533   int nmap;
1534   Repodata *olddata;
1535 };
1536
1537
1538 static int
1539 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1540 {
1541   struct ducbdata *cbd = cbdata;
1542   Id mp;
1543
1544   if (data != cbd->olddata)
1545     {
1546       Id dn, mp, comp, *dirmap, *dirs;
1547       int i, compl;
1548       const char *compstr;
1549       struct mptree *mptree;
1550
1551       /* create map from dir to mptree */
1552       cbd->dirmap = solv_free(cbd->dirmap);
1553       cbd->nmap = 0;
1554       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1555       mptree = cbd->mptree;
1556       mp = 0;
1557       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1558         {
1559           comp = *dirs++;
1560           if (comp <= 0)
1561             {
1562               mp = dirmap[-comp];
1563               continue;
1564             }
1565           if (mp < 0)
1566             {
1567               /* unconnected */
1568               dirmap[dn] = mp;
1569               continue;
1570             }
1571           if (!mptree[mp].child)
1572             {
1573               dirmap[dn] = -mp;
1574               continue;
1575             }
1576           if (data->localpool)
1577             compstr = stringpool_id2str(&data->spool, comp);
1578           else
1579             compstr = pool_id2str(data->repo->pool, comp);
1580           compl = strlen(compstr);
1581           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1582             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1583               break;
1584           dirmap[dn] = i ? i : -mp;
1585         }
1586       /* change dirmap to point to mountpoint instead of mptree */
1587       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1588         {
1589           mp = dirmap[dn];
1590           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1591         }
1592       cbd->dirmap = dirmap;
1593       cbd->nmap = data->dirpool.ndirs;
1594       cbd->olddata = data;
1595     }
1596   cbd->hasdu = 1;
1597   if (value->id < 0 || value->id >= cbd->nmap)
1598     return 0;
1599   mp = cbd->dirmap[value->id];
1600   if (mp < 0)
1601     return 0;
1602   if (cbd->addsub > 0)
1603     {
1604       cbd->mps[mp].kbytes += value->num;
1605       cbd->mps[mp].files += value->num2;
1606     }
1607   else
1608     {
1609       cbd->mps[mp].kbytes -= value->num;
1610       cbd->mps[mp].files -= value->num2;
1611     }
1612   return 0;
1613 }
1614
1615 static void
1616 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1617 {
1618   int i;
1619   if (mptree[pos].mountpoint == -1)
1620     mptree[pos].mountpoint = mountpoint;
1621   else
1622     mountpoint = mptree[pos].mountpoint;
1623   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1624     propagate_mountpoints(mptree, i, mountpoint);
1625 }
1626
1627 #define MPTREE_BLOCK 15
1628
1629 void
1630 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1631 {
1632   char *p;
1633   const char *path, *compstr;
1634   struct mptree *mptree;
1635   int i, nmptree;
1636   int pos, compl;
1637   int mp;
1638   struct ducbdata cbd;
1639   Solvable *s;
1640   Id sp;
1641   Map ignoredu;
1642   Repo *oldinstalled = pool->installed;
1643
1644   memset(&ignoredu, 0, sizeof(ignoredu));
1645   cbd.mps = mps;
1646   cbd.addsub = 0;
1647   cbd.dirmap = 0;
1648   cbd.nmap = 0;
1649   cbd.olddata = 0;
1650
1651   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1652
1653   /* our root node */
1654   mptree[0].sibling = 0;
1655   mptree[0].child = 0;
1656   mptree[0].comp = 0;
1657   mptree[0].compl = 0;
1658   mptree[0].mountpoint = -1;
1659   nmptree = 1;
1660   
1661   /* create component tree */
1662   for (mp = 0; mp < nmps; mp++)
1663     {
1664       mps[mp].kbytes = 0;
1665       mps[mp].files = 0;
1666       pos = 0;
1667       path = mps[mp].path;
1668       while(*path == '/')
1669         path++;
1670       while (*path)
1671         {
1672           if ((p = strchr(path, '/')) == 0)
1673             {
1674               compstr = path;
1675               compl = strlen(compstr);
1676               path += compl;
1677             }
1678           else
1679             {
1680               compstr = path;
1681               compl = p - path;
1682               path = p + 1;
1683               while(*path == '/')
1684                 path++;
1685             }
1686           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1687             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1688               break;
1689           if (!i)
1690             {
1691               /* create new node */
1692               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1693               i = nmptree++;
1694               mptree[i].sibling = mptree[pos].child;
1695               mptree[i].child = 0;
1696               mptree[i].comp = compstr;
1697               mptree[i].compl = compl;
1698               mptree[i].mountpoint = -1;
1699               mptree[pos].child = i;
1700             }
1701           pos = i;
1702         }
1703       mptree[pos].mountpoint = mp;
1704     }
1705
1706   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1707
1708 #if 0
1709   for (i = 0; i < nmptree; i++)
1710     {
1711       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1712       printf("#%d child: %d\n", i, mptree[i].child);
1713       printf("#%d comp: %s\n", i, mptree[i].comp);
1714       printf("#%d compl: %d\n", i, mptree[i].compl);
1715       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1716     }
1717 #endif
1718
1719   cbd.mptree = mptree;
1720   cbd.addsub = 1;
1721   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1722     {
1723       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1724         continue;
1725       if (!MAPTST(installedmap, sp))
1726         continue;
1727       cbd.hasdu = 0;
1728       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1729       if (!cbd.hasdu && oldinstalled)
1730         {
1731           Id op, opp;
1732           /* no du data available, ignore data of all installed solvables we obsolete */
1733           if (!ignoredu.map)
1734             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1735           if (s->obsoletes)
1736             {
1737               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1738               while ((obs = *obsp++) != 0)
1739                 FOR_PROVIDES(op, opp, obs)
1740                   if (op >= oldinstalled->start && op < oldinstalled->end)
1741                     MAPSET(&ignoredu, op - oldinstalled->start);
1742             }
1743           FOR_PROVIDES(op, opp, s->name)
1744             if (pool->solvables[op].name == s->name)
1745               if (op >= oldinstalled->start && op < oldinstalled->end)
1746                 MAPSET(&ignoredu, op - oldinstalled->start);
1747         }
1748     }
1749   cbd.addsub = -1;
1750   if (oldinstalled)
1751     {
1752       /* assumes we allways have du data for installed solvables */
1753       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1754         {
1755           if (MAPTST(installedmap, sp))
1756             continue;
1757           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1758             continue;
1759           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1760         }
1761     }
1762   if (ignoredu.map)
1763     map_free(&ignoredu);
1764   solv_free(cbd.dirmap);
1765   solv_free(mptree);
1766 }
1767
1768 int
1769 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1770 {
1771   Id sp;
1772   Solvable *s;
1773   int change = 0;
1774   Repo *oldinstalled = pool->installed;
1775
1776   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1777     {
1778       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1779         continue;
1780       if (!MAPTST(installedmap, sp))
1781         continue;
1782       change += solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
1783     }
1784   if (oldinstalled)
1785     {
1786       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1787         {
1788           if (MAPTST(installedmap, sp))
1789             continue;
1790           change -= solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
1791         }
1792     }
1793   return change;
1794 }
1795
1796 /* map:
1797  *  1: installed
1798  *  2: conflicts with installed
1799  *  8: interesting (only true if installed)
1800  * 16: undecided
1801  */
1802  
1803 static inline Id dep2name(Pool *pool, Id dep)
1804 {
1805   while (ISRELDEP(dep))
1806     {
1807       Reldep *rd = rd = GETRELDEP(pool, dep);
1808       dep = rd->name;
1809     }
1810   return dep;
1811 }
1812
1813 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
1814 {
1815   Id p, pp;
1816   Solvable *sn = pool->solvables + n; 
1817
1818   FOR_PROVIDES(p, pp, sn->name)
1819     {    
1820       Solvable *s = pool->solvables + p; 
1821       if (s->name != sn->name || s->arch != sn->arch)
1822         continue;
1823       if ((map[p] & 9) != 9)
1824         continue;
1825       if (pool_match_nevr(pool, pool->solvables + p, con))
1826         continue;
1827       return 1;         /* found installed package that doesn't conflict */
1828     }
1829   return 0;
1830 }
1831
1832 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *noobsoletesmap)
1833 {
1834   Id p, pp;
1835   int r = 0;
1836   FOR_PROVIDES(p, pp, dep)
1837     {
1838       if (p == SYSTEMSOLVABLE)
1839         return 1;       /* always boring, as never constraining */
1840       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
1841         continue;
1842       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
1843         if (providedbyinstalled_multiversion(pool, map, p, dep))
1844           continue;
1845       if ((map[p] & 9) == 9)
1846         return 9;
1847       r |= map[p] & 17;
1848     }
1849   return r;
1850 }
1851
1852 /*
1853  * pool_trivial_installable - calculate if a set of solvables is
1854  * trivial installable without any other installs/deinstalls of
1855  * packages not belonging to the set.
1856  *
1857  * the state is returned in the result queue:
1858  * 1:  solvable is installable without any other package changes
1859  * 0:  solvable is not installable
1860  * -1: solvable is installable, but doesn't constrain any installed packages
1861  */
1862
1863 void
1864 pool_trivial_installable_noobsoletesmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *noobsoletesmap)
1865 {
1866   int i, r, m, did;
1867   Id p, *dp, con, *conp, req, *reqp;
1868   unsigned char *map;
1869   Solvable *s;
1870
1871   map = solv_calloc(pool->nsolvables, 1);
1872   for (p = 1; p < pool->nsolvables; p++)
1873     {
1874       if (!MAPTST(installedmap, p))
1875         continue;
1876       map[p] |= 9;
1877       s = pool->solvables + p;
1878       if (!s->conflicts)
1879         continue;
1880       conp = s->repo->idarraydata + s->conflicts;
1881       while ((con = *conp++) != 0)
1882         {
1883           dp = pool_whatprovides_ptr(pool, con);
1884           for (; *dp; dp++)
1885             map[p] |= 2;        /* XXX: self conflict ? */
1886         }
1887     }
1888   for (i = 0; i < pkgs->count; i++)
1889     map[pkgs->elements[i]] = 16;
1890
1891   for (i = 0, did = 0; did < pkgs->count; i++, did++)
1892     {
1893       if (i == pkgs->count)
1894         i = 0;
1895       p = pkgs->elements[i];
1896       if ((map[p] & 16) == 0)
1897         continue;
1898       if ((map[p] & 2) != 0)
1899         {
1900           map[p] = 2;
1901           continue;
1902         }
1903       s = pool->solvables + p;
1904       m = 1;
1905       if (s->requires)
1906         {
1907           reqp = s->repo->idarraydata + s->requires;
1908           while ((req = *reqp++) != 0)
1909             {
1910               if (req == SOLVABLE_PREREQMARKER)
1911                 continue;
1912               r = providedbyinstalled(pool, map, req, 0, 0);
1913               if (!r)
1914                 {
1915                   /* decided and miss */
1916                   map[p] = 2;
1917                   did = 0;
1918                   break;
1919                 }
1920               if (r == 16)
1921                 break;  /* undecided */
1922               m |= r;   /* 1 | 9 | 17 */
1923             }
1924           if (req)
1925             continue;
1926           if ((m & 9) == 9)
1927             m = 9;
1928         }
1929       if (s->conflicts)
1930         {
1931           int ispatch = 0;      /* see solver.c patch handling */
1932
1933           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
1934             ispatch = 1;
1935           conp = s->repo->idarraydata + s->conflicts;
1936           while ((con = *conp++) != 0)
1937             {
1938               if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1939                 {
1940                   map[p] = 2;
1941                   did = 0;
1942                   break;
1943                 }
1944               if ((m == 1 || m == 17) && ISRELDEP(con))
1945                 {
1946                   con = dep2name(pool, con);
1947                   if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1948                     m = 9;
1949                 }
1950             }
1951           if (con)
1952             continue;   /* found a conflict */
1953         }
1954 #if 0
1955       if (s->repo && s->repo != oldinstalled)
1956         {
1957           Id p2, obs, *obsp, *pp;
1958           Solvable *s2;
1959           if (s->obsoletes)
1960             {
1961               obsp = s->repo->idarraydata + s->obsoletes;
1962               while ((obs = *obsp++) != 0)
1963                 {
1964                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
1965                     {
1966                       map[p] = 2;
1967                       break;
1968                     }
1969                 }
1970               if (obs)
1971                 continue;
1972             }
1973           FOR_PROVIDES(p2, pp, s->name)
1974             {
1975               s2 = pool->solvables + p2;
1976               if (s2->name == s->name && (map[p2] & 1) != 0)
1977                 {
1978                   map[p] = 2;
1979                   break;
1980                 }
1981             }
1982           if (p2)
1983             continue;
1984         }
1985 #endif
1986       if (m != map[p])
1987         {
1988           map[p] = m;
1989           did = 0;
1990         }
1991     }
1992   queue_free(res);
1993   queue_init_clone(res, pkgs);
1994   for (i = 0; i < pkgs->count; i++)
1995     {
1996       m = map[pkgs->elements[i]];
1997       if ((m & 9) == 9)
1998         r = 1;
1999       else if (m & 1)
2000         r = -1;
2001       else
2002         r = 0;
2003       res->elements[i] = r;
2004     }
2005   free(map);
2006 }
2007
2008 void
2009 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
2010 {
2011   pool_trivial_installable_noobsoletesmap(pool, installedmap, pkgs, res, 0);
2012 }
2013
2014 const char *
2015 pool_lookup_str(Pool *pool, Id entry, Id keyname)
2016 {
2017   if (entry == SOLVID_POS && pool->pos.repo)
2018     return repodata_lookup_str(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
2019   if (entry <= 0)
2020     return 0;
2021   return solvable_lookup_str(pool->solvables + entry, keyname);
2022 }
2023
2024 Id
2025 pool_lookup_id(Pool *pool, Id entry, Id keyname)
2026 {
2027   if (entry == SOLVID_POS && pool->pos.repo)
2028     {
2029       Repodata *data = pool->pos.repo->repodata + pool->pos.repodataid;
2030       Id id = repodata_lookup_id(data, SOLVID_POS, keyname);
2031       return data->localpool ? repodata_globalize_id(data, id, 1) : id;
2032     }
2033   if (entry <= 0)
2034     return 0;
2035   return solvable_lookup_id(pool->solvables + entry, keyname);
2036 }
2037
2038 unsigned long long
2039 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned long long notfound)
2040 {
2041   if (entry == SOLVID_POS && pool->pos.repo)
2042     {
2043       unsigned long long value;
2044       if (repodata_lookup_num(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, &value))
2045         return value;
2046       return notfound;
2047     }
2048   if (entry <= 0)
2049     return notfound;
2050   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
2051 }
2052
2053 int
2054 pool_lookup_void(Pool *pool, Id entry, Id keyname)
2055 {
2056   if (entry == SOLVID_POS && pool->pos.repo)
2057     return repodata_lookup_void(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
2058   if (entry <= 0)
2059     return 0;
2060   return solvable_lookup_void(pool->solvables + entry, keyname);
2061 }
2062
2063 const unsigned char *
2064 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2065 {
2066   if (entry == SOLVID_POS && pool->pos.repo)
2067     return repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
2068   if (entry <= 0)
2069     return 0;
2070   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
2071 }
2072
2073 const char *
2074 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2075 {
2076   if (entry == SOLVID_POS && pool->pos.repo)
2077     {
2078       const unsigned char *chk = repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
2079       return chk ? repodata_chk2str(pool->pos.repo->repodata + pool->pos.repodataid, *typep, chk) : 0;
2080     }
2081   if (entry <= 0)
2082     return 0;
2083   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2084 }
2085
2086 const char *
2087 pool_lookup_deltalocation(Pool *pool, Id entry, unsigned int *medianrp)
2088 {
2089   const char *loc;
2090   if (medianrp)
2091     *medianrp = 0;
2092   if (entry != SOLVID_POS)
2093     return 0;
2094   loc = pool_lookup_str(pool, entry, DELTA_LOCATION_DIR);
2095   loc = pool_tmpjoin(pool, loc, loc ? "/" : 0, pool_lookup_str(pool, entry, DELTA_LOCATION_NAME));
2096   loc = pool_tmpappend(pool, loc, "-", pool_lookup_str(pool, entry, DELTA_LOCATION_EVR));
2097   loc = pool_tmpappend(pool, loc, ".", pool_lookup_str(pool, entry, DELTA_LOCATION_SUFFIX));
2098   return loc;
2099 }
2100
2101 static void
2102 add_new_provider(Pool *pool, Id id, Id p)
2103 {
2104   Queue q;
2105   Id *pp;
2106
2107   while (ISRELDEP(id))
2108     {
2109       Reldep *rd = GETRELDEP(pool, id);
2110       id = rd->name;
2111     }
2112
2113   queue_init(&q);
2114   for (pp = pool->whatprovidesdata + pool->whatprovides[id]; *pp; pp++)
2115     {
2116       if (*pp == p)
2117         {
2118           queue_free(&q);
2119           return;
2120         }
2121       if (*pp > p)
2122         {
2123           queue_push(&q, p);
2124           p = 0;
2125         }
2126       queue_push(&q, *pp);
2127     }
2128   if (p)
2129     queue_push(&q, p);
2130   pool->whatprovides[id] = pool_queuetowhatprovides(pool, &q);
2131   queue_free(&q);
2132 }
2133
2134 void
2135 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2136 {
2137   int hadhashes = pool->relhashtbl ? 1 : 0;
2138   Solvable *s;
2139   Id fn, p, q, md5;
2140   Id id;
2141   int i;
2142
2143   if (!conflicts->count)
2144     return;
2145   for (i = 0; i < conflicts->count; i += 5)
2146     {
2147       fn = conflicts->elements[i];
2148       p = conflicts->elements[i + 1];
2149       md5 = conflicts->elements[i + 2];
2150       q = conflicts->elements[i + 3];
2151       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2152       s = pool->solvables + p;
2153       if (!s->repo)
2154         continue;
2155       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2156       if (pool->whatprovides)
2157         add_new_provider(pool, fn, p);
2158       if (pool->whatprovides_rel)
2159         pool->whatprovides_rel[GETRELID(id)] = 0;       /* clear cache */
2160       s = pool->solvables + q;
2161       if (!s->repo)
2162         continue;
2163       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2164     }
2165   if (!hadhashes)
2166     pool_freeidhashes(pool);
2167 }
2168
2169 char *
2170 pool_prepend_rootdir(Pool *pool, const char *path)
2171 {
2172   if (!path)
2173     return 0;
2174   if (!pool->rootdir)
2175     return solv_strdup(path);
2176   return solv_dupjoin(pool->rootdir, "/", *path == '/' ? path + 1 : path);
2177 }
2178
2179 const char *
2180 pool_prepend_rootdir_tmp(Pool *pool, const char *path)
2181 {
2182   if (!path)
2183     return 0;
2184   if (!pool->rootdir)
2185     return path;
2186   return pool_tmpjoin(pool, pool->rootdir, "/", *path == '/' ? path + 1 : path);
2187 }
2188
2189 void
2190 pool_set_rootdir(Pool *pool, const char *rootdir)
2191 {
2192   solv_free(pool->rootdir);
2193   pool->rootdir = solv_strdup(rootdir);
2194 }
2195
2196 const char *
2197 pool_get_rootdir(Pool *pool)
2198 {
2199   return pool->rootdir;
2200 }
2201
2202 /* only used in libzypp */
2203 void
2204 pool_set_custom_vendorcheck(Pool *pool, int (*vendorcheck)(Pool *, Solvable *, Solvable *))
2205 {
2206   pool->custom_vendorcheck = vendorcheck;
2207 }
2208
2209 /* EOF */