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