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