- speed up pool_addrelproviders a bit
[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 (!ISRELDEP(pid))
799                 {
800                   if (pid != name)
801                     continue;           /* wrong provides name */
802                   if (pool->disttype == DISTTYPE_DEB)
803                     continue;           /* unversioned provides can never match versioned deps */
804                   break;
805                 }
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, Queue *idqinst)
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   if (idqinst)
1171     queue_empty(idqinst);
1172   isfp = installed ? &isf : 0;
1173   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1174     {
1175       repo = s->repo;
1176       if (!repo)
1177         continue;
1178       if (s->obsoletes)
1179         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1180       if (s->conflicts)
1181         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1182       if (s->requires)
1183         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1184       if (s->recommends)
1185         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1186       if (s->suggests)
1187         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1188       if (s->supplements)
1189         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1190       if (s->enhances)
1191         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1192     }
1193   map_free(&sf.seen);
1194   map_free(&isf.seen);
1195   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1196   cbd.dids = 0;
1197   if (sf.nfiles)
1198     {
1199 #if 0
1200       for (i = 0; i < sf.nfiles; i++)
1201         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1202 #endif
1203       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1204       if (idq)
1205         for (i = 0; i < sf.nfiles; i++)
1206           queue_push(idq, sf.ids[i]);
1207       if (idqinst)
1208         for (i = 0; i < sf.nfiles; i++)
1209           queue_push(idqinst, sf.ids[i]);
1210       solv_free(sf.ids);
1211       for (i = 0; i < sf.nfiles; i++)
1212         {
1213           solv_free(sf.dirs[i]);
1214           solv_free(sf.names[i]);
1215         }
1216       solv_free(sf.dirs);
1217       solv_free(sf.names);
1218     }
1219   if (isf.nfiles)
1220     {
1221 #if 0
1222       for (i = 0; i < isf.nfiles; i++)
1223         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1224 #endif
1225       if (installed)
1226         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1227       if (installed && idqinst)
1228         for (i = 0; i < isf.nfiles; i++)
1229           queue_pushunique(idqinst, isf.ids[i]);
1230       solv_free(isf.ids);
1231       for (i = 0; i < isf.nfiles; i++)
1232         {
1233           solv_free(isf.dirs[i]);
1234           solv_free(isf.names[i]);
1235         }
1236       solv_free(isf.dirs);
1237       solv_free(isf.names);
1238     }
1239   solv_free(cbd.dids);
1240   pool_freewhatprovides(pool);  /* as we have added provides */
1241   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1242 }
1243
1244 void
1245 pool_addfileprovides(Pool *pool)
1246 {
1247   pool_addfileprovides_queue(pool, 0, 0);
1248 }
1249
1250 void
1251 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)
1252 {
1253   if (p)
1254     {
1255       if (pool->solvables[p].repo)
1256         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1257       return;
1258     }
1259   /* FIXME: obey callback return value! */
1260   for (p = 1; p < pool->nsolvables; p++)
1261     if (pool->solvables[p].repo)
1262       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1263 }
1264
1265 void
1266 pool_clear_pos(Pool *pool)
1267 {
1268   memset(&pool->pos, 0, sizeof(pool->pos));
1269 }
1270
1271
1272 void
1273 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1274 {
1275   int i;
1276
1277   pool->languagecache = solv_free(pool->languagecache);
1278   pool->languagecacheother = 0;
1279   if (pool->nlanguages)
1280     {
1281       for (i = 0; i < pool->nlanguages; i++)
1282         free((char *)pool->languages[i]);
1283       free(pool->languages);
1284     }
1285   pool->nlanguages = nlanguages;
1286   if (!nlanguages)
1287     return;
1288   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1289   for (i = 0; i < pool->nlanguages; i++)
1290     pool->languages[i] = solv_strdup(languages[i]);
1291 }
1292
1293 Id
1294 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1295 {
1296   const char *n;
1297   char buf[256], *p;
1298   int l;
1299
1300   if (!lang || !*lang)
1301     return id;
1302   n = pool_id2str(pool, id);
1303   l = strlen(n) + strlen(lang) + 2;
1304   if (l > sizeof(buf))
1305     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1306   else
1307     p = buf;
1308   sprintf(p, "%s:%s", n, lang);
1309   id = pool_str2id(pool, p, create);
1310   if (p != buf)
1311     free(p);
1312   return id;
1313 }
1314
1315 char *
1316 pool_alloctmpspace(Pool *pool, int len)
1317 {
1318   int n = pool->tmpspace.n;
1319   if (!len)
1320     return 0;
1321   if (len > pool->tmpspace.len[n])
1322     {
1323       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1324       pool->tmpspace.len[n] = len + 32;
1325     }
1326   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1327   return pool->tmpspace.buf[n];
1328 }
1329
1330 static char *
1331 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1332 {
1333   if (space)
1334     {
1335       int n, oldn;
1336       n = oldn = pool->tmpspace.n;
1337       for (;;)
1338         {
1339           if (!n--)
1340             n = POOL_TMPSPACEBUF - 1;
1341           if (n == oldn)
1342             break;
1343           if (pool->tmpspace.buf[n] != space)
1344             continue;
1345           if (len > pool->tmpspace.len[n])
1346             {
1347               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1348               pool->tmpspace.len[n] = len + 32;
1349             }
1350           return pool->tmpspace.buf[n];
1351         }
1352     }
1353   return 0;
1354 }
1355
1356 void
1357 pool_freetmpspace(Pool *pool, const char *space)
1358 {
1359   int n = pool->tmpspace.n;
1360   if (!space)
1361     return;
1362   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1363   if (pool->tmpspace.buf[n] == space)
1364     pool->tmpspace.n = n;
1365 }
1366
1367 char *
1368 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1369 {
1370   int l1, l2, l3;
1371   char *s, *str;
1372   l1 = str1 ? strlen(str1) : 0;
1373   l2 = str2 ? strlen(str2) : 0;
1374   l3 = str3 ? strlen(str3) : 0;
1375   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1376   if (l1)
1377     {
1378       strcpy(s, str1);
1379       s += l1;
1380     }
1381   if (l2)
1382     {
1383       strcpy(s, str2);
1384       s += l2;
1385     }
1386   if (l3)
1387     {
1388       strcpy(s, str3);
1389       s += l3;
1390     }
1391   *s = 0;
1392   return str;
1393 }
1394
1395 char *
1396 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1397 {
1398   int l1, l2, l3;
1399   char *s, *str;
1400
1401   l1 = str1 ? strlen(str1) : 0;
1402   l2 = str2 ? strlen(str2) : 0;
1403   l3 = str3 ? strlen(str3) : 0;
1404   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1405   if (str)
1406     str1 = str;
1407   else
1408     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1409   s = str;
1410   if (l1)
1411     {
1412       if (s != str1)
1413         strcpy(s, str1);
1414       s += l1;
1415     }
1416   if (l2)
1417     {
1418       strcpy(s, str2);
1419       s += l2;
1420     }
1421   if (l3)
1422     {
1423       strcpy(s, str3);
1424       s += l3;
1425     }
1426   *s = 0;
1427   return str;
1428 }
1429
1430 const char *
1431 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1432 {
1433   char *s;
1434   if (!len)
1435     return "";
1436   s = pool_alloctmpspace(pool, 2 * len + 1);
1437   solv_bin2hex(buf, len, s);
1438   return s;
1439 }
1440
1441 /*******************************************************************/
1442
1443 struct mptree {
1444   Id sibling;
1445   Id child;
1446   const char *comp;
1447   int compl;
1448   Id mountpoint;
1449 };
1450
1451 struct ducbdata {
1452   DUChanges *mps;
1453   struct mptree *mptree;
1454   int addsub;
1455   int hasdu;
1456
1457   Id *dirmap;
1458   int nmap;
1459   Repodata *olddata;
1460 };
1461
1462
1463 static int
1464 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1465 {
1466   struct ducbdata *cbd = cbdata;
1467   Id mp;
1468
1469   if (data != cbd->olddata)
1470     {
1471       Id dn, mp, comp, *dirmap, *dirs;
1472       int i, compl;
1473       const char *compstr;
1474       struct mptree *mptree;
1475
1476       /* create map from dir to mptree */
1477       cbd->dirmap = solv_free(cbd->dirmap);
1478       cbd->nmap = 0;
1479       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1480       mptree = cbd->mptree;
1481       mp = 0;
1482       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1483         {
1484           comp = *dirs++;
1485           if (comp <= 0)
1486             {
1487               mp = dirmap[-comp];
1488               continue;
1489             }
1490           if (mp < 0)
1491             {
1492               /* unconnected */
1493               dirmap[dn] = mp;
1494               continue;
1495             }
1496           if (!mptree[mp].child)
1497             {
1498               dirmap[dn] = -mp;
1499               continue;
1500             }
1501           if (data->localpool)
1502             compstr = stringpool_id2str(&data->spool, comp);
1503           else
1504             compstr = pool_id2str(data->repo->pool, comp);
1505           compl = strlen(compstr);
1506           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1507             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1508               break;
1509           dirmap[dn] = i ? i : -mp;
1510         }
1511       /* change dirmap to point to mountpoint instead of mptree */
1512       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1513         {
1514           mp = dirmap[dn];
1515           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1516         }
1517       cbd->dirmap = dirmap;
1518       cbd->nmap = data->dirpool.ndirs;
1519       cbd->olddata = data;
1520     }
1521   cbd->hasdu = 1;
1522   if (value->id < 0 || value->id >= cbd->nmap)
1523     return 0;
1524   mp = cbd->dirmap[value->id];
1525   if (mp < 0)
1526     return 0;
1527   if (cbd->addsub > 0)
1528     {
1529       cbd->mps[mp].kbytes += value->num;
1530       cbd->mps[mp].files += value->num2;
1531     }
1532   else
1533     {
1534       cbd->mps[mp].kbytes -= value->num;
1535       cbd->mps[mp].files -= value->num2;
1536     }
1537   return 0;
1538 }
1539
1540 static void
1541 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1542 {
1543   int i;
1544   if (mptree[pos].mountpoint == -1)
1545     mptree[pos].mountpoint = mountpoint;
1546   else
1547     mountpoint = mptree[pos].mountpoint;
1548   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1549     propagate_mountpoints(mptree, i, mountpoint);
1550 }
1551
1552 #define MPTREE_BLOCK 15
1553
1554 void
1555 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
1556 {
1557   char *p;
1558   const char *path, *compstr;
1559   struct mptree *mptree;
1560   int i, nmptree;
1561   int pos, compl;
1562   int mp;
1563   struct ducbdata cbd;
1564   Solvable *s;
1565   Id sp;
1566   Map ignoredu;
1567   Repo *oldinstalled = pool->installed;
1568
1569   memset(&ignoredu, 0, sizeof(ignoredu));
1570   cbd.mps = mps;
1571   cbd.addsub = 0;
1572   cbd.dirmap = 0;
1573   cbd.nmap = 0;
1574   cbd.olddata = 0;
1575
1576   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1577
1578   /* our root node */
1579   mptree[0].sibling = 0;
1580   mptree[0].child = 0;
1581   mptree[0].comp = 0;
1582   mptree[0].compl = 0;
1583   mptree[0].mountpoint = -1;
1584   nmptree = 1;
1585   
1586   /* create component tree */
1587   for (mp = 0; mp < nmps; mp++)
1588     {
1589       mps[mp].kbytes = 0;
1590       mps[mp].files = 0;
1591       pos = 0;
1592       path = mps[mp].path;
1593       while(*path == '/')
1594         path++;
1595       while (*path)
1596         {
1597           if ((p = strchr(path, '/')) == 0)
1598             {
1599               compstr = path;
1600               compl = strlen(compstr);
1601               path += compl;
1602             }
1603           else
1604             {
1605               compstr = path;
1606               compl = p - path;
1607               path = p + 1;
1608               while(*path == '/')
1609                 path++;
1610             }
1611           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1612             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1613               break;
1614           if (!i)
1615             {
1616               /* create new node */
1617               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1618               i = nmptree++;
1619               mptree[i].sibling = mptree[pos].child;
1620               mptree[i].child = 0;
1621               mptree[i].comp = compstr;
1622               mptree[i].compl = compl;
1623               mptree[i].mountpoint = -1;
1624               mptree[pos].child = i;
1625             }
1626           pos = i;
1627         }
1628       mptree[pos].mountpoint = mp;
1629     }
1630
1631   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1632
1633 #if 0
1634   for (i = 0; i < nmptree; i++)
1635     {
1636       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1637       printf("#%d child: %d\n", i, mptree[i].child);
1638       printf("#%d comp: %s\n", i, mptree[i].comp);
1639       printf("#%d compl: %d\n", i, mptree[i].compl);
1640       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1641     }
1642 #endif
1643
1644   cbd.mptree = mptree;
1645   cbd.addsub = 1;
1646   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1647     {
1648       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1649         continue;
1650       if (!MAPTST(installedmap, sp))
1651         continue;
1652       cbd.hasdu = 0;
1653       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1654       if (!cbd.hasdu && oldinstalled)
1655         {
1656           Id op, opp;
1657           /* no du data available, ignore data of all installed solvables we obsolete */
1658           if (!ignoredu.map)
1659             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1660           if (s->obsoletes)
1661             {
1662               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1663               while ((obs = *obsp++) != 0)
1664                 FOR_PROVIDES(op, opp, obs)
1665                   if (op >= oldinstalled->start && op < oldinstalled->end)
1666                     MAPSET(&ignoredu, op - oldinstalled->start);
1667             }
1668           FOR_PROVIDES(op, opp, s->name)
1669             if (pool->solvables[op].name == s->name)
1670               if (op >= oldinstalled->start && op < oldinstalled->end)
1671                 MAPSET(&ignoredu, op - oldinstalled->start);
1672         }
1673     }
1674   cbd.addsub = -1;
1675   if (oldinstalled)
1676     {
1677       /* assumes we allways have du data for installed solvables */
1678       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1679         {
1680           if (MAPTST(installedmap, sp))
1681             continue;
1682           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1683             continue;
1684           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1685         }
1686     }
1687   if (ignoredu.map)
1688     map_free(&ignoredu);
1689   solv_free(cbd.dirmap);
1690   solv_free(mptree);
1691 }
1692
1693 int
1694 pool_calc_installsizechange(Pool *pool, Map *installedmap)
1695 {
1696   Id sp;
1697   Solvable *s;
1698   int change = 0;
1699   Repo *oldinstalled = pool->installed;
1700
1701   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1702     {
1703       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1704         continue;
1705       if (!MAPTST(installedmap, sp))
1706         continue;
1707       change += solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1708     }
1709   if (oldinstalled)
1710     {
1711       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1712         {
1713           if (MAPTST(installedmap, sp))
1714             continue;
1715           change -= solvable_lookup_num(s, SOLVABLE_INSTALLSIZE, 0);
1716         }
1717     }
1718   return change;
1719 }
1720
1721 /* map:
1722  *  1: installed
1723  *  2: conflicts with installed
1724  *  8: interesting (only true if installed)
1725  * 16: undecided
1726  */
1727  
1728 static inline Id dep2name(Pool *pool, Id dep)
1729 {
1730   while (ISRELDEP(dep))
1731     {
1732       Reldep *rd = rd = GETRELDEP(pool, dep);
1733       dep = rd->name;
1734     }
1735   return dep;
1736 }
1737
1738 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con) 
1739 {
1740   Id p, pp;
1741   Solvable *sn = pool->solvables + n; 
1742
1743   FOR_PROVIDES(p, pp, sn->name)
1744     {    
1745       Solvable *s = pool->solvables + p; 
1746       if (s->name != sn->name || s->arch != sn->arch)
1747         continue;
1748       if ((map[p] & 9) != 9)
1749         continue;
1750       if (pool_match_nevr(pool, pool->solvables + p, con))
1751         continue;
1752       return 1;         /* found installed package that doesn't conflict */
1753     }
1754   return 0;
1755 }
1756
1757 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *noobsoletesmap)
1758 {
1759   Id p, pp;
1760   int r = 0;
1761   FOR_PROVIDES(p, pp, dep)
1762     {
1763       if (p == SYSTEMSOLVABLE)
1764         return 1;       /* always boring, as never constraining */
1765       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
1766         continue;
1767       if (ispatch && noobsoletesmap && noobsoletesmap->size && MAPTST(noobsoletesmap, p) && ISRELDEP(dep))
1768         if (providedbyinstalled_multiversion(pool, map, p, dep))
1769           continue;
1770       if ((map[p] & 9) == 9)
1771         return 9;
1772       r |= map[p] & 17;
1773     }
1774   return r;
1775 }
1776
1777 /*
1778  * pool_trivial_installable - calculate if a set of solvables is
1779  * trivial installable without any other installs/deinstalls of
1780  * packages not belonging to the set.
1781  *
1782  * the state is returned in the result queue:
1783  * 1:  solvable is installable without any other package changes
1784  * 0:  solvable is not installable
1785  * -1: solvable is installable, but doesn't constrain any installed packages
1786  */
1787
1788 void
1789 pool_trivial_installable_noobsoletesmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *noobsoletesmap)
1790 {
1791   int i, r, m, did;
1792   Id p, *dp, con, *conp, req, *reqp;
1793   unsigned char *map;
1794   Solvable *s;
1795
1796   map = solv_calloc(pool->nsolvables, 1);
1797   for (p = 1; p < pool->nsolvables; p++)
1798     {
1799       if (!MAPTST(installedmap, p))
1800         continue;
1801       map[p] |= 9;
1802       s = pool->solvables + p;
1803       if (!s->conflicts)
1804         continue;
1805       conp = s->repo->idarraydata + s->conflicts;
1806       while ((con = *conp++) != 0)
1807         {
1808           dp = pool_whatprovides_ptr(pool, con);
1809           for (; *dp; dp++)
1810             map[p] |= 2;        /* XXX: self conflict ? */
1811         }
1812     }
1813   for (i = 0; i < pkgs->count; i++)
1814     map[pkgs->elements[i]] = 16;
1815
1816   for (i = 0, did = 0; did < pkgs->count; i++, did++)
1817     {
1818       if (i == pkgs->count)
1819         i = 0;
1820       p = pkgs->elements[i];
1821       if ((map[p] & 16) == 0)
1822         continue;
1823       if ((map[p] & 2) != 0)
1824         {
1825           map[p] = 2;
1826           continue;
1827         }
1828       s = pool->solvables + p;
1829       m = 1;
1830       if (s->requires)
1831         {
1832           reqp = s->repo->idarraydata + s->requires;
1833           while ((req = *reqp++) != 0)
1834             {
1835               if (req == SOLVABLE_PREREQMARKER)
1836                 continue;
1837               r = providedbyinstalled(pool, map, req, 0, 0);
1838               if (!r)
1839                 {
1840                   /* decided and miss */
1841                   map[p] = 2;
1842                   break;
1843                 }
1844               m |= r;   /* 1 | 9 | 16 | 17 */
1845             }
1846           if (req)
1847             continue;
1848           if ((m & 9) == 9)
1849             m = 9;
1850         }
1851       if (s->conflicts)
1852         {
1853           int ispatch = 0;      /* see solver.c patch handling */
1854
1855           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
1856             ispatch = 1;
1857           conp = s->repo->idarraydata + s->conflicts;
1858           while ((con = *conp++) != 0)
1859             {
1860               if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1861                 {
1862                   map[p] = 2;
1863                   break;
1864                 }
1865               if ((m == 1 || m == 17) && ISRELDEP(con))
1866                 {
1867                   con = dep2name(pool, con);
1868                   if ((providedbyinstalled(pool, map, con, ispatch, noobsoletesmap) & 1) != 0)
1869                     m = 9;
1870                 }
1871             }
1872           if (con)
1873             continue;   /* found a conflict */
1874         }
1875 #if 0
1876       if (s->repo && s->repo != oldinstalled)
1877         {
1878           Id p2, obs, *obsp, *pp;
1879           Solvable *s2;
1880           if (s->obsoletes)
1881             {
1882               obsp = s->repo->idarraydata + s->obsoletes;
1883               while ((obs = *obsp++) != 0)
1884                 {
1885                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
1886                     {
1887                       map[p] = 2;
1888                       break;
1889                     }
1890                 }
1891               if (obs)
1892                 continue;
1893             }
1894           FOR_PROVIDES(p2, pp, s->name)
1895             {
1896               s2 = pool->solvables + p2;
1897               if (s2->name == s->name && (map[p2] & 1) != 0)
1898                 {
1899                   map[p] = 2;
1900                   break;
1901                 }
1902             }
1903           if (p2)
1904             continue;
1905         }
1906 #endif
1907       if (m != map[p])
1908         {
1909           map[p] = m;
1910           did = 0;
1911         }
1912     }
1913   queue_free(res);
1914   queue_init_clone(res, pkgs);
1915   for (i = 0; i < pkgs->count; i++)
1916     {
1917       m = map[pkgs->elements[i]];
1918       if ((m & 9) == 9)
1919         r = 1;
1920       else if (m & 1)
1921         r = -1;
1922       else
1923         r = 0;
1924       res->elements[i] = r;
1925     }
1926   free(map);
1927 }
1928
1929 void
1930 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
1931 {
1932   pool_trivial_installable_noobsoletesmap(pool, installedmap, pkgs, res, 0);
1933 }
1934
1935 const char *
1936 pool_lookup_str(Pool *pool, Id entry, Id keyname)
1937 {
1938   if (entry == SOLVID_POS && pool->pos.repo)
1939     return repodata_lookup_str(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1940   if (entry <= 0)
1941     return 0;
1942   return solvable_lookup_str(pool->solvables + entry, keyname);
1943 }
1944
1945 Id
1946 pool_lookup_id(Pool *pool, Id entry, Id keyname)
1947 {
1948   if (entry == SOLVID_POS && pool->pos.repo)
1949     {
1950       Repodata *data = pool->pos.repo->repodata + pool->pos.repodataid;
1951       Id id = repodata_lookup_id(data, SOLVID_POS, keyname);
1952       return data->localpool ? repodata_globalize_id(data, id, 1) : id;
1953     }
1954   if (entry <= 0)
1955     return 0;
1956   return solvable_lookup_id(pool->solvables + entry, keyname);
1957 }
1958
1959 unsigned int
1960 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned int notfound)
1961 {
1962   if (entry == SOLVID_POS && pool->pos.repo)
1963     {
1964       unsigned int value;
1965       if (repodata_lookup_num(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, &value))
1966         return value;
1967       return notfound;
1968     }
1969   if (entry <= 0)
1970     return notfound;
1971   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
1972 }
1973
1974 int
1975 pool_lookup_void(Pool *pool, Id entry, Id keyname)
1976 {
1977   if (entry == SOLVID_POS && pool->pos.repo)
1978     return repodata_lookup_void(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname);
1979   if (entry <= 0)
1980     return 0;
1981   return solvable_lookup_void(pool->solvables + entry, keyname);
1982 }
1983
1984 const unsigned char *
1985 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1986 {
1987   if (entry == SOLVID_POS && pool->pos.repo)
1988     return repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
1989   if (entry <= 0)
1990     return 0;
1991   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
1992 }
1993
1994 const char *
1995 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
1996 {
1997   if (entry == SOLVID_POS && pool->pos.repo)
1998     {
1999       const unsigned char *chk = repodata_lookup_bin_checksum(pool->pos.repo->repodata + pool->pos.repodataid, SOLVID_POS, keyname, typep);
2000       return chk ? repodata_chk2str(pool->pos.repo->repodata + pool->pos.repodataid, *typep, chk) : 0;
2001     }
2002   if (entry <= 0)
2003     return 0;
2004   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2005 }
2006
2007 void
2008 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2009 {
2010   int hadhashes = pool->relhashtbl ? 1 : 0;
2011   Solvable *s;
2012   Id fn, p, q, md5;
2013   Id id;
2014   int i;
2015
2016   if (!conflicts->count)
2017     return;
2018   pool_freewhatprovides(pool);
2019   for (i = 0; i < conflicts->count; i += 5)
2020     {
2021       fn = conflicts->elements[i];
2022       p = conflicts->elements[i + 1];
2023       md5 = conflicts->elements[i + 2];
2024       q = conflicts->elements[i + 3];
2025       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2026       s = pool->solvables + p;
2027       if (!s->repo)
2028         continue;
2029       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2030       s = pool->solvables + q;
2031       if (!s->repo)
2032         continue;
2033       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2034     }
2035   if (!hadhashes)
2036     pool_freeidhashes(pool);
2037 }
2038
2039 /* EOF */