fix archpolicy handling of '>'
[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 /*************************************************************************/
1069
1070 void
1071 pool_debug(Pool *pool, int type, const char *format, ...)
1072 {
1073   va_list args;
1074   char buf[1024];
1075
1076   if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0)
1077     {
1078       if ((pool->debugmask & type) == 0)
1079         return;
1080     }
1081   va_start(args, format);
1082   if (!pool->debugcallback)
1083     {
1084       if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0 && !(pool->debugmask & SOLV_DEBUG_TO_STDERR))
1085         vprintf(format, args);
1086       else
1087         vfprintf(stderr, format, args);
1088       return;
1089     }
1090   vsnprintf(buf, sizeof(buf), format, args);
1091   va_end(args);
1092   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
1093 }
1094
1095 int
1096 pool_error(Pool *pool, int ret, const char *format, ...)
1097 {
1098   va_list args;
1099   int l;
1100   va_start(args, format);
1101   if (!pool->errstr)
1102     {
1103       pool->errstra = 1024;
1104       pool->errstr = solv_malloc(pool->errstra);
1105     }
1106   if (!*format)
1107     {
1108       *pool->errstr = 0;
1109       l = 0;
1110     }
1111   else
1112     l = vsnprintf(pool->errstr, pool->errstra, format, args);
1113   va_end(args);
1114   if (l >= 0 && l + 1 > pool->errstra)
1115     {
1116       pool->errstra = l + 256;
1117       pool->errstr = solv_realloc(pool->errstr, pool->errstra);
1118       va_start(args, format);
1119       l = vsnprintf(pool->errstr, pool->errstra, format, args);
1120       va_end(args);
1121     }
1122   if (l < 0)
1123     strcpy(pool->errstr, "unknown error");
1124   if (pool->debugmask & SOLV_ERROR)
1125     pool_debug(pool, SOLV_ERROR, "%s\n", pool->errstr);
1126   return ret;
1127 }
1128
1129 char *
1130 pool_errstr(Pool *pool)
1131 {
1132   return pool->errstr ? pool->errstr : "no error";
1133 }
1134
1135 void
1136 pool_setdebuglevel(Pool *pool, int level)
1137 {
1138   int mask = SOLV_DEBUG_RESULT;
1139   if (level > 0)
1140     mask |= SOLV_DEBUG_STATS|SOLV_DEBUG_ANALYZE|SOLV_DEBUG_UNSOLVABLE|SOLV_DEBUG_SOLVER|SOLV_DEBUG_TRANSACTION|SOLV_ERROR;
1141   if (level > 1)
1142     mask |= SOLV_DEBUG_JOB|SOLV_DEBUG_SOLUTIONS|SOLV_DEBUG_POLICY;
1143   if (level > 2)
1144     mask |= SOLV_DEBUG_PROPAGATE;
1145   if (level > 3)
1146     mask |= SOLV_DEBUG_RULE_CREATION;
1147   mask |= pool->debugmask & SOLV_DEBUG_TO_STDERR;       /* keep bit */
1148   pool->debugmask = mask;
1149 }
1150
1151 void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
1152 {
1153   pool->debugcallback = debugcallback;
1154   pool->debugcallbackdata = debugcallbackdata;
1155 }
1156
1157 void pool_setdebugmask(Pool *pool, int mask)
1158 {
1159   pool->debugmask = mask;
1160 }
1161
1162 void pool_setloadcallback(Pool *pool, int (*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
1163 {
1164   pool->loadcallback = cb;
1165   pool->loadcallbackdata = loadcbdata;
1166 }
1167
1168 void pool_setnamespacecallback(Pool *pool, Id (*cb)(struct _Pool *, void *, Id, Id), void *nscbdata)
1169 {
1170   pool->nscallback = cb;
1171   pool->nscallbackdata = nscbdata;
1172 }
1173
1174 /*************************************************************************/
1175
1176 struct searchfiles {
1177   Id *ids;
1178   char **dirs;
1179   char **names;
1180   int nfiles;
1181   Map seen;
1182 };
1183
1184 #define SEARCHFILES_BLOCK 127
1185
1186 static void
1187 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
1188 {
1189   Id dep, sid;
1190   const char *s, *sr;
1191   struct searchfiles *csf;
1192
1193   while ((dep = *ida++) != 0)
1194     {
1195       csf = sf;
1196       while (ISRELDEP(dep))
1197         {
1198           Reldep *rd;
1199           sid = pool->ss.nstrings + GETRELID(dep);
1200           if (MAPTST(&csf->seen, sid))
1201             {
1202               dep = 0;
1203               break;
1204             }
1205           MAPSET(&csf->seen, sid);
1206           rd = GETRELDEP(pool, dep);
1207           if (rd->flags < 8)
1208             dep = rd->name;
1209           else if (rd->flags == REL_NAMESPACE)
1210             {
1211               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
1212                 {
1213                   csf = isf;
1214                   if (!csf || MAPTST(&csf->seen, sid))
1215                     {
1216                       dep = 0;
1217                       break;
1218                     }
1219                   MAPSET(&csf->seen, sid);
1220                 }
1221               dep = rd->evr;
1222             }
1223           else if (rd->flags == REL_FILECONFLICT)
1224             {
1225               dep = 0;
1226               break;
1227             }
1228           else
1229             {
1230               Id ids[2];
1231               ids[0] = rd->name;
1232               ids[1] = 0;
1233               pool_addfileprovides_dep(pool, ids, csf, isf);
1234               dep = rd->evr;
1235             }
1236         }
1237       if (!dep)
1238         continue;
1239       if (MAPTST(&csf->seen, dep))
1240         continue;
1241       MAPSET(&csf->seen, dep);
1242       s = pool_id2str(pool, dep);
1243       if (*s != '/')
1244         continue;
1245       if (csf != isf && pool->addedfileprovides == 1 && !repodata_filelistfilter_matches(0, s))
1246         continue;       /* skip non-standard locations csf == isf: installed case */
1247       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1248       csf->dirs = solv_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1249       csf->names = solv_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1250       csf->ids[csf->nfiles] = dep;
1251       sr = strrchr(s, '/');
1252       csf->names[csf->nfiles] = solv_strdup(sr + 1);
1253       csf->dirs[csf->nfiles] = solv_malloc(sr - s + 1);
1254       if (sr != s)
1255         strncpy(csf->dirs[csf->nfiles], s, sr - s);
1256       csf->dirs[csf->nfiles][sr - s] = 0;
1257       csf->nfiles++;
1258     }
1259 }
1260
1261 struct addfileprovides_cbdata {
1262   int nfiles;
1263   Id *ids;
1264   char **dirs;
1265   char **names;
1266
1267   Id *dids;
1268
1269   Map providedids;
1270
1271   Map useddirs;
1272 };
1273
1274 static int
1275 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1276 {
1277   struct addfileprovides_cbdata *cbd = cbdata;
1278   int i;
1279
1280   if (!cbd->useddirs.size)
1281     {
1282       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
1283       for (i = 0; i < cbd->nfiles; i++)
1284         {
1285           Id did;
1286           if (MAPTST(&cbd->providedids, cbd->ids[i]))
1287             {
1288               cbd->dids[i] = 0;
1289               continue;
1290             }
1291           did = repodata_str2dir(data, cbd->dirs[i], 0);
1292           cbd->dids[i] = did;
1293           if (did)
1294             MAPSET(&cbd->useddirs, did);
1295         }
1296       repodata_free_dircache(data);
1297     }
1298   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
1299     return 0;
1300   for (i = 0; i < cbd->nfiles; i++)
1301     {
1302       if (cbd->dids[i] != value->id)
1303         continue;
1304       if (!strcmp(cbd->names[i], value->str))
1305         break;
1306     }
1307   if (i == cbd->nfiles)
1308     return 0;
1309   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
1310   return 0;
1311 }
1312
1313 static void
1314 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
1315 {
1316   Id p;
1317   Repodata *data;
1318   Repo *repo;
1319   Queue fileprovidesq;
1320   int i, j, repoid, repodataid;
1321   int provstart, provend;
1322   Map donemap;
1323   int ndone, incomplete;
1324
1325   if (!pool->urepos)
1326     return;
1327
1328   cbd->nfiles = sf->nfiles;
1329   cbd->ids = sf->ids;
1330   cbd->dirs = sf->dirs;
1331   cbd->names = sf->names;
1332   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
1333   map_init(&cbd->providedids, pool->ss.nstrings);
1334
1335   repoid = 1;
1336   repo = repoonly ? repoonly : pool->repos[repoid];
1337   map_init(&donemap, pool->nsolvables);
1338   queue_init(&fileprovidesq);
1339   provstart = provend = 0;
1340   for (;;)
1341     {
1342       if (!repo || repo->disabled)
1343         {
1344           if (repoonly || ++repoid == pool->nrepos)
1345             break;
1346           repo = pool->repos[repoid];
1347           continue;
1348         }
1349       ndone = 0;
1350       FOR_REPODATAS(repo, repodataid, data)
1351         {
1352           if (ndone >= repo->nsolvables)
1353             break;
1354
1355           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1356             {
1357               map_empty(&cbd->providedids);
1358               for (i = 0; i < fileprovidesq.count; i++)
1359                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1360               provstart = data->start;
1361               provend = data->end;
1362               for (i = 0; i < cbd->nfiles; i++)
1363                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1364                   break;
1365               if (i == cbd->nfiles)
1366                 {
1367                   /* great! no need to search files */
1368                   for (p = data->start; p < data->end; p++)
1369                     if (pool->solvables[p].repo == repo)
1370                       {
1371                         if (MAPTST(&donemap, p))
1372                           continue;
1373                         MAPSET(&donemap, p);
1374                         ndone++;
1375                       }
1376                   continue;
1377                 }
1378             }
1379
1380           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1381             continue;
1382
1383           if (data->start < provstart || data->end > provend)
1384             {
1385               map_empty(&cbd->providedids);
1386               provstart = provend = 0;
1387             }
1388
1389           /* check if the data is incomplete */
1390           incomplete = 0;
1391           if (data->state == REPODATA_AVAILABLE)
1392             {
1393               for (j = 1; j < data->nkeys; j++)
1394                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1395                   break;
1396               if (j < data->nkeys)
1397                 {
1398 #if 0
1399                   for (i = 0; i < cbd->nfiles; i++)
1400                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1401                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1402 #endif
1403                   for (i = 0; i < cbd->nfiles; i++)
1404                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1405                       break;
1406                   if (i < cbd->nfiles)
1407                     incomplete = 1;
1408                 }
1409             }
1410
1411           /* do the search */
1412           map_init(&cbd->useddirs, 0);
1413           for (p = data->start; p < data->end; p++)
1414             if (pool->solvables[p].repo == repo)
1415               {
1416                 if (MAPTST(&donemap, p))
1417                   continue;
1418                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1419                 if (!incomplete)
1420                   {
1421                     MAPSET(&donemap, p);
1422                     ndone++;
1423                   }
1424               }
1425           map_free(&cbd->useddirs);
1426         }
1427
1428       if (repoonly || ++repoid == pool->nrepos)
1429         break;
1430       repo = pool->repos[repoid];
1431     }
1432   map_free(&donemap);
1433   queue_free(&fileprovidesq);
1434   map_free(&cbd->providedids);
1435 }
1436
1437 void
1438 pool_addfileprovides_queue(Pool *pool, Queue *idq, Queue *idqinst)
1439 {
1440   Solvable *s;
1441   Repo *installed, *repo;
1442   struct searchfiles sf, isf, *isfp;
1443   struct addfileprovides_cbdata cbd;
1444   int i;
1445   unsigned int now;
1446
1447   installed = pool->installed;
1448   now = solv_timems(0);
1449   memset(&sf, 0, sizeof(sf));
1450   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1451   memset(&isf, 0, sizeof(isf));
1452   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1453   pool->addedfileprovides = pool->addfileprovidesfiltered ? 1 : 2;
1454
1455   if (idq)
1456     queue_empty(idq);
1457   if (idqinst)
1458     queue_empty(idqinst);
1459   isfp = installed ? &isf : 0;
1460   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1461     {
1462       repo = s->repo;
1463       if (!repo)
1464         continue;
1465       if (s->obsoletes)
1466         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1467       if (s->conflicts)
1468         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1469       if (s->requires)
1470         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1471       if (s->recommends)
1472         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1473       if (s->suggests)
1474         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1475       if (s->supplements)
1476         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1477       if (s->enhances)
1478         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1479     }
1480   map_free(&sf.seen);
1481   map_free(&isf.seen);
1482   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1483   cbd.dids = 0;
1484   if (sf.nfiles)
1485     {
1486 #if 0
1487       for (i = 0; i < sf.nfiles; i++)
1488         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1489 #endif
1490       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1491       if (idq)
1492         for (i = 0; i < sf.nfiles; i++)
1493           queue_push(idq, sf.ids[i]);
1494       if (idqinst)
1495         for (i = 0; i < sf.nfiles; i++)
1496           queue_push(idqinst, sf.ids[i]);
1497       solv_free(sf.ids);
1498       for (i = 0; i < sf.nfiles; i++)
1499         {
1500           solv_free(sf.dirs[i]);
1501           solv_free(sf.names[i]);
1502         }
1503       solv_free(sf.dirs);
1504       solv_free(sf.names);
1505     }
1506   if (isf.nfiles)
1507     {
1508 #if 0
1509       for (i = 0; i < isf.nfiles; i++)
1510         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1511 #endif
1512       if (installed)
1513         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1514       if (installed && idqinst)
1515         for (i = 0; i < isf.nfiles; i++)
1516           queue_pushunique(idqinst, isf.ids[i]);
1517       solv_free(isf.ids);
1518       for (i = 0; i < isf.nfiles; i++)
1519         {
1520           solv_free(isf.dirs[i]);
1521           solv_free(isf.names[i]);
1522         }
1523       solv_free(isf.dirs);
1524       solv_free(isf.names);
1525     }
1526   solv_free(cbd.dids);
1527   pool_freewhatprovides(pool);  /* as we have added provides */
1528   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1529 }
1530
1531 void
1532 pool_addfileprovides(Pool *pool)
1533 {
1534   pool_addfileprovides_queue(pool, 0, 0);
1535 }
1536
1537 void
1538 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)
1539 {
1540   if (p)
1541     {
1542       if (pool->solvables[p].repo)
1543         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1544       return;
1545     }
1546   /* FIXME: obey callback return value! */
1547   for (p = 1; p < pool->nsolvables; p++)
1548     if (pool->solvables[p].repo)
1549       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1550 }
1551
1552 void
1553 pool_clear_pos(Pool *pool)
1554 {
1555   memset(&pool->pos, 0, sizeof(pool->pos));
1556 }
1557
1558
1559 void
1560 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1561 {
1562   int i;
1563
1564   pool->languagecache = solv_free(pool->languagecache);
1565   pool->languagecacheother = 0;
1566   if (pool->nlanguages)
1567     {
1568       for (i = 0; i < pool->nlanguages; i++)
1569         free((char *)pool->languages[i]);
1570       free(pool->languages);
1571     }
1572   pool->nlanguages = nlanguages;
1573   if (!nlanguages)
1574     return;
1575   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1576   for (i = 0; i < pool->nlanguages; i++)
1577     pool->languages[i] = solv_strdup(languages[i]);
1578 }
1579
1580 Id
1581 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1582 {
1583   const char *n;
1584   char buf[256], *p;
1585   int l;
1586
1587   if (!lang || !*lang)
1588     return id;
1589   n = pool_id2str(pool, id);
1590   l = strlen(n) + strlen(lang) + 2;
1591   if (l > sizeof(buf))
1592     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1593   else
1594     p = buf;
1595   sprintf(p, "%s:%s", n, lang);
1596   id = pool_str2id(pool, p, create);
1597   if (p != buf)
1598     free(p);
1599   return id;
1600 }
1601
1602 char *
1603 pool_alloctmpspace(Pool *pool, int len)
1604 {
1605   int n = pool->tmpspace.n;
1606   if (!len)
1607     return 0;
1608   if (len > pool->tmpspace.len[n])
1609     {
1610       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1611       pool->tmpspace.len[n] = len + 32;
1612     }
1613   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1614   return pool->tmpspace.buf[n];
1615 }
1616
1617 static char *
1618 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1619 {
1620   if (space)
1621     {
1622       int n, oldn;
1623       n = oldn = pool->tmpspace.n;
1624       for (;;)
1625         {
1626           if (!n--)
1627             n = POOL_TMPSPACEBUF - 1;
1628           if (n == oldn)
1629             break;
1630           if (pool->tmpspace.buf[n] != space)
1631             continue;
1632           if (len > pool->tmpspace.len[n])
1633             {
1634               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1635               pool->tmpspace.len[n] = len + 32;
1636             }
1637           return pool->tmpspace.buf[n];
1638         }
1639     }
1640   return 0;
1641 }
1642
1643 void
1644 pool_freetmpspace(Pool *pool, const char *space)
1645 {
1646   int n = pool->tmpspace.n;
1647   if (!space)
1648     return;
1649   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1650   if (pool->tmpspace.buf[n] == space)
1651     pool->tmpspace.n = n;
1652 }
1653
1654 char *
1655 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1656 {
1657   int l1, l2, l3;
1658   char *s, *str;
1659   l1 = str1 ? strlen(str1) : 0;
1660   l2 = str2 ? strlen(str2) : 0;
1661   l3 = str3 ? strlen(str3) : 0;
1662   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1663   if (l1)
1664     {
1665       strcpy(s, str1);
1666       s += l1;
1667     }
1668   if (l2)
1669     {
1670       strcpy(s, str2);
1671       s += l2;
1672     }
1673   if (l3)
1674     {
1675       strcpy(s, str3);
1676       s += l3;
1677     }
1678   *s = 0;
1679   return str;
1680 }
1681
1682 char *
1683 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1684 {
1685   int l1, l2, l3;
1686   char *s, *str;
1687
1688   l1 = str1 ? strlen(str1) : 0;
1689   l2 = str2 ? strlen(str2) : 0;
1690   l3 = str3 ? strlen(str3) : 0;
1691   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1692   if (str)
1693     str1 = str;
1694   else
1695     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1696   s = str;
1697   if (l1)
1698     {
1699       if (s != str1)
1700         strcpy(s, str1);
1701       s += l1;
1702     }
1703   if (l2)
1704     {
1705       strcpy(s, str2);
1706       s += l2;
1707     }
1708   if (l3)
1709     {
1710       strcpy(s, str3);
1711       s += l3;
1712     }
1713   *s = 0;
1714   return str;
1715 }
1716
1717 const char *
1718 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1719 {
1720   char *s;
1721   if (!len)
1722     return "";
1723   s = pool_alloctmpspace(pool, 2 * len + 1);
1724   solv_bin2hex(buf, len, s);
1725   return s;
1726 }
1727
1728 /*******************************************************************/
1729
1730 struct mptree {
1731   Id sibling;
1732   Id child;
1733   const char *comp;
1734   int compl;
1735   Id mountpoint;
1736 };
1737
1738 struct ducbdata {
1739   DUChanges *mps;
1740   struct mptree *mptree;
1741   int addsub;
1742   int hasdu;
1743
1744   Id *dirmap;
1745   int nmap;
1746   Repodata *olddata;
1747 };
1748
1749
1750 static int
1751 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1752 {
1753   struct ducbdata *cbd = cbdata;
1754   Id mp;
1755
1756   if (data != cbd->olddata)
1757     {
1758       Id dn, mp, comp, *dirmap, *dirs;
1759       int i, compl;
1760       const char *compstr;
1761       struct mptree *mptree;
1762
1763       /* create map from dir to mptree */
1764       cbd->dirmap = solv_free(cbd->dirmap);
1765       cbd->nmap = 0;
1766       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1767       mptree = cbd->mptree;
1768       mp = 0;
1769       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1770         {
1771           comp = *dirs++;
1772           if (comp <= 0)
1773             {
1774               mp = dirmap[-comp];
1775               continue;
1776             }
1777           if (mp < 0)
1778             {
1779               /* unconnected */
1780               dirmap[dn] = mp;
1781               continue;
1782             }
1783           if (!mptree[mp].child)
1784             {
1785               dirmap[dn] = -mp;
1786               continue;
1787             }
1788           if (data->localpool)
1789             compstr = stringpool_id2str(&data->spool, comp);
1790           else
1791             compstr = pool_id2str(data->repo->pool, comp);
1792           compl = strlen(compstr);
1793           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1794             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1795               break;
1796           dirmap[dn] = i ? i : -mp;
1797         }
1798       /* change dirmap to point to mountpoint instead of mptree */
1799       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1800         {
1801           mp = dirmap[dn];
1802           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1803         }
1804       cbd->dirmap = dirmap;
1805       cbd->nmap = data->dirpool.ndirs;
1806       cbd->olddata = data;
1807     }
1808   cbd->hasdu = 1;
1809   if (value->id < 0 || value->id >= cbd->nmap)
1810     return 0;
1811   mp = cbd->dirmap[value->id];
1812   if (mp < 0)
1813     return 0;
1814   if (cbd->addsub > 0)
1815     {
1816       cbd->mps[mp].kbytes += value->num;
1817       cbd->mps[mp].files += value->num2;
1818     }
1819   else
1820     {
1821       cbd->mps[mp].kbytes -= value->num;
1822       cbd->mps[mp].files -= value->num2;
1823     }
1824   return 0;
1825 }
1826
1827 static void
1828 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1829 {
1830   int i;
1831   if (mptree[pos].mountpoint == -1)
1832     mptree[pos].mountpoint = mountpoint;
1833   else
1834     mountpoint = mptree[pos].mountpoint;
1835   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1836     propagate_mountpoints(mptree, i, mountpoint);
1837 }
1838
1839 #define MPTREE_BLOCK 15
1840
1841 void
1842 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1843 {
1844   char *p;
1845   const char *path, *compstr;
1846   struct mptree *mptree;
1847   int i, nmptree;
1848   int pos, compl;
1849   int mp;
1850   struct ducbdata cbd;
1851   Solvable *s;
1852   Id sp;
1853   Map ignoredu;
1854   Repo *oldinstalled = pool->installed;
1855
1856   memset(&ignoredu, 0, sizeof(ignoredu));
1857   cbd.mps = mps;
1858   cbd.addsub = 0;
1859   cbd.dirmap = 0;
1860   cbd.nmap = 0;
1861   cbd.olddata = 0;
1862
1863   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1864
1865   /* our root node */
1866   mptree[0].sibling = 0;
1867   mptree[0].child = 0;
1868   mptree[0].comp = 0;
1869   mptree[0].compl = 0;
1870   mptree[0].mountpoint = -1;
1871   nmptree = 1;
1872   
1873   /* create component tree */
1874   for (mp = 0; mp < nmps; mp++)
1875     {
1876       mps[mp].kbytes = 0;
1877       mps[mp].files = 0;
1878       pos = 0;
1879       path = mps[mp].path;
1880       while(*path == '/')
1881         path++;
1882       while (*path)
1883         {
1884           if ((p = strchr(path, '/')) == 0)
1885             {
1886               compstr = path;
1887               compl = strlen(compstr);
1888               path += compl;
1889             }
1890           else
1891             {
1892               compstr = path;
1893               compl = p - path;
1894               path = p + 1;
1895               while(*path == '/')
1896                 path++;
1897             }
1898           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1899             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1900               break;
1901           if (!i)
1902             {
1903               /* create new node */
1904               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1905               i = nmptree++;
1906               mptree[i].sibling = mptree[pos].child;
1907               mptree[i].child = 0;
1908               mptree[i].comp = compstr;
1909               mptree[i].compl = compl;
1910               mptree[i].mountpoint = -1;
1911               mptree[pos].child = i;
1912             }
1913           pos = i;
1914         }
1915       mptree[pos].mountpoint = mp;
1916     }
1917
1918   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1919
1920 #if 0
1921   for (i = 0; i < nmptree; i++)
1922     {
1923       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1924       printf("#%d child: %d\n", i, mptree[i].child);
1925       printf("#%d comp: %s\n", i, mptree[i].comp);
1926       printf("#%d compl: %d\n", i, mptree[i].compl);
1927       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1928     }
1929 #endif
1930
1931   cbd.mptree = mptree;
1932   cbd.addsub = 1;
1933   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1934     {
1935       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1936         continue;
1937       if (!MAPTST(installedmap, sp))
1938         continue;
1939       cbd.hasdu = 0;
1940       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1941       if (!cbd.hasdu && oldinstalled)
1942         {
1943           Id op, opp;
1944           /* no du data available, ignore data of all installed solvables we obsolete */
1945           if (!ignoredu.map)
1946             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1947           if (s->obsoletes)
1948             {
1949               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1950               while ((obs = *obsp++) != 0)
1951                 FOR_PROVIDES(op, opp, obs)
1952                   if (op >= oldinstalled->start && op < oldinstalled->end)
1953                     MAPSET(&ignoredu, op - oldinstalled->start);
1954             }
1955           FOR_PROVIDES(op, opp, s->name)
1956             if (pool->solvables[op].name == s->name)
1957               if (op >= oldinstalled->start && op < oldinstalled->end)
1958                 MAPSET(&ignoredu, op - oldinstalled->start);
1959         }
1960     }
1961   cbd.addsub = -1;
1962   if (oldinstalled)
1963     {
1964       /* assumes we allways have du data for installed solvables */
1965       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1966         {
1967           if (MAPTST(installedmap, sp))
1968             continue;
1969           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1970             continue;
1971           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1972         }
1973     }
1974   if (ignoredu.map)
1975     map_free(&ignoredu);
1976   solv_free(cbd.dirmap);
1977   solv_free(mptree);
1978 }
1979
1980 int
1981 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1982 {
1983   Id sp;
1984   Solvable *s;
1985   int change = 0;
1986   Repo *oldinstalled = pool->installed;
1987
1988   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1989     {
1990       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1991         continue;
1992       if (!MAPTST(installedmap, sp))
1993         continue;
1994       change += solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
1995     }
1996   if (oldinstalled)
1997     {
1998       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1999         {
2000           if (MAPTST(installedmap, sp))
2001             continue;
2002           change -= solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2003         }
2004     }
2005   return change;
2006 }
2007
2008 /* map:
2009  *  1: installed
2010  *  2: conflicts with installed
2011  *  8: interesting (only true if installed)
2012  * 16: undecided
2013  */
2014  
2015 static inline Id dep2name(Pool *pool, Id dep)
2016 {
2017   while (ISRELDEP(dep))
2018     {
2019       Reldep *rd = rd = GETRELDEP(pool, dep);
2020       dep = rd->name;
2021     }
2022   return dep;
2023 }
2024
2025 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
2026 {
2027   Id p, pp;
2028   Solvable *sn = pool->solvables + n; 
2029
2030   FOR_PROVIDES(p, pp, sn->name)
2031     {    
2032       Solvable *s = pool->solvables + p; 
2033       if (s->name != sn->name || s->arch != sn->arch)
2034         continue;
2035       if ((map[p] & 9) != 9)
2036         continue;
2037       if (pool_match_nevr(pool, pool->solvables + p, con))
2038         continue;
2039       return 1;         /* found installed package that doesn't conflict */
2040     }
2041   return 0;
2042 }
2043
2044 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *multiversionmap)
2045 {
2046   Id p, pp;
2047   int r = 0;
2048   FOR_PROVIDES(p, pp, dep)
2049     {
2050       if (p == SYSTEMSOLVABLE)
2051         return 1;       /* always boring, as never constraining */
2052       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
2053         continue;
2054       if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
2055         if (providedbyinstalled_multiversion(pool, map, p, dep))
2056           continue;
2057       if ((map[p] & 9) == 9)
2058         return 9;
2059       r |= map[p] & 17;
2060     }
2061   return r;
2062 }
2063
2064 /*
2065  * pool_trivial_installable - calculate if a set of solvables is
2066  * trivial installable without any other installs/deinstalls of
2067  * packages not belonging to the set.
2068  *
2069  * the state is returned in the result queue:
2070  * 1:  solvable is installable without any other package changes
2071  * 0:  solvable is not installable
2072  * -1: solvable is installable, but doesn't constrain any installed packages
2073  */
2074
2075 void
2076 pool_trivial_installable_multiversionmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *multiversionmap)
2077 {
2078   int i, r, m, did;
2079   Id p, *dp, con, *conp, req, *reqp;
2080   unsigned char *map;
2081   Solvable *s;
2082
2083   map = solv_calloc(pool->nsolvables, 1);
2084   for (p = 1; p < pool->nsolvables; p++)
2085     {
2086       if (!MAPTST(installedmap, p))
2087         continue;
2088       map[p] |= 9;
2089       s = pool->solvables + p;
2090       if (!s->conflicts)
2091         continue;
2092       conp = s->repo->idarraydata + s->conflicts;
2093       while ((con = *conp++) != 0)
2094         {
2095           dp = pool_whatprovides_ptr(pool, con);
2096           for (; *dp; dp++)
2097             map[p] |= 2;        /* XXX: self conflict ? */
2098         }
2099     }
2100   for (i = 0; i < pkgs->count; i++)
2101     map[pkgs->elements[i]] = 16;
2102
2103   for (i = 0, did = 0; did < pkgs->count; i++, did++)
2104     {
2105       if (i == pkgs->count)
2106         i = 0;
2107       p = pkgs->elements[i];
2108       if ((map[p] & 16) == 0)
2109         continue;
2110       if ((map[p] & 2) != 0)
2111         {
2112           map[p] = 2;
2113           continue;
2114         }
2115       s = pool->solvables + p;
2116       m = 1;
2117       if (s->requires)
2118         {
2119           reqp = s->repo->idarraydata + s->requires;
2120           while ((req = *reqp++) != 0)
2121             {
2122               if (req == SOLVABLE_PREREQMARKER)
2123                 continue;
2124               r = providedbyinstalled(pool, map, req, 0, 0);
2125               if (!r)
2126                 {
2127                   /* decided and miss */
2128                   map[p] = 2;
2129                   did = 0;
2130                   break;
2131                 }
2132               if (r == 16)
2133                 break;  /* undecided */
2134               m |= r;   /* 1 | 9 | 17 */
2135             }
2136           if (req)
2137             continue;
2138           if ((m & 9) == 9)
2139             m = 9;
2140         }
2141       if (s->conflicts)
2142         {
2143           int ispatch = 0;      /* see solver.c patch handling */
2144
2145           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
2146             ispatch = 1;
2147           conp = s->repo->idarraydata + s->conflicts;
2148           while ((con = *conp++) != 0)
2149             {
2150               if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2151                 {
2152                   map[p] = 2;
2153                   did = 0;
2154                   break;
2155                 }
2156               if ((m == 1 || m == 17) && ISRELDEP(con))
2157                 {
2158                   con = dep2name(pool, con);
2159                   if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2160                     m = 9;
2161                 }
2162             }
2163           if (con)
2164             continue;   /* found a conflict */
2165         }
2166 #if 0
2167       if (s->repo && s->repo != oldinstalled)
2168         {
2169           Id p2, obs, *obsp, *pp;
2170           Solvable *s2;
2171           if (s->obsoletes)
2172             {
2173               obsp = s->repo->idarraydata + s->obsoletes;
2174               while ((obs = *obsp++) != 0)
2175                 {
2176                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
2177                     {
2178                       map[p] = 2;
2179                       break;
2180                     }
2181                 }
2182               if (obs)
2183                 continue;
2184             }
2185           FOR_PROVIDES(p2, pp, s->name)
2186             {
2187               s2 = pool->solvables + p2;
2188               if (s2->name == s->name && (map[p2] & 1) != 0)
2189                 {
2190                   map[p] = 2;
2191                   break;
2192                 }
2193             }
2194           if (p2)
2195             continue;
2196         }
2197 #endif
2198       if (m != map[p])
2199         {
2200           map[p] = m;
2201           did = 0;
2202         }
2203     }
2204   queue_free(res);
2205   queue_init_clone(res, pkgs);
2206   for (i = 0; i < pkgs->count; i++)
2207     {
2208       m = map[pkgs->elements[i]];
2209       if ((m & 9) == 9)
2210         r = 1;
2211       else if (m & 1)
2212         r = -1;
2213       else
2214         r = 0;
2215       res->elements[i] = r;
2216     }
2217   free(map);
2218 }
2219
2220 void
2221 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
2222 {
2223   pool_trivial_installable_multiversionmap(pool, installedmap, pkgs, res, 0);
2224 }
2225
2226 const char *
2227 pool_lookup_str(Pool *pool, Id entry, Id keyname)
2228 {
2229   if (entry == SOLVID_POS && pool->pos.repo)
2230     return repo_lookup_str(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2231   if (entry <= 0)
2232     return 0;
2233   return solvable_lookup_str(pool->solvables + entry, keyname);
2234 }
2235
2236 Id
2237 pool_lookup_id(Pool *pool, Id entry, Id keyname)
2238 {
2239   if (entry == SOLVID_POS && pool->pos.repo)
2240     return repo_lookup_id(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2241   if (entry <= 0)
2242     return 0;
2243   return solvable_lookup_id(pool->solvables + entry, keyname);
2244 }
2245
2246 unsigned long long
2247 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned long long notfound)
2248 {
2249   if (entry == SOLVID_POS && pool->pos.repo)
2250     return repo_lookup_num(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, notfound);
2251   if (entry <= 0)
2252     return notfound;
2253   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
2254 }
2255
2256 int
2257 pool_lookup_void(Pool *pool, Id entry, Id keyname)
2258 {
2259   if (entry == SOLVID_POS && pool->pos.repo)
2260     return repo_lookup_void(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2261   if (entry <= 0)
2262     return 0;
2263   return solvable_lookup_void(pool->solvables + entry, keyname);
2264 }
2265
2266 const unsigned char *
2267 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2268 {
2269   if (entry == SOLVID_POS && pool->pos.repo)
2270     return repo_lookup_bin_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2271   if (entry <= 0)
2272     return 0;
2273   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
2274 }
2275
2276 const char *
2277 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2278 {
2279   if (entry == SOLVID_POS && pool->pos.repo)
2280     return repo_lookup_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2281   if (entry <= 0)
2282     return 0;
2283   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2284 }
2285
2286 int
2287 pool_lookup_idarray(Pool *pool, Id entry, Id keyname, Queue *q)
2288 {
2289   if (entry == SOLVID_POS && pool->pos.repo)
2290     return repo_lookup_idarray(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, q);
2291   if (entry <= 0)
2292     return 0;
2293   return solvable_lookup_idarray(pool->solvables + entry, keyname, q);
2294 }
2295
2296 const char *
2297 pool_lookup_deltalocation(Pool *pool, Id entry, unsigned int *medianrp)
2298 {
2299   const char *loc;
2300   if (medianrp)
2301     *medianrp = 0;
2302   if (entry != SOLVID_POS)
2303     return 0;
2304   loc = pool_lookup_str(pool, entry, DELTA_LOCATION_DIR);
2305   loc = pool_tmpjoin(pool, loc, loc ? "/" : 0, pool_lookup_str(pool, entry, DELTA_LOCATION_NAME));
2306   loc = pool_tmpappend(pool, loc, "-", pool_lookup_str(pool, entry, DELTA_LOCATION_EVR));
2307   loc = pool_tmpappend(pool, loc, ".", pool_lookup_str(pool, entry, DELTA_LOCATION_SUFFIX));
2308   return loc;
2309 }
2310
2311 static void
2312 add_new_provider(Pool *pool, Id id, Id p)
2313 {
2314   Queue q;
2315   Id *pp;
2316
2317   while (ISRELDEP(id))
2318     {
2319       Reldep *rd = GETRELDEP(pool, id);
2320       id = rd->name;
2321     }
2322
2323   queue_init(&q);
2324   for (pp = pool->whatprovidesdata + pool->whatprovides[id]; *pp; pp++)
2325     {
2326       if (*pp == p)
2327         {
2328           queue_free(&q);
2329           return;
2330         }
2331       if (*pp > p)
2332         {
2333           queue_push(&q, p);
2334           p = 0;
2335         }
2336       queue_push(&q, *pp);
2337     }
2338   if (p)
2339     queue_push(&q, p);
2340   pool->whatprovides[id] = pool_queuetowhatprovides(pool, &q);
2341   queue_free(&q);
2342 }
2343
2344 void
2345 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2346 {
2347   int hadhashes = pool->relhashtbl ? 1 : 0;
2348   Solvable *s;
2349   Id fn, p, q, md5;
2350   Id id;
2351   int i;
2352
2353   if (!conflicts->count)
2354     return;
2355   for (i = 0; i < conflicts->count; i += 6)
2356     {
2357       fn = conflicts->elements[i];
2358       p = conflicts->elements[i + 1];
2359       md5 = conflicts->elements[i + 2];
2360       q = conflicts->elements[i + 4];
2361       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2362       s = pool->solvables + p;
2363       if (!s->repo)
2364         continue;
2365       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2366       if (pool->whatprovides)
2367         add_new_provider(pool, fn, p);
2368       if (pool->whatprovides_rel)
2369         pool->whatprovides_rel[GETRELID(id)] = 0;       /* clear cache */
2370       s = pool->solvables + q;
2371       if (!s->repo)
2372         continue;
2373       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2374     }
2375   if (!hadhashes)
2376     pool_freeidhashes(pool);
2377 }
2378
2379 char *
2380 pool_prepend_rootdir(Pool *pool, const char *path)
2381 {
2382   if (!path)
2383     return 0;
2384   if (!pool->rootdir)
2385     return solv_strdup(path);
2386   return solv_dupjoin(pool->rootdir, "/", *path == '/' ? path + 1 : path);
2387 }
2388
2389 const char *
2390 pool_prepend_rootdir_tmp(Pool *pool, const char *path)
2391 {
2392   if (!path)
2393     return 0;
2394   if (!pool->rootdir)
2395     return path;
2396   return pool_tmpjoin(pool, pool->rootdir, "/", *path == '/' ? path + 1 : path);
2397 }
2398
2399 void
2400 pool_set_rootdir(Pool *pool, const char *rootdir)
2401 {
2402   solv_free(pool->rootdir);
2403   pool->rootdir = solv_strdup(rootdir);
2404 }
2405
2406 const char *
2407 pool_get_rootdir(Pool *pool)
2408 {
2409   return pool->rootdir;
2410 }
2411
2412 /* only used in libzypp */
2413 void
2414 pool_set_custom_vendorcheck(Pool *pool, int (*vendorcheck)(Pool *, Solvable *, Solvable *))
2415 {
2416   pool->custom_vendorcheck = vendorcheck;
2417 }
2418
2419 /* EOF */