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