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