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