Merge branch 'master' of git://git.opensuse.org/projects/zypp/sat-solver
[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 (!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           if (!repodata_precheck_keyname(data, SOLVABLE_FILELIST))
960             continue;
961           for (j = 1; j < data->nkeys; j++)
962             if (data->keys[j].name == SOLVABLE_FILELIST)
963               break;
964           if (j == data->nkeys)
965             continue;
966           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
967             {
968               map_empty(&cbd->providedids);
969               for (i = 0; i < fileprovidesq.count; i++)
970                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
971               provstart = data->start;
972               provend = data->end;
973               for (i = 0; i < cbd->nfiles; i++)
974                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
975                   break;
976               if (i == cbd->nfiles)
977                 {
978                   /* great! no need to search files */
979                   for (p = data->start; p < data->end; p++)
980                     if (pool->solvables[p].repo == repo)
981                       {
982                         if (MAPTST(&donemap, p))
983                           continue;
984                         MAPSET(&donemap, p);
985                         ndone++;
986                       }
987                   continue;
988                 }
989             }
990           else
991             {
992               if (data->start < provstart || data->end > provend)
993                 {
994                   map_empty(&cbd->providedids);
995                   provstart = provend = 0;
996                 }
997             }
998
999           /* check if the data is incomplete */
1000           incomplete = 0;
1001           if (data->state == REPODATA_AVAILABLE)
1002             {
1003               for (j = 1; j < data->nkeys; j++)
1004                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1005                   break;
1006               if (j < data->nkeys)
1007                 {
1008 #if 0
1009                   for (i = 0; i < cbd->nfiles; i++)
1010                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, id2str(pool, cbd->ids[i])))
1011                       printf("need complete filelist because of %s\n", id2str(pool, cbd->ids[i]));
1012 #endif
1013                   for (i = 0; i < cbd->nfiles; i++)
1014                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, id2str(pool, cbd->ids[i])))
1015                       break;
1016                   if (i < cbd->nfiles)
1017                     incomplete = 1;
1018                 }
1019             }
1020
1021           /* do the search */
1022           map_init(&cbd->useddirs, 0);
1023           for (p = data->start; p < data->end; p++)
1024             if (pool->solvables[p].repo == repo)
1025               {
1026                 if (MAPTST(&donemap, p))
1027                   continue;
1028                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1029                 if (!incomplete)
1030                   {
1031                     MAPSET(&donemap, p);
1032                     ndone++;
1033                   }
1034               }
1035           map_free(&cbd->useddirs);
1036         }
1037
1038       if (repoonly || ++repoid == pool->nrepos)
1039         break;
1040       repo = pool->repos[repoid];
1041     }
1042   map_free(&donemap);
1043   queue_free(&fileprovidesq);
1044   map_free(&cbd->providedids);
1045 }
1046
1047 void
1048 pool_addfileprovides_ids(Pool *pool, Repo *installed, Id **idp)
1049 {
1050   Solvable *s;
1051   Repo *repo;
1052   struct searchfiles sf, isf, *isfp;
1053   struct addfileprovides_cbdata cbd;
1054   int i;
1055   unsigned int now;
1056
1057   now = sat_timems(0);
1058   memset(&sf, 0, sizeof(sf));
1059   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1060   memset(&isf, 0, sizeof(isf));
1061   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1062
1063   isfp = installed ? &isf : 0;
1064   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1065     {
1066       repo = s->repo;
1067       if (!repo)
1068         continue;
1069       if (s->obsoletes)
1070         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1071       if (s->conflicts)
1072         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1073       if (s->requires)
1074         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1075       if (s->recommends)
1076         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1077       if (s->suggests)
1078         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1079       if (s->supplements)
1080         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1081       if (s->enhances)
1082         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1083     }
1084   map_free(&sf.seen);
1085   map_free(&isf.seen);
1086   POOL_DEBUG(SAT_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1087   cbd.dids = 0;
1088   if (idp)
1089     *idp = 0;
1090   if (sf.nfiles)
1091     {
1092 #if 0
1093       for (i = 0; i < sf.nfiles; i++)
1094         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in filelist\n", id2str(pool, sf.ids[i]));
1095 #endif
1096       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1097       if (idp)
1098         {
1099           sf.ids = sat_extend(sf.ids, sf.nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1100           sf.ids[sf.nfiles] = 0;
1101           *idp = sf.ids;
1102           sf.ids = 0;
1103         }
1104       sat_free(sf.ids);
1105       for (i = 0; i < sf.nfiles; i++)
1106         {
1107           sat_free(sf.dirs[i]);
1108           sat_free(sf.names[i]);
1109         }
1110       sat_free(sf.dirs);
1111       sat_free(sf.names);
1112     }
1113   if (isf.nfiles)
1114     {
1115 #if 0
1116       for (i = 0; i < isf.nfiles; i++)
1117         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in installed filelist\n", id2str(pool, isf.ids[i]));
1118 #endif
1119       if (installed)
1120         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1121       sat_free(isf.ids);
1122       for (i = 0; i < isf.nfiles; i++)
1123         {
1124           sat_free(isf.dirs[i]);
1125           sat_free(isf.names[i]);
1126         }
1127       sat_free(isf.dirs);
1128       sat_free(isf.names);
1129     }
1130   sat_free(cbd.dids);
1131   pool_freewhatprovides(pool);  /* as we have added provides */
1132   POOL_DEBUG(SAT_DEBUG_STATS, "addfileprovides took %d ms\n", sat_timems(now));
1133 }
1134
1135 void
1136 pool_addfileprovides(Pool *pool)
1137 {
1138   pool_addfileprovides_ids(pool, pool->installed, 0);
1139 }
1140
1141 void
1142 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)
1143 {
1144   if (p)
1145     {
1146       if (pool->solvables[p].repo)
1147         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1148       return;
1149     }
1150   /* FIXME: obey callback return value! */
1151   for (p = 1; p < pool->nsolvables; p++)
1152     if (pool->solvables[p].repo)
1153       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1154 }
1155
1156 void
1157 pool_clear_pos(Pool *pool)
1158 {
1159   memset(&pool->pos, 0, sizeof(pool->pos));
1160 }
1161
1162
1163 void
1164 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1165 {
1166   int i;
1167
1168   pool->languagecache = sat_free(pool->languagecache);
1169   pool->languagecacheother = 0;
1170   if (pool->nlanguages)
1171     {
1172       for (i = 0; i < pool->nlanguages; i++)
1173         free((char *)pool->languages[i]);
1174       free(pool->languages);
1175     }
1176   pool->nlanguages = nlanguages;
1177   if (!nlanguages)
1178     return;
1179   pool->languages = sat_calloc(nlanguages, sizeof(const char **));
1180   for (i = 0; i < pool->nlanguages; i++)
1181     pool->languages[i] = strdup(languages[i]);
1182 }
1183
1184 Id
1185 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1186 {
1187   const char *n;
1188   char buf[256], *p;
1189   int l;
1190
1191   if (!lang)
1192     return id;
1193   n = id2str(pool, id);
1194   l = strlen(n) + strlen(lang) + 2;
1195   if (l > sizeof(buf))
1196     p = sat_malloc(strlen(n) + strlen(lang) + 2);
1197   else
1198     p = buf;
1199   sprintf(p, "%s:%s", n, lang);
1200   id = str2id(pool, p, create);
1201   if (p != buf)
1202     free(p);
1203   return id;
1204 }
1205
1206 char *
1207 pool_alloctmpspace(Pool *pool, int len)
1208 {
1209   int n = pool->tmpspacen;
1210   if (!len)
1211     return 0;
1212   if (len > pool->tmpspacelen[n])
1213     {
1214       pool->tmpspacebuf[n] = sat_realloc(pool->tmpspacebuf[n], len + 32);
1215       pool->tmpspacelen[n] = len + 32;
1216     }
1217   pool->tmpspacen = (n + 1) % POOL_TMPSPACEBUF;
1218   return pool->tmpspacebuf[n];
1219 }
1220
1221 char *
1222 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1223 {
1224   int l1, l2, l3;
1225   char *s, *str;
1226   l1 = str1 ? strlen(str1) : 0;
1227   l2 = str2 ? strlen(str2) : 0;
1228   l3 = str3 ? strlen(str3) : 0;
1229   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1230   if (l1)
1231     {
1232       strcpy(s, str1);
1233       s += l1;
1234     }
1235   if (l2)
1236     {
1237       strcpy(s, str2);
1238       s += l2;
1239     }
1240   if (l3)
1241     {
1242       strcpy(s, str3);
1243       s += l3;
1244     }
1245   *s = 0;
1246   return str;
1247 }
1248
1249
1250 /*******************************************************************/
1251
1252 struct mptree {
1253   Id sibling;
1254   Id child;
1255   const char *comp;
1256   int compl;
1257   Id mountpoint;
1258 };
1259
1260 struct ducbdata {
1261   DUChanges *mps;
1262   struct mptree *mptree;
1263   int addsub;
1264   int hasdu;
1265
1266   Id *dirmap;
1267   int nmap;
1268   Repodata *olddata;
1269 };
1270
1271
1272 static int
1273 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1274 {
1275   struct ducbdata *cbd = cbdata;
1276   Id mp;
1277
1278   if (data != cbd->olddata)
1279     {
1280       Id dn, mp, comp, *dirmap, *dirs;
1281       int i, compl;
1282       const char *compstr;
1283       struct mptree *mptree;
1284
1285       /* create map from dir to mptree */
1286       cbd->dirmap = sat_free(cbd->dirmap);
1287       cbd->nmap = 0;
1288       dirmap = sat_calloc(data->dirpool.ndirs, sizeof(Id));
1289       mptree = cbd->mptree;
1290       mp = 0;
1291       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1292         {
1293           comp = *dirs++;
1294           if (comp <= 0)
1295             {
1296               mp = dirmap[-comp];
1297               continue;
1298             }
1299           if (mp < 0)
1300             {
1301               /* unconnected */
1302               dirmap[dn] = mp;
1303               continue;
1304             }
1305           if (!mptree[mp].child)
1306             {
1307               dirmap[dn] = -mp;
1308               continue;
1309             }
1310           if (data->localpool)
1311             compstr = stringpool_id2str(&data->spool, comp);
1312           else
1313             compstr = id2str(data->repo->pool, comp);
1314           compl = strlen(compstr);
1315           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1316             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1317               break;
1318           dirmap[dn] = i ? i : -mp;
1319         }
1320       /* change dirmap to point to mountpoint instead of mptree */
1321       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1322         {
1323           mp = dirmap[dn];
1324           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1325         }
1326       cbd->dirmap = dirmap;
1327       cbd->nmap = data->dirpool.ndirs;
1328       cbd->olddata = data;
1329     }
1330   cbd->hasdu = 1;
1331   if (value->id < 0 || value->id >= cbd->nmap)
1332     return 0;
1333   mp = cbd->dirmap[value->id];
1334   if (mp < 0)
1335     return 0;
1336   if (cbd->addsub > 0)
1337     {
1338       cbd->mps[mp].kbytes += value->num;
1339       cbd->mps[mp].files += value->num2;
1340     }
1341   else
1342     {
1343       cbd->mps[mp].kbytes -= value->num;
1344       cbd->mps[mp].files -= value->num2;
1345     }
1346   return 0;
1347 }
1348
1349 static void
1350 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1351 {
1352   int i;
1353   if (mptree[pos].mountpoint == -1)
1354     mptree[pos].mountpoint = mountpoint;
1355   else
1356     mountpoint = mptree[pos].mountpoint;
1357   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1358     propagate_mountpoints(mptree, i, mountpoint);
1359 }
1360
1361 #define MPTREE_BLOCK 15
1362
1363 void
1364 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1365 {
1366   char *p;
1367   const char *path, *compstr;
1368   struct mptree *mptree;
1369   int i, nmptree;
1370   int pos, compl;
1371   int mp;
1372   struct ducbdata cbd;
1373   Solvable *s;
1374   Id sp;
1375   Map ignoredu;
1376   Repo *oldinstalled = pool->installed;
1377
1378   memset(&ignoredu, 0, sizeof(ignoredu));
1379   cbd.mps = mps;
1380   cbd.addsub = 0;
1381   cbd.dirmap = 0;
1382   cbd.nmap = 0;
1383   cbd.olddata = 0;
1384
1385   mptree = sat_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1386
1387   /* our root node */
1388   mptree[0].sibling = 0;
1389   mptree[0].child = 0;
1390   mptree[0].comp = 0;
1391   mptree[0].compl = 0;
1392   mptree[0].mountpoint = -1;
1393   nmptree = 1;
1394   
1395   /* create component tree */
1396   for (mp = 0; mp < nmps; mp++)
1397     {
1398       mps[mp].kbytes = 0;
1399       mps[mp].files = 0;
1400       pos = 0;
1401       path = mps[mp].path;
1402       while(*path == '/')
1403         path++;
1404       while (*path)
1405         {
1406           if ((p = strchr(path, '/')) == 0)
1407             {
1408               compstr = path;
1409               compl = strlen(compstr);
1410               path += compl;
1411             }
1412           else
1413             {
1414               compstr = path;
1415               compl = p - path;
1416               path = p + 1;
1417               while(*path == '/')
1418                 path++;
1419             }
1420           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1421             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1422               break;
1423           if (!i)
1424             {
1425               /* create new node */
1426               mptree = sat_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1427               i = nmptree++;
1428               mptree[i].sibling = mptree[pos].child;
1429               mptree[i].child = 0;
1430               mptree[i].comp = compstr;
1431               mptree[i].compl = compl;
1432               mptree[i].mountpoint = -1;
1433               mptree[pos].child = i;
1434             }
1435           pos = i;
1436         }
1437       mptree[pos].mountpoint = mp;
1438     }
1439
1440   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1441
1442 #if 0
1443   for (i = 0; i < nmptree; i++)
1444     {
1445       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1446       printf("#%d child: %d\n", i, mptree[i].child);
1447       printf("#%d comp: %s\n", i, mptree[i].comp);
1448       printf("#%d compl: %d\n", i, mptree[i].compl);
1449       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1450     }
1451 #endif
1452
1453   cbd.mptree = mptree;
1454   cbd.addsub = 1;
1455   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1456     {
1457       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1458         continue;
1459       if (!MAPTST(installedmap, sp))
1460         continue;
1461       cbd.hasdu = 0;
1462       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1463       if (!cbd.hasdu && oldinstalled)
1464         {
1465           Id op, opp;
1466           /* no du data available, ignore data of all installed solvables we obsolete */
1467           if (!ignoredu.map)
1468             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1469           if (s->obsoletes)
1470             {
1471               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1472               while ((obs = *obsp++) != 0)
1473                 FOR_PROVIDES(op, opp, obs)
1474                   if (op >= oldinstalled->start && op < oldinstalled->end)
1475                     MAPSET(&ignoredu, op - oldinstalled->start);
1476             }
1477           FOR_PROVIDES(op, opp, s->name)
1478             if (pool->solvables[op].name == s->name)
1479               if (op >= oldinstalled->start && op < oldinstalled->end)
1480                 MAPSET(&ignoredu, op - oldinstalled->start);
1481         }
1482     }
1483   cbd.addsub = -1;
1484   if (oldinstalled)
1485     {
1486       /* assumes we allways have du data for installed solvables */
1487       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1488         {
1489           if (MAPTST(installedmap, sp))
1490             continue;
1491           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1492             continue;
1493           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1494         }
1495     }
1496   if (ignoredu.map)
1497     map_free(&ignoredu);
1498   sat_free(cbd.dirmap);
1499   sat_free(mptree);
1500 }
1501
1502 int
1503 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1504 {
1505   Id sp;
1506   Solvable *s;
1507   int change = 0;
1508   Repo *oldinstalled = pool->installed;
1509
1510   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1511     {
1512       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1513         continue;
1514       if (!MAPTST(installedmap, sp))
1515         continue;
1516       change += solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1517     }
1518   if (oldinstalled)
1519     {
1520       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1521         {
1522           if (MAPTST(installedmap, sp))
1523             continue;
1524           change -= solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1525         }
1526     }
1527   return change;
1528 }
1529
1530 /* map:
1531  *  1: installed
1532  *  2: conflicts with installed
1533  *  8: interesting (only true if installed)
1534  * 16: undecided
1535  */
1536  
1537 static inline Id dep2name(Pool *pool, Id dep)
1538 {
1539   while (ISRELDEP(dep))
1540     {
1541       Reldep *rd = rd = GETRELDEP(pool, dep);
1542       dep = rd->name;
1543     }
1544   return dep;
1545 }
1546
1547 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
1548 {
1549   Id p, pp;
1550   Solvable *sn = pool->solvables + n; 
1551
1552   FOR_PROVIDES(p, pp, sn->name)
1553     {    
1554       Solvable *s = pool->solvables + p; 
1555       if (s->name != sn->name || s->arch != sn->arch)
1556         continue;
1557       if ((map[p] & 9) != 9)
1558         continue;
1559       if (pool_match_nevr(pool, pool->solvables + p, con))
1560         continue;
1561       return 1;         /* found installed package that doesn't conflict */
1562     }
1563   return 0;
1564 }
1565
1566 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *noobsoletesmap)
1567 {
1568   Id p, pp;
1569   int r = 0;
1570   FOR_PROVIDES(p, pp, dep)
1571     {
1572       if (p == SYSTEMSOLVABLE)
1573         return 1;       /* always boring, as never constraining */
1574       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
1575         continue;
1576       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
1577         if (providedbyinstalled_multiversion(pool, map, p, dep))
1578           continue;
1579       if ((map[p] & 9) == 9)
1580         return 9;
1581       r |= map[p] & 17;
1582     }
1583   return r;
1584 }
1585
1586 /*
1587  * pool_trivial_installable - calculate if a set of solvables is
1588  * trivial installable without any other installs/deinstalls of
1589  * packages not belonging to the set.
1590  *
1591  * the state is returned in the result queue:
1592  * 1:  solvable is installable without any other package changes
1593  * 0:  solvable is not installable
1594  * -1: solvable is installable, but doesn't constrain any installed packages
1595  */
1596
1597 void
1598 pool_trivial_installable_noobsoletesmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *noobsoletesmap)
1599 {
1600   int i, r, m, did;
1601   Id p, *dp, con, *conp, req, *reqp;
1602   unsigned char *map;
1603   Solvable *s;
1604
1605   map = sat_calloc(pool->nsolvables, 1);
1606   for (p = 1; p < pool->nsolvables; p++)
1607     {
1608       if (!MAPTST(installedmap, p))
1609         continue;
1610       map[p] |= 9;
1611       s = pool->solvables + p;
1612       if (!s->conflicts)
1613         continue;
1614       conp = s->repo->idarraydata + s->conflicts;
1615       while ((con = *conp++) != 0)
1616         {
1617           dp = pool_whatprovides_ptr(pool, con);
1618           for (; *dp; dp++)
1619             map[p] |= 2;        /* XXX: self conflict ? */
1620         }
1621     }
1622   for (i = 0; i < pkgs->count; i++)
1623     map[pkgs->elements[i]] = 16;
1624
1625   for (i = 0, did = 0; did < pkgs->count; i++, did++)
1626     {
1627       if (i == pkgs->count)
1628         i = 0;
1629       p = pkgs->elements[i];
1630       if ((map[p] & 16) == 0)
1631         continue;
1632       if ((map[p] & 2) != 0)
1633         {
1634           map[p] = 2;
1635           continue;
1636         }
1637       s = pool->solvables + p;
1638       m = 1;
1639       if (s->requires)
1640         {
1641           reqp = s->repo->idarraydata + s->requires;
1642           while ((req = *reqp++) != 0)
1643             {
1644               if (req == SOLVABLE_PREREQMARKER)
1645                 continue;
1646               r = providedbyinstalled(pool, map, req, 0, 0);
1647               if (!r)
1648                 {
1649                   /* decided and miss */
1650                   map[p] = 2;
1651                   break;
1652                 }
1653               m |= r;   /* 1 | 9 | 16 | 17 */
1654             }
1655           if (req)
1656             continue;
1657           if ((m & 9) == 9)
1658             m = 9;
1659         }
1660       if (s->conflicts)
1661         {
1662           int ispatch = 0;      /* see solver.c patch handling */
1663
1664           if (!strncmp("patch:", id2str(pool, s->name), 6))
1665             ispatch = 1;
1666           conp = s->repo->idarraydata + s->conflicts;
1667           while ((con = *conp++) != 0)
1668             {
1669               if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1670                 {
1671                   map[p] = 2;
1672                   break;
1673                 }
1674               if ((m == 1 || m == 17) && ISRELDEP(con))
1675                 {
1676                   con = dep2name(pool, con);
1677                   if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1678                     m = 9;
1679                 }
1680             }
1681           if (con)
1682             continue;   /* found a conflict */
1683         }
1684 #if 0
1685       if (s->repo && s->repo != oldinstalled)
1686         {
1687           Id p2, obs, *obsp, *pp;
1688           Solvable *s2;
1689           if (s->obsoletes)
1690             {
1691               obsp = s->repo->idarraydata + s->obsoletes;
1692               while ((obs = *obsp++) != 0)
1693                 {
1694                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
1695                     {
1696                       map[p] = 2;
1697                       break;
1698                     }
1699                 }
1700               if (obs)
1701                 continue;
1702             }
1703           FOR_PROVIDES(p2, pp, s->name)
1704             {
1705               s2 = pool->solvables + p2;
1706               if (s2->name == s->name && (map[p2] & 1) != 0)
1707                 {
1708                   map[p] = 2;
1709                   break;
1710                 }
1711             }
1712           if (p2)
1713             continue;
1714         }
1715 #endif
1716       if (m != map[p])
1717         {
1718           map[p] = m;
1719           did = 0;
1720         }
1721     }
1722   queue_free(res);
1723   queue_init_clone(res, pkgs);
1724   for (i = 0; i < pkgs->count; i++)
1725     {
1726       m = map[pkgs->elements[i]];
1727       if ((m & 9) == 9)
1728         r = 1;
1729       else if (m & 1)
1730         r = -1;
1731       else
1732         r = 0;
1733       res->elements[i] = r;
1734     }
1735   free(map);
1736 }
1737
1738 void
1739 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
1740 {
1741   pool_trivial_installable_noobsoletesmap(pool, installedmap, pkgs, res, 0);
1742 }
1743
1744 const char *
1745 pool_lookup_str(Pool *pool, Id entry, Id keyname)
1746 {
1747   if (entry == SOLVID_POS && pool->pos.repo)
1748     return repodata_lookup_str(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1749   if (entry <= 0)
1750     return 0;
1751   return solvable_lookup_str(pool->solvables + entry, keyname);
1752 }
1753
1754 Id
1755 pool_lookup_id(Pool *pool, Id entry, Id keyname)
1756 {
1757   if (entry == SOLVID_POS && pool->pos.repo)
1758     {
1759       Repodata *data = pool->pos.repo->repodata + pool->pos.repodataid;
1760       Id id = repodata_lookup_id(data, SOLVID_POS, keyname);
1761       return data->localpool ? repodata_globalize_id(data, id, 1) : id;
1762     }
1763   if (entry <= 0)
1764     return 0;
1765   return solvable_lookup_id(pool->solvables + entry, keyname);
1766 }
1767
1768 unsigned int
1769 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned int notfound)
1770 {
1771   if (entry == SOLVID_POS && pool->pos.repo)
1772     {
1773       unsigned int value;
1774       if (repodata_lookup_num(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, &value))
1775         return value;
1776       return notfound;
1777     }
1778   if (entry <= 0)
1779     return notfound;
1780   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
1781 }
1782
1783 int
1784 pool_lookup_void(Pool *pool, Id entry, Id keyname)
1785 {
1786   if (entry == SOLVID_POS && pool->pos.repo)
1787     return repodata_lookup_void(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1788   if (entry <= 0)
1789     return 0;
1790   return solvable_lookup_void(pool->solvables + entry, keyname);
1791 }
1792
1793 const unsigned char *
1794 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1795 {
1796   if (entry == SOLVID_POS && pool->pos.repo)
1797     return repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1798   if (entry <= 0)
1799     return 0;
1800   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
1801 }
1802
1803 const char *
1804 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1805 {
1806   if (entry == SOLVID_POS && pool->pos.repo)
1807     {
1808       const unsigned char *chk = repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1809       return chk ? repodata_chk2str(pool->pos.repo->repodata + pool->pos.repodataid, *typep, chk) : 0;
1810     }
1811   if (entry <= 0)
1812     return 0;
1813   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
1814 }
1815
1816 void
1817 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
1818 {
1819   int hadhashes = pool->relhashtbl ? 1 : 0;
1820   Solvable *s;
1821   Id fn, p, q, md5;
1822   Id id;
1823   int i;
1824
1825   if (!conflicts->count)
1826     return;
1827   pool_freewhatprovides(pool);
1828   for (i = 0; i < conflicts->count; i += 5)
1829     {
1830       fn = conflicts->elements[i];
1831       p = conflicts->elements[i + 1];
1832       md5 = conflicts->elements[i + 2];
1833       q = conflicts->elements[i + 3];
1834       id = rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
1835       s = pool->solvables + p;
1836       if (!s->repo)
1837         continue;
1838       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
1839       s = pool->solvables + q;
1840       if (!s->repo)
1841         continue;
1842       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
1843     }
1844   if (!hadhashes)
1845     pool_freeidhashes(pool);
1846 }
1847
1848 /* EOF */