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