- fix mem leak
[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, wp, *pp, *pp2, *pp3;
558
559   d = GETRELID(d);
560   queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
561   switch (flags)
562     {
563     case REL_AND:
564     case REL_WITH:
565       pp = pool_whatprovides_ptr(pool, name);
566       pp2 = pool_whatprovides_ptr(pool, evr);
567       while ((p = *pp++) != 0)
568         {
569           for (pp3 = pp2; *pp3;)
570             if (*pp3++ == p)
571               {
572                 queue_push(&plist, p);
573                 break;
574               }
575         }
576       break;
577     case REL_OR:
578       pp = pool_whatprovides_ptr(pool, name);
579       while ((p = *pp++) != 0)
580         queue_push(&plist, p);
581       pp = pool_whatprovides_ptr(pool, evr);
582       while ((p = *pp++) != 0)
583         queue_pushunique(&plist, p);
584       break;
585     case REL_NAMESPACE:
586       if (name == NAMESPACE_OTHERPROVIDERS)
587         {
588           wp = pool_whatprovides(pool, evr);
589           pool->whatprovides_rel[d] = wp;
590           return wp;
591         }
592       if (pool->nscallback)
593         {
594           /* ask callback which packages provide the dependency
595            * 0:  none
596            * 1:  the system (aka SYSTEMSOLVABLE)
597            * >1: a set of packages, stored as offset on whatprovidesdata
598            */
599           p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
600           if (p > 1)
601             {
602               queue_free(&plist);
603               pool->whatprovides_rel[d] = p;
604               return p;
605             }
606           if (p == 1)
607             queue_push(&plist, SYSTEMSOLVABLE);
608         }
609       break;
610     case REL_ARCH:
611       /* small hack: make it possible to match <pkg>.src
612        * we have to iterate over the solvables as src packages do not
613        * provide anything, thus they are not indexed in our
614        * whatprovides hash */
615       if (evr == ARCH_SRC)
616         {
617           Solvable *s;
618           for (p = 1, s = pool->solvables + p; p < pool->nsolvables; p++, s++)
619             {
620               if (s->arch != ARCH_SRC && s->arch != ARCH_NOSRC)
621                 continue;
622               if (pool_match_nevr(pool, s, name))
623                 queue_push(&plist, p);
624             }
625           break;
626         }
627       wp = pool_whatprovides(pool, name);
628       pp = pool->whatprovidesdata + wp;
629       while ((p = *pp++) != 0)
630         {
631           Solvable *s = pool->solvables + p;
632           if (s->arch == evr)
633             queue_push(&plist, p);
634           else
635             wp = 0;
636         }
637       if (wp)
638         {
639           /* all solvables match, no need to create a new list */
640           queue_free(&plist);
641           pool->whatprovides_rel[d] = wp;
642           return wp;
643         }
644       break;
645     case REL_FILECONFLICT:
646       pp = pool_whatprovides_ptr(pool, name);
647       while ((p = *pp++) != 0)
648         {
649           Id origd = MAKERELDEP(d);
650           Solvable *s = pool->solvables + p;
651           if (!s->provides)
652             continue;
653           pidp = s->repo->idarraydata + s->provides;
654           while ((pid = *pidp++) != 0)
655             if (pid == origd)
656               break;
657           if (pid)
658             queue_push(&plist, p);
659         }
660       break;
661     default:
662       break;
663     }
664
665   /* convert to whatprovides id */
666 #if 0
667   POOL_DEBUG(SAT_DEBUG_STATS, "addrelproviders: what provides %s?\n", dep2str(pool, name));
668 #endif
669   if (flags && flags < 8)
670     {
671       pp = pool_whatprovides_ptr(pool, name);
672       while (ISRELDEP(name))
673         {
674           rd = GETRELDEP(pool, name);
675           name = rd->name;
676         }
677       while ((p = *pp++) != 0)
678         {
679           Solvable *s = pool->solvables + p;
680 #if 0
681           POOL_DEBUG(DEBUG_1, "addrelproviders: checking package %s\n", id2str(pool, s->name));
682 #endif
683           if (!s->provides)
684             {
685               /* no provides - check nevr */
686               if (pool_match_nevr_rel(pool, s, MAKERELDEP(d)))
687                 queue_push(&plist, p);
688               continue;
689             }
690           /* solvable p provides name in some rels */
691           pidp = s->repo->idarraydata + s->provides;
692           while ((pid = *pidp++) != 0)
693             {
694               if (pid == name)
695                 {
696 #if defined(MULTI_SEMANTICS)
697                   if (pool->disttype == DISTTYPE_DEB)
698                     continue;
699                   else
700                     break;
701 #elif defined(DEBIAN_SEMANTICS)
702                   continue;             /* unversioned provides can
703                                          * never match versioned deps */
704 #else
705                   break;                /* yes, provides all versions */
706 #endif
707                 }
708               if (!ISRELDEP(pid))
709                 continue;               /* wrong provides name */
710               prd = GETRELDEP(pool, pid);
711               if (prd->name != name)
712                 continue;               /* wrong provides name */
713               /* right package, both deps are rels. check flags/evr */
714               if (pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
715                 break;  /* matches */
716             }
717           if (!pid)
718             continue;   /* none of the providers matched */
719           queue_push(&plist, p);
720         }
721       /* make our system solvable provide all unknown rpmlib() stuff */
722       if (plist.count == 0 && !strncmp(id2str(pool, name), "rpmlib(", 7))
723         queue_push(&plist, SYSTEMSOLVABLE);
724     }
725   /* add providers to whatprovides */
726 #if 0
727   POOL_DEBUG(SAT_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
728 #endif
729   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
730   queue_free(&plist);
731
732   return pool->whatprovides_rel[d];
733 }
734
735 /*************************************************************************/
736
737 void
738 pool_debug(Pool *pool, int type, const char *format, ...)
739 {
740   va_list args;
741   char buf[1024];
742
743   if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
744     {
745       if ((pool->debugmask & type) == 0)
746         return;
747     }
748   va_start(args, format);
749   if (!pool->debugcallback)
750     {
751       if ((type & (SAT_FATAL|SAT_ERROR)) == 0 && !(pool->debugmask & SAT_DEBUG_TO_STDERR))
752         vprintf(format, args);
753       else
754         vfprintf(stderr, format, args);
755       return;
756     }
757   vsnprintf(buf, sizeof(buf), format, args);
758   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
759 }
760
761 void
762 pool_setdebuglevel(Pool *pool, int level)
763 {
764   int mask = SAT_DEBUG_RESULT;
765   if (level > 0)
766     mask |= SAT_DEBUG_STATS|SAT_DEBUG_ANALYZE|SAT_DEBUG_UNSOLVABLE|SAT_DEBUG_SOLVER|SAT_DEBUG_TRANSACTION;
767   if (level > 1)
768     mask |= SAT_DEBUG_JOB|SAT_DEBUG_SOLUTIONS|SAT_DEBUG_POLICY;
769   if (level > 2)
770     mask |= SAT_DEBUG_PROPAGATE;
771   if (level > 3)
772     mask |= SAT_DEBUG_RULE_CREATION;
773   if (level > 4)
774     mask |= SAT_DEBUG_SCHUBI;
775   mask |= pool->debugmask & SAT_DEBUG_TO_STDERR;        /* keep bit */
776   pool->debugmask = mask;
777 }
778
779 /*************************************************************************/
780
781 struct searchfiles {
782   Id *ids;
783   char **dirs;
784   char **names;
785   int nfiles;
786   Map seen;
787 };
788
789 #define SEARCHFILES_BLOCK 127
790
791 static void
792 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
793 {
794   Id dep, sid;
795   const char *s, *sr;
796   struct searchfiles *csf;
797
798   while ((dep = *ida++) != 0)
799     {
800       csf = sf;
801       while (ISRELDEP(dep))
802         {
803           Reldep *rd;
804           sid = pool->ss.nstrings + GETRELID(dep);
805           if (MAPTST(&csf->seen, sid))
806             {
807               dep = 0;
808               break;
809             }
810           MAPSET(&csf->seen, sid);
811           rd = GETRELDEP(pool, dep);
812           if (rd->flags < 8)
813             dep = rd->name;
814           else if (rd->flags == REL_NAMESPACE)
815             {
816               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
817                 {
818                   csf = isf;
819                   if (!csf || MAPTST(&csf->seen, sid))
820                     {
821                       dep = 0;
822                       break;
823                     }
824                   MAPSET(&csf->seen, sid);
825                 }
826               dep = rd->evr;
827             }
828           else if (rd->flags == REL_FILECONFLICT)
829             {
830               dep = 0;
831               break;
832             }
833           else
834             {
835               Id ids[2];
836               ids[0] = rd->name;
837               ids[1] = 0;
838               pool_addfileprovides_dep(pool, ids, csf, isf);
839               dep = rd->evr;
840             }
841         }
842       if (!dep)
843         continue;
844       if (MAPTST(&csf->seen, dep))
845         continue;
846       MAPSET(&csf->seen, dep);
847       s = id2str(pool, dep);
848       if (*s != '/')
849         continue;
850       csf->ids = sat_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
851       csf->dirs = sat_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
852       csf->names = sat_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
853       csf->ids[csf->nfiles] = dep;
854       sr = strrchr(s, '/');
855       csf->names[csf->nfiles] = strdup(sr + 1);
856       csf->dirs[csf->nfiles] = sat_malloc(sr - s + 1);
857       if (sr != s)
858         strncpy(csf->dirs[csf->nfiles], s, sr - s);
859       csf->dirs[csf->nfiles][sr - s] = 0;
860       csf->nfiles++;
861     }
862 }
863
864 struct addfileprovides_cbdata {
865   int nfiles;
866   Id *ids;
867   char **dirs;
868   char **names;
869
870   Id *dids;
871
872   Map providedids;
873
874   Map useddirs;
875 };
876
877 static int
878 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
879 {
880   struct addfileprovides_cbdata *cbd = cbdata;
881   int i;
882
883   if (!cbd->useddirs.size)
884     {
885       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
886       for (i = 0; i < cbd->nfiles; i++)
887         {
888           Id did;
889           if (MAPTST(&cbd->providedids, cbd->ids[i]))
890             {
891               cbd->dids[i] = 0;
892               continue;
893             }
894           did = repodata_str2dir(data, cbd->dirs[i], 0);
895           cbd->dids[i] = did;
896           if (did)
897             MAPSET(&cbd->useddirs, did);
898         }
899     }
900   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
901     return 0;
902   for (i = 0; i < cbd->nfiles; i++)
903     {
904       if (cbd->dids[i] != value->id)
905         continue;
906       if (!strcmp(cbd->names[i], value->str))
907         break;
908     }
909   if (i == cbd->nfiles)
910     return 0;
911   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
912   return 0;
913 }
914
915 static void
916 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
917 {
918   Id p;
919   Repodata *data;
920   Repo *repo;
921   Queue fileprovidesq;
922   int i, j, repoid, repodataid;
923   int provstart, provend;
924   Map donemap;
925   int ndone, incomplete;
926
927   if (!pool->nrepos)
928     return;
929
930   cbd->nfiles = sf->nfiles;
931   cbd->ids = sf->ids;
932   cbd->dirs = sf->dirs;
933   cbd->names = sf->names;
934   cbd->dids = sat_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
935   map_init(&cbd->providedids, pool->ss.nstrings);
936
937   repoid = 0;
938   repo = repoonly ? repoonly : pool->repos[0];
939   map_init(&donemap, pool->nsolvables);
940   queue_init(&fileprovidesq);
941   provstart = provend = 0;
942   for (;;)
943     {
944       if (repo->disabled)
945         {
946           if (repoonly || ++repoid == pool->nrepos)
947             break;
948           repo = pool->repos[repoid];
949           continue;
950         }
951       ndone = 0;
952       for (data = repo->repodata, repodataid = 0; repodataid < repo->nrepodata; repodataid++, data++)
953         {
954           if (ndone >= repo->nsolvables)
955             break;
956
957           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
958             {
959               map_empty(&cbd->providedids);
960               for (i = 0; i < fileprovidesq.count; i++)
961                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
962               provstart = data->start;
963               provend = data->end;
964               for (i = 0; i < cbd->nfiles; i++)
965                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
966                   break;
967               if (i == cbd->nfiles)
968                 {
969                   /* great! no need to search files */
970                   for (p = data->start; p < data->end; p++)
971                     if (pool->solvables[p].repo == repo)
972                       {
973                         if (MAPTST(&donemap, p))
974                           continue;
975                         MAPSET(&donemap, p);
976                         ndone++;
977                       }
978                   continue;
979                 }
980             }
981
982           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
983             continue;
984
985           if (data->start < provstart || data->end > provend)
986             {
987               map_empty(&cbd->providedids);
988               provstart = provend = 0;
989             }
990
991           /* check if the data is incomplete */
992           incomplete = 0;
993           if (data->state == REPODATA_AVAILABLE)
994             {
995               for (j = 1; j < data->nkeys; j++)
996                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
997                   break;
998               if (j < data->nkeys)
999                 {
1000 #if 0
1001                   for (i = 0; i < cbd->nfiles; i++)
1002                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, id2str(pool, cbd->ids[i])))
1003                       printf("need complete filelist because of %s\n", id2str(pool, cbd->ids[i]));
1004 #endif
1005                   for (i = 0; i < cbd->nfiles; i++)
1006                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, id2str(pool, cbd->ids[i])))
1007                       break;
1008                   if (i < cbd->nfiles)
1009                     incomplete = 1;
1010                 }
1011             }
1012
1013           /* do the search */
1014           map_init(&cbd->useddirs, 0);
1015           for (p = data->start; p < data->end; p++)
1016             if (pool->solvables[p].repo == repo)
1017               {
1018                 if (MAPTST(&donemap, p))
1019                   continue;
1020                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1021                 if (!incomplete)
1022                   {
1023                     MAPSET(&donemap, p);
1024                     ndone++;
1025                   }
1026               }
1027           map_free(&cbd->useddirs);
1028         }
1029
1030       if (repoonly || ++repoid == pool->nrepos)
1031         break;
1032       repo = pool->repos[repoid];
1033     }
1034   map_free(&donemap);
1035   queue_free(&fileprovidesq);
1036   map_free(&cbd->providedids);
1037 }
1038
1039 void
1040 pool_addfileprovides_ids(Pool *pool, Repo *installed, Id **idp)
1041 {
1042   Solvable *s;
1043   Repo *repo;
1044   struct searchfiles sf, isf, *isfp;
1045   struct addfileprovides_cbdata cbd;
1046   int i;
1047   unsigned int now;
1048
1049   now = sat_timems(0);
1050   memset(&sf, 0, sizeof(sf));
1051   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1052   memset(&isf, 0, sizeof(isf));
1053   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1054
1055   isfp = installed ? &isf : 0;
1056   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1057     {
1058       repo = s->repo;
1059       if (!repo)
1060         continue;
1061       if (s->obsoletes)
1062         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1063       if (s->conflicts)
1064         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1065       if (s->requires)
1066         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1067       if (s->recommends)
1068         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1069       if (s->suggests)
1070         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1071       if (s->supplements)
1072         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1073       if (s->enhances)
1074         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1075     }
1076   map_free(&sf.seen);
1077   map_free(&isf.seen);
1078   POOL_DEBUG(SAT_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1079   cbd.dids = 0;
1080   if (idp)
1081     *idp = 0;
1082   if (sf.nfiles)
1083     {
1084 #if 0
1085       for (i = 0; i < sf.nfiles; i++)
1086         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in filelist\n", id2str(pool, sf.ids[i]));
1087 #endif
1088       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1089       if (idp)
1090         {
1091           sf.ids = sat_extend(sf.ids, sf.nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1092           sf.ids[sf.nfiles] = 0;
1093           *idp = sf.ids;
1094           sf.ids = 0;
1095         }
1096       sat_free(sf.ids);
1097       for (i = 0; i < sf.nfiles; i++)
1098         {
1099           sat_free(sf.dirs[i]);
1100           sat_free(sf.names[i]);
1101         }
1102       sat_free(sf.dirs);
1103       sat_free(sf.names);
1104     }
1105   if (isf.nfiles)
1106     {
1107 #if 0
1108       for (i = 0; i < isf.nfiles; i++)
1109         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in installed filelist\n", id2str(pool, isf.ids[i]));
1110 #endif
1111       if (installed)
1112         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1113       sat_free(isf.ids);
1114       for (i = 0; i < isf.nfiles; i++)
1115         {
1116           sat_free(isf.dirs[i]);
1117           sat_free(isf.names[i]);
1118         }
1119       sat_free(isf.dirs);
1120       sat_free(isf.names);
1121     }
1122   sat_free(cbd.dids);
1123   pool_freewhatprovides(pool);  /* as we have added provides */
1124   POOL_DEBUG(SAT_DEBUG_STATS, "addfileprovides took %d ms\n", sat_timems(now));
1125 }
1126
1127 void
1128 pool_addfileprovides(Pool *pool)
1129 {
1130   pool_addfileprovides_ids(pool, pool->installed, 0);
1131 }
1132
1133 void
1134 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)
1135 {
1136   if (p)
1137     {
1138       if (pool->solvables[p].repo)
1139         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1140       return;
1141     }
1142   /* FIXME: obey callback return value! */
1143   for (p = 1; p < pool->nsolvables; p++)
1144     if (pool->solvables[p].repo)
1145       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1146 }
1147
1148 void
1149 pool_clear_pos(Pool *pool)
1150 {
1151   memset(&pool->pos, 0, sizeof(pool->pos));
1152 }
1153
1154
1155 void
1156 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1157 {
1158   int i;
1159
1160   pool->languagecache = sat_free(pool->languagecache);
1161   pool->languagecacheother = 0;
1162   if (pool->nlanguages)
1163     {
1164       for (i = 0; i < pool->nlanguages; i++)
1165         free((char *)pool->languages[i]);
1166       free(pool->languages);
1167     }
1168   pool->nlanguages = nlanguages;
1169   if (!nlanguages)
1170     return;
1171   pool->languages = sat_calloc(nlanguages, sizeof(const char **));
1172   for (i = 0; i < pool->nlanguages; i++)
1173     pool->languages[i] = strdup(languages[i]);
1174 }
1175
1176 Id
1177 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1178 {
1179   const char *n;
1180   char buf[256], *p;
1181   int l;
1182
1183   if (!lang)
1184     return id;
1185   n = id2str(pool, id);
1186   l = strlen(n) + strlen(lang) + 2;
1187   if (l > sizeof(buf))
1188     p = sat_malloc(strlen(n) + strlen(lang) + 2);
1189   else
1190     p = buf;
1191   sprintf(p, "%s:%s", n, lang);
1192   id = str2id(pool, p, create);
1193   if (p != buf)
1194     free(p);
1195   return id;
1196 }
1197
1198 char *
1199 pool_alloctmpspace(Pool *pool, int len)
1200 {
1201   int n = pool->tmpspacen;
1202   if (!len)
1203     return 0;
1204   if (len > pool->tmpspacelen[n])
1205     {
1206       pool->tmpspacebuf[n] = sat_realloc(pool->tmpspacebuf[n], len + 32);
1207       pool->tmpspacelen[n] = len + 32;
1208     }
1209   pool->tmpspacen = (n + 1) % POOL_TMPSPACEBUF;
1210   return pool->tmpspacebuf[n];
1211 }
1212
1213 char *
1214 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1215 {
1216   int l1, l2, l3;
1217   char *s, *str;
1218   l1 = str1 ? strlen(str1) : 0;
1219   l2 = str2 ? strlen(str2) : 0;
1220   l3 = str3 ? strlen(str3) : 0;
1221   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1222   if (l1)
1223     {
1224       strcpy(s, str1);
1225       s += l1;
1226     }
1227   if (l2)
1228     {
1229       strcpy(s, str2);
1230       s += l2;
1231     }
1232   if (l3)
1233     {
1234       strcpy(s, str3);
1235       s += l3;
1236     }
1237   *s = 0;
1238   return str;
1239 }
1240
1241
1242 /*******************************************************************/
1243
1244 struct mptree {
1245   Id sibling;
1246   Id child;
1247   const char *comp;
1248   int compl;
1249   Id mountpoint;
1250 };
1251
1252 struct ducbdata {
1253   DUChanges *mps;
1254   struct mptree *mptree;
1255   int addsub;
1256   int hasdu;
1257
1258   Id *dirmap;
1259   int nmap;
1260   Repodata *olddata;
1261 };
1262
1263
1264 static int
1265 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1266 {
1267   struct ducbdata *cbd = cbdata;
1268   Id mp;
1269
1270   if (data != cbd->olddata)
1271     {
1272       Id dn, mp, comp, *dirmap, *dirs;
1273       int i, compl;
1274       const char *compstr;
1275       struct mptree *mptree;
1276
1277       /* create map from dir to mptree */
1278       cbd->dirmap = sat_free(cbd->dirmap);
1279       cbd->nmap = 0;
1280       dirmap = sat_calloc(data->dirpool.ndirs, sizeof(Id));
1281       mptree = cbd->mptree;
1282       mp = 0;
1283       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1284         {
1285           comp = *dirs++;
1286           if (comp <= 0)
1287             {
1288               mp = dirmap[-comp];
1289               continue;
1290             }
1291           if (mp < 0)
1292             {
1293               /* unconnected */
1294               dirmap[dn] = mp;
1295               continue;
1296             }
1297           if (!mptree[mp].child)
1298             {
1299               dirmap[dn] = -mp;
1300               continue;
1301             }
1302           if (data->localpool)
1303             compstr = stringpool_id2str(&data->spool, comp);
1304           else
1305             compstr = id2str(data->repo->pool, comp);
1306           compl = strlen(compstr);
1307           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1308             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1309               break;
1310           dirmap[dn] = i ? i : -mp;
1311         }
1312       /* change dirmap to point to mountpoint instead of mptree */
1313       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1314         {
1315           mp = dirmap[dn];
1316           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1317         }
1318       cbd->dirmap = dirmap;
1319       cbd->nmap = data->dirpool.ndirs;
1320       cbd->olddata = data;
1321     }
1322   cbd->hasdu = 1;
1323   if (value->id < 0 || value->id >= cbd->nmap)
1324     return 0;
1325   mp = cbd->dirmap[value->id];
1326   if (mp < 0)
1327     return 0;
1328   if (cbd->addsub > 0)
1329     {
1330       cbd->mps[mp].kbytes += value->num;
1331       cbd->mps[mp].files += value->num2;
1332     }
1333   else
1334     {
1335       cbd->mps[mp].kbytes -= value->num;
1336       cbd->mps[mp].files -= value->num2;
1337     }
1338   return 0;
1339 }
1340
1341 static void
1342 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1343 {
1344   int i;
1345   if (mptree[pos].mountpoint == -1)
1346     mptree[pos].mountpoint = mountpoint;
1347   else
1348     mountpoint = mptree[pos].mountpoint;
1349   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1350     propagate_mountpoints(mptree, i, mountpoint);
1351 }
1352
1353 #define MPTREE_BLOCK 15
1354
1355 void
1356 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1357 {
1358   char *p;
1359   const char *path, *compstr;
1360   struct mptree *mptree;
1361   int i, nmptree;
1362   int pos, compl;
1363   int mp;
1364   struct ducbdata cbd;
1365   Solvable *s;
1366   Id sp;
1367   Map ignoredu;
1368   Repo *oldinstalled = pool->installed;
1369
1370   memset(&ignoredu, 0, sizeof(ignoredu));
1371   cbd.mps = mps;
1372   cbd.addsub = 0;
1373   cbd.dirmap = 0;
1374   cbd.nmap = 0;
1375   cbd.olddata = 0;
1376
1377   mptree = sat_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1378
1379   /* our root node */
1380   mptree[0].sibling = 0;
1381   mptree[0].child = 0;
1382   mptree[0].comp = 0;
1383   mptree[0].compl = 0;
1384   mptree[0].mountpoint = -1;
1385   nmptree = 1;
1386   
1387   /* create component tree */
1388   for (mp = 0; mp < nmps; mp++)
1389     {
1390       mps[mp].kbytes = 0;
1391       mps[mp].files = 0;
1392       pos = 0;
1393       path = mps[mp].path;
1394       while(*path == '/')
1395         path++;
1396       while (*path)
1397         {
1398           if ((p = strchr(path, '/')) == 0)
1399             {
1400               compstr = path;
1401               compl = strlen(compstr);
1402               path += compl;
1403             }
1404           else
1405             {
1406               compstr = path;
1407               compl = p - path;
1408               path = p + 1;
1409               while(*path == '/')
1410                 path++;
1411             }
1412           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1413             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1414               break;
1415           if (!i)
1416             {
1417               /* create new node */
1418               mptree = sat_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1419               i = nmptree++;
1420               mptree[i].sibling = mptree[pos].child;
1421               mptree[i].child = 0;
1422               mptree[i].comp = compstr;
1423               mptree[i].compl = compl;
1424               mptree[i].mountpoint = -1;
1425               mptree[pos].child = i;
1426             }
1427           pos = i;
1428         }
1429       mptree[pos].mountpoint = mp;
1430     }
1431
1432   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1433
1434 #if 0
1435   for (i = 0; i < nmptree; i++)
1436     {
1437       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1438       printf("#%d child: %d\n", i, mptree[i].child);
1439       printf("#%d comp: %s\n", i, mptree[i].comp);
1440       printf("#%d compl: %d\n", i, mptree[i].compl);
1441       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1442     }
1443 #endif
1444
1445   cbd.mptree = mptree;
1446   cbd.addsub = 1;
1447   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1448     {
1449       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1450         continue;
1451       if (!MAPTST(installedmap, sp))
1452         continue;
1453       cbd.hasdu = 0;
1454       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1455       if (!cbd.hasdu && oldinstalled)
1456         {
1457           Id op, opp;
1458           /* no du data available, ignore data of all installed solvables we obsolete */
1459           if (!ignoredu.map)
1460             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1461           if (s->obsoletes)
1462             {
1463               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1464               while ((obs = *obsp++) != 0)
1465                 FOR_PROVIDES(op, opp, obs)
1466                   if (op >= oldinstalled->start && op < oldinstalled->end)
1467                     MAPSET(&ignoredu, op - oldinstalled->start);
1468             }
1469           FOR_PROVIDES(op, opp, s->name)
1470             if (pool->solvables[op].name == s->name)
1471               if (op >= oldinstalled->start && op < oldinstalled->end)
1472                 MAPSET(&ignoredu, op - oldinstalled->start);
1473         }
1474     }
1475   cbd.addsub = -1;
1476   if (oldinstalled)
1477     {
1478       /* assumes we allways have du data for installed solvables */
1479       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1480         {
1481           if (MAPTST(installedmap, sp))
1482             continue;
1483           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1484             continue;
1485           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1486         }
1487     }
1488   if (ignoredu.map)
1489     map_free(&ignoredu);
1490   sat_free(cbd.dirmap);
1491   sat_free(mptree);
1492 }
1493
1494 int
1495 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1496 {
1497   Id sp;
1498   Solvable *s;
1499   int change = 0;
1500   Repo *oldinstalled = pool->installed;
1501
1502   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1503     {
1504       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1505         continue;
1506       if (!MAPTST(installedmap, sp))
1507         continue;
1508       change += solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1509     }
1510   if (oldinstalled)
1511     {
1512       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1513         {
1514           if (MAPTST(installedmap, sp))
1515             continue;
1516           change -= solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1517         }
1518     }
1519   return change;
1520 }
1521
1522 /* map:
1523  *  1: installed
1524  *  2: conflicts with installed
1525  *  8: interesting (only true if installed)
1526  * 16: undecided
1527  */
1528  
1529 static inline Id dep2name(Pool *pool, Id dep)
1530 {
1531   while (ISRELDEP(dep))
1532     {
1533       Reldep *rd = rd = GETRELDEP(pool, dep);
1534       dep = rd->name;
1535     }
1536   return dep;
1537 }
1538
1539 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
1540 {
1541   Id p, pp;
1542   Solvable *sn = pool->solvables + n; 
1543
1544   FOR_PROVIDES(p, pp, sn->name)
1545     {    
1546       Solvable *s = pool->solvables + p; 
1547       if (s->name != sn->name || s->arch != sn->arch)
1548         continue;
1549       if ((map[p] & 9) != 9)
1550         continue;
1551       if (pool_match_nevr(pool, pool->solvables + p, con))
1552         continue;
1553       return 1;         /* found installed package that doesn't conflict */
1554     }
1555   return 0;
1556 }
1557
1558 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *noobsoletesmap)
1559 {
1560   Id p, pp;
1561   int r = 0;
1562   FOR_PROVIDES(p, pp, dep)
1563     {
1564       if (p == SYSTEMSOLVABLE)
1565         return 1;       /* always boring, as never constraining */
1566       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
1567         continue;
1568       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
1569         if (providedbyinstalled_multiversion(pool, map, p, dep))
1570           continue;
1571       if ((map[p] & 9) == 9)
1572         return 9;
1573       r |= map[p] & 17;
1574     }
1575   return r;
1576 }
1577
1578 /*
1579  * pool_trivial_installable - calculate if a set of solvables is
1580  * trivial installable without any other installs/deinstalls of
1581  * packages not belonging to the set.
1582  *
1583  * the state is returned in the result queue:
1584  * 1:  solvable is installable without any other package changes
1585  * 0:  solvable is not installable
1586  * -1: solvable is installable, but doesn't constrain any installed packages
1587  */
1588
1589 void
1590 pool_trivial_installable_noobsoletesmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *noobsoletesmap)
1591 {
1592   int i, r, m, did;
1593   Id p, *dp, con, *conp, req, *reqp;
1594   unsigned char *map;
1595   Solvable *s;
1596
1597   map = sat_calloc(pool->nsolvables, 1);
1598   for (p = 1; p < pool->nsolvables; p++)
1599     {
1600       if (!MAPTST(installedmap, p))
1601         continue;
1602       map[p] |= 9;
1603       s = pool->solvables + p;
1604       if (!s->conflicts)
1605         continue;
1606       conp = s->repo->idarraydata + s->conflicts;
1607       while ((con = *conp++) != 0)
1608         {
1609           dp = pool_whatprovides_ptr(pool, con);
1610           for (; *dp; dp++)
1611             map[p] |= 2;        /* XXX: self conflict ? */
1612         }
1613     }
1614   for (i = 0; i < pkgs->count; i++)
1615     map[pkgs->elements[i]] = 16;
1616
1617   for (i = 0, did = 0; did < pkgs->count; i++, did++)
1618     {
1619       if (i == pkgs->count)
1620         i = 0;
1621       p = pkgs->elements[i];
1622       if ((map[p] & 16) == 0)
1623         continue;
1624       if ((map[p] & 2) != 0)
1625         {
1626           map[p] = 2;
1627           continue;
1628         }
1629       s = pool->solvables + p;
1630       m = 1;
1631       if (s->requires)
1632         {
1633           reqp = s->repo->idarraydata + s->requires;
1634           while ((req = *reqp++) != 0)
1635             {
1636               if (req == SOLVABLE_PREREQMARKER)
1637                 continue;
1638               r = providedbyinstalled(pool, map, req, 0, 0);
1639               if (!r)
1640                 {
1641                   /* decided and miss */
1642                   map[p] = 2;
1643                   break;
1644                 }
1645               m |= r;   /* 1 | 9 | 16 | 17 */
1646             }
1647           if (req)
1648             continue;
1649           if ((m & 9) == 9)
1650             m = 9;
1651         }
1652       if (s->conflicts)
1653         {
1654           int ispatch = 0;      /* see solver.c patch handling */
1655
1656           if (!strncmp("patch:", id2str(pool, s->name), 6))
1657             ispatch = 1;
1658           conp = s->repo->idarraydata + s->conflicts;
1659           while ((con = *conp++) != 0)
1660             {
1661               if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1662                 {
1663                   map[p] = 2;
1664                   break;
1665                 }
1666               if ((m == 1 || m == 17) && ISRELDEP(con))
1667                 {
1668                   con = dep2name(pool, con);
1669                   if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1670                     m = 9;
1671                 }
1672             }
1673           if (con)
1674             continue;   /* found a conflict */
1675         }
1676 #if 0
1677       if (s->repo && s->repo != oldinstalled)
1678         {
1679           Id p2, obs, *obsp, *pp;
1680           Solvable *s2;
1681           if (s->obsoletes)
1682             {
1683               obsp = s->repo->idarraydata + s->obsoletes;
1684               while ((obs = *obsp++) != 0)
1685                 {
1686                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
1687                     {
1688                       map[p] = 2;
1689                       break;
1690                     }
1691                 }
1692               if (obs)
1693                 continue;
1694             }
1695           FOR_PROVIDES(p2, pp, s->name)
1696             {
1697               s2 = pool->solvables + p2;
1698               if (s2->name == s->name && (map[p2] & 1) != 0)
1699                 {
1700                   map[p] = 2;
1701                   break;
1702                 }
1703             }
1704           if (p2)
1705             continue;
1706         }
1707 #endif
1708       if (m != map[p])
1709         {
1710           map[p] = m;
1711           did = 0;
1712         }
1713     }
1714   queue_free(res);
1715   queue_init_clone(res, pkgs);
1716   for (i = 0; i < pkgs->count; i++)
1717     {
1718       m = map[pkgs->elements[i]];
1719       if ((m & 9) == 9)
1720         r = 1;
1721       else if (m & 1)
1722         r = -1;
1723       else
1724         r = 0;
1725       res->elements[i] = r;
1726     }
1727   free(map);
1728 }
1729
1730 void
1731 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
1732 {
1733   pool_trivial_installable_noobsoletesmap(pool, installedmap, pkgs, res, 0);
1734 }
1735
1736 const char *
1737 pool_lookup_str(Pool *pool, Id entry, Id keyname)
1738 {
1739   if (entry == SOLVID_POS && pool->pos.repo)
1740     return repodata_lookup_str(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1741   if (entry <= 0)
1742     return 0;
1743   return solvable_lookup_str(pool->solvables + entry, keyname);
1744 }
1745
1746 Id
1747 pool_lookup_id(Pool *pool, Id entry, Id keyname)
1748 {
1749   if (entry == SOLVID_POS && pool->pos.repo)
1750     {
1751       Repodata *data = pool->pos.repo->repodata + pool->pos.repodataid;
1752       Id id = repodata_lookup_id(data, SOLVID_POS, keyname);
1753       return data->localpool ? repodata_globalize_id(data, id, 1) : id;
1754     }
1755   if (entry <= 0)
1756     return 0;
1757   return solvable_lookup_id(pool->solvables + entry, keyname);
1758 }
1759
1760 unsigned int
1761 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned int notfound)
1762 {
1763   if (entry == SOLVID_POS && pool->pos.repo)
1764     {
1765       unsigned int value;
1766       if (repodata_lookup_num(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, &value))
1767         return value;
1768       return notfound;
1769     }
1770   if (entry <= 0)
1771     return notfound;
1772   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
1773 }
1774
1775 int
1776 pool_lookup_void(Pool *pool, Id entry, Id keyname)
1777 {
1778   if (entry == SOLVID_POS && pool->pos.repo)
1779     return repodata_lookup_void(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1780   if (entry <= 0)
1781     return 0;
1782   return solvable_lookup_void(pool->solvables + entry, keyname);
1783 }
1784
1785 const unsigned char *
1786 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1787 {
1788   if (entry == SOLVID_POS && pool->pos.repo)
1789     return repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1790   if (entry <= 0)
1791     return 0;
1792   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
1793 }
1794
1795 const char *
1796 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1797 {
1798   if (entry == SOLVID_POS && pool->pos.repo)
1799     {
1800       const unsigned char *chk = repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1801       return chk ? repodata_chk2str(pool->pos.repo->repodata + pool->pos.repodataid, *typep, chk) : 0;
1802     }
1803   if (entry <= 0)
1804     return 0;
1805   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
1806 }
1807
1808 void
1809 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
1810 {
1811   int hadhashes = pool->relhashtbl ? 1 : 0;
1812   Solvable *s;
1813   Id fn, p, q, md5;
1814   Id id;
1815   int i;
1816
1817   if (!conflicts->count)
1818     return;
1819   pool_freewhatprovides(pool);
1820   for (i = 0; i < conflicts->count; i += 5)
1821     {
1822       fn = conflicts->elements[i];
1823       p = conflicts->elements[i + 1];
1824       md5 = conflicts->elements[i + 2];
1825       q = conflicts->elements[i + 3];
1826       id = rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
1827       s = pool->solvables + p;
1828       if (!s->repo)
1829         continue;
1830       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
1831       s = pool->solvables + q;
1832       if (!s->repo)
1833         continue;
1834       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
1835     }
1836   if (!hadhashes)
1837     pool_freeidhashes(pool);
1838 }
1839
1840 /* EOF */