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