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