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