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