- INCOMPATIBLE CHANGE: index with repoid (thus pool->repos[repoid] == repo->repoid)
[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 # ifdef MULTI_SEMANTICS
72   pool->disttype = DISTTYPE_DEB;
73 # endif
74 #endif
75 #ifdef RPM5
76   pool->obsoleteusesprovides = 1;
77   pool->implicitobsoleteusesprovides = 1;
78 #endif
79   return pool;
80 }
81
82
83 /* free all the resources of our pool */
84 void
85 pool_free(Pool *pool)
86 {
87   int i;
88
89   pool_freewhatprovides(pool);
90   pool_freeidhashes(pool);
91   pool_freeallrepos(pool, 1);
92   solv_free(pool->id2arch);
93   solv_free(pool->solvables);
94   stringpool_free(&pool->ss);
95   solv_free(pool->rels);
96   pool_setvendorclasses(pool, 0);
97   queue_free(&pool->vendormap);
98   for (i = 0; i < POOL_TMPSPACEBUF; i++)
99     solv_free(pool->tmpspace.buf[i]);
100   for (i = 0; i < pool->nlanguages; i++)
101     free((char *)pool->languages[i]);
102   solv_free(pool->languages);
103   solv_free(pool->languagecache);
104   solv_free(pool);
105 }
106
107 void
108 pool_freeallrepos(Pool *pool, int reuseids)
109 {
110   int i;
111
112   pool_freewhatprovides(pool);
113   for (i = 1; i < pool->nrepos; 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 defined(MULTI_SEMANTICS)
730                   if (pool->disttype == DISTTYPE_DEB)
731                     continue;
732                   else
733                     break;
734 #elif defined(DEBIAN_SEMANTICS)
735                   continue;             /* unversioned provides can
736                                          * never match versioned deps */
737 #else
738                   break;                /* yes, provides all versions */
739 #endif
740                 }
741               if (!ISRELDEP(pid))
742                 continue;               /* wrong provides name */
743               prd = GETRELDEP(pool, pid);
744               if (prd->name != name)
745                 continue;               /* wrong provides name */
746               /* right package, both deps are rels. check flags/evr */
747               if (pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
748                 break;  /* matches */
749             }
750           if (!pid)
751             continue;   /* none of the providers matched */
752           queue_push(&plist, p);
753         }
754       /* make our system solvable provide all unknown rpmlib() stuff */
755       if (plist.count == 0 && !strncmp(pool_id2str(pool, name), "rpmlib(", 7))
756         queue_push(&plist, SYSTEMSOLVABLE);
757     }
758   /* add providers to whatprovides */
759 #if 0
760   POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
761 #endif
762   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
763   queue_free(&plist);
764
765   return pool->whatprovides_rel[d];
766 }
767
768 /*************************************************************************/
769
770 void
771 pool_debug(Pool *pool, int type, const char *format, ...)
772 {
773   va_list args;
774   char buf[1024];
775
776   if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0)
777     {
778       if ((pool->debugmask & type) == 0)
779         return;
780     }
781   va_start(args, format);
782   if (!pool->debugcallback)
783     {
784       if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0 && !(pool->debugmask & SOLV_DEBUG_TO_STDERR))
785         vprintf(format, args);
786       else
787         vfprintf(stderr, format, args);
788       return;
789     }
790   vsnprintf(buf, sizeof(buf), format, args);
791   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
792 }
793
794 void
795 pool_setdebuglevel(Pool *pool, int level)
796 {
797   int mask = SOLV_DEBUG_RESULT;
798   if (level > 0)
799     mask |= SOLV_DEBUG_STATS|SOLV_DEBUG_ANALYZE|SOLV_DEBUG_UNSOLVABLE|SOLV_DEBUG_SOLVER|SOLV_DEBUG_TRANSACTION;
800   if (level > 1)
801     mask |= SOLV_DEBUG_JOB|SOLV_DEBUG_SOLUTIONS|SOLV_DEBUG_POLICY;
802   if (level > 2)
803     mask |= SOLV_DEBUG_PROPAGATE;
804   if (level > 3)
805     mask |= SOLV_DEBUG_RULE_CREATION;
806   mask |= pool->debugmask & SOLV_DEBUG_TO_STDERR;       /* keep bit */
807   pool->debugmask = mask;
808 }
809
810 void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
811 {
812   pool->debugcallback = debugcallback;
813   pool->debugcallbackdata = debugcallbackdata;
814 }
815
816 void pool_setdebugmask(Pool *pool, int mask)
817 {
818   pool->debugmask = mask;
819 }
820
821 void pool_setloadcallback(Pool *pool, int (*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
822 {
823   pool->loadcallback = cb;
824   pool->loadcallbackdata = loadcbdata;
825 }
826
827 /*************************************************************************/
828
829 struct searchfiles {
830   Id *ids;
831   char **dirs;
832   char **names;
833   int nfiles;
834   Map seen;
835 };
836
837 #define SEARCHFILES_BLOCK 127
838
839 static void
840 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
841 {
842   Id dep, sid;
843   const char *s, *sr;
844   struct searchfiles *csf;
845
846   while ((dep = *ida++) != 0)
847     {
848       csf = sf;
849       while (ISRELDEP(dep))
850         {
851           Reldep *rd;
852           sid = pool->ss.nstrings + GETRELID(dep);
853           if (MAPTST(&csf->seen, sid))
854             {
855               dep = 0;
856               break;
857             }
858           MAPSET(&csf->seen, sid);
859           rd = GETRELDEP(pool, dep);
860           if (rd->flags < 8)
861             dep = rd->name;
862           else if (rd->flags == REL_NAMESPACE)
863             {
864               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
865                 {
866                   csf = isf;
867                   if (!csf || MAPTST(&csf->seen, sid))
868                     {
869                       dep = 0;
870                       break;
871                     }
872                   MAPSET(&csf->seen, sid);
873                 }
874               dep = rd->evr;
875             }
876           else if (rd->flags == REL_FILECONFLICT)
877             {
878               dep = 0;
879               break;
880             }
881           else
882             {
883               Id ids[2];
884               ids[0] = rd->name;
885               ids[1] = 0;
886               pool_addfileprovides_dep(pool, ids, csf, isf);
887               dep = rd->evr;
888             }
889         }
890       if (!dep)
891         continue;
892       if (MAPTST(&csf->seen, dep))
893         continue;
894       MAPSET(&csf->seen, dep);
895       s = pool_id2str(pool, dep);
896       if (*s != '/')
897         continue;
898       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
899       csf->dirs = solv_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
900       csf->names = solv_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
901       csf->ids[csf->nfiles] = dep;
902       sr = strrchr(s, '/');
903       csf->names[csf->nfiles] = solv_strdup(sr + 1);
904       csf->dirs[csf->nfiles] = solv_malloc(sr - s + 1);
905       if (sr != s)
906         strncpy(csf->dirs[csf->nfiles], s, sr - s);
907       csf->dirs[csf->nfiles][sr - s] = 0;
908       csf->nfiles++;
909     }
910 }
911
912 struct addfileprovides_cbdata {
913   int nfiles;
914   Id *ids;
915   char **dirs;
916   char **names;
917
918   Id *dids;
919
920   Map providedids;
921
922   Map useddirs;
923 };
924
925 static int
926 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
927 {
928   struct addfileprovides_cbdata *cbd = cbdata;
929   int i;
930
931   if (!cbd->useddirs.size)
932     {
933       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
934       for (i = 0; i < cbd->nfiles; i++)
935         {
936           Id did;
937           if (MAPTST(&cbd->providedids, cbd->ids[i]))
938             {
939               cbd->dids[i] = 0;
940               continue;
941             }
942           did = repodata_str2dir(data, cbd->dirs[i], 0);
943           cbd->dids[i] = did;
944           if (did)
945             MAPSET(&cbd->useddirs, did);
946         }
947     }
948   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
949     return 0;
950   for (i = 0; i < cbd->nfiles; i++)
951     {
952       if (cbd->dids[i] != value->id)
953         continue;
954       if (!strcmp(cbd->names[i], value->str))
955         break;
956     }
957   if (i == cbd->nfiles)
958     return 0;
959   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
960   return 0;
961 }
962
963 static void
964 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
965 {
966   Id p;
967   Repodata *data;
968   Repo *repo;
969   Queue fileprovidesq;
970   int i, j, repoid, repodataid;
971   int provstart, provend;
972   Map donemap;
973   int ndone, incomplete;
974
975   if (!pool->urepos)
976     return;
977
978   cbd->nfiles = sf->nfiles;
979   cbd->ids = sf->ids;
980   cbd->dirs = sf->dirs;
981   cbd->names = sf->names;
982   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
983   map_init(&cbd->providedids, pool->ss.nstrings);
984
985   repoid = 1;
986   repo = repoonly ? repoonly : pool->repos[repoid];
987   map_init(&donemap, pool->nsolvables);
988   queue_init(&fileprovidesq);
989   provstart = provend = 0;
990   for (;;)
991     {
992       if (!repo || repo->disabled)
993         {
994           if (repoonly || ++repoid == pool->nrepos)
995             break;
996           repo = pool->repos[repoid];
997           continue;
998         }
999       ndone = 0;
1000       FOR_REPODATAS(repo, repodataid, data)
1001         {
1002           if (ndone >= repo->nsolvables)
1003             break;
1004
1005           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1006             {
1007               map_empty(&cbd->providedids);
1008               for (i = 0; i < fileprovidesq.count; i++)
1009                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1010               provstart = data->start;
1011               provend = data->end;
1012               for (i = 0; i < cbd->nfiles; i++)
1013                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1014                   break;
1015               if (i == cbd->nfiles)
1016                 {
1017                   /* great! no need to search files */
1018                   for (p = data->start; p < data->end; p++)
1019                     if (pool->solvables[p].repo == repo)
1020                       {
1021                         if (MAPTST(&donemap, p))
1022                           continue;
1023                         MAPSET(&donemap, p);
1024                         ndone++;
1025                       }
1026                   continue;
1027                 }
1028             }
1029
1030           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1031             continue;
1032
1033           if (data->start < provstart || data->end > provend)
1034             {
1035               map_empty(&cbd->providedids);
1036               provstart = provend = 0;
1037             }
1038
1039           /* check if the data is incomplete */
1040           incomplete = 0;
1041           if (data->state == REPODATA_AVAILABLE)
1042             {
1043               for (j = 1; j < data->nkeys; j++)
1044                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1045                   break;
1046               if (j < data->nkeys)
1047                 {
1048 #if 0
1049                   for (i = 0; i < cbd->nfiles; i++)
1050                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1051                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1052 #endif
1053                   for (i = 0; i < cbd->nfiles; i++)
1054                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1055                       break;
1056                   if (i < cbd->nfiles)
1057                     incomplete = 1;
1058                 }
1059             }
1060
1061           /* do the search */
1062           map_init(&cbd->useddirs, 0);
1063           for (p = data->start; p < data->end; p++)
1064             if (pool->solvables[p].repo == repo)
1065               {
1066                 if (MAPTST(&donemap, p))
1067                   continue;
1068                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1069                 if (!incomplete)
1070                   {
1071                     MAPSET(&donemap, p);
1072                     ndone++;
1073                   }
1074               }
1075           map_free(&cbd->useddirs);
1076         }
1077
1078       if (repoonly || ++repoid == pool->nrepos)
1079         break;
1080       repo = pool->repos[repoid];
1081     }
1082   map_free(&donemap);
1083   queue_free(&fileprovidesq);
1084   map_free(&cbd->providedids);
1085 }
1086
1087 void
1088 pool_addfileprovides_ids(Pool *pool, Repo *installed, Id **idp)
1089 {
1090   Solvable *s;
1091   Repo *repo;
1092   struct searchfiles sf, isf, *isfp;
1093   struct addfileprovides_cbdata cbd;
1094   int i;
1095   unsigned int now;
1096
1097   now = solv_timems(0);
1098   memset(&sf, 0, sizeof(sf));
1099   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1100   memset(&isf, 0, sizeof(isf));
1101   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1102
1103   isfp = installed ? &isf : 0;
1104   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1105     {
1106       repo = s->repo;
1107       if (!repo)
1108         continue;
1109       if (s->obsoletes)
1110         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1111       if (s->conflicts)
1112         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1113       if (s->requires)
1114         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1115       if (s->recommends)
1116         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1117       if (s->suggests)
1118         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1119       if (s->supplements)
1120         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1121       if (s->enhances)
1122         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1123     }
1124   map_free(&sf.seen);
1125   map_free(&isf.seen);
1126   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1127   cbd.dids = 0;
1128   if (idp)
1129     *idp = 0;
1130   if (sf.nfiles)
1131     {
1132 #if 0
1133       for (i = 0; i < sf.nfiles; i++)
1134         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1135 #endif
1136       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1137       if (idp)
1138         {
1139           sf.ids = solv_extend(sf.ids, sf.nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1140           sf.ids[sf.nfiles] = 0;
1141           *idp = sf.ids;
1142           sf.ids = 0;
1143         }
1144       solv_free(sf.ids);
1145       for (i = 0; i < sf.nfiles; i++)
1146         {
1147           solv_free(sf.dirs[i]);
1148           solv_free(sf.names[i]);
1149         }
1150       solv_free(sf.dirs);
1151       solv_free(sf.names);
1152     }
1153   if (isf.nfiles)
1154     {
1155 #if 0
1156       for (i = 0; i < isf.nfiles; i++)
1157         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1158 #endif
1159       if (installed)
1160         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1161       solv_free(isf.ids);
1162       for (i = 0; i < isf.nfiles; i++)
1163         {
1164           solv_free(isf.dirs[i]);
1165           solv_free(isf.names[i]);
1166         }
1167       solv_free(isf.dirs);
1168       solv_free(isf.names);
1169     }
1170   solv_free(cbd.dids);
1171   pool_freewhatprovides(pool);  /* as we have added provides */
1172   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1173 }
1174
1175 void
1176 pool_addfileprovides(Pool *pool)
1177 {
1178   pool_addfileprovides_ids(pool, pool->installed, 0);
1179 }
1180
1181 void
1182 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)
1183 {
1184   if (p)
1185     {
1186       if (pool->solvables[p].repo)
1187         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1188       return;
1189     }
1190   /* FIXME: obey callback return value! */
1191   for (p = 1; p < pool->nsolvables; p++)
1192     if (pool->solvables[p].repo)
1193       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1194 }
1195
1196 void
1197 pool_clear_pos(Pool *pool)
1198 {
1199   memset(&pool->pos, 0, sizeof(pool->pos));
1200 }
1201
1202
1203 void
1204 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1205 {
1206   int i;
1207
1208   pool->languagecache = solv_free(pool->languagecache);
1209   pool->languagecacheother = 0;
1210   if (pool->nlanguages)
1211     {
1212       for (i = 0; i < pool->nlanguages; i++)
1213         free((char *)pool->languages[i]);
1214       free(pool->languages);
1215     }
1216   pool->nlanguages = nlanguages;
1217   if (!nlanguages)
1218     return;
1219   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1220   for (i = 0; i < pool->nlanguages; i++)
1221     pool->languages[i] = solv_strdup(languages[i]);
1222 }
1223
1224 Id
1225 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1226 {
1227   const char *n;
1228   char buf[256], *p;
1229   int l;
1230
1231   if (!lang)
1232     return id;
1233   n = pool_id2str(pool, id);
1234   l = strlen(n) + strlen(lang) + 2;
1235   if (l > sizeof(buf))
1236     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1237   else
1238     p = buf;
1239   sprintf(p, "%s:%s", n, lang);
1240   id = pool_str2id(pool, p, create);
1241   if (p != buf)
1242     free(p);
1243   return id;
1244 }
1245
1246 char *
1247 pool_alloctmpspace(Pool *pool, int len)
1248 {
1249   int n = pool->tmpspace.n;
1250   if (!len)
1251     return 0;
1252   if (len > pool->tmpspace.len[n])
1253     {
1254       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1255       pool->tmpspace.len[n] = len + 32;
1256     }
1257   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1258   return pool->tmpspace.buf[n];
1259 }
1260
1261 static char *
1262 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1263 {
1264   if (space)
1265     {
1266       int n, oldn;
1267       n = oldn = pool->tmpspace.n;
1268       for (;;)
1269         {
1270           if (!n--)
1271             n = POOL_TMPSPACEBUF - 1;
1272           if (n == oldn)
1273             break;
1274           if (pool->tmpspace.buf[n] != space)
1275             continue;
1276           if (len > pool->tmpspace.len[n])
1277             {
1278               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1279               pool->tmpspace.len[n] = len + 32;
1280             }
1281           return pool->tmpspace.buf[n];
1282         }
1283     }
1284   return 0;
1285 }
1286
1287 void
1288 pool_freetmpspace(Pool *pool, const char *space)
1289 {
1290   int n = pool->tmpspace.n;
1291   if (!space)
1292     return;
1293   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1294   if (pool->tmpspace.buf[n] == space)
1295     pool->tmpspace.n = n;
1296 }
1297
1298 char *
1299 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1300 {
1301   int l1, l2, l3;
1302   char *s, *str;
1303   l1 = str1 ? strlen(str1) : 0;
1304   l2 = str2 ? strlen(str2) : 0;
1305   l3 = str3 ? strlen(str3) : 0;
1306   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1307   if (l1)
1308     {
1309       strcpy(s, str1);
1310       s += l1;
1311     }
1312   if (l2)
1313     {
1314       strcpy(s, str2);
1315       s += l2;
1316     }
1317   if (l3)
1318     {
1319       strcpy(s, str3);
1320       s += l3;
1321     }
1322   *s = 0;
1323   return str;
1324 }
1325
1326 char *
1327 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1328 {
1329   int l1, l2, l3;
1330   char *s, *str;
1331
1332   l1 = str1 ? strlen(str1) : 0;
1333   l2 = str2 ? strlen(str2) : 0;
1334   l3 = str3 ? strlen(str3) : 0;
1335   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1336   if (str)
1337     str1 = str;
1338   else
1339     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1340   s = str;
1341   if (l1)
1342     {
1343       if (s != str1)
1344         strcpy(s, str1);
1345       s += l1;
1346     }
1347   if (l2)
1348     {
1349       strcpy(s, str2);
1350       s += l2;
1351     }
1352   if (l3)
1353     {
1354       strcpy(s, str3);
1355       s += l3;
1356     }
1357   *s = 0;
1358   return str;
1359 }
1360
1361 const char *
1362 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1363 {
1364   char *s;
1365   if (!len)
1366     return "";
1367   s = pool_alloctmpspace(pool, 2 * len + 1);
1368   solv_bin2hex(buf, len, s);
1369   return s;
1370 }
1371
1372 /*******************************************************************/
1373
1374 struct mptree {
1375   Id sibling;
1376   Id child;
1377   const char *comp;
1378   int compl;
1379   Id mountpoint;
1380 };
1381
1382 struct ducbdata {
1383   DUChanges *mps;
1384   struct mptree *mptree;
1385   int addsub;
1386   int hasdu;
1387
1388   Id *dirmap;
1389   int nmap;
1390   Repodata *olddata;
1391 };
1392
1393
1394 static int
1395 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1396 {
1397   struct ducbdata *cbd = cbdata;
1398   Id mp;
1399
1400   if (data != cbd->olddata)
1401     {
1402       Id dn, mp, comp, *dirmap, *dirs;
1403       int i, compl;
1404       const char *compstr;
1405       struct mptree *mptree;
1406
1407       /* create map from dir to mptree */
1408       cbd->dirmap = solv_free(cbd->dirmap);
1409       cbd->nmap = 0;
1410       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1411       mptree = cbd->mptree;
1412       mp = 0;
1413       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1414         {
1415           comp = *dirs++;
1416           if (comp <= 0)
1417             {
1418               mp = dirmap[-comp];
1419               continue;
1420             }
1421           if (mp < 0)
1422             {
1423               /* unconnected */
1424               dirmap[dn] = mp;
1425               continue;
1426             }
1427           if (!mptree[mp].child)
1428             {
1429               dirmap[dn] = -mp;
1430               continue;
1431             }
1432           if (data->localpool)
1433             compstr = stringpool_id2str(&data->spool, comp);
1434           else
1435             compstr = pool_id2str(data->repo->pool, comp);
1436           compl = strlen(compstr);
1437           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1438             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1439               break;
1440           dirmap[dn] = i ? i : -mp;
1441         }
1442       /* change dirmap to point to mountpoint instead of mptree */
1443       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1444         {
1445           mp = dirmap[dn];
1446           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1447         }
1448       cbd->dirmap = dirmap;
1449       cbd->nmap = data->dirpool.ndirs;
1450       cbd->olddata = data;
1451     }
1452   cbd->hasdu = 1;
1453   if (value->id < 0 || value->id >= cbd->nmap)
1454     return 0;
1455   mp = cbd->dirmap[value->id];
1456   if (mp < 0)
1457     return 0;
1458   if (cbd->addsub > 0)
1459     {
1460       cbd->mps[mp].kbytes += value->num;
1461       cbd->mps[mp].files += value->num2;
1462     }
1463   else
1464     {
1465       cbd->mps[mp].kbytes -= value->num;
1466       cbd->mps[mp].files -= value->num2;
1467     }
1468   return 0;
1469 }
1470
1471 static void
1472 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1473 {
1474   int i;
1475   if (mptree[pos].mountpoint == -1)
1476     mptree[pos].mountpoint = mountpoint;
1477   else
1478     mountpoint = mptree[pos].mountpoint;
1479   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1480     propagate_mountpoints(mptree, i, mountpoint);
1481 }
1482
1483 #define MPTREE_BLOCK 15
1484
1485 void
1486 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1487 {
1488   char *p;
1489   const char *path, *compstr;
1490   struct mptree *mptree;
1491   int i, nmptree;
1492   int pos, compl;
1493   int mp;
1494   struct ducbdata cbd;
1495   Solvable *s;
1496   Id sp;
1497   Map ignoredu;
1498   Repo *oldinstalled = pool->installed;
1499
1500   memset(&ignoredu, 0, sizeof(ignoredu));
1501   cbd.mps = mps;
1502   cbd.addsub = 0;
1503   cbd.dirmap = 0;
1504   cbd.nmap = 0;
1505   cbd.olddata = 0;
1506
1507   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1508
1509   /* our root node */
1510   mptree[0].sibling = 0;
1511   mptree[0].child = 0;
1512   mptree[0].comp = 0;
1513   mptree[0].compl = 0;
1514   mptree[0].mountpoint = -1;
1515   nmptree = 1;
1516   
1517   /* create component tree */
1518   for (mp = 0; mp < nmps; mp++)
1519     {
1520       mps[mp].kbytes = 0;
1521       mps[mp].files = 0;
1522       pos = 0;
1523       path = mps[mp].path;
1524       while(*path == '/')
1525         path++;
1526       while (*path)
1527         {
1528           if ((p = strchr(path, '/')) == 0)
1529             {
1530               compstr = path;
1531               compl = strlen(compstr);
1532               path += compl;
1533             }
1534           else
1535             {
1536               compstr = path;
1537               compl = p - path;
1538               path = p + 1;
1539               while(*path == '/')
1540                 path++;
1541             }
1542           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1543             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1544               break;
1545           if (!i)
1546             {
1547               /* create new node */
1548               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1549               i = nmptree++;
1550               mptree[i].sibling = mptree[pos].child;
1551               mptree[i].child = 0;
1552               mptree[i].comp = compstr;
1553               mptree[i].compl = compl;
1554               mptree[i].mountpoint = -1;
1555               mptree[pos].child = i;
1556             }
1557           pos = i;
1558         }
1559       mptree[pos].mountpoint = mp;
1560     }
1561
1562   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1563
1564 #if 0
1565   for (i = 0; i < nmptree; i++)
1566     {
1567       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1568       printf("#%d child: %d\n", i, mptree[i].child);
1569       printf("#%d comp: %s\n", i, mptree[i].comp);
1570       printf("#%d compl: %d\n", i, mptree[i].compl);
1571       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1572     }
1573 #endif
1574
1575   cbd.mptree = mptree;
1576   cbd.addsub = 1;
1577   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1578     {
1579       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1580         continue;
1581       if (!MAPTST(installedmap, sp))
1582         continue;
1583       cbd.hasdu = 0;
1584       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1585       if (!cbd.hasdu && oldinstalled)
1586         {
1587           Id op, opp;
1588           /* no du data available, ignore data of all installed solvables we obsolete */
1589           if (!ignoredu.map)
1590             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1591           if (s->obsoletes)
1592             {
1593               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1594               while ((obs = *obsp++) != 0)
1595                 FOR_PROVIDES(op, opp, obs)
1596                   if (op >= oldinstalled->start && op < oldinstalled->end)
1597                     MAPSET(&ignoredu, op - oldinstalled->start);
1598             }
1599           FOR_PROVIDES(op, opp, s->name)
1600             if (pool->solvables[op].name == s->name)
1601               if (op >= oldinstalled->start && op < oldinstalled->end)
1602                 MAPSET(&ignoredu, op - oldinstalled->start);
1603         }
1604     }
1605   cbd.addsub = -1;
1606   if (oldinstalled)
1607     {
1608       /* assumes we allways have du data for installed solvables */
1609       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1610         {
1611           if (MAPTST(installedmap, sp))
1612             continue;
1613           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1614             continue;
1615           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1616         }
1617     }
1618   if (ignoredu.map)
1619     map_free(&ignoredu);
1620   solv_free(cbd.dirmap);
1621   solv_free(mptree);
1622 }
1623
1624 int
1625 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1626 {
1627   Id sp;
1628   Solvable *s;
1629   int change = 0;
1630   Repo *oldinstalled = pool->installed;
1631
1632   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1633     {
1634       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1635         continue;
1636       if (!MAPTST(installedmap, sp))
1637         continue;
1638       change += solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1639     }
1640   if (oldinstalled)
1641     {
1642       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1643         {
1644           if (MAPTST(installedmap, sp))
1645             continue;
1646           change -= solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1647         }
1648     }
1649   return change;
1650 }
1651
1652 /* map:
1653  *  1: installed
1654  *  2: conflicts with installed
1655  *  8: interesting (only true if installed)
1656  * 16: undecided
1657  */
1658  
1659 static inline Id dep2name(Pool *pool, Id dep)
1660 {
1661   while (ISRELDEP(dep))
1662     {
1663       Reldep *rd = rd = GETRELDEP(pool, dep);
1664       dep = rd->name;
1665     }
1666   return dep;
1667 }
1668
1669 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
1670 {
1671   Id p, pp;
1672   Solvable *sn = pool->solvables + n; 
1673
1674   FOR_PROVIDES(p, pp, sn->name)
1675     {    
1676       Solvable *s = pool->solvables + p; 
1677       if (s->name != sn->name || s->arch != sn->arch)
1678         continue;
1679       if ((map[p] & 9) != 9)
1680         continue;
1681       if (pool_match_nevr(pool, pool->solvables + p, con))
1682         continue;
1683       return 1;         /* found installed package that doesn't conflict */
1684     }
1685   return 0;
1686 }
1687
1688 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *noobsoletesmap)
1689 {
1690   Id p, pp;
1691   int r = 0;
1692   FOR_PROVIDES(p, pp, dep)
1693     {
1694       if (p == SYSTEMSOLVABLE)
1695         return 1;       /* always boring, as never constraining */
1696       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
1697         continue;
1698       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
1699         if (providedbyinstalled_multiversion(pool, map, p, dep))
1700           continue;
1701       if ((map[p] & 9) == 9)
1702         return 9;
1703       r |= map[p] & 17;
1704     }
1705   return r;
1706 }
1707
1708 /*
1709  * pool_trivial_installable - calculate if a set of solvables is
1710  * trivial installable without any other installs/deinstalls of
1711  * packages not belonging to the set.
1712  *
1713  * the state is returned in the result queue:
1714  * 1:  solvable is installable without any other package changes
1715  * 0:  solvable is not installable
1716  * -1: solvable is installable, but doesn't constrain any installed packages
1717  */
1718
1719 void
1720 pool_trivial_installable_noobsoletesmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *noobsoletesmap)
1721 {
1722   int i, r, m, did;
1723   Id p, *dp, con, *conp, req, *reqp;
1724   unsigned char *map;
1725   Solvable *s;
1726
1727   map = solv_calloc(pool->nsolvables, 1);
1728   for (p = 1; p < pool->nsolvables; p++)
1729     {
1730       if (!MAPTST(installedmap, p))
1731         continue;
1732       map[p] |= 9;
1733       s = pool->solvables + p;
1734       if (!s->conflicts)
1735         continue;
1736       conp = s->repo->idarraydata + s->conflicts;
1737       while ((con = *conp++) != 0)
1738         {
1739           dp = pool_whatprovides_ptr(pool, con);
1740           for (; *dp; dp++)
1741             map[p] |= 2;        /* XXX: self conflict ? */
1742         }
1743     }
1744   for (i = 0; i < pkgs->count; i++)
1745     map[pkgs->elements[i]] = 16;
1746
1747   for (i = 0, did = 0; did < pkgs->count; i++, did++)
1748     {
1749       if (i == pkgs->count)
1750         i = 0;
1751       p = pkgs->elements[i];
1752       if ((map[p] & 16) == 0)
1753         continue;
1754       if ((map[p] & 2) != 0)
1755         {
1756           map[p] = 2;
1757           continue;
1758         }
1759       s = pool->solvables + p;
1760       m = 1;
1761       if (s->requires)
1762         {
1763           reqp = s->repo->idarraydata + s->requires;
1764           while ((req = *reqp++) != 0)
1765             {
1766               if (req == SOLVABLE_PREREQMARKER)
1767                 continue;
1768               r = providedbyinstalled(pool, map, req, 0, 0);
1769               if (!r)
1770                 {
1771                   /* decided and miss */
1772                   map[p] = 2;
1773                   break;
1774                 }
1775               m |= r;   /* 1 | 9 | 16 | 17 */
1776             }
1777           if (req)
1778             continue;
1779           if ((m & 9) == 9)
1780             m = 9;
1781         }
1782       if (s->conflicts)
1783         {
1784           int ispatch = 0;      /* see solver.c patch handling */
1785
1786           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
1787             ispatch = 1;
1788           conp = s->repo->idarraydata + s->conflicts;
1789           while ((con = *conp++) != 0)
1790             {
1791               if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1792                 {
1793                   map[p] = 2;
1794                   break;
1795                 }
1796               if ((m == 1 || m == 17) && ISRELDEP(con))
1797                 {
1798                   con = dep2name(pool, con);
1799                   if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1800                     m = 9;
1801                 }
1802             }
1803           if (con)
1804             continue;   /* found a conflict */
1805         }
1806 #if 0
1807       if (s->repo && s->repo != oldinstalled)
1808         {
1809           Id p2, obs, *obsp, *pp;
1810           Solvable *s2;
1811           if (s->obsoletes)
1812             {
1813               obsp = s->repo->idarraydata + s->obsoletes;
1814               while ((obs = *obsp++) != 0)
1815                 {
1816                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
1817                     {
1818                       map[p] = 2;
1819                       break;
1820                     }
1821                 }
1822               if (obs)
1823                 continue;
1824             }
1825           FOR_PROVIDES(p2, pp, s->name)
1826             {
1827               s2 = pool->solvables + p2;
1828               if (s2->name == s->name && (map[p2] & 1) != 0)
1829                 {
1830                   map[p] = 2;
1831                   break;
1832                 }
1833             }
1834           if (p2)
1835             continue;
1836         }
1837 #endif
1838       if (m != map[p])
1839         {
1840           map[p] = m;
1841           did = 0;
1842         }
1843     }
1844   queue_free(res);
1845   queue_init_clone(res, pkgs);
1846   for (i = 0; i < pkgs->count; i++)
1847     {
1848       m = map[pkgs->elements[i]];
1849       if ((m & 9) == 9)
1850         r = 1;
1851       else if (m & 1)
1852         r = -1;
1853       else
1854         r = 0;
1855       res->elements[i] = r;
1856     }
1857   free(map);
1858 }
1859
1860 void
1861 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
1862 {
1863   pool_trivial_installable_noobsoletesmap(pool, installedmap, pkgs, res, 0);
1864 }
1865
1866 const char *
1867 pool_lookup_str(Pool *pool, Id entry, Id keyname)
1868 {
1869   if (entry == SOLVID_POS && pool->pos.repo)
1870     return repodata_lookup_str(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1871   if (entry <= 0)
1872     return 0;
1873   return solvable_lookup_str(pool->solvables + entry, keyname);
1874 }
1875
1876 Id
1877 pool_lookup_id(Pool *pool, Id entry, Id keyname)
1878 {
1879   if (entry == SOLVID_POS && pool->pos.repo)
1880     {
1881       Repodata *data = pool->pos.repo->repodata + pool->pos.repodataid;
1882       Id id = repodata_lookup_id(data, SOLVID_POS, keyname);
1883       return data->localpool ? repodata_globalize_id(data, id, 1) : id;
1884     }
1885   if (entry <= 0)
1886     return 0;
1887   return solvable_lookup_id(pool->solvables + entry, keyname);
1888 }
1889
1890 unsigned int
1891 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned int notfound)
1892 {
1893   if (entry == SOLVID_POS && pool->pos.repo)
1894     {
1895       unsigned int value;
1896       if (repodata_lookup_num(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, &value))
1897         return value;
1898       return notfound;
1899     }
1900   if (entry <= 0)
1901     return notfound;
1902   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
1903 }
1904
1905 int
1906 pool_lookup_void(Pool *pool, Id entry, Id keyname)
1907 {
1908   if (entry == SOLVID_POS && pool->pos.repo)
1909     return repodata_lookup_void(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1910   if (entry <= 0)
1911     return 0;
1912   return solvable_lookup_void(pool->solvables + entry, keyname);
1913 }
1914
1915 const unsigned char *
1916 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1917 {
1918   if (entry == SOLVID_POS && pool->pos.repo)
1919     return repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1920   if (entry <= 0)
1921     return 0;
1922   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
1923 }
1924
1925 const char *
1926 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1927 {
1928   if (entry == SOLVID_POS && pool->pos.repo)
1929     {
1930       const unsigned char *chk = repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1931       return chk ? repodata_chk2str(pool->pos.repo->repodata + pool->pos.repodataid, *typep, chk) : 0;
1932     }
1933   if (entry <= 0)
1934     return 0;
1935   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
1936 }
1937
1938 void
1939 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
1940 {
1941   int hadhashes = pool->relhashtbl ? 1 : 0;
1942   Solvable *s;
1943   Id fn, p, q, md5;
1944   Id id;
1945   int i;
1946
1947   if (!conflicts->count)
1948     return;
1949   pool_freewhatprovides(pool);
1950   for (i = 0; i < conflicts->count; i += 5)
1951     {
1952       fn = conflicts->elements[i];
1953       p = conflicts->elements[i + 1];
1954       md5 = conflicts->elements[i + 2];
1955       q = conflicts->elements[i + 3];
1956       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
1957       s = pool->solvables + p;
1958       if (!s->repo)
1959         continue;
1960       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
1961       s = pool->solvables + q;
1962       if (!s->repo)
1963         continue;
1964       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
1965     }
1966   if (!hadhashes)
1967     pool_freeidhashes(pool);
1968 }
1969
1970 /* EOF */