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