Imported Upstream version 0.6.11
[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 #ifdef FEDORA
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   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           wp = pool_whatprovides(pool, name);
1018           if (!pool->whatprovidesdata[wp])
1019             wp = pool_whatprovides(pool, evr);
1020           else
1021             {
1022               /* sorted merge */
1023               pp2 = pool_whatprovides_ptr(pool, evr);
1024               pp = pool->whatprovidesdata + wp;
1025               while (*pp && *pp2)
1026                 {
1027                   if (*pp < *pp2)
1028                     queue_push(&plist, *pp++);
1029                   else
1030                     {
1031                       if (*pp == *pp2)
1032                         pp++;
1033                       queue_push(&plist, *pp2++);
1034                     }
1035                 }
1036               while (*pp)
1037                 queue_push(&plist, *pp++);
1038               while (*pp2)
1039                 queue_push(&plist, *pp2++);
1040               /* if the number of elements did not change, we can reuse wp */
1041               if (pp - (pool->whatprovidesdata + wp) != plist.count)
1042                 wp = 0;
1043             }
1044           break;
1045
1046         case REL_COND:
1047           /* assume the condition is true */
1048           wp = pool_whatprovides(pool, name);
1049           break;
1050
1051         case REL_NAMESPACE:
1052           if (name == NAMESPACE_OTHERPROVIDERS)
1053             {
1054               wp = pool_whatprovides(pool, evr);
1055               break;
1056             }
1057           if (pool->nscallback)
1058             {
1059               /* ask callback which packages provide the dependency
1060                * 0:  none
1061                * 1:  the system (aka SYSTEMSOLVABLE)
1062                * >1: set of packages, stored as offset on whatprovidesdata
1063                */
1064               p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
1065               if (p > 1)
1066                 wp = p;
1067               if (p == 1)
1068                 queue_push(&plist, SYSTEMSOLVABLE);
1069             }
1070           break;
1071         case REL_ARCH:
1072           /* small hack: make it possible to match <pkg>.src
1073            * we have to iterate over the solvables as src packages do not
1074            * provide anything, thus they are not indexed in our
1075            * whatprovides hash */
1076           if (evr == ARCH_SRC || evr == ARCH_NOSRC)
1077             {
1078               Solvable *s;
1079               for (p = 1, s = pool->solvables + p; p < pool->nsolvables; p++, s++)
1080                 {
1081                   if (!s->repo)
1082                     continue;
1083                   if (s->arch != evr && s->arch != ARCH_NOSRC)
1084                     continue;
1085                   if (pool_disabled_solvable(pool, s))
1086                     continue;
1087                   if (!name || pool_match_nevr(pool, s, name))
1088                     queue_push(&plist, p);
1089                 }
1090               break;
1091             }
1092           if (!name)
1093             {
1094               FOR_POOL_SOLVABLES(p)
1095                 {
1096                   Solvable *s = pool->solvables + p;
1097                   if (s->repo != pool->installed && !pool_installable(pool, s))
1098                     continue;
1099                   if (s->arch == evr)
1100                     queue_push(&plist, p);
1101                 }
1102               break;
1103             }
1104           wp = pool_whatprovides(pool, name);
1105           pp = pool->whatprovidesdata + wp;
1106           while ((p = *pp++) != 0)
1107             {
1108               Solvable *s = pool->solvables + p;
1109               if (s->arch == evr)
1110                 queue_push(&plist, p);
1111               else
1112                 wp = 0;
1113             }
1114           break;
1115         case REL_MULTIARCH:
1116           if (evr != ARCH_ANY)
1117             break;
1118           /* XXX : need to check for Multi-Arch: allowed! */
1119           wp = pool_whatprovides(pool, name);
1120           break;
1121         case REL_KIND:
1122           /* package kind filtering */
1123           if (!name)
1124             {
1125               FOR_POOL_SOLVABLES(p)
1126                 {
1127                   Solvable *s = pool->solvables + p;
1128                   if (s->repo != pool->installed && !pool_installable(pool, s))
1129                     continue;
1130                   if (pool_is_kind(pool, s->name, evr))
1131                     queue_push(&plist, p);
1132                 }
1133               break;
1134             }
1135           wp = pool_whatprovides(pool, name);
1136           pp = pool->whatprovidesdata + wp;
1137           while ((p = *pp++) != 0)
1138             {
1139               Solvable *s = pool->solvables + p;
1140               if (pool_is_kind(pool, s->name, evr))
1141                 queue_push(&plist, p);
1142               else
1143                 wp = 0;
1144             }
1145           break;
1146         case REL_FILECONFLICT:
1147           pp = pool_whatprovides_ptr(pool, name);
1148           while ((p = *pp++) != 0)
1149             {
1150               Id origd = MAKERELDEP(d);
1151               Solvable *s = pool->solvables + p;
1152               if (!s->provides)
1153                 continue;
1154               pidp = s->repo->idarraydata + s->provides;
1155               while ((pid = *pidp++) != 0)
1156                 if (pid == origd)
1157                   break;
1158               if (pid)
1159                 queue_push(&plist, p);
1160             }
1161           break;
1162         default:
1163           break;
1164         }
1165       if (wp)
1166         {
1167           /* we can reuse an existing entry */
1168           queue_free(&plist);
1169           pool->whatprovides_rel[d] = wp;
1170           return wp;
1171         }
1172     }
1173   else if (flags)
1174     {
1175       Id *ppaux = 0;
1176       /* simple version comparison relation */
1177 #if 0
1178       POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: what provides %s?\n", pool_dep2str(pool, name));
1179 #endif
1180       pp = pool_whatprovides_ptr(pool, name);
1181       if (!ISRELDEP(name) && name < pool->whatprovidesauxoff)
1182         ppaux = pool->whatprovidesaux[name] ? pool->whatprovidesauxdata + pool->whatprovidesaux[name] : 0;
1183       while (ISRELDEP(name))
1184         {
1185           rd = GETRELDEP(pool, name);
1186           name = rd->name;
1187         }
1188       while ((p = *pp++) != 0)
1189         {
1190           Solvable *s = pool->solvables + p;
1191           if (ppaux)
1192             {
1193               pid = *ppaux++;
1194               if (pid && pid != 1)
1195                 {
1196 #if 0
1197                   POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: aux hit %d %s\n", p, pool_dep2str(pool, pid));
1198 #endif
1199                   if (!ISRELDEP(pid))
1200                     {
1201                       if (pid != name)
1202                         continue;               /* wrong provides name */
1203                       if (pool->disttype == DISTTYPE_DEB)
1204                         continue;               /* unversioned provides can never match versioned deps */
1205                     }
1206                   else
1207                     {
1208                       prd = GETRELDEP(pool, pid);
1209                       if (prd->name != name)
1210                         continue;               /* wrong provides name */
1211                       /* right package, both deps are rels. check flags/evr */
1212                       if (!pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
1213                         continue;
1214                     }
1215                   queue_push(&plist, p);
1216                   continue;
1217                 }
1218             }
1219           if (!s->provides)
1220             {
1221               /* no provides - check nevr */
1222               if (pool_match_nevr_rel(pool, s, MAKERELDEP(d)))
1223                 queue_push(&plist, p);
1224               continue;
1225             }
1226           /* solvable p provides name in some rels */
1227           pidp = s->repo->idarraydata + s->provides;
1228           while ((pid = *pidp++) != 0)
1229             {
1230               if (!ISRELDEP(pid))
1231                 {
1232                   if (pid != name)
1233                     continue;           /* wrong provides name */
1234                   if (pool->disttype == DISTTYPE_DEB)
1235                     continue;           /* unversioned provides can never match versioned deps */
1236                   break;
1237                 }
1238               prd = GETRELDEP(pool, pid);
1239               if (prd->name != name)
1240                 continue;               /* wrong provides name */
1241               /* right package, both deps are rels. check flags/evr */
1242               if (pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
1243                 break;  /* matches */
1244             }
1245           if (!pid)
1246             continue;   /* none of the providers matched */
1247           queue_push(&plist, p);
1248         }
1249       /* make our system solvable provide all unknown rpmlib() stuff */
1250       if (plist.count == 0 && !strncmp(pool_id2str(pool, name), "rpmlib(", 7))
1251         queue_push(&plist, SYSTEMSOLVABLE);
1252     }
1253   /* add providers to whatprovides */
1254 #if 0
1255   POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
1256 #endif
1257   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
1258   queue_free(&plist);
1259
1260   return pool->whatprovides_rel[d];
1261 }
1262
1263 void
1264 pool_flush_namespaceproviders(Pool *pool, Id ns, Id evr)
1265 {
1266   int nrels = pool->nrels;
1267   Id d;
1268   Reldep *rd;
1269
1270   if (!pool->whatprovides_rel)
1271     return;
1272   for (d = 1, rd = pool->rels + d; d < nrels; d++, rd++)
1273     {
1274       if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
1275         continue;
1276       if (ns && rd->name != ns)
1277         continue;
1278       if (evr && rd->evr != evr)
1279         continue;
1280       pool->whatprovides_rel[d] = 0;
1281     }
1282 }
1283
1284 /* intersect dependencies in keyname with dep, return list of matching packages */
1285 void
1286 pool_whatmatchesdep(Pool *pool, Id keyname, Id dep, Queue *q, int marker)
1287 {
1288   Id p;
1289
1290   queue_empty(q);
1291   FOR_POOL_SOLVABLES(p)
1292     {
1293       Solvable *s = pool->solvables + p;
1294       if (s->repo->disabled)
1295         continue;
1296       if (s->repo != pool->installed && !pool_installable(pool, s))
1297         continue;
1298       if (solvable_matchesdep(s, keyname, dep, marker))
1299         queue_push(q, p);
1300     }
1301 }
1302
1303 /*************************************************************************/
1304
1305 void
1306 pool_debug(Pool *pool, int type, const char *format, ...)
1307 {
1308   va_list args;
1309   char buf[1024];
1310
1311   if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0)
1312     {
1313       if ((pool->debugmask & type) == 0)
1314         return;
1315     }
1316   va_start(args, format);
1317   if (!pool->debugcallback)
1318     {
1319       if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0 && !(pool->debugmask & SOLV_DEBUG_TO_STDERR))
1320         vprintf(format, args);
1321       else
1322         vfprintf(stderr, format, args);
1323       return;
1324     }
1325   vsnprintf(buf, sizeof(buf), format, args);
1326   va_end(args);
1327   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
1328 }
1329
1330 int
1331 pool_error(Pool *pool, int ret, const char *format, ...)
1332 {
1333   va_list args;
1334   int l;
1335   va_start(args, format);
1336   if (!pool->errstr)
1337     {
1338       pool->errstra = 1024;
1339       pool->errstr = solv_malloc(pool->errstra);
1340     }
1341   if (!*format)
1342     {
1343       *pool->errstr = 0;
1344       l = 0;
1345     }
1346   else
1347     l = vsnprintf(pool->errstr, pool->errstra, format, args);
1348   va_end(args);
1349   if (l >= 0 && l + 1 > pool->errstra)
1350     {
1351       pool->errstra = l + 256;
1352       pool->errstr = solv_realloc(pool->errstr, pool->errstra);
1353       va_start(args, format);
1354       l = vsnprintf(pool->errstr, pool->errstra, format, args);
1355       va_end(args);
1356     }
1357   if (l < 0)
1358     strcpy(pool->errstr, "unknown error");
1359   if (pool->debugmask & SOLV_ERROR)
1360     pool_debug(pool, SOLV_ERROR, "%s\n", pool->errstr);
1361   return ret;
1362 }
1363
1364 char *
1365 pool_errstr(Pool *pool)
1366 {
1367   return pool->errstr ? pool->errstr : "no error";
1368 }
1369
1370 void
1371 pool_setdebuglevel(Pool *pool, int level)
1372 {
1373   int mask = SOLV_DEBUG_RESULT;
1374   if (level > 0)
1375     mask |= SOLV_DEBUG_STATS|SOLV_DEBUG_ANALYZE|SOLV_DEBUG_UNSOLVABLE|SOLV_DEBUG_SOLVER|SOLV_DEBUG_TRANSACTION|SOLV_ERROR;
1376   if (level > 1)
1377     mask |= SOLV_DEBUG_JOB|SOLV_DEBUG_SOLUTIONS|SOLV_DEBUG_POLICY;
1378   if (level > 2)
1379     mask |= SOLV_DEBUG_PROPAGATE;
1380   if (level > 3)
1381     mask |= SOLV_DEBUG_RULE_CREATION;
1382   mask |= pool->debugmask & SOLV_DEBUG_TO_STDERR;       /* keep bit */
1383   pool->debugmask = mask;
1384 }
1385
1386 void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
1387 {
1388   pool->debugcallback = debugcallback;
1389   pool->debugcallbackdata = debugcallbackdata;
1390 }
1391
1392 void pool_setdebugmask(Pool *pool, int mask)
1393 {
1394   pool->debugmask = mask;
1395 }
1396
1397 void pool_setloadcallback(Pool *pool, int (*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
1398 {
1399   pool->loadcallback = cb;
1400   pool->loadcallbackdata = loadcbdata;
1401 }
1402
1403 void pool_setnamespacecallback(Pool *pool, Id (*cb)(struct _Pool *, void *, Id, Id), void *nscbdata)
1404 {
1405   pool->nscallback = cb;
1406   pool->nscallbackdata = nscbdata;
1407 }
1408
1409 /*************************************************************************/
1410
1411 struct searchfiles {
1412   Id *ids;
1413   int nfiles;
1414   Map seen;
1415 };
1416
1417 #define SEARCHFILES_BLOCK 127
1418
1419 static void
1420 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
1421 {
1422   Id dep, sid;
1423   const char *s;
1424   struct searchfiles *csf;
1425
1426   while ((dep = *ida++) != 0)
1427     {
1428       csf = sf;
1429       while (ISRELDEP(dep))
1430         {
1431           Reldep *rd;
1432           sid = pool->ss.nstrings + GETRELID(dep);
1433           if (MAPTST(&csf->seen, sid))
1434             {
1435               dep = 0;
1436               break;
1437             }
1438           MAPSET(&csf->seen, sid);
1439           rd = GETRELDEP(pool, dep);
1440           if (rd->flags < 8)
1441             dep = rd->name;
1442           else if (rd->flags == REL_NAMESPACE)
1443             {
1444               if (rd->name == NAMESPACE_SPLITPROVIDES)
1445                 {
1446                   csf = isf;
1447                   if (!csf || MAPTST(&csf->seen, sid))
1448                     {
1449                       dep = 0;
1450                       break;
1451                     }
1452                   MAPSET(&csf->seen, sid);
1453                 }
1454               dep = rd->evr;
1455             }
1456           else if (rd->flags == REL_FILECONFLICT)
1457             {
1458               dep = 0;
1459               break;
1460             }
1461           else
1462             {
1463               Id ids[2];
1464               ids[0] = rd->name;
1465               ids[1] = 0;
1466               pool_addfileprovides_dep(pool, ids, csf, isf);
1467               dep = rd->evr;
1468             }
1469         }
1470       if (!dep)
1471         continue;
1472       if (MAPTST(&csf->seen, dep))
1473         continue;
1474       MAPSET(&csf->seen, dep);
1475       s = pool_id2str(pool, dep);
1476       if (*s != '/')
1477         continue;
1478       if (csf != isf && pool->addedfileprovides == 1 && !repodata_filelistfilter_matches(0, s))
1479         continue;       /* skip non-standard locations csf == isf: installed case */
1480       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1481       csf->ids[csf->nfiles++] = dep;
1482     }
1483 }
1484
1485 struct addfileprovides_cbdata {
1486   int nfiles;
1487   Id *ids;
1488   char **dirs;
1489   char **names;
1490
1491   Id *dids;
1492
1493   Map providedids;
1494
1495   Map useddirs;
1496 };
1497
1498 static int
1499 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1500 {
1501   struct addfileprovides_cbdata *cbd = cbdata;
1502   int i;
1503
1504   if (!cbd->useddirs.size)
1505     {
1506       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
1507       if (!cbd->dirs)
1508         {
1509           cbd->dirs = solv_malloc2(cbd->nfiles, sizeof(char *));
1510           cbd->names = solv_malloc2(cbd->nfiles, sizeof(char *));
1511           for (i = 0; i < cbd->nfiles; i++)
1512             {
1513               char *s = solv_strdup(pool_id2str(data->repo->pool, cbd->ids[i]));
1514               cbd->dirs[i] = s;
1515               s = strrchr(s, '/');
1516               *s = 0;
1517               cbd->names[i] = s + 1;
1518             }
1519         }
1520       for (i = 0; i < cbd->nfiles; i++)
1521         {
1522           Id did;
1523           if (MAPTST(&cbd->providedids, cbd->ids[i]))
1524             {
1525               cbd->dids[i] = 0;
1526               continue;
1527             }
1528           did = repodata_str2dir(data, cbd->dirs[i], 0);
1529           cbd->dids[i] = did;
1530           if (did)
1531             MAPSET(&cbd->useddirs, did);
1532         }
1533       repodata_free_dircache(data);
1534     }
1535   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
1536     return 0;
1537   for (i = 0; i < cbd->nfiles; i++)
1538     if (cbd->dids[i] == value->id && !strcmp(cbd->names[i], value->str))
1539       s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
1540   return 0;
1541 }
1542
1543 static void
1544 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
1545 {
1546   Id p;
1547   Repodata *data;
1548   Repo *repo;
1549   Queue fileprovidesq;
1550   int i, j, repoid, repodataid;
1551   int provstart, provend;
1552   Map donemap;
1553   int ndone, incomplete;
1554
1555   if (!pool->urepos)
1556     return;
1557
1558   cbd->nfiles = sf->nfiles;
1559   cbd->ids = sf->ids;
1560   cbd->dirs = 0;
1561   cbd->names = 0;
1562   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
1563   map_init(&cbd->providedids, pool->ss.nstrings);
1564
1565   repoid = 1;
1566   repo = repoonly ? repoonly : pool->repos[repoid];
1567   map_init(&donemap, pool->nsolvables);
1568   queue_init(&fileprovidesq);
1569   provstart = provend = 0;
1570   for (;;)
1571     {
1572       if (!repo || repo->disabled)
1573         {
1574           if (repoonly || ++repoid == pool->nrepos)
1575             break;
1576           repo = pool->repos[repoid];
1577           continue;
1578         }
1579       ndone = 0;
1580       FOR_REPODATAS(repo, repodataid, data)
1581         {
1582           if (ndone >= repo->nsolvables)
1583             break;
1584
1585           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1586             {
1587               map_empty(&cbd->providedids);
1588               for (i = 0; i < fileprovidesq.count; i++)
1589                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1590               provstart = data->start;
1591               provend = data->end;
1592               for (i = 0; i < cbd->nfiles; i++)
1593                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1594                   break;
1595               if (i == cbd->nfiles)
1596                 {
1597                   /* great! no need to search files */
1598                   for (p = data->start; p < data->end; p++)
1599                     if (pool->solvables[p].repo == repo)
1600                       {
1601                         if (MAPTST(&donemap, p))
1602                           continue;
1603                         MAPSET(&donemap, p);
1604                         ndone++;
1605                       }
1606                   continue;
1607                 }
1608             }
1609
1610           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1611             continue;
1612
1613           if (data->start < provstart || data->end > provend)
1614             {
1615               map_empty(&cbd->providedids);
1616               provstart = provend = 0;
1617             }
1618
1619           /* check if the data is incomplete */
1620           incomplete = 0;
1621           if (data->state == REPODATA_AVAILABLE)
1622             {
1623               for (j = 1; j < data->nkeys; j++)
1624                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1625                   break;
1626               if (j < data->nkeys)
1627                 {
1628 #if 0
1629                   for (i = 0; i < cbd->nfiles; i++)
1630                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1631                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1632 #endif
1633                   for (i = 0; i < cbd->nfiles; i++)
1634                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1635                       break;
1636                   if (i < cbd->nfiles)
1637                     incomplete = 1;
1638                 }
1639             }
1640
1641           /* do the search */
1642           map_init(&cbd->useddirs, 0);
1643           for (p = data->start; p < data->end; p++)
1644             if (pool->solvables[p].repo == repo)
1645               {
1646                 if (MAPTST(&donemap, p))
1647                   continue;
1648                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1649                 if (!incomplete)
1650                   {
1651                     MAPSET(&donemap, p);
1652                     ndone++;
1653                   }
1654               }
1655           map_free(&cbd->useddirs);
1656         }
1657
1658       if (repoonly || ++repoid == pool->nrepos)
1659         break;
1660       repo = pool->repos[repoid];
1661     }
1662   map_free(&donemap);
1663   queue_free(&fileprovidesq);
1664   map_free(&cbd->providedids);
1665   if (cbd->dirs)
1666     {
1667       for (i = 0; i < cbd->nfiles; i++)
1668         solv_free(cbd->dirs[i]);
1669       cbd->dirs = solv_free(cbd->dirs);
1670       cbd->names = solv_free(cbd->names);
1671     }
1672 }
1673
1674 void
1675 pool_addfileprovides_queue(Pool *pool, Queue *idq, Queue *idqinst)
1676 {
1677   Solvable *s;
1678   Repo *installed, *repo;
1679   struct searchfiles sf, isf, *isfp;
1680   struct addfileprovides_cbdata cbd;
1681   int i;
1682   unsigned int now;
1683
1684   installed = pool->installed;
1685   now = solv_timems(0);
1686   memset(&sf, 0, sizeof(sf));
1687   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1688   memset(&isf, 0, sizeof(isf));
1689   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1690   pool->addedfileprovides = pool->addfileprovidesfiltered ? 1 : 2;
1691
1692   if (idq)
1693     queue_empty(idq);
1694   if (idqinst)
1695     queue_empty(idqinst);
1696   isfp = installed ? &isf : 0;
1697   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1698     {
1699       repo = s->repo;
1700       if (!repo)
1701         continue;
1702       if (s->obsoletes)
1703         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1704       if (s->conflicts)
1705         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1706       if (s->requires)
1707         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1708       if (s->recommends)
1709         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1710       if (s->suggests)
1711         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1712       if (s->supplements)
1713         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1714       if (s->enhances)
1715         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1716     }
1717   map_free(&sf.seen);
1718   map_free(&isf.seen);
1719   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1720   cbd.dids = 0;
1721   if (sf.nfiles)
1722     {
1723 #if 0
1724       for (i = 0; i < sf.nfiles; i++)
1725         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1726 #endif
1727       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1728       if (idq)
1729         for (i = 0; i < sf.nfiles; i++)
1730           queue_push(idq, sf.ids[i]);
1731       if (idqinst)
1732         for (i = 0; i < sf.nfiles; i++)
1733           queue_push(idqinst, sf.ids[i]);
1734       solv_free(sf.ids);
1735     }
1736   if (isf.nfiles)
1737     {
1738 #if 0
1739       for (i = 0; i < isf.nfiles; i++)
1740         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1741 #endif
1742       if (installed)
1743         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1744       if (installed && idqinst)
1745         for (i = 0; i < isf.nfiles; i++)
1746           queue_pushunique(idqinst, isf.ids[i]);
1747       solv_free(isf.ids);
1748     }
1749   solv_free(cbd.dids);
1750   pool_freewhatprovides(pool);  /* as we have added provides */
1751   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1752 }
1753
1754 void
1755 pool_addfileprovides(Pool *pool)
1756 {
1757   pool_addfileprovides_queue(pool, 0, 0);
1758 }
1759
1760 void
1761 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)
1762 {
1763   if (p)
1764     {
1765       if (pool->solvables[p].repo)
1766         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1767       return;
1768     }
1769   /* FIXME: obey callback return value! */
1770   for (p = 1; p < pool->nsolvables; p++)
1771     if (pool->solvables[p].repo)
1772       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1773 }
1774
1775 void
1776 pool_clear_pos(Pool *pool)
1777 {
1778   memset(&pool->pos, 0, sizeof(pool->pos));
1779 }
1780
1781
1782 void
1783 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1784 {
1785   int i;
1786
1787   pool->languagecache = solv_free(pool->languagecache);
1788   pool->languagecacheother = 0;
1789   for (i = 0; i < pool->nlanguages; i++)
1790     free((char *)pool->languages[i]);
1791   pool->languages = solv_free((void *)pool->languages);
1792   pool->nlanguages = nlanguages;
1793   if (!nlanguages)
1794     return;
1795   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1796   for (i = 0; i < pool->nlanguages; i++)
1797     pool->languages[i] = solv_strdup(languages[i]);
1798 }
1799
1800 Id
1801 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1802 {
1803   const char *n;
1804   char buf[256], *p;
1805   int l;
1806
1807   if (!lang || !*lang)
1808     return id;
1809   n = pool_id2str(pool, id);
1810   l = strlen(n) + strlen(lang) + 2;
1811   if (l > sizeof(buf))
1812     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1813   else
1814     p = buf;
1815   sprintf(p, "%s:%s", n, lang);
1816   id = pool_str2id(pool, p, create);
1817   if (p != buf)
1818     free(p);
1819   return id;
1820 }
1821
1822 char *
1823 pool_alloctmpspace(Pool *pool, int len)
1824 {
1825   int n = pool->tmpspace.n;
1826   if (!len)
1827     return 0;
1828   if (len > pool->tmpspace.len[n])
1829     {
1830       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1831       pool->tmpspace.len[n] = len + 32;
1832     }
1833   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1834   return pool->tmpspace.buf[n];
1835 }
1836
1837 static char *
1838 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1839 {
1840   if (space)
1841     {
1842       int n, oldn;
1843       n = oldn = pool->tmpspace.n;
1844       for (;;)
1845         {
1846           if (!n--)
1847             n = POOL_TMPSPACEBUF - 1;
1848           if (n == oldn)
1849             break;
1850           if (pool->tmpspace.buf[n] != space)
1851             continue;
1852           if (len > pool->tmpspace.len[n])
1853             {
1854               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1855               pool->tmpspace.len[n] = len + 32;
1856             }
1857           return pool->tmpspace.buf[n];
1858         }
1859     }
1860   return 0;
1861 }
1862
1863 void
1864 pool_freetmpspace(Pool *pool, const char *space)
1865 {
1866   int n = pool->tmpspace.n;
1867   if (!space)
1868     return;
1869   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1870   if (pool->tmpspace.buf[n] == space)
1871     pool->tmpspace.n = n;
1872 }
1873
1874 char *
1875 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1876 {
1877   int l1, l2, l3;
1878   char *s, *str;
1879   l1 = str1 ? strlen(str1) : 0;
1880   l2 = str2 ? strlen(str2) : 0;
1881   l3 = str3 ? strlen(str3) : 0;
1882   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1883   if (l1)
1884     {
1885       strcpy(s, str1);
1886       s += l1;
1887     }
1888   if (l2)
1889     {
1890       strcpy(s, str2);
1891       s += l2;
1892     }
1893   if (l3)
1894     {
1895       strcpy(s, str3);
1896       s += l3;
1897     }
1898   *s = 0;
1899   return str;
1900 }
1901
1902 char *
1903 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1904 {
1905   int l1, l2, l3;
1906   char *s, *str;
1907
1908   l1 = str1 ? strlen(str1) : 0;
1909   l2 = str2 ? strlen(str2) : 0;
1910   l3 = str3 ? strlen(str3) : 0;
1911   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1912   if (str)
1913     str1 = str;
1914   else
1915     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1916   s = str;
1917   if (l1)
1918     {
1919       if (s != str1)
1920         strcpy(s, str1);
1921       s += l1;
1922     }
1923   if (l2)
1924     {
1925       strcpy(s, str2);
1926       s += l2;
1927     }
1928   if (l3)
1929     {
1930       strcpy(s, str3);
1931       s += l3;
1932     }
1933   *s = 0;
1934   return str;
1935 }
1936
1937 const char *
1938 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1939 {
1940   char *s;
1941   if (!len)
1942     return "";
1943   s = pool_alloctmpspace(pool, 2 * len + 1);
1944   solv_bin2hex(buf, len, s);
1945   return s;
1946 }
1947
1948 /*******************************************************************/
1949
1950 struct mptree {
1951   Id sibling;
1952   Id child;
1953   const char *comp;
1954   int compl;
1955   Id mountpoint;
1956 };
1957
1958 struct ducbdata {
1959   DUChanges *mps;
1960   struct mptree *mptree;
1961   int addsub;
1962   int hasdu;
1963
1964   Id *dirmap;
1965   int nmap;
1966   Repodata *olddata;
1967 };
1968
1969
1970 static int
1971 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1972 {
1973   struct ducbdata *cbd = cbdata;
1974   Id mp;
1975
1976   if (data != cbd->olddata)
1977     {
1978       Id dn, mp, comp, *dirmap, *dirs;
1979       int i, compl;
1980       const char *compstr;
1981       struct mptree *mptree;
1982
1983       /* create map from dir to mptree */
1984       cbd->dirmap = solv_free(cbd->dirmap);
1985       cbd->nmap = 0;
1986       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1987       mptree = cbd->mptree;
1988       mp = 0;
1989       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1990         {
1991           comp = *dirs++;
1992           if (comp <= 0)
1993             {
1994               mp = dirmap[-comp];
1995               continue;
1996             }
1997           if (mp < 0)
1998             {
1999               /* unconnected */
2000               dirmap[dn] = mp;
2001               continue;
2002             }
2003           if (!mptree[mp].child)
2004             {
2005               dirmap[dn] = -mp;
2006               continue;
2007             }
2008           if (data->localpool)
2009             compstr = stringpool_id2str(&data->spool, comp);
2010           else
2011             compstr = pool_id2str(data->repo->pool, comp);
2012           compl = strlen(compstr);
2013           for (i = mptree[mp].child; i; i = mptree[i].sibling)
2014             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
2015               break;
2016           dirmap[dn] = i ? i : -mp;
2017         }
2018       /* change dirmap to point to mountpoint instead of mptree */
2019       for (dn = 0; dn < data->dirpool.ndirs; dn++)
2020         {
2021           mp = dirmap[dn];
2022           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
2023         }
2024       cbd->dirmap = dirmap;
2025       cbd->nmap = data->dirpool.ndirs;
2026       cbd->olddata = data;
2027     }
2028   cbd->hasdu = 1;
2029   if (value->id < 0 || value->id >= cbd->nmap)
2030     return 0;
2031   mp = cbd->dirmap[value->id];
2032   if (mp < 0)
2033     return 0;
2034   if (cbd->addsub > 0)
2035     {
2036       cbd->mps[mp].kbytes += value->num;
2037       cbd->mps[mp].files += value->num2;
2038     }
2039   else if (!(cbd->mps[mp].flags & DUCHANGES_ONLYADD))
2040     {
2041       cbd->mps[mp].kbytes -= value->num;
2042       cbd->mps[mp].files -= value->num2;
2043     }
2044   return 0;
2045 }
2046
2047 static void
2048 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
2049 {
2050   int i;
2051   if (mptree[pos].mountpoint == -1)
2052     mptree[pos].mountpoint = mountpoint;
2053   else
2054     mountpoint = mptree[pos].mountpoint;
2055   for (i = mptree[pos].child; i; i = mptree[i].sibling)
2056     propagate_mountpoints(mptree, i, mountpoint);
2057 }
2058
2059 #define MPTREE_BLOCK 15
2060
2061 static struct mptree *
2062 create_mptree(DUChanges *mps, int nmps)
2063 {
2064   int i, nmptree;
2065   struct mptree *mptree;
2066   int pos, compl;
2067   int mp;
2068   const char *p, *path, *compstr;
2069
2070   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
2071
2072   /* our root node */
2073   mptree[0].sibling = 0;
2074   mptree[0].child = 0;
2075   mptree[0].comp = 0;
2076   mptree[0].compl = 0;
2077   mptree[0].mountpoint = -1;
2078   nmptree = 1;
2079
2080   /* create component tree */
2081   for (mp = 0; mp < nmps; mp++)
2082     {
2083       mps[mp].kbytes = 0;
2084       mps[mp].files = 0;
2085       pos = 0;
2086       path = mps[mp].path;
2087       while(*path == '/')
2088         path++;
2089       while (*path)
2090         {
2091           if ((p = strchr(path, '/')) == 0)
2092             {
2093               compstr = path;
2094               compl = strlen(compstr);
2095               path += compl;
2096             }
2097           else
2098             {
2099               compstr = path;
2100               compl = p - path;
2101               path = p + 1;
2102               while(*path == '/')
2103                 path++;
2104             }
2105           for (i = mptree[pos].child; i; i = mptree[i].sibling)
2106             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
2107               break;
2108           if (!i)
2109             {
2110               /* create new node */
2111               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
2112               i = nmptree++;
2113               mptree[i].sibling = mptree[pos].child;
2114               mptree[i].child = 0;
2115               mptree[i].comp = compstr;
2116               mptree[i].compl = compl;
2117               mptree[i].mountpoint = -1;
2118               mptree[pos].child = i;
2119             }
2120           pos = i;
2121         }
2122       mptree[pos].mountpoint = mp;
2123     }
2124
2125   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
2126
2127 #if 0
2128   for (i = 0; i < nmptree; i++)
2129     {
2130       printf("#%d sibling: %d\n", i, mptree[i].sibling);
2131       printf("#%d child: %d\n", i, mptree[i].child);
2132       printf("#%d comp: %s\n", i, mptree[i].comp);
2133       printf("#%d compl: %d\n", i, mptree[i].compl);
2134       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
2135     }
2136 #endif
2137
2138   return mptree;
2139 }
2140
2141 void
2142 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
2143 {
2144   struct mptree *mptree;
2145   struct ducbdata cbd;
2146   Solvable *s;
2147   int i, sp;
2148   Map ignoredu;
2149   Repo *oldinstalled = pool->installed;
2150   int haveonlyadd = 0;
2151
2152   map_init(&ignoredu, 0);
2153   mptree = create_mptree(mps, nmps);
2154
2155   for (i = 0; i < nmps; i++)
2156     if ((mps[i].flags & DUCHANGES_ONLYADD) != 0)
2157       haveonlyadd = 1;
2158   cbd.mps = mps;
2159   cbd.dirmap = 0;
2160   cbd.nmap = 0;
2161   cbd.olddata = 0;
2162   cbd.mptree = mptree;
2163   cbd.addsub = 1;
2164   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2165     {
2166       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2167         continue;
2168       if (!MAPTST(installedmap, sp))
2169         continue;
2170       cbd.hasdu = 0;
2171       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2172       if (!cbd.hasdu && oldinstalled)
2173         {
2174           Id op, opp;
2175           int didonlyadd = 0;
2176           /* no du data available, ignore data of all installed solvables we obsolete */
2177           if (!ignoredu.size)
2178             map_grow(&ignoredu, oldinstalled->end - oldinstalled->start);
2179           FOR_PROVIDES(op, opp, s->name)
2180             {
2181               Solvable *s2 = pool->solvables + op;
2182               if (!pool->implicitobsoleteusesprovides && s->name != s2->name)
2183                 continue;
2184               if (pool->implicitobsoleteusescolors && !pool_colormatch(pool, s, s2))
2185                 continue;
2186               if (op >= oldinstalled->start && op < oldinstalled->end)
2187                 {
2188                   MAPSET(&ignoredu, op - oldinstalled->start);
2189                   if (haveonlyadd && pool->solvables[op].repo == oldinstalled && !didonlyadd)
2190                     {
2191                       repo_search(oldinstalled, op, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2192                       cbd.addsub = -1;
2193                       repo_search(oldinstalled, op, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2194                       cbd.addsub = 1;
2195                       didonlyadd = 1;
2196                     }
2197                 }
2198             }
2199           if (s->obsoletes)
2200             {
2201               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2202               while ((obs = *obsp++) != 0)
2203                 FOR_PROVIDES(op, opp, obs)
2204                   {
2205                     Solvable *s2 = pool->solvables + op;
2206                     if (!pool->obsoleteusesprovides && !pool_match_nevr(pool, s2, obs))
2207                       continue;
2208                     if (pool->obsoleteusescolors && !pool_colormatch(pool, s, s2))
2209                       continue;
2210                     if (op >= oldinstalled->start && op < oldinstalled->end)
2211                       {
2212                         MAPSET(&ignoredu, op - oldinstalled->start);
2213                         if (haveonlyadd && pool->solvables[op].repo == oldinstalled && !didonlyadd)
2214                           {
2215                             repo_search(oldinstalled, op, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2216                             cbd.addsub = -1;
2217                             repo_search(oldinstalled, op, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2218                             cbd.addsub = 1;
2219                             didonlyadd = 1;
2220                           }
2221                       }
2222                   }
2223             }
2224         }
2225     }
2226   cbd.addsub = -1;
2227   if (oldinstalled)
2228     {
2229       /* assumes we allways have du data for installed solvables */
2230       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2231         {
2232           if (MAPTST(installedmap, sp))
2233             continue;
2234           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
2235             continue;
2236           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2237         }
2238     }
2239   map_free(&ignoredu);
2240   solv_free(cbd.dirmap);
2241   solv_free(mptree);
2242 }
2243
2244 int
2245 pool_calc_installsizechange(Pool *pool, Map *installedmap)
2246 {
2247   Id sp;
2248   Solvable *s;
2249   int change = 0;
2250   Repo *oldinstalled = pool->installed;
2251
2252   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2253     {
2254       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2255         continue;
2256       if (!MAPTST(installedmap, sp))
2257         continue;
2258       change += solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2259     }
2260   if (oldinstalled)
2261     {
2262       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2263         {
2264           if (MAPTST(installedmap, sp))
2265             continue;
2266           change -= solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2267         }
2268     }
2269   return change;
2270 }
2271
2272 /* map:
2273  *  1: installed
2274  *  2: conflicts with installed
2275  *  8: interesting (only true if installed)
2276  * 16: undecided
2277  */
2278
2279 static inline Id dep2name(Pool *pool, Id dep)
2280 {
2281   while (ISRELDEP(dep))
2282     {
2283       Reldep *rd = GETRELDEP(pool, dep);
2284       dep = rd->name;
2285     }
2286   return dep;
2287 }
2288
2289 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con)
2290 {
2291   Id p, pp;
2292   Solvable *sn = pool->solvables + n;
2293
2294   FOR_PROVIDES(p, pp, sn->name)
2295     {
2296       Solvable *s = pool->solvables + p;
2297       if (s->name != sn->name || s->arch != sn->arch)
2298         continue;
2299       if ((map[p] & 9) != 9)
2300         continue;
2301       if (pool_match_nevr(pool, pool->solvables + p, con))
2302         continue;
2303       return 1;         /* found installed package that doesn't conflict */
2304     }
2305   return 0;
2306 }
2307
2308 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *multiversionmap)
2309 {
2310   Id p, pp;
2311   int r = 0;
2312   FOR_PROVIDES(p, pp, dep)
2313     {
2314       if (p == SYSTEMSOLVABLE)
2315         return 1;       /* always boring, as never constraining */
2316       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
2317         continue;
2318       if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
2319         if (providedbyinstalled_multiversion(pool, map, p, dep))
2320           continue;
2321       if ((map[p] & 9) == 9)
2322         return 9;
2323       r |= map[p] & 17;
2324     }
2325   return r;
2326 }
2327
2328 /*
2329  * pool_trivial_installable - calculate if a set of solvables is
2330  * trivial installable without any other installs/deinstalls of
2331  * packages not belonging to the set.
2332  *
2333  * the state is returned in the result queue:
2334  * 1:  solvable is installable without any other package changes
2335  * 0:  solvable is not installable
2336  * -1: solvable is installable, but doesn't constrain any installed packages
2337  */
2338
2339 void
2340 pool_trivial_installable_multiversionmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *multiversionmap)
2341 {
2342   int i, r, m, did;
2343   Id p, *dp, con, *conp, req, *reqp;
2344   unsigned char *map;
2345   Solvable *s;
2346
2347   map = solv_calloc(pool->nsolvables, 1);
2348   for (p = 1; p < pool->nsolvables; p++)
2349     {
2350       if (!MAPTST(installedmap, p))
2351         continue;
2352       map[p] |= 9;
2353       s = pool->solvables + p;
2354       if (!s->conflicts)
2355         continue;
2356       conp = s->repo->idarraydata + s->conflicts;
2357       while ((con = *conp++) != 0)
2358         {
2359           dp = pool_whatprovides_ptr(pool, con);
2360           for (; *dp; dp++)
2361             map[p] |= 2;        /* XXX: self conflict ? */
2362         }
2363     }
2364   for (i = 0; i < pkgs->count; i++)
2365     map[pkgs->elements[i]] = 16;
2366
2367   for (i = 0, did = 0; did < pkgs->count; i++, did++)
2368     {
2369       if (i == pkgs->count)
2370         i = 0;
2371       p = pkgs->elements[i];
2372       if ((map[p] & 16) == 0)
2373         continue;
2374       if ((map[p] & 2) != 0)
2375         {
2376           map[p] = 2;
2377           continue;
2378         }
2379       s = pool->solvables + p;
2380       m = 1;
2381       if (s->requires)
2382         {
2383           reqp = s->repo->idarraydata + s->requires;
2384           while ((req = *reqp++) != 0)
2385             {
2386               if (req == SOLVABLE_PREREQMARKER)
2387                 continue;
2388               r = providedbyinstalled(pool, map, req, 0, 0);
2389               if (!r)
2390                 {
2391                   /* decided and miss */
2392                   map[p] = 2;
2393                   did = 0;
2394                   break;
2395                 }
2396               if (r == 16)
2397                 break;  /* undecided */
2398               m |= r;   /* 1 | 9 | 17 */
2399             }
2400           if (req)
2401             continue;
2402           if ((m & 9) == 9)
2403             m = 9;
2404         }
2405       if (s->conflicts)
2406         {
2407           int ispatch = 0;      /* see solver.c patch handling */
2408
2409           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
2410             ispatch = 1;
2411           conp = s->repo->idarraydata + s->conflicts;
2412           while ((con = *conp++) != 0)
2413             {
2414               if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2415                 {
2416                   map[p] = 2;
2417                   did = 0;
2418                   break;
2419                 }
2420               if ((m == 1 || m == 17) && ISRELDEP(con))
2421                 {
2422                   con = dep2name(pool, con);
2423                   if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2424                     m = 9;
2425                 }
2426             }
2427           if (con)
2428             continue;   /* found a conflict */
2429         }
2430 #if 0
2431       if (s->repo && s->repo != oldinstalled)
2432         {
2433           Id p2, obs, *obsp, *pp;
2434           Solvable *s2;
2435           if (s->obsoletes)
2436             {
2437               obsp = s->repo->idarraydata + s->obsoletes;
2438               while ((obs = *obsp++) != 0)
2439                 {
2440                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
2441                     {
2442                       map[p] = 2;
2443                       break;
2444                     }
2445                 }
2446               if (obs)
2447                 continue;
2448             }
2449           FOR_PROVIDES(p2, pp, s->name)
2450             {
2451               s2 = pool->solvables + p2;
2452               if (s2->name == s->name && (map[p2] & 1) != 0)
2453                 {
2454                   map[p] = 2;
2455                   break;
2456                 }
2457             }
2458           if (p2)
2459             continue;
2460         }
2461 #endif
2462       if (m != map[p])
2463         {
2464           map[p] = m;
2465           did = 0;
2466         }
2467     }
2468   queue_free(res);
2469   queue_init_clone(res, pkgs);
2470   for (i = 0; i < pkgs->count; i++)
2471     {
2472       m = map[pkgs->elements[i]];
2473       if ((m & 9) == 9)
2474         r = 1;
2475       else if (m & 1)
2476         r = -1;
2477       else
2478         r = 0;
2479       res->elements[i] = r;
2480     }
2481   free(map);
2482 }
2483
2484 void
2485 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
2486 {
2487   pool_trivial_installable_multiversionmap(pool, installedmap, pkgs, res, 0);
2488 }
2489
2490 const char *
2491 pool_lookup_str(Pool *pool, Id entry, Id keyname)
2492 {
2493   if (entry == SOLVID_POS && pool->pos.repo)
2494     return repo_lookup_str(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2495   if (entry <= 0)
2496     return 0;
2497   return solvable_lookup_str(pool->solvables + entry, keyname);
2498 }
2499
2500 Id
2501 pool_lookup_id(Pool *pool, Id entry, Id keyname)
2502 {
2503   if (entry == SOLVID_POS && pool->pos.repo)
2504     return repo_lookup_id(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2505   if (entry <= 0)
2506     return 0;
2507   return solvable_lookup_id(pool->solvables + entry, keyname);
2508 }
2509
2510 unsigned long long
2511 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned long long notfound)
2512 {
2513   if (entry == SOLVID_POS && pool->pos.repo)
2514     return repo_lookup_num(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, notfound);
2515   if (entry <= 0)
2516     return notfound;
2517   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
2518 }
2519
2520 int
2521 pool_lookup_void(Pool *pool, Id entry, Id keyname)
2522 {
2523   if (entry == SOLVID_POS && pool->pos.repo)
2524     return repo_lookup_void(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2525   if (entry <= 0)
2526     return 0;
2527   return solvable_lookup_void(pool->solvables + entry, keyname);
2528 }
2529
2530 const unsigned char *
2531 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2532 {
2533   if (entry == SOLVID_POS && pool->pos.repo)
2534     return repo_lookup_bin_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2535   if (entry <= 0)
2536     return 0;
2537   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
2538 }
2539
2540 const char *
2541 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2542 {
2543   if (entry == SOLVID_POS && pool->pos.repo)
2544     return repo_lookup_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2545   if (entry <= 0)
2546     return 0;
2547   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2548 }
2549
2550 int
2551 pool_lookup_idarray(Pool *pool, Id entry, Id keyname, Queue *q)
2552 {
2553   if (entry == SOLVID_POS && pool->pos.repo)
2554     return repo_lookup_idarray(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, q);
2555   if (entry <= 0)
2556     return 0;
2557   return solvable_lookup_idarray(pool->solvables + entry, keyname, q);
2558 }
2559
2560 const char *
2561 pool_lookup_deltalocation(Pool *pool, Id entry, unsigned int *medianrp)
2562 {
2563   const char *loc;
2564   if (medianrp)
2565     *medianrp = 0;
2566   if (entry != SOLVID_POS)
2567     return 0;
2568   loc = pool_lookup_str(pool, entry, DELTA_LOCATION_DIR);
2569   loc = pool_tmpjoin(pool, loc, loc ? "/" : 0, pool_lookup_str(pool, entry, DELTA_LOCATION_NAME));
2570   loc = pool_tmpappend(pool, loc, "-", pool_lookup_str(pool, entry, DELTA_LOCATION_EVR));
2571   loc = pool_tmpappend(pool, loc, ".", pool_lookup_str(pool, entry, DELTA_LOCATION_SUFFIX));
2572   return loc;
2573 }
2574
2575 static void
2576 add_new_provider(Pool *pool, Id id, Id p)
2577 {
2578   Queue q;
2579   Id *pp;
2580
2581   while (ISRELDEP(id))
2582     {
2583       Reldep *rd = GETRELDEP(pool, id);
2584       id = rd->name;
2585     }
2586
2587   queue_init(&q);
2588   for (pp = pool->whatprovidesdata + pool->whatprovides[id]; *pp; pp++)
2589     {
2590       if (*pp == p)
2591         {
2592           queue_free(&q);
2593           return;
2594         }
2595       if (*pp > p)
2596         {
2597           queue_push(&q, p);
2598           p = 0;
2599         }
2600       queue_push(&q, *pp);
2601     }
2602   if (p)
2603     queue_push(&q, p);
2604   pool->whatprovides[id] = pool_queuetowhatprovides(pool, &q);
2605   if (id < pool->whatprovidesauxoff)
2606     pool->whatprovidesaux[id] = 0;      /* sorry */
2607   queue_free(&q);
2608 }
2609
2610 void
2611 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2612 {
2613   int hadhashes = pool->relhashtbl ? 1 : 0;
2614   Solvable *s;
2615   Id fn, p, q, md5;
2616   Id id;
2617   int i;
2618
2619   if (!conflicts->count)
2620     return;
2621   for (i = 0; i < conflicts->count; i += 6)
2622     {
2623       fn = conflicts->elements[i];
2624       p = conflicts->elements[i + 1];
2625       md5 = conflicts->elements[i + 2];
2626       q = conflicts->elements[i + 4];
2627       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2628       s = pool->solvables + p;
2629       if (!s->repo)
2630         continue;
2631       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2632       if (pool->whatprovides)
2633         add_new_provider(pool, fn, p);
2634       if (pool->whatprovides_rel)
2635         pool->whatprovides_rel[GETRELID(id)] = 0;       /* clear cache */
2636       s = pool->solvables + q;
2637       if (!s->repo)
2638         continue;
2639       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2640     }
2641   if (!hadhashes)
2642     pool_freeidhashes(pool);
2643 }
2644
2645 char *
2646 pool_prepend_rootdir(Pool *pool, const char *path)
2647 {
2648   if (!path)
2649     return 0;
2650   if (!pool->rootdir)
2651     return solv_strdup(path);
2652   return solv_dupjoin(pool->rootdir, "/", *path == '/' ? path + 1 : path);
2653 }
2654
2655 const char *
2656 pool_prepend_rootdir_tmp(Pool *pool, const char *path)
2657 {
2658   if (!path)
2659     return 0;
2660   if (!pool->rootdir)
2661     return path;
2662   return pool_tmpjoin(pool, pool->rootdir, "/", *path == '/' ? path + 1 : path);
2663 }
2664
2665 void
2666 pool_set_rootdir(Pool *pool, const char *rootdir)
2667 {
2668   solv_free(pool->rootdir);
2669   pool->rootdir = solv_strdup(rootdir);
2670 }
2671
2672 const char *
2673 pool_get_rootdir(Pool *pool)
2674 {
2675   return pool->rootdir;
2676 }
2677
2678 /* only used in libzypp */
2679 void
2680 pool_set_custom_vendorcheck(Pool *pool, int (*vendorcheck)(Pool *, Solvable *, Solvable *))
2681 {
2682   pool->custom_vendorcheck = vendorcheck;
2683 }
2684
2685 /* EOF */