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