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