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