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