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