b48cc4622fffd84846500f3a605f888aad01b211
[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   char **dirs;
1300   char **names;
1301   int nfiles;
1302   Map seen;
1303 };
1304
1305 #define SEARCHFILES_BLOCK 127
1306
1307 static void
1308 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
1309 {
1310   Id dep, sid;
1311   const char *s, *sr;
1312   struct searchfiles *csf;
1313
1314   while ((dep = *ida++) != 0)
1315     {
1316       csf = sf;
1317       while (ISRELDEP(dep))
1318         {
1319           Reldep *rd;
1320           sid = pool->ss.nstrings + GETRELID(dep);
1321           if (MAPTST(&csf->seen, sid))
1322             {
1323               dep = 0;
1324               break;
1325             }
1326           MAPSET(&csf->seen, sid);
1327           rd = GETRELDEP(pool, dep);
1328           if (rd->flags < 8)
1329             dep = rd->name;
1330           else if (rd->flags == REL_NAMESPACE)
1331             {
1332               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
1333                 {
1334                   csf = isf;
1335                   if (!csf || MAPTST(&csf->seen, sid))
1336                     {
1337                       dep = 0;
1338                       break;
1339                     }
1340                   MAPSET(&csf->seen, sid);
1341                 }
1342               dep = rd->evr;
1343             }
1344           else if (rd->flags == REL_FILECONFLICT)
1345             {
1346               dep = 0;
1347               break;
1348             }
1349           else
1350             {
1351               Id ids[2];
1352               ids[0] = rd->name;
1353               ids[1] = 0;
1354               pool_addfileprovides_dep(pool, ids, csf, isf);
1355               dep = rd->evr;
1356             }
1357         }
1358       if (!dep)
1359         continue;
1360       if (MAPTST(&csf->seen, dep))
1361         continue;
1362       MAPSET(&csf->seen, dep);
1363       s = pool_id2str(pool, dep);
1364       if (*s != '/')
1365         continue;
1366       if (csf != isf && pool->addedfileprovides == 1 && !repodata_filelistfilter_matches(0, s))
1367         continue;       /* skip non-standard locations csf == isf: installed case */
1368       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1369       csf->dirs = solv_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1370       csf->names = solv_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1371       csf->ids[csf->nfiles] = dep;
1372       sr = strrchr(s, '/');
1373       csf->names[csf->nfiles] = solv_strdup(sr + 1);
1374       csf->dirs[csf->nfiles] = solv_malloc(sr - s + 1);
1375       if (sr != s)
1376         strncpy(csf->dirs[csf->nfiles], s, sr - s);
1377       csf->dirs[csf->nfiles][sr - s] = 0;
1378       csf->nfiles++;
1379     }
1380 }
1381
1382 struct addfileprovides_cbdata {
1383   int nfiles;
1384   Id *ids;
1385   char **dirs;
1386   char **names;
1387
1388   Id *dids;
1389
1390   Map providedids;
1391
1392   Map useddirs;
1393 };
1394
1395 static int
1396 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1397 {
1398   struct addfileprovides_cbdata *cbd = cbdata;
1399   int i;
1400
1401   if (!cbd->useddirs.size)
1402     {
1403       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
1404       for (i = 0; i < cbd->nfiles; i++)
1405         {
1406           Id did;
1407           if (MAPTST(&cbd->providedids, cbd->ids[i]))
1408             {
1409               cbd->dids[i] = 0;
1410               continue;
1411             }
1412           did = repodata_str2dir(data, cbd->dirs[i], 0);
1413           cbd->dids[i] = did;
1414           if (did)
1415             MAPSET(&cbd->useddirs, did);
1416         }
1417       repodata_free_dircache(data);
1418     }
1419   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
1420     return 0;
1421   for (i = 0; i < cbd->nfiles; i++)
1422     {
1423       if (cbd->dids[i] != value->id)
1424         continue;
1425       if (!strcmp(cbd->names[i], value->str))
1426         break;
1427     }
1428   if (i == cbd->nfiles)
1429     return 0;
1430   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
1431   return 0;
1432 }
1433
1434 static void
1435 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
1436 {
1437   Id p;
1438   Repodata *data;
1439   Repo *repo;
1440   Queue fileprovidesq;
1441   int i, j, repoid, repodataid;
1442   int provstart, provend;
1443   Map donemap;
1444   int ndone, incomplete;
1445
1446   if (!pool->urepos)
1447     return;
1448
1449   cbd->nfiles = sf->nfiles;
1450   cbd->ids = sf->ids;
1451   cbd->dirs = sf->dirs;
1452   cbd->names = sf->names;
1453   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
1454   map_init(&cbd->providedids, pool->ss.nstrings);
1455
1456   repoid = 1;
1457   repo = repoonly ? repoonly : pool->repos[repoid];
1458   map_init(&donemap, pool->nsolvables);
1459   queue_init(&fileprovidesq);
1460   provstart = provend = 0;
1461   for (;;)
1462     {
1463       if (!repo || repo->disabled)
1464         {
1465           if (repoonly || ++repoid == pool->nrepos)
1466             break;
1467           repo = pool->repos[repoid];
1468           continue;
1469         }
1470       ndone = 0;
1471       FOR_REPODATAS(repo, repodataid, data)
1472         {
1473           if (ndone >= repo->nsolvables)
1474             break;
1475
1476           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1477             {
1478               map_empty(&cbd->providedids);
1479               for (i = 0; i < fileprovidesq.count; i++)
1480                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1481               provstart = data->start;
1482               provend = data->end;
1483               for (i = 0; i < cbd->nfiles; i++)
1484                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1485                   break;
1486               if (i == cbd->nfiles)
1487                 {
1488                   /* great! no need to search files */
1489                   for (p = data->start; p < data->end; p++)
1490                     if (pool->solvables[p].repo == repo)
1491                       {
1492                         if (MAPTST(&donemap, p))
1493                           continue;
1494                         MAPSET(&donemap, p);
1495                         ndone++;
1496                       }
1497                   continue;
1498                 }
1499             }
1500
1501           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1502             continue;
1503
1504           if (data->start < provstart || data->end > provend)
1505             {
1506               map_empty(&cbd->providedids);
1507               provstart = provend = 0;
1508             }
1509
1510           /* check if the data is incomplete */
1511           incomplete = 0;
1512           if (data->state == REPODATA_AVAILABLE)
1513             {
1514               for (j = 1; j < data->nkeys; j++)
1515                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1516                   break;
1517               if (j < data->nkeys)
1518                 {
1519 #if 0
1520                   for (i = 0; i < cbd->nfiles; i++)
1521                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1522                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1523 #endif
1524                   for (i = 0; i < cbd->nfiles; i++)
1525                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1526                       break;
1527                   if (i < cbd->nfiles)
1528                     incomplete = 1;
1529                 }
1530             }
1531
1532           /* do the search */
1533           map_init(&cbd->useddirs, 0);
1534           for (p = data->start; p < data->end; p++)
1535             if (pool->solvables[p].repo == repo)
1536               {
1537                 if (MAPTST(&donemap, p))
1538                   continue;
1539                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1540                 if (!incomplete)
1541                   {
1542                     MAPSET(&donemap, p);
1543                     ndone++;
1544                   }
1545               }
1546           map_free(&cbd->useddirs);
1547         }
1548
1549       if (repoonly || ++repoid == pool->nrepos)
1550         break;
1551       repo = pool->repos[repoid];
1552     }
1553   map_free(&donemap);
1554   queue_free(&fileprovidesq);
1555   map_free(&cbd->providedids);
1556 }
1557
1558 void
1559 pool_addfileprovides_queue(Pool *pool, Queue *idq, Queue *idqinst)
1560 {
1561   Solvable *s;
1562   Repo *installed, *repo;
1563   struct searchfiles sf, isf, *isfp;
1564   struct addfileprovides_cbdata cbd;
1565   int i;
1566   unsigned int now;
1567
1568   installed = pool->installed;
1569   now = solv_timems(0);
1570   memset(&sf, 0, sizeof(sf));
1571   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1572   memset(&isf, 0, sizeof(isf));
1573   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1574   pool->addedfileprovides = pool->addfileprovidesfiltered ? 1 : 2;
1575
1576   if (idq)
1577     queue_empty(idq);
1578   if (idqinst)
1579     queue_empty(idqinst);
1580   isfp = installed ? &isf : 0;
1581   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1582     {
1583       repo = s->repo;
1584       if (!repo)
1585         continue;
1586       if (s->obsoletes)
1587         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1588       if (s->conflicts)
1589         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1590       if (s->requires)
1591         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1592       if (s->recommends)
1593         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1594       if (s->suggests)
1595         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1596       if (s->supplements)
1597         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1598       if (s->enhances)
1599         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1600     }
1601   map_free(&sf.seen);
1602   map_free(&isf.seen);
1603   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1604   cbd.dids = 0;
1605   if (sf.nfiles)
1606     {
1607 #if 0
1608       for (i = 0; i < sf.nfiles; i++)
1609         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1610 #endif
1611       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1612       if (idq)
1613         for (i = 0; i < sf.nfiles; i++)
1614           queue_push(idq, sf.ids[i]);
1615       if (idqinst)
1616         for (i = 0; i < sf.nfiles; i++)
1617           queue_push(idqinst, sf.ids[i]);
1618       solv_free(sf.ids);
1619       for (i = 0; i < sf.nfiles; i++)
1620         {
1621           solv_free(sf.dirs[i]);
1622           solv_free(sf.names[i]);
1623         }
1624       solv_free(sf.dirs);
1625       solv_free(sf.names);
1626     }
1627   if (isf.nfiles)
1628     {
1629 #if 0
1630       for (i = 0; i < isf.nfiles; i++)
1631         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1632 #endif
1633       if (installed)
1634         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1635       if (installed && idqinst)
1636         for (i = 0; i < isf.nfiles; i++)
1637           queue_pushunique(idqinst, isf.ids[i]);
1638       solv_free(isf.ids);
1639       for (i = 0; i < isf.nfiles; i++)
1640         {
1641           solv_free(isf.dirs[i]);
1642           solv_free(isf.names[i]);
1643         }
1644       solv_free(isf.dirs);
1645       solv_free(isf.names);
1646     }
1647   solv_free(cbd.dids);
1648   pool_freewhatprovides(pool);  /* as we have added provides */
1649   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1650 }
1651
1652 void
1653 pool_addfileprovides(Pool *pool)
1654 {
1655   pool_addfileprovides_queue(pool, 0, 0);
1656 }
1657
1658 void
1659 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)
1660 {
1661   if (p)
1662     {
1663       if (pool->solvables[p].repo)
1664         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1665       return;
1666     }
1667   /* FIXME: obey callback return value! */
1668   for (p = 1; p < pool->nsolvables; p++)
1669     if (pool->solvables[p].repo)
1670       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1671 }
1672
1673 void
1674 pool_clear_pos(Pool *pool)
1675 {
1676   memset(&pool->pos, 0, sizeof(pool->pos));
1677 }
1678
1679
1680 void
1681 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1682 {
1683   int i;
1684
1685   pool->languagecache = solv_free(pool->languagecache);
1686   pool->languagecacheother = 0;
1687   for (i = 0; i < pool->nlanguages; i++)
1688     free((char *)pool->languages[i]);
1689   pool->languages = solv_free(pool->languages);
1690   pool->nlanguages = nlanguages;
1691   if (!nlanguages)
1692     return;
1693   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1694   for (i = 0; i < pool->nlanguages; i++)
1695     pool->languages[i] = solv_strdup(languages[i]);
1696 }
1697
1698 Id
1699 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1700 {
1701   const char *n;
1702   char buf[256], *p;
1703   int l;
1704
1705   if (!lang || !*lang)
1706     return id;
1707   n = pool_id2str(pool, id);
1708   l = strlen(n) + strlen(lang) + 2;
1709   if (l > sizeof(buf))
1710     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1711   else
1712     p = buf;
1713   sprintf(p, "%s:%s", n, lang);
1714   id = pool_str2id(pool, p, create);
1715   if (p != buf)
1716     free(p);
1717   return id;
1718 }
1719
1720 char *
1721 pool_alloctmpspace(Pool *pool, int len)
1722 {
1723   int n = pool->tmpspace.n;
1724   if (!len)
1725     return 0;
1726   if (len > pool->tmpspace.len[n])
1727     {
1728       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1729       pool->tmpspace.len[n] = len + 32;
1730     }
1731   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1732   return pool->tmpspace.buf[n];
1733 }
1734
1735 static char *
1736 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1737 {
1738   if (space)
1739     {
1740       int n, oldn;
1741       n = oldn = pool->tmpspace.n;
1742       for (;;)
1743         {
1744           if (!n--)
1745             n = POOL_TMPSPACEBUF - 1;
1746           if (n == oldn)
1747             break;
1748           if (pool->tmpspace.buf[n] != space)
1749             continue;
1750           if (len > pool->tmpspace.len[n])
1751             {
1752               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1753               pool->tmpspace.len[n] = len + 32;
1754             }
1755           return pool->tmpspace.buf[n];
1756         }
1757     }
1758   return 0;
1759 }
1760
1761 void
1762 pool_freetmpspace(Pool *pool, const char *space)
1763 {
1764   int n = pool->tmpspace.n;
1765   if (!space)
1766     return;
1767   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1768   if (pool->tmpspace.buf[n] == space)
1769     pool->tmpspace.n = n;
1770 }
1771
1772 char *
1773 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1774 {
1775   int l1, l2, l3;
1776   char *s, *str;
1777   l1 = str1 ? strlen(str1) : 0;
1778   l2 = str2 ? strlen(str2) : 0;
1779   l3 = str3 ? strlen(str3) : 0;
1780   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1781   if (l1)
1782     {
1783       strcpy(s, str1);
1784       s += l1;
1785     }
1786   if (l2)
1787     {
1788       strcpy(s, str2);
1789       s += l2;
1790     }
1791   if (l3)
1792     {
1793       strcpy(s, str3);
1794       s += l3;
1795     }
1796   *s = 0;
1797   return str;
1798 }
1799
1800 char *
1801 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1802 {
1803   int l1, l2, l3;
1804   char *s, *str;
1805
1806   l1 = str1 ? strlen(str1) : 0;
1807   l2 = str2 ? strlen(str2) : 0;
1808   l3 = str3 ? strlen(str3) : 0;
1809   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1810   if (str)
1811     str1 = str;
1812   else
1813     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1814   s = str;
1815   if (l1)
1816     {
1817       if (s != str1)
1818         strcpy(s, str1);
1819       s += l1;
1820     }
1821   if (l2)
1822     {
1823       strcpy(s, str2);
1824       s += l2;
1825     }
1826   if (l3)
1827     {
1828       strcpy(s, str3);
1829       s += l3;
1830     }
1831   *s = 0;
1832   return str;
1833 }
1834
1835 const char *
1836 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1837 {
1838   char *s;
1839   if (!len)
1840     return "";
1841   s = pool_alloctmpspace(pool, 2 * len + 1);
1842   solv_bin2hex(buf, len, s);
1843   return s;
1844 }
1845
1846 /*******************************************************************/
1847
1848 struct mptree {
1849   Id sibling;
1850   Id child;
1851   const char *comp;
1852   int compl;
1853   Id mountpoint;
1854 };
1855
1856 struct ducbdata {
1857   DUChanges *mps;
1858   struct mptree *mptree;
1859   int addsub;
1860   int hasdu;
1861
1862   Id *dirmap;
1863   int nmap;
1864   Repodata *olddata;
1865 };
1866
1867
1868 static int
1869 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1870 {
1871   struct ducbdata *cbd = cbdata;
1872   Id mp;
1873
1874   if (data != cbd->olddata)
1875     {
1876       Id dn, mp, comp, *dirmap, *dirs;
1877       int i, compl;
1878       const char *compstr;
1879       struct mptree *mptree;
1880
1881       /* create map from dir to mptree */
1882       cbd->dirmap = solv_free(cbd->dirmap);
1883       cbd->nmap = 0;
1884       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1885       mptree = cbd->mptree;
1886       mp = 0;
1887       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1888         {
1889           comp = *dirs++;
1890           if (comp <= 0)
1891             {
1892               mp = dirmap[-comp];
1893               continue;
1894             }
1895           if (mp < 0)
1896             {
1897               /* unconnected */
1898               dirmap[dn] = mp;
1899               continue;
1900             }
1901           if (!mptree[mp].child)
1902             {
1903               dirmap[dn] = -mp;
1904               continue;
1905             }
1906           if (data->localpool)
1907             compstr = stringpool_id2str(&data->spool, comp);
1908           else
1909             compstr = pool_id2str(data->repo->pool, comp);
1910           compl = strlen(compstr);
1911           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1912             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1913               break;
1914           dirmap[dn] = i ? i : -mp;
1915         }
1916       /* change dirmap to point to mountpoint instead of mptree */
1917       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1918         {
1919           mp = dirmap[dn];
1920           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1921         }
1922       cbd->dirmap = dirmap;
1923       cbd->nmap = data->dirpool.ndirs;
1924       cbd->olddata = data;
1925     }
1926   cbd->hasdu = 1;
1927   if (value->id < 0 || value->id >= cbd->nmap)
1928     return 0;
1929   mp = cbd->dirmap[value->id];
1930   if (mp < 0)
1931     return 0;
1932   if (cbd->addsub > 0)
1933     {
1934       cbd->mps[mp].kbytes += value->num;
1935       cbd->mps[mp].files += value->num2;
1936     }
1937   else
1938     {
1939       cbd->mps[mp].kbytes -= value->num;
1940       cbd->mps[mp].files -= value->num2;
1941     }
1942   return 0;
1943 }
1944
1945 static void
1946 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1947 {
1948   int i;
1949   if (mptree[pos].mountpoint == -1)
1950     mptree[pos].mountpoint = mountpoint;
1951   else
1952     mountpoint = mptree[pos].mountpoint;
1953   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1954     propagate_mountpoints(mptree, i, mountpoint);
1955 }
1956
1957 #define MPTREE_BLOCK 15
1958
1959 static struct mptree *
1960 create_mptree(DUChanges *mps, int nmps)
1961 {
1962   int i, nmptree;
1963   struct mptree *mptree;
1964   int pos, compl;
1965   int mp;
1966   const char *p, *path, *compstr;
1967
1968   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1969
1970   /* our root node */
1971   mptree[0].sibling = 0;
1972   mptree[0].child = 0;
1973   mptree[0].comp = 0;
1974   mptree[0].compl = 0;
1975   mptree[0].mountpoint = -1;
1976   nmptree = 1;
1977
1978   /* create component tree */
1979   for (mp = 0; mp < nmps; mp++)
1980     {
1981       mps[mp].kbytes = 0;
1982       mps[mp].files = 0;
1983       pos = 0;
1984       path = mps[mp].path;
1985       while(*path == '/')
1986         path++;
1987       while (*path)
1988         {
1989           if ((p = strchr(path, '/')) == 0)
1990             {
1991               compstr = path;
1992               compl = strlen(compstr);
1993               path += compl;
1994             }
1995           else
1996             {
1997               compstr = path;
1998               compl = p - path;
1999               path = p + 1;
2000               while(*path == '/')
2001                 path++;
2002             }
2003           for (i = mptree[pos].child; i; i = mptree[i].sibling)
2004             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
2005               break;
2006           if (!i)
2007             {
2008               /* create new node */
2009               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
2010               i = nmptree++;
2011               mptree[i].sibling = mptree[pos].child;
2012               mptree[i].child = 0;
2013               mptree[i].comp = compstr;
2014               mptree[i].compl = compl;
2015               mptree[i].mountpoint = -1;
2016               mptree[pos].child = i;
2017             }
2018           pos = i;
2019         }
2020       mptree[pos].mountpoint = mp;
2021     }
2022
2023   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
2024
2025 #if 0
2026   for (i = 0; i < nmptree; i++)
2027     {
2028       printf("#%d sibling: %d\n", i, mptree[i].sibling);
2029       printf("#%d child: %d\n", i, mptree[i].child);
2030       printf("#%d comp: %s\n", i, mptree[i].comp);
2031       printf("#%d compl: %d\n", i, mptree[i].compl);
2032       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
2033     }
2034 #endif
2035
2036   return mptree;
2037 }
2038
2039 void
2040 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
2041 {
2042   struct mptree *mptree;
2043   struct ducbdata cbd;
2044   Solvable *s;
2045   int sp;
2046   Map ignoredu;
2047   Repo *oldinstalled = pool->installed;
2048
2049   map_init(&ignoredu, 0);
2050   mptree = create_mptree(mps, nmps);
2051
2052   cbd.mps = mps;
2053   cbd.dirmap = 0;
2054   cbd.nmap = 0;
2055   cbd.olddata = 0;
2056   cbd.mptree = mptree;
2057   cbd.addsub = 1;
2058   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2059     {
2060       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2061         continue;
2062       if (!MAPTST(installedmap, sp))
2063         continue;
2064       cbd.hasdu = 0;
2065       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2066       if (!cbd.hasdu && oldinstalled)
2067         {
2068           Id op, opp;
2069           /* no du data available, ignore data of all installed solvables we obsolete */
2070           if (!ignoredu.size)
2071             map_grow(&ignoredu, oldinstalled->end - oldinstalled->start);
2072           if (s->obsoletes)
2073             {
2074               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2075               while ((obs = *obsp++) != 0)
2076                 FOR_PROVIDES(op, opp, obs)
2077                   if (op >= oldinstalled->start && op < oldinstalled->end)
2078                     MAPSET(&ignoredu, op - oldinstalled->start);
2079             }
2080           FOR_PROVIDES(op, opp, s->name)
2081             if (pool->solvables[op].name == s->name)
2082               if (op >= oldinstalled->start && op < oldinstalled->end)
2083                 MAPSET(&ignoredu, op - oldinstalled->start);
2084         }
2085     }
2086   cbd.addsub = -1;
2087   if (oldinstalled)
2088     {
2089       /* assumes we allways have du data for installed solvables */
2090       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2091         {
2092           if (MAPTST(installedmap, sp))
2093             continue;
2094           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
2095             continue;
2096           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2097         }
2098     }
2099   map_free(&ignoredu);
2100   solv_free(cbd.dirmap);
2101   solv_free(mptree);
2102 }
2103
2104 int
2105 pool_calc_installsizechange(Pool *pool, Map *installedmap)
2106 {
2107   Id sp;
2108   Solvable *s;
2109   int change = 0;
2110   Repo *oldinstalled = pool->installed;
2111
2112   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2113     {
2114       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2115         continue;
2116       if (!MAPTST(installedmap, sp))
2117         continue;
2118       change += solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2119     }
2120   if (oldinstalled)
2121     {
2122       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2123         {
2124           if (MAPTST(installedmap, sp))
2125             continue;
2126           change -= solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2127         }
2128     }
2129   return change;
2130 }
2131
2132 /* map:
2133  *  1: installed
2134  *  2: conflicts with installed
2135  *  8: interesting (only true if installed)
2136  * 16: undecided
2137  */
2138
2139 static inline Id dep2name(Pool *pool, Id dep)
2140 {
2141   while (ISRELDEP(dep))
2142     {
2143       Reldep *rd = GETRELDEP(pool, dep);
2144       dep = rd->name;
2145     }
2146   return dep;
2147 }
2148
2149 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con)
2150 {
2151   Id p, pp;
2152   Solvable *sn = pool->solvables + n;
2153
2154   FOR_PROVIDES(p, pp, sn->name)
2155     {
2156       Solvable *s = pool->solvables + p;
2157       if (s->name != sn->name || s->arch != sn->arch)
2158         continue;
2159       if ((map[p] & 9) != 9)
2160         continue;
2161       if (pool_match_nevr(pool, pool->solvables + p, con))
2162         continue;
2163       return 1;         /* found installed package that doesn't conflict */
2164     }
2165   return 0;
2166 }
2167
2168 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *multiversionmap)
2169 {
2170   Id p, pp;
2171   int r = 0;
2172   FOR_PROVIDES(p, pp, dep)
2173     {
2174       if (p == SYSTEMSOLVABLE)
2175         return 1;       /* always boring, as never constraining */
2176       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
2177         continue;
2178       if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
2179         if (providedbyinstalled_multiversion(pool, map, p, dep))
2180           continue;
2181       if ((map[p] & 9) == 9)
2182         return 9;
2183       r |= map[p] & 17;
2184     }
2185   return r;
2186 }
2187
2188 /*
2189  * pool_trivial_installable - calculate if a set of solvables is
2190  * trivial installable without any other installs/deinstalls of
2191  * packages not belonging to the set.
2192  *
2193  * the state is returned in the result queue:
2194  * 1:  solvable is installable without any other package changes
2195  * 0:  solvable is not installable
2196  * -1: solvable is installable, but doesn't constrain any installed packages
2197  */
2198
2199 void
2200 pool_trivial_installable_multiversionmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *multiversionmap)
2201 {
2202   int i, r, m, did;
2203   Id p, *dp, con, *conp, req, *reqp;
2204   unsigned char *map;
2205   Solvable *s;
2206
2207   map = solv_calloc(pool->nsolvables, 1);
2208   for (p = 1; p < pool->nsolvables; p++)
2209     {
2210       if (!MAPTST(installedmap, p))
2211         continue;
2212       map[p] |= 9;
2213       s = pool->solvables + p;
2214       if (!s->conflicts)
2215         continue;
2216       conp = s->repo->idarraydata + s->conflicts;
2217       while ((con = *conp++) != 0)
2218         {
2219           dp = pool_whatprovides_ptr(pool, con);
2220           for (; *dp; dp++)
2221             map[p] |= 2;        /* XXX: self conflict ? */
2222         }
2223     }
2224   for (i = 0; i < pkgs->count; i++)
2225     map[pkgs->elements[i]] = 16;
2226
2227   for (i = 0, did = 0; did < pkgs->count; i++, did++)
2228     {
2229       if (i == pkgs->count)
2230         i = 0;
2231       p = pkgs->elements[i];
2232       if ((map[p] & 16) == 0)
2233         continue;
2234       if ((map[p] & 2) != 0)
2235         {
2236           map[p] = 2;
2237           continue;
2238         }
2239       s = pool->solvables + p;
2240       m = 1;
2241       if (s->requires)
2242         {
2243           reqp = s->repo->idarraydata + s->requires;
2244           while ((req = *reqp++) != 0)
2245             {
2246               if (req == SOLVABLE_PREREQMARKER)
2247                 continue;
2248               r = providedbyinstalled(pool, map, req, 0, 0);
2249               if (!r)
2250                 {
2251                   /* decided and miss */
2252                   map[p] = 2;
2253                   did = 0;
2254                   break;
2255                 }
2256               if (r == 16)
2257                 break;  /* undecided */
2258               m |= r;   /* 1 | 9 | 17 */
2259             }
2260           if (req)
2261             continue;
2262           if ((m & 9) == 9)
2263             m = 9;
2264         }
2265       if (s->conflicts)
2266         {
2267           int ispatch = 0;      /* see solver.c patch handling */
2268
2269           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
2270             ispatch = 1;
2271           conp = s->repo->idarraydata + s->conflicts;
2272           while ((con = *conp++) != 0)
2273             {
2274               if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2275                 {
2276                   map[p] = 2;
2277                   did = 0;
2278                   break;
2279                 }
2280               if ((m == 1 || m == 17) && ISRELDEP(con))
2281                 {
2282                   con = dep2name(pool, con);
2283                   if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2284                     m = 9;
2285                 }
2286             }
2287           if (con)
2288             continue;   /* found a conflict */
2289         }
2290 #if 0
2291       if (s->repo && s->repo != oldinstalled)
2292         {
2293           Id p2, obs, *obsp, *pp;
2294           Solvable *s2;
2295           if (s->obsoletes)
2296             {
2297               obsp = s->repo->idarraydata + s->obsoletes;
2298               while ((obs = *obsp++) != 0)
2299                 {
2300                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
2301                     {
2302                       map[p] = 2;
2303                       break;
2304                     }
2305                 }
2306               if (obs)
2307                 continue;
2308             }
2309           FOR_PROVIDES(p2, pp, s->name)
2310             {
2311               s2 = pool->solvables + p2;
2312               if (s2->name == s->name && (map[p2] & 1) != 0)
2313                 {
2314                   map[p] = 2;
2315                   break;
2316                 }
2317             }
2318           if (p2)
2319             continue;
2320         }
2321 #endif
2322       if (m != map[p])
2323         {
2324           map[p] = m;
2325           did = 0;
2326         }
2327     }
2328   queue_free(res);
2329   queue_init_clone(res, pkgs);
2330   for (i = 0; i < pkgs->count; i++)
2331     {
2332       m = map[pkgs->elements[i]];
2333       if ((m & 9) == 9)
2334         r = 1;
2335       else if (m & 1)
2336         r = -1;
2337       else
2338         r = 0;
2339       res->elements[i] = r;
2340     }
2341   free(map);
2342 }
2343
2344 void
2345 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
2346 {
2347   pool_trivial_installable_multiversionmap(pool, installedmap, pkgs, res, 0);
2348 }
2349
2350 const char *
2351 pool_lookup_str(Pool *pool, Id entry, Id keyname)
2352 {
2353   if (entry == SOLVID_POS && pool->pos.repo)
2354     return repo_lookup_str(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2355   if (entry <= 0)
2356     return 0;
2357   return solvable_lookup_str(pool->solvables + entry, keyname);
2358 }
2359
2360 Id
2361 pool_lookup_id(Pool *pool, Id entry, Id keyname)
2362 {
2363   if (entry == SOLVID_POS && pool->pos.repo)
2364     return repo_lookup_id(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2365   if (entry <= 0)
2366     return 0;
2367   return solvable_lookup_id(pool->solvables + entry, keyname);
2368 }
2369
2370 unsigned long long
2371 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned long long notfound)
2372 {
2373   if (entry == SOLVID_POS && pool->pos.repo)
2374     return repo_lookup_num(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, notfound);
2375   if (entry <= 0)
2376     return notfound;
2377   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
2378 }
2379
2380 int
2381 pool_lookup_void(Pool *pool, Id entry, Id keyname)
2382 {
2383   if (entry == SOLVID_POS && pool->pos.repo)
2384     return repo_lookup_void(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2385   if (entry <= 0)
2386     return 0;
2387   return solvable_lookup_void(pool->solvables + entry, keyname);
2388 }
2389
2390 const unsigned char *
2391 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2392 {
2393   if (entry == SOLVID_POS && pool->pos.repo)
2394     return repo_lookup_bin_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2395   if (entry <= 0)
2396     return 0;
2397   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
2398 }
2399
2400 const char *
2401 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2402 {
2403   if (entry == SOLVID_POS && pool->pos.repo)
2404     return repo_lookup_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2405   if (entry <= 0)
2406     return 0;
2407   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2408 }
2409
2410 int
2411 pool_lookup_idarray(Pool *pool, Id entry, Id keyname, Queue *q)
2412 {
2413   if (entry == SOLVID_POS && pool->pos.repo)
2414     return repo_lookup_idarray(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, q);
2415   if (entry <= 0)
2416     return 0;
2417   return solvable_lookup_idarray(pool->solvables + entry, keyname, q);
2418 }
2419
2420 const char *
2421 pool_lookup_deltalocation(Pool *pool, Id entry, unsigned int *medianrp)
2422 {
2423   const char *loc;
2424   if (medianrp)
2425     *medianrp = 0;
2426   if (entry != SOLVID_POS)
2427     return 0;
2428   loc = pool_lookup_str(pool, entry, DELTA_LOCATION_DIR);
2429   loc = pool_tmpjoin(pool, loc, loc ? "/" : 0, pool_lookup_str(pool, entry, DELTA_LOCATION_NAME));
2430   loc = pool_tmpappend(pool, loc, "-", pool_lookup_str(pool, entry, DELTA_LOCATION_EVR));
2431   loc = pool_tmpappend(pool, loc, ".", pool_lookup_str(pool, entry, DELTA_LOCATION_SUFFIX));
2432   return loc;
2433 }
2434
2435 static void
2436 add_new_provider(Pool *pool, Id id, Id p)
2437 {
2438   Queue q;
2439   Id *pp;
2440
2441   while (ISRELDEP(id))
2442     {
2443       Reldep *rd = GETRELDEP(pool, id);
2444       id = rd->name;
2445     }
2446
2447   queue_init(&q);
2448   for (pp = pool->whatprovidesdata + pool->whatprovides[id]; *pp; pp++)
2449     {
2450       if (*pp == p)
2451         {
2452           queue_free(&q);
2453           return;
2454         }
2455       if (*pp > p)
2456         {
2457           queue_push(&q, p);
2458           p = 0;
2459         }
2460       queue_push(&q, *pp);
2461     }
2462   if (p)
2463     queue_push(&q, p);
2464   pool->whatprovides[id] = pool_queuetowhatprovides(pool, &q);
2465   queue_free(&q);
2466 }
2467
2468 void
2469 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2470 {
2471   int hadhashes = pool->relhashtbl ? 1 : 0;
2472   Solvable *s;
2473   Id fn, p, q, md5;
2474   Id id;
2475   int i;
2476
2477   if (!conflicts->count)
2478     return;
2479   for (i = 0; i < conflicts->count; i += 6)
2480     {
2481       fn = conflicts->elements[i];
2482       p = conflicts->elements[i + 1];
2483       md5 = conflicts->elements[i + 2];
2484       q = conflicts->elements[i + 4];
2485       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2486       s = pool->solvables + p;
2487       if (!s->repo)
2488         continue;
2489       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2490       if (pool->whatprovides)
2491         add_new_provider(pool, fn, p);
2492       if (pool->whatprovides_rel)
2493         pool->whatprovides_rel[GETRELID(id)] = 0;       /* clear cache */
2494       s = pool->solvables + q;
2495       if (!s->repo)
2496         continue;
2497       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2498     }
2499   if (!hadhashes)
2500     pool_freeidhashes(pool);
2501 }
2502
2503 char *
2504 pool_prepend_rootdir(Pool *pool, const char *path)
2505 {
2506   if (!path)
2507     return 0;
2508   if (!pool->rootdir)
2509     return solv_strdup(path);
2510   return solv_dupjoin(pool->rootdir, "/", *path == '/' ? path + 1 : path);
2511 }
2512
2513 const char *
2514 pool_prepend_rootdir_tmp(Pool *pool, const char *path)
2515 {
2516   if (!path)
2517     return 0;
2518   if (!pool->rootdir)
2519     return path;
2520   return pool_tmpjoin(pool, pool->rootdir, "/", *path == '/' ? path + 1 : path);
2521 }
2522
2523 void
2524 pool_set_rootdir(Pool *pool, const char *rootdir)
2525 {
2526   solv_free(pool->rootdir);
2527   pool->rootdir = solv_strdup(rootdir);
2528 }
2529
2530 const char *
2531 pool_get_rootdir(Pool *pool)
2532 {
2533   return pool->rootdir;
2534 }
2535
2536 /* only used in libzypp */
2537 void
2538 pool_set_custom_vendorcheck(Pool *pool, int (*vendorcheck)(Pool *, Solvable *, Solvable *))
2539 {
2540   pool->custom_vendorcheck = vendorcheck;
2541 }
2542
2543 /* EOF */