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