Merge pull request #6 from akozumpl/upstream
[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     }
940   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
941     return 0;
942   for (i = 0; i < cbd->nfiles; i++)
943     {
944       if (cbd->dids[i] != value->id)
945         continue;
946       if (!strcmp(cbd->names[i], value->str))
947         break;
948     }
949   if (i == cbd->nfiles)
950     return 0;
951   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
952   return 0;
953 }
954
955 static void
956 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
957 {
958   Id p;
959   Repodata *data;
960   Repo *repo;
961   Queue fileprovidesq;
962   int i, j, repoid, repodataid;
963   int provstart, provend;
964   Map donemap;
965   int ndone, incomplete;
966
967   if (!pool->urepos)
968     return;
969
970   cbd->nfiles = sf->nfiles;
971   cbd->ids = sf->ids;
972   cbd->dirs = sf->dirs;
973   cbd->names = sf->names;
974   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
975   map_init(&cbd->providedids, pool->ss.nstrings);
976
977   repoid = 1;
978   repo = repoonly ? repoonly : pool->repos[repoid];
979   map_init(&donemap, pool->nsolvables);
980   queue_init(&fileprovidesq);
981   provstart = provend = 0;
982   for (;;)
983     {
984       if (!repo || repo->disabled)
985         {
986           if (repoonly || ++repoid == pool->nrepos)
987             break;
988           repo = pool->repos[repoid];
989           continue;
990         }
991       ndone = 0;
992       FOR_REPODATAS(repo, repodataid, data)
993         {
994           if (ndone >= repo->nsolvables)
995             break;
996
997           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
998             {
999               map_empty(&cbd->providedids);
1000               for (i = 0; i < fileprovidesq.count; i++)
1001                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1002               provstart = data->start;
1003               provend = data->end;
1004               for (i = 0; i < cbd->nfiles; i++)
1005                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1006                   break;
1007               if (i == cbd->nfiles)
1008                 {
1009                   /* great! no need to search files */
1010                   for (p = data->start; p < data->end; p++)
1011                     if (pool->solvables[p].repo == repo)
1012                       {
1013                         if (MAPTST(&donemap, p))
1014                           continue;
1015                         MAPSET(&donemap, p);
1016                         ndone++;
1017                       }
1018                   continue;
1019                 }
1020             }
1021
1022           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1023             continue;
1024
1025           if (data->start < provstart || data->end > provend)
1026             {
1027               map_empty(&cbd->providedids);
1028               provstart = provend = 0;
1029             }
1030
1031           /* check if the data is incomplete */
1032           incomplete = 0;
1033           if (data->state == REPODATA_AVAILABLE)
1034             {
1035               for (j = 1; j < data->nkeys; j++)
1036                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1037                   break;
1038               if (j < data->nkeys)
1039                 {
1040 #if 0
1041                   for (i = 0; i < cbd->nfiles; i++)
1042                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1043                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1044 #endif
1045                   for (i = 0; i < cbd->nfiles; i++)
1046                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1047                       break;
1048                   if (i < cbd->nfiles)
1049                     incomplete = 1;
1050                 }
1051             }
1052
1053           /* do the search */
1054           map_init(&cbd->useddirs, 0);
1055           for (p = data->start; p < data->end; p++)
1056             if (pool->solvables[p].repo == repo)
1057               {
1058                 if (MAPTST(&donemap, p))
1059                   continue;
1060                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1061                 if (!incomplete)
1062                   {
1063                     MAPSET(&donemap, p);
1064                     ndone++;
1065                   }
1066               }
1067           map_free(&cbd->useddirs);
1068         }
1069
1070       if (repoonly || ++repoid == pool->nrepos)
1071         break;
1072       repo = pool->repos[repoid];
1073     }
1074   map_free(&donemap);
1075   queue_free(&fileprovidesq);
1076   map_free(&cbd->providedids);
1077 }
1078
1079 void
1080 pool_addfileprovides_queue(Pool *pool, Queue *idq)
1081 {
1082   Solvable *s;
1083   Repo *installed, *repo;
1084   struct searchfiles sf, isf, *isfp;
1085   struct addfileprovides_cbdata cbd;
1086   int i;
1087   unsigned int now;
1088
1089   installed = pool->installed;
1090   now = solv_timems(0);
1091   memset(&sf, 0, sizeof(sf));
1092   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1093   memset(&isf, 0, sizeof(isf));
1094   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1095
1096   if (idq)
1097     queue_empty(idq);
1098   isfp = installed ? &isf : 0;
1099   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1100     {
1101       repo = s->repo;
1102       if (!repo)
1103         continue;
1104       if (s->obsoletes)
1105         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1106       if (s->conflicts)
1107         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1108       if (s->requires)
1109         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1110       if (s->recommends)
1111         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1112       if (s->suggests)
1113         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1114       if (s->supplements)
1115         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1116       if (s->enhances)
1117         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1118     }
1119   map_free(&sf.seen);
1120   map_free(&isf.seen);
1121   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1122   cbd.dids = 0;
1123   if (sf.nfiles)
1124     {
1125 #if 0
1126       for (i = 0; i < sf.nfiles; i++)
1127         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1128 #endif
1129       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1130       if (idq)
1131         for (i = 0; i < sf.nfiles; i++)
1132           queue_push(idq, sf.ids[i]);
1133       solv_free(sf.ids);
1134       for (i = 0; i < sf.nfiles; i++)
1135         {
1136           solv_free(sf.dirs[i]);
1137           solv_free(sf.names[i]);
1138         }
1139       solv_free(sf.dirs);
1140       solv_free(sf.names);
1141     }
1142   if (isf.nfiles)
1143     {
1144 #if 0
1145       for (i = 0; i < isf.nfiles; i++)
1146         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1147 #endif
1148       if (installed)
1149         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1150       solv_free(isf.ids);
1151       for (i = 0; i < isf.nfiles; i++)
1152         {
1153           solv_free(isf.dirs[i]);
1154           solv_free(isf.names[i]);
1155         }
1156       solv_free(isf.dirs);
1157       solv_free(isf.names);
1158     }
1159   solv_free(cbd.dids);
1160   pool_freewhatprovides(pool);  /* as we have added provides */
1161   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1162 }
1163
1164 void
1165 pool_addfileprovides(Pool *pool)
1166 {
1167   pool_addfileprovides_queue(pool, 0);
1168 }
1169
1170 void
1171 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)
1172 {
1173   if (p)
1174     {
1175       if (pool->solvables[p].repo)
1176         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1177       return;
1178     }
1179   /* FIXME: obey callback return value! */
1180   for (p = 1; p < pool->nsolvables; p++)
1181     if (pool->solvables[p].repo)
1182       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1183 }
1184
1185 void
1186 pool_clear_pos(Pool *pool)
1187 {
1188   memset(&pool->pos, 0, sizeof(pool->pos));
1189 }
1190
1191
1192 void
1193 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1194 {
1195   int i;
1196
1197   pool->languagecache = solv_free(pool->languagecache);
1198   pool->languagecacheother = 0;
1199   if (pool->nlanguages)
1200     {
1201       for (i = 0; i < pool->nlanguages; i++)
1202         free((char *)pool->languages[i]);
1203       free(pool->languages);
1204     }
1205   pool->nlanguages = nlanguages;
1206   if (!nlanguages)
1207     return;
1208   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1209   for (i = 0; i < pool->nlanguages; i++)
1210     pool->languages[i] = solv_strdup(languages[i]);
1211 }
1212
1213 Id
1214 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1215 {
1216   const char *n;
1217   char buf[256], *p;
1218   int l;
1219
1220   if (!lang || !*lang)
1221     return id;
1222   n = pool_id2str(pool, id);
1223   l = strlen(n) + strlen(lang) + 2;
1224   if (l > sizeof(buf))
1225     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1226   else
1227     p = buf;
1228   sprintf(p, "%s:%s", n, lang);
1229   id = pool_str2id(pool, p, create);
1230   if (p != buf)
1231     free(p);
1232   return id;
1233 }
1234
1235 char *
1236 pool_alloctmpspace(Pool *pool, int len)
1237 {
1238   int n = pool->tmpspace.n;
1239   if (!len)
1240     return 0;
1241   if (len > pool->tmpspace.len[n])
1242     {
1243       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1244       pool->tmpspace.len[n] = len + 32;
1245     }
1246   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1247   return pool->tmpspace.buf[n];
1248 }
1249
1250 static char *
1251 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1252 {
1253   if (space)
1254     {
1255       int n, oldn;
1256       n = oldn = pool->tmpspace.n;
1257       for (;;)
1258         {
1259           if (!n--)
1260             n = POOL_TMPSPACEBUF - 1;
1261           if (n == oldn)
1262             break;
1263           if (pool->tmpspace.buf[n] != space)
1264             continue;
1265           if (len > pool->tmpspace.len[n])
1266             {
1267               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1268               pool->tmpspace.len[n] = len + 32;
1269             }
1270           return pool->tmpspace.buf[n];
1271         }
1272     }
1273   return 0;
1274 }
1275
1276 void
1277 pool_freetmpspace(Pool *pool, const char *space)
1278 {
1279   int n = pool->tmpspace.n;
1280   if (!space)
1281     return;
1282   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1283   if (pool->tmpspace.buf[n] == space)
1284     pool->tmpspace.n = n;
1285 }
1286
1287 char *
1288 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1289 {
1290   int l1, l2, l3;
1291   char *s, *str;
1292   l1 = str1 ? strlen(str1) : 0;
1293   l2 = str2 ? strlen(str2) : 0;
1294   l3 = str3 ? strlen(str3) : 0;
1295   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1296   if (l1)
1297     {
1298       strcpy(s, str1);
1299       s += l1;
1300     }
1301   if (l2)
1302     {
1303       strcpy(s, str2);
1304       s += l2;
1305     }
1306   if (l3)
1307     {
1308       strcpy(s, str3);
1309       s += l3;
1310     }
1311   *s = 0;
1312   return str;
1313 }
1314
1315 char *
1316 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1317 {
1318   int l1, l2, l3;
1319   char *s, *str;
1320
1321   l1 = str1 ? strlen(str1) : 0;
1322   l2 = str2 ? strlen(str2) : 0;
1323   l3 = str3 ? strlen(str3) : 0;
1324   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1325   if (str)
1326     str1 = str;
1327   else
1328     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1329   s = str;
1330   if (l1)
1331     {
1332       if (s != str1)
1333         strcpy(s, str1);
1334       s += l1;
1335     }
1336   if (l2)
1337     {
1338       strcpy(s, str2);
1339       s += l2;
1340     }
1341   if (l3)
1342     {
1343       strcpy(s, str3);
1344       s += l3;
1345     }
1346   *s = 0;
1347   return str;
1348 }
1349
1350 const char *
1351 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1352 {
1353   char *s;
1354   if (!len)
1355     return "";
1356   s = pool_alloctmpspace(pool, 2 * len + 1);
1357   solv_bin2hex(buf, len, s);
1358   return s;
1359 }
1360
1361 /*******************************************************************/
1362
1363 struct mptree {
1364   Id sibling;
1365   Id child;
1366   const char *comp;
1367   int compl;
1368   Id mountpoint;
1369 };
1370
1371 struct ducbdata {
1372   DUChanges *mps;
1373   struct mptree *mptree;
1374   int addsub;
1375   int hasdu;
1376
1377   Id *dirmap;
1378   int nmap;
1379   Repodata *olddata;
1380 };
1381
1382
1383 static int
1384 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1385 {
1386   struct ducbdata *cbd = cbdata;
1387   Id mp;
1388
1389   if (data != cbd->olddata)
1390     {
1391       Id dn, mp, comp, *dirmap, *dirs;
1392       int i, compl;
1393       const char *compstr;
1394       struct mptree *mptree;
1395
1396       /* create map from dir to mptree */
1397       cbd->dirmap = solv_free(cbd->dirmap);
1398       cbd->nmap = 0;
1399       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1400       mptree = cbd->mptree;
1401       mp = 0;
1402       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1403         {
1404           comp = *dirs++;
1405           if (comp <= 0)
1406             {
1407               mp = dirmap[-comp];
1408               continue;
1409             }
1410           if (mp < 0)
1411             {
1412               /* unconnected */
1413               dirmap[dn] = mp;
1414               continue;
1415             }
1416           if (!mptree[mp].child)
1417             {
1418               dirmap[dn] = -mp;
1419               continue;
1420             }
1421           if (data->localpool)
1422             compstr = stringpool_id2str(&data->spool, comp);
1423           else
1424             compstr = pool_id2str(data->repo->pool, comp);
1425           compl = strlen(compstr);
1426           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1427             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1428               break;
1429           dirmap[dn] = i ? i : -mp;
1430         }
1431       /* change dirmap to point to mountpoint instead of mptree */
1432       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1433         {
1434           mp = dirmap[dn];
1435           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1436         }
1437       cbd->dirmap = dirmap;
1438       cbd->nmap = data->dirpool.ndirs;
1439       cbd->olddata = data;
1440     }
1441   cbd->hasdu = 1;
1442   if (value->id < 0 || value->id >= cbd->nmap)
1443     return 0;
1444   mp = cbd->dirmap[value->id];
1445   if (mp < 0)
1446     return 0;
1447   if (cbd->addsub > 0)
1448     {
1449       cbd->mps[mp].kbytes += value->num;
1450       cbd->mps[mp].files += value->num2;
1451     }
1452   else
1453     {
1454       cbd->mps[mp].kbytes -= value->num;
1455       cbd->mps[mp].files -= value->num2;
1456     }
1457   return 0;
1458 }
1459
1460 static void
1461 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1462 {
1463   int i;
1464   if (mptree[pos].mountpoint == -1)
1465     mptree[pos].mountpoint = mountpoint;
1466   else
1467     mountpoint = mptree[pos].mountpoint;
1468   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1469     propagate_mountpoints(mptree, i, mountpoint);
1470 }
1471
1472 #define MPTREE_BLOCK 15
1473
1474 void
1475 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1476 {
1477   char *p;
1478   const char *path, *compstr;
1479   struct mptree *mptree;
1480   int i, nmptree;
1481   int pos, compl;
1482   int mp;
1483   struct ducbdata cbd;
1484   Solvable *s;
1485   Id sp;
1486   Map ignoredu;
1487   Repo *oldinstalled = pool->installed;
1488
1489   memset(&ignoredu, 0, sizeof(ignoredu));
1490   cbd.mps = mps;
1491   cbd.addsub = 0;
1492   cbd.dirmap = 0;
1493   cbd.nmap = 0;
1494   cbd.olddata = 0;
1495
1496   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1497
1498   /* our root node */
1499   mptree[0].sibling = 0;
1500   mptree[0].child = 0;
1501   mptree[0].comp = 0;
1502   mptree[0].compl = 0;
1503   mptree[0].mountpoint = -1;
1504   nmptree = 1;
1505   
1506   /* create component tree */
1507   for (mp = 0; mp < nmps; mp++)
1508     {
1509       mps[mp].kbytes = 0;
1510       mps[mp].files = 0;
1511       pos = 0;
1512       path = mps[mp].path;
1513       while(*path == '/')
1514         path++;
1515       while (*path)
1516         {
1517           if ((p = strchr(path, '/')) == 0)
1518             {
1519               compstr = path;
1520               compl = strlen(compstr);
1521               path += compl;
1522             }
1523           else
1524             {
1525               compstr = path;
1526               compl = p - path;
1527               path = p + 1;
1528               while(*path == '/')
1529                 path++;
1530             }
1531           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1532             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1533               break;
1534           if (!i)
1535             {
1536               /* create new node */
1537               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1538               i = nmptree++;
1539               mptree[i].sibling = mptree[pos].child;
1540               mptree[i].child = 0;
1541               mptree[i].comp = compstr;
1542               mptree[i].compl = compl;
1543               mptree[i].mountpoint = -1;
1544               mptree[pos].child = i;
1545             }
1546           pos = i;
1547         }
1548       mptree[pos].mountpoint = mp;
1549     }
1550
1551   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1552
1553 #if 0
1554   for (i = 0; i < nmptree; i++)
1555     {
1556       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1557       printf("#%d child: %d\n", i, mptree[i].child);
1558       printf("#%d comp: %s\n", i, mptree[i].comp);
1559       printf("#%d compl: %d\n", i, mptree[i].compl);
1560       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1561     }
1562 #endif
1563
1564   cbd.mptree = mptree;
1565   cbd.addsub = 1;
1566   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1567     {
1568       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1569         continue;
1570       if (!MAPTST(installedmap, sp))
1571         continue;
1572       cbd.hasdu = 0;
1573       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1574       if (!cbd.hasdu && oldinstalled)
1575         {
1576           Id op, opp;
1577           /* no du data available, ignore data of all installed solvables we obsolete */
1578           if (!ignoredu.map)
1579             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1580           if (s->obsoletes)
1581             {
1582               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1583               while ((obs = *obsp++) != 0)
1584                 FOR_PROVIDES(op, opp, obs)
1585                   if (op >= oldinstalled->start && op < oldinstalled->end)
1586                     MAPSET(&ignoredu, op - oldinstalled->start);
1587             }
1588           FOR_PROVIDES(op, opp, s->name)
1589             if (pool->solvables[op].name == s->name)
1590               if (op >= oldinstalled->start && op < oldinstalled->end)
1591                 MAPSET(&ignoredu, op - oldinstalled->start);
1592         }
1593     }
1594   cbd.addsub = -1;
1595   if (oldinstalled)
1596     {
1597       /* assumes we allways have du data for installed solvables */
1598       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1599         {
1600           if (MAPTST(installedmap, sp))
1601             continue;
1602           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1603             continue;
1604           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1605         }
1606     }
1607   if (ignoredu.map)
1608     map_free(&ignoredu);
1609   solv_free(cbd.dirmap);
1610   solv_free(mptree);
1611 }
1612
1613 int
1614 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1615 {
1616   Id sp;
1617   Solvable *s;
1618   int change = 0;
1619   Repo *oldinstalled = pool->installed;
1620
1621   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1622     {
1623       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1624         continue;
1625       if (!MAPTST(installedmap, sp))
1626         continue;
1627       change += solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1628     }
1629   if (oldinstalled)
1630     {
1631       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1632         {
1633           if (MAPTST(installedmap, sp))
1634             continue;
1635           change -= solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1636         }
1637     }
1638   return change;
1639 }
1640
1641 /* map:
1642  *  1: installed
1643  *  2: conflicts with installed
1644  *  8: interesting (only true if installed)
1645  * 16: undecided
1646  */
1647  
1648 static inline Id dep2name(Pool *pool, Id dep)
1649 {
1650   while (ISRELDEP(dep))
1651     {
1652       Reldep *rd = rd = GETRELDEP(pool, dep);
1653       dep = rd->name;
1654     }
1655   return dep;
1656 }
1657
1658 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
1659 {
1660   Id p, pp;
1661   Solvable *sn = pool->solvables + n; 
1662
1663   FOR_PROVIDES(p, pp, sn->name)
1664     {    
1665       Solvable *s = pool->solvables + p; 
1666       if (s->name != sn->name || s->arch != sn->arch)
1667         continue;
1668       if ((map[p] & 9) != 9)
1669         continue;
1670       if (pool_match_nevr(pool, pool->solvables + p, con))
1671         continue;
1672       return 1;         /* found installed package that doesn't conflict */
1673     }
1674   return 0;
1675 }
1676
1677 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *noobsoletesmap)
1678 {
1679   Id p, pp;
1680   int r = 0;
1681   FOR_PROVIDES(p, pp, dep)
1682     {
1683       if (p == SYSTEMSOLVABLE)
1684         return 1;       /* always boring, as never constraining */
1685       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
1686         continue;
1687       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
1688         if (providedbyinstalled_multiversion(pool, map, p, dep))
1689           continue;
1690       if ((map[p] & 9) == 9)
1691         return 9;
1692       r |= map[p] & 17;
1693     }
1694   return r;
1695 }
1696
1697 /*
1698  * pool_trivial_installable - calculate if a set of solvables is
1699  * trivial installable without any other installs/deinstalls of
1700  * packages not belonging to the set.
1701  *
1702  * the state is returned in the result queue:
1703  * 1:  solvable is installable without any other package changes
1704  * 0:  solvable is not installable
1705  * -1: solvable is installable, but doesn't constrain any installed packages
1706  */
1707
1708 void
1709 pool_trivial_installable_noobsoletesmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *noobsoletesmap)
1710 {
1711   int i, r, m, did;
1712   Id p, *dp, con, *conp, req, *reqp;
1713   unsigned char *map;
1714   Solvable *s;
1715
1716   map = solv_calloc(pool->nsolvables, 1);
1717   for (p = 1; p < pool->nsolvables; p++)
1718     {
1719       if (!MAPTST(installedmap, p))
1720         continue;
1721       map[p] |= 9;
1722       s = pool->solvables + p;
1723       if (!s->conflicts)
1724         continue;
1725       conp = s->repo->idarraydata + s->conflicts;
1726       while ((con = *conp++) != 0)
1727         {
1728           dp = pool_whatprovides_ptr(pool, con);
1729           for (; *dp; dp++)
1730             map[p] |= 2;        /* XXX: self conflict ? */
1731         }
1732     }
1733   for (i = 0; i < pkgs->count; i++)
1734     map[pkgs->elements[i]] = 16;
1735
1736   for (i = 0, did = 0; did < pkgs->count; i++, did++)
1737     {
1738       if (i == pkgs->count)
1739         i = 0;
1740       p = pkgs->elements[i];
1741       if ((map[p] & 16) == 0)
1742         continue;
1743       if ((map[p] & 2) != 0)
1744         {
1745           map[p] = 2;
1746           continue;
1747         }
1748       s = pool->solvables + p;
1749       m = 1;
1750       if (s->requires)
1751         {
1752           reqp = s->repo->idarraydata + s->requires;
1753           while ((req = *reqp++) != 0)
1754             {
1755               if (req == SOLVABLE_PREREQMARKER)
1756                 continue;
1757               r = providedbyinstalled(pool, map, req, 0, 0);
1758               if (!r)
1759                 {
1760                   /* decided and miss */
1761                   map[p] = 2;
1762                   break;
1763                 }
1764               m |= r;   /* 1 | 9 | 16 | 17 */
1765             }
1766           if (req)
1767             continue;
1768           if ((m & 9) == 9)
1769             m = 9;
1770         }
1771       if (s->conflicts)
1772         {
1773           int ispatch = 0;      /* see solver.c patch handling */
1774
1775           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
1776             ispatch = 1;
1777           conp = s->repo->idarraydata + s->conflicts;
1778           while ((con = *conp++) != 0)
1779             {
1780               if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1781                 {
1782                   map[p] = 2;
1783                   break;
1784                 }
1785               if ((m == 1 || m == 17) && ISRELDEP(con))
1786                 {
1787                   con = dep2name(pool, con);
1788                   if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1789                     m = 9;
1790                 }
1791             }
1792           if (con)
1793             continue;   /* found a conflict */
1794         }
1795 #if 0
1796       if (s->repo && s->repo != oldinstalled)
1797         {
1798           Id p2, obs, *obsp, *pp;
1799           Solvable *s2;
1800           if (s->obsoletes)
1801             {
1802               obsp = s->repo->idarraydata + s->obsoletes;
1803               while ((obs = *obsp++) != 0)
1804                 {
1805                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
1806                     {
1807                       map[p] = 2;
1808                       break;
1809                     }
1810                 }
1811               if (obs)
1812                 continue;
1813             }
1814           FOR_PROVIDES(p2, pp, s->name)
1815             {
1816               s2 = pool->solvables + p2;
1817               if (s2->name == s->name && (map[p2] & 1) != 0)
1818                 {
1819                   map[p] = 2;
1820                   break;
1821                 }
1822             }
1823           if (p2)
1824             continue;
1825         }
1826 #endif
1827       if (m != map[p])
1828         {
1829           map[p] = m;
1830           did = 0;
1831         }
1832     }
1833   queue_free(res);
1834   queue_init_clone(res, pkgs);
1835   for (i = 0; i < pkgs->count; i++)
1836     {
1837       m = map[pkgs->elements[i]];
1838       if ((m & 9) == 9)
1839         r = 1;
1840       else if (m & 1)
1841         r = -1;
1842       else
1843         r = 0;
1844       res->elements[i] = r;
1845     }
1846   free(map);
1847 }
1848
1849 void
1850 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
1851 {
1852   pool_trivial_installable_noobsoletesmap(pool, installedmap, pkgs, res, 0);
1853 }
1854
1855 const char *
1856 pool_lookup_str(Pool *pool, Id entry, Id keyname)
1857 {
1858   if (entry == SOLVID_POS && pool->pos.repo)
1859     return repodata_lookup_str(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1860   if (entry <= 0)
1861     return 0;
1862   return solvable_lookup_str(pool->solvables + entry, keyname);
1863 }
1864
1865 Id
1866 pool_lookup_id(Pool *pool, Id entry, Id keyname)
1867 {
1868   if (entry == SOLVID_POS && pool->pos.repo)
1869     {
1870       Repodata *data = pool->pos.repo->repodata + pool->pos.repodataid;
1871       Id id = repodata_lookup_id(data, SOLVID_POS, keyname);
1872       return data->localpool ? repodata_globalize_id(data, id, 1) : id;
1873     }
1874   if (entry <= 0)
1875     return 0;
1876   return solvable_lookup_id(pool->solvables + entry, keyname);
1877 }
1878
1879 unsigned int
1880 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned int notfound)
1881 {
1882   if (entry == SOLVID_POS && pool->pos.repo)
1883     {
1884       unsigned int value;
1885       if (repodata_lookup_num(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, &value))
1886         return value;
1887       return notfound;
1888     }
1889   if (entry <= 0)
1890     return notfound;
1891   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
1892 }
1893
1894 int
1895 pool_lookup_void(Pool *pool, Id entry, Id keyname)
1896 {
1897   if (entry == SOLVID_POS && pool->pos.repo)
1898     return repodata_lookup_void(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1899   if (entry <= 0)
1900     return 0;
1901   return solvable_lookup_void(pool->solvables + entry, keyname);
1902 }
1903
1904 const unsigned char *
1905 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1906 {
1907   if (entry == SOLVID_POS && pool->pos.repo)
1908     return repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1909   if (entry <= 0)
1910     return 0;
1911   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
1912 }
1913
1914 const char *
1915 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1916 {
1917   if (entry == SOLVID_POS && pool->pos.repo)
1918     {
1919       const unsigned char *chk = repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1920       return chk ? repodata_chk2str(pool->pos.repo->repodata + pool->pos.repodataid, *typep, chk) : 0;
1921     }
1922   if (entry <= 0)
1923     return 0;
1924   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
1925 }
1926
1927 void
1928 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
1929 {
1930   int hadhashes = pool->relhashtbl ? 1 : 0;
1931   Solvable *s;
1932   Id fn, p, q, md5;
1933   Id id;
1934   int i;
1935
1936   if (!conflicts->count)
1937     return;
1938   pool_freewhatprovides(pool);
1939   for (i = 0; i < conflicts->count; i += 5)
1940     {
1941       fn = conflicts->elements[i];
1942       p = conflicts->elements[i + 1];
1943       md5 = conflicts->elements[i + 2];
1944       q = conflicts->elements[i + 3];
1945       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
1946       s = pool->solvables + p;
1947       if (!s->repo)
1948         continue;
1949       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
1950       s = pool->solvables + q;
1951       if (!s->repo)
1952         continue;
1953       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
1954     }
1955   if (!hadhashes)
1956     pool_freeidhashes(pool);
1957 }
1958
1959 /* EOF */