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