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