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