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