add a single space
[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 "poolvendor.h"
23 #include "repo.h"
24 #include "poolid.h"
25 #include "poolid_private.h"
26 #include "poolarch.h"
27 #include "util.h"
28 #include "bitmap.h"
29 #include "evr.h"
30
31 #define SOLVABLE_BLOCK  255
32
33 #undef LIBSOLV_KNOWNID_H
34 #define KNOWNID_INITIALIZE
35 #include "knownid.h"
36 #undef KNOWNID_INITIALIZE
37
38 /* create pool */
39 Pool *
40 pool_create(void)
41 {
42   Pool *pool;
43   Solvable *s;
44
45   pool = (Pool *)solv_calloc(1, sizeof(*pool));
46
47   stringpool_init (&pool->ss, initpool_data);
48
49   /* alloc space for RelDep 0 */
50   pool->rels = solv_extend_resize(0, 1, sizeof(Reldep), REL_BLOCK);
51   pool->nrels = 1;
52   memset(pool->rels, 0, sizeof(Reldep));
53
54   /* alloc space for Solvable 0 and system solvable */
55   pool->solvables = solv_extend_resize(0, 2, sizeof(Solvable), SOLVABLE_BLOCK);
56   pool->nsolvables = 2;
57   memset(pool->solvables, 0, 2 * sizeof(Solvable));
58
59   queue_init(&pool->vendormap);
60   queue_init(&pool->pooljobs);
61   queue_init(&pool->lazywhatprovidesq);
62
63 #if defined(DEBIAN)
64   pool->disttype = DISTTYPE_DEB;
65   pool->noarchid = ARCH_ALL;
66 #elif defined(ARCHLINUX)
67   pool->disttype = DISTTYPE_ARCH;
68   pool->noarchid = ARCH_ANY;
69 #elif defined(HAIKU)
70   pool->disttype = DISTTYPE_HAIKU;
71   pool->noarchid = ARCH_ANY;
72   pool->obsoleteusesprovides = 1;
73 #else
74   pool->disttype = DISTTYPE_RPM;
75   pool->noarchid = ARCH_NOARCH;
76 #endif
77
78   /* initialize the system solvable */
79   s = pool->solvables + SYSTEMSOLVABLE;
80   s->name = SYSTEM_SYSTEM;
81   s->arch = pool->noarchid;
82   s->evr = ID_EMPTY;
83
84   pool->debugmask = SOLV_DEBUG_RESULT;  /* FIXME */
85 #ifdef FEDORA
86   pool->implicitobsoleteusescolors = 1;
87 #endif
88 #ifdef RPM5
89   pool->noobsoletesmultiversion = 1;
90   pool->forbidselfconflicts = 1;
91   pool->obsoleteusesprovides = 1;
92   pool->implicitobsoleteusesprovides = 1;
93   pool->havedistepoch = 1;
94 #endif
95   return pool;
96 }
97
98
99 /* free all the resources of our pool */
100 void
101 pool_free(Pool *pool)
102 {
103   int i;
104
105   pool_freewhatprovides(pool);
106   pool_freeidhashes(pool);
107   pool_freeallrepos(pool, 1);
108   solv_free(pool->id2arch);
109   solv_free(pool->id2color);
110   solv_free(pool->solvables);
111   stringpool_free(&pool->ss);
112   solv_free(pool->rels);
113   pool_setvendorclasses(pool, 0);
114   queue_free(&pool->vendormap);
115   queue_free(&pool->pooljobs);
116   queue_free(&pool->lazywhatprovidesq);
117   for (i = 0; i < POOL_TMPSPACEBUF; i++)
118     solv_free(pool->tmpspace.buf[i]);
119   for (i = 0; i < pool->nlanguages; i++)
120     free((char *)pool->languages[i]);
121   solv_free(pool->languages);
122   solv_free(pool->languagecache);
123   solv_free(pool->errstr);
124   solv_free(pool->rootdir);
125   solv_free(pool);
126 }
127
128 void
129 pool_freeallrepos(Pool *pool, int reuseids)
130 {
131   int i;
132
133   pool_freewhatprovides(pool);
134   for (i = 1; i < pool->nrepos; i++)
135     if (pool->repos[i])
136       repo_freedata(pool->repos[i]);
137   pool->repos = solv_free(pool->repos);
138   pool->nrepos = 0;
139   pool->urepos = 0;
140   /* the first two solvables don't belong to a repo */
141   pool_free_solvable_block(pool, 2, pool->nsolvables - 2, reuseids);
142 }
143
144 #ifdef MULTI_SEMANTICS
145 void
146 pool_setdisttype(Pool *pool, int disttype)
147 {
148   pool->disttype = disttype;
149   if (disttype == DISTTYPE_RPM)
150     pool->noarchid = ARCH_NOARCH;
151   if (disttype == DISTTYPE_DEB)
152     pool->noarchid = ARCH_ALL;
153   if (disttype == DISTTYPE_ARCH)
154     pool->noarchid = ARCH_ANY;
155   if (disttype == DISTTYPE_HAIKU)
156     pool->noarchid = ARCH_ANY;
157   pool->solvables[SYSTEMSOLVABLE].arch = pool->noarchid;
158 }
159 #endif
160
161 int
162 pool_get_flag(Pool *pool, int flag)
163 {
164   switch (flag)
165     {
166     case POOL_FLAG_PROMOTEEPOCH:
167       return pool->promoteepoch;
168     case POOL_FLAG_FORBIDSELFCONFLICTS:
169       return pool->forbidselfconflicts;
170     case POOL_FLAG_OBSOLETEUSESPROVIDES:
171       return pool->obsoleteusesprovides;
172     case POOL_FLAG_IMPLICITOBSOLETEUSESPROVIDES:
173       return pool->implicitobsoleteusesprovides;
174     case POOL_FLAG_OBSOLETEUSESCOLORS:
175       return pool->obsoleteusescolors;
176     case POOL_FLAG_IMPLICITOBSOLETEUSESCOLORS:
177       return pool->implicitobsoleteusescolors;
178     case POOL_FLAG_NOINSTALLEDOBSOLETES:
179       return pool->noinstalledobsoletes;
180     case POOL_FLAG_HAVEDISTEPOCH:
181       return pool->havedistepoch;
182     case POOL_FLAG_NOOBSOLETESMULTIVERSION:
183       return pool->noobsoletesmultiversion;
184     case POOL_FLAG_ADDFILEPROVIDESFILTERED:
185       return pool->addfileprovidesfiltered;
186     default:
187       break;
188     }
189   return -1;
190 }
191
192 int
193 pool_set_flag(Pool *pool, int flag, int value)
194 {
195   int old = pool_get_flag(pool, flag);
196   switch (flag)
197     {
198     case POOL_FLAG_PROMOTEEPOCH:
199       pool->promoteepoch = value;
200       break;
201     case POOL_FLAG_FORBIDSELFCONFLICTS:
202       pool->forbidselfconflicts = value;
203       break;
204     case POOL_FLAG_OBSOLETEUSESPROVIDES:
205       pool->obsoleteusesprovides = value;
206       break;
207     case POOL_FLAG_IMPLICITOBSOLETEUSESPROVIDES:
208       pool->implicitobsoleteusesprovides = value;
209       break;
210     case POOL_FLAG_OBSOLETEUSESCOLORS:
211       pool->obsoleteusescolors = value;
212       break;
213     case POOL_FLAG_IMPLICITOBSOLETEUSESCOLORS:
214       pool->implicitobsoleteusescolors = value;
215       break;
216     case POOL_FLAG_NOINSTALLEDOBSOLETES:
217       pool->noinstalledobsoletes = value;
218       break;
219     case POOL_FLAG_HAVEDISTEPOCH:
220       pool->havedistepoch = value;
221       break;
222     case POOL_FLAG_NOOBSOLETESMULTIVERSION:
223       pool->noobsoletesmultiversion = value;
224       break;
225     case POOL_FLAG_ADDFILEPROVIDESFILTERED:
226       pool->addfileprovidesfiltered = value;
227       break;
228     default:
229       break;
230     }
231   return old;
232 }
233
234
235 Id
236 pool_add_solvable(Pool *pool)
237 {
238   pool->solvables = solv_extend(pool->solvables, pool->nsolvables, 1, sizeof(Solvable), SOLVABLE_BLOCK);
239   memset(pool->solvables + pool->nsolvables, 0, sizeof(Solvable));
240   return pool->nsolvables++;
241 }
242
243 Id
244 pool_add_solvable_block(Pool *pool, int count)
245 {
246   Id nsolvables = pool->nsolvables;
247   if (!count)
248     return nsolvables;
249   pool->solvables = solv_extend(pool->solvables, pool->nsolvables, count, sizeof(Solvable), SOLVABLE_BLOCK);
250   memset(pool->solvables + nsolvables, 0, sizeof(Solvable) * count);
251   pool->nsolvables += count;
252   return nsolvables;
253 }
254
255 void
256 pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids)
257 {
258   if (!count)
259     return;
260   if (reuseids && start + count == pool->nsolvables)
261     {
262       /* might want to shrink solvable array */
263       pool->nsolvables = start;
264       return;
265     }
266   memset(pool->solvables + start, 0, sizeof(Solvable) * count);
267 }
268
269
270 void
271 pool_set_installed(Pool *pool, Repo *installed)
272 {
273   if (pool->installed == installed)
274     return;
275   pool->installed = installed;
276   pool_freewhatprovides(pool);
277 }
278
279 static int
280 pool_shrink_whatprovides_sortcmp(const void *ap, const void *bp, void *dp)
281 {
282   int r;
283   Pool *pool = dp;
284   Id oa, ob, *da, *db;
285   oa = pool->whatprovides[*(Id *)ap];
286   ob = pool->whatprovides[*(Id *)bp];
287   if (oa == ob)
288     return *(Id *)ap - *(Id *)bp;
289   da = pool->whatprovidesdata + oa;
290   db = pool->whatprovidesdata + ob;
291   while (*db)
292     if ((r = (*da++ - *db++)) != 0)
293       return r;
294   if (*da)
295     return *da;
296   return *(Id *)ap - *(Id *)bp;
297 }
298
299 /*
300  * pool_shrink_whatprovides  - unify whatprovides data
301  *
302  * whatprovides_rel must be empty for this to work!
303  *
304  */
305 static void
306 pool_shrink_whatprovides(Pool *pool)
307 {
308   Id i, n, id;
309   Id *sorted;
310   Id lastid, *last, *dp, *lp;
311   Offset o;
312   int r;
313
314   if (pool->ss.nstrings < 3)
315     return;
316   sorted = solv_malloc2(pool->ss.nstrings, sizeof(Id));
317   for (i = id = 0; id < pool->ss.nstrings; id++)
318     if (pool->whatprovides[id] >= 4)
319       sorted[i++] = id;
320   n = i;
321   solv_sort(sorted, n, sizeof(Id), pool_shrink_whatprovides_sortcmp, pool);
322   last = 0;
323   lastid = 0;
324   for (i = 0; i < n; i++)
325     {
326       id = sorted[i];
327       o = pool->whatprovides[id];
328       dp = pool->whatprovidesdata + o;
329       if (last)
330         {
331           lp = last;
332           while (*dp)   
333             if (*dp++ != *lp++)
334               {
335                 last = 0;
336                 break;
337               }
338           if (last && *lp)
339             last = 0;
340           if (last)
341             {
342               pool->whatprovides[id] = -lastid;
343               continue;
344             }
345         }
346       last = pool->whatprovidesdata + o;
347       lastid = id;
348     }
349   solv_free(sorted);
350   dp = pool->whatprovidesdata + 4;
351   for (id = 1; id < pool->ss.nstrings; id++)
352     {
353       o = pool->whatprovides[id];
354       if (!o)
355         continue;
356       if ((Id)o < 0)
357         {
358           i = -(Id)o;
359           if (i >= id)
360             abort();
361           pool->whatprovides[id] = pool->whatprovides[i];
362           continue;
363         }
364       if (o < 4)
365         continue;
366       lp = pool->whatprovidesdata + o;
367       if (lp < dp)
368         abort();
369       pool->whatprovides[id] = dp - pool->whatprovidesdata;
370       while ((*dp++ = *lp++) != 0)
371         ;
372     }
373   o = dp - pool->whatprovidesdata;
374   POOL_DEBUG(SOLV_DEBUG_STATS, "shrunk whatprovidesdata from %d to %d\n", pool->whatprovidesdataoff, o);
375   if (pool->whatprovidesdataoff == o)
376     return;
377   r = pool->whatprovidesdataoff - o;
378   pool->whatprovidesdataoff = o;
379   pool->whatprovidesdata = solv_realloc(pool->whatprovidesdata, (o + pool->whatprovidesdataleft) * sizeof(Id));
380   if (r > pool->whatprovidesdataleft)
381     r = pool->whatprovidesdataleft;
382   memset(pool->whatprovidesdata + o, 0, r * sizeof(Id));
383 }
384
385
386 /*
387  * pool_createwhatprovides()
388  *
389  * create hashes over pool of solvables to ease provide lookups
390  *
391  */
392 void
393 pool_createwhatprovides(Pool *pool)
394 {
395   int i, num, np, extra;
396   Offset off;
397   Solvable *s;
398   Id id;
399   Offset *idp, n;
400   Offset *whatprovides;
401   Id *whatprovidesdata, *d;
402   Repo *installed = pool->installed;
403   unsigned int now;
404
405   now = solv_timems(0);
406   POOL_DEBUG(SOLV_DEBUG_STATS, "number of solvables: %d, memory used: %d K\n", pool->nsolvables, pool->nsolvables * (int)sizeof(Solvable) / 1024);
407   POOL_DEBUG(SOLV_DEBUG_STATS, "number of ids: %d + %d\n", pool->ss.nstrings, pool->nrels);
408   POOL_DEBUG(SOLV_DEBUG_STATS, "string memory used: %d K array + %d K data,  rel memory used: %d K array\n", pool->ss.nstrings / (1024 / (int)sizeof(Id)), pool->ss.sstrings / 1024, pool->nrels * (int)sizeof(Reldep) / 1024);
409   if (pool->ss.stringhashmask || pool->relhashmask)
410     POOL_DEBUG(SOLV_DEBUG_STATS, "string hash memory: %d K, rel hash memory : %d K\n", (pool->ss.stringhashmask + 1) / (int)(1024/sizeof(Id)), (pool->relhashmask + 1) / (int)(1024/sizeof(Id)));
411
412   pool_freeidhashes(pool);      /* XXX: should not be here! */
413   pool_freewhatprovides(pool);
414   num = pool->ss.nstrings;
415   pool->whatprovides = whatprovides = solv_calloc_block(num, sizeof(Offset), WHATPROVIDES_BLOCK);
416   pool->whatprovides_rel = solv_calloc_block(pool->nrels, sizeof(Offset), WHATPROVIDES_BLOCK);
417
418   /* count providers for each name */
419   for (i = pool->nsolvables - 1; i > 0; i--)
420     {
421       Id *pp;
422       s = pool->solvables + i;
423       if (!s->provides || !s->repo || s->repo->disabled)
424         continue;
425       /* we always need the installed solvable in the whatprovides data,
426          otherwise obsoletes/conflicts on them won't work */
427       if (s->repo != installed && !pool_installable(pool, s))
428         continue;
429       pp = s->repo->idarraydata + s->provides;
430       while ((id = *pp++) != 0)
431         {
432           while (ISRELDEP(id))
433             {
434               Reldep *rd = GETRELDEP(pool, id);
435               id = rd->name;
436             }
437           whatprovides[id]++;          /* inc count of providers */
438         }
439     }
440
441   off = 4;      /* first entry is undef, second is empty list, third is system solvable  */
442   np = 0;                              /* number of names provided */
443   for (i = 0, idp = whatprovides; i < num; i++, idp++)
444     {
445       n = *idp;
446       if (!n)                           /* no providers */
447         {
448           *idp = 1;                     /* offset for empty list */
449           continue;
450         }
451       off += n;                         /* make space for all providers */
452       *idp = off++;                     /* now idp points to terminating zero */
453       np++;                             /* inc # of provider 'slots' for stats */
454     }
455
456   POOL_DEBUG(SOLV_DEBUG_STATS, "provide ids: %d\n", np);
457
458   /* reserve some space for relation data */
459   extra = 2 * pool->nrels;
460   if (extra < 256)
461     extra = 256;
462
463   POOL_DEBUG(SOLV_DEBUG_STATS, "provide space needed: %d + %d\n", off, extra);
464
465   /* alloc space for all providers + extra */
466   whatprovidesdata = solv_calloc(off + extra, sizeof(Id));
467   whatprovidesdata[2] = SYSTEMSOLVABLE;
468
469   /* now fill data for all provides */
470   for (i = pool->nsolvables - 1; i > 0; i--)
471     {
472       Id *pp;
473       s = pool->solvables + i;
474       if (!s->provides || !s->repo || s->repo->disabled)
475         continue;
476       if (s->repo != installed && !pool_installable(pool, s))
477         continue;
478
479       /* for all provides of this solvable */
480       pp = s->repo->idarraydata + s->provides;
481       while ((id = *pp++) != 0)
482         {
483           while (ISRELDEP(id))
484             {
485               Reldep *rd = GETRELDEP(pool, id);
486               id = rd->name;
487             }
488           d = whatprovidesdata + whatprovides[id];   /* offset into whatprovidesdata */
489           if (*d != i)          /* don't add same solvable twice */
490             {
491               d[-1] = i;
492               whatprovides[id]--;
493             }
494         }
495     }
496   pool->whatprovidesdata = whatprovidesdata;
497   pool->whatprovidesdataoff = off;
498   pool->whatprovidesdataleft = extra;
499   pool_shrink_whatprovides(pool);
500   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)));
501
502   queue_empty(&pool->lazywhatprovidesq);
503   if ((!pool->addedfileprovides && pool->disttype == DISTTYPE_RPM) || pool->addedfileprovides == 1)
504     {
505       if (!pool->addedfileprovides)
506         POOL_DEBUG(SOLV_DEBUG_STATS, "WARNING: pool_addfileprovides was not called, this may result in slow operation\n");
507       /* lazyly add file provides */
508       for (i = 1; i < num; i++)
509         {
510           const char *str = pool->ss.stringspace + pool->ss.strings[i];
511           if (str[0] != '/')
512             continue;
513           if (pool->addedfileprovides == 1 && repodata_filelistfilter_matches(0, str))
514             continue;
515           /* setup lazy adding, but remember old value */
516           if (pool->whatprovides[i] > 1)
517             queue_push2(&pool->lazywhatprovidesq, i, pool->whatprovides[i]);
518           pool->whatprovides[i] = 0;
519         }
520       POOL_DEBUG(SOLV_DEBUG_STATS, "lazywhatprovidesq size: %d entries\n", pool->lazywhatprovidesq.count / 2);
521     }
522
523   POOL_DEBUG(SOLV_DEBUG_STATS, "createwhatprovides took %d ms\n", solv_timems(now));
524 }
525
526 /*
527  * free all of our whatprovides data
528  * be careful, everything internalized with pool_queuetowhatprovides is
529  * gone, too
530  */
531 void
532 pool_freewhatprovides(Pool *pool)
533 {
534   pool->whatprovides = solv_free(pool->whatprovides);
535   pool->whatprovides_rel = solv_free(pool->whatprovides_rel);
536   pool->whatprovidesdata = solv_free(pool->whatprovidesdata);
537   pool->whatprovidesdataoff = 0;
538   pool->whatprovidesdataleft = 0;
539 }
540
541
542 /******************************************************************************/
543
544 /*
545  * pool_queuetowhatprovides  - add queue contents to whatprovidesdata
546  *
547  * used for whatprovides, jobs, learnt rules, selections
548  * input: q: queue of Ids
549  * returns: Offset into whatprovidesdata
550  *
551  */
552 Id
553 pool_queuetowhatprovides(Pool *pool, Queue *q)
554 {
555   Offset off;
556   int count = q->count;
557
558   if (count == 0)                      /* queue empty -> 1 */
559     return 1;
560   if (count == 1 && q->elements[0] == SYSTEMSOLVABLE)
561     return 2;
562
563   /* extend whatprovidesdata if needed, +1 for 0-termination */
564   if (pool->whatprovidesdataleft < count + 1)
565     {
566       POOL_DEBUG(SOLV_DEBUG_STATS, "growing provides hash data...\n");
567       pool->whatprovidesdata = solv_realloc(pool->whatprovidesdata, (pool->whatprovidesdataoff + count + 4096) * sizeof(Id));
568       pool->whatprovidesdataleft = count + 4096;
569     }
570
571   /* copy queue to next free slot */
572   off = pool->whatprovidesdataoff;
573   memcpy(pool->whatprovidesdata + pool->whatprovidesdataoff, q->elements, count * sizeof(Id));
574
575   /* adapt count and 0-terminate */
576   pool->whatprovidesdataoff += count;
577   pool->whatprovidesdata[pool->whatprovidesdataoff++] = 0;
578   pool->whatprovidesdataleft -= count + 1;
579
580   return (Id)off;
581 }
582
583
584 /*************************************************************************/
585
586 #if defined(MULTI_SEMANTICS)
587 # define EVRCMP_DEPCMP (pool->disttype == DISTTYPE_DEB ? EVRCMP_COMPARE : EVRCMP_MATCH_RELEASE)
588 #elif defined(DEBIAN)
589 # define EVRCMP_DEPCMP EVRCMP_COMPARE
590 #else
591 # define EVRCMP_DEPCMP EVRCMP_MATCH_RELEASE
592 #endif
593
594 /* check if a package's nevr matches a dependency */
595 /* semi-private, called from public pool_match_nevr */
596
597 int
598 pool_match_nevr_rel(Pool *pool, Solvable *s, Id d)
599 {
600   Reldep *rd = GETRELDEP(pool, d);
601   Id name = rd->name;
602   Id evr = rd->evr;
603   int flags = rd->flags;
604
605   if (flags > 7)
606     {
607       switch (flags)
608         {
609         case REL_ARCH:
610           if (s->arch != evr)
611             {
612               if (evr != ARCH_SRC || s->arch != ARCH_NOSRC)
613                 return 0;
614             }
615           return pool_match_nevr(pool, s, name);
616         case REL_OR:
617           if (pool_match_nevr(pool, s, name))
618             return 1;
619           return pool_match_nevr(pool, s, evr);
620         case REL_AND:
621         case REL_WITH:
622           if (!pool_match_nevr(pool, s, name))
623             return 0;
624           return pool_match_nevr(pool, s, evr);
625         case REL_MULTIARCH:
626           if (evr != ARCH_ANY)
627             return 0;
628           /* XXX : need to check for Multi-Arch: allowed! */
629           return pool_match_nevr(pool, s, name);
630         default:
631           return 0;
632         }
633     }
634   if (!pool_match_nevr(pool, s, name))
635     return 0;
636   if (evr == s->evr)
637     return (flags & REL_EQ) ? 1 : 0;
638   if (!flags)
639     return 0;
640   if (flags == 7)
641     return 1;
642   switch (pool_evrcmp(pool, s->evr, evr, EVRCMP_DEPCMP))
643     {
644     case -2:
645       return 1;
646     case -1:
647       return (flags & REL_LT) ? 1 : 0;
648     case 0:
649       return (flags & REL_EQ) ? 1 : 0;
650     case 1:
651       return (flags & REL_GT) ? 1 : 0;
652     case 2:
653       return (flags & REL_EQ) ? 1 : 0;
654     default:
655       break;
656     }
657   return 0;
658 }
659
660 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
661 /* forward declaration */
662 static int pool_match_flags_evr_rel_compat(Pool *pool, Reldep *range, int flags, int evr);
663 #endif
664
665 /* match (flags, evr) against provider (pflags, pevr) */
666 static inline int
667 pool_match_flags_evr(Pool *pool, int pflags, Id pevr, int flags, int evr)
668 {
669   if (!pflags || !flags || pflags >= 8 || flags >= 8)
670     return 0;
671   if (flags == 7 || pflags == 7)
672     return 1;           /* rel provides every version */
673   if ((pflags & flags & (REL_LT | REL_GT)) != 0)
674     return 1;           /* both rels show in the same direction */
675   if (pevr == evr)
676     return (flags & pflags & REL_EQ) ? 1 : 0;
677 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
678   if (ISRELDEP(pevr))
679     {
680       Reldep *rd = GETRELDEP(pool, pevr);
681       if (rd->flags == REL_COMPAT)
682         return pool_match_flags_evr_rel_compat(pool, rd, flags, evr);
683     }
684 #endif
685   switch (pool_evrcmp(pool, pevr, evr, EVRCMP_DEPCMP))
686     {
687     case -2:
688       return (pflags & REL_EQ) ? 1 : 0;
689     case -1:
690       return (flags & REL_LT) || (pflags & REL_GT) ? 1 : 0;
691     case 0:
692       return (flags & pflags & REL_EQ) ? 1 : 0;
693     case 1:
694       return (flags & REL_GT) || (pflags & REL_LT) ? 1 : 0;
695     case 2:
696       return (flags & REL_EQ) ? 1 : 0;
697     default:
698       break;
699     }
700   return 0;
701 }
702
703 #if defined(HAIKU) || defined(MULTI_SEMANTICS)
704 static int
705 pool_match_flags_evr_rel_compat(Pool *pool, Reldep *range, int flags, int evr)
706 {
707   /* range->name is the actual version, range->evr the backwards compatibility
708      version. If flags are '>=' or '>', we match the compatibility version
709      as well, otherwise only the actual version. */
710   if (!(flags & REL_GT) || (flags & REL_LT))
711     return pool_match_flags_evr(pool, REL_EQ, range->name, flags, evr);
712   return pool_match_flags_evr(pool, REL_LT | REL_EQ, range->name, flags, evr) &&
713          pool_match_flags_evr(pool, REL_GT | REL_EQ, range->evr, REL_EQ, evr);
714 }
715 #endif
716
717 /* public (i.e. not inlined) version of pool_match_flags_evr */
718 int
719 pool_intersect_evrs(Pool *pool, int pflags, Id pevr, int flags, int evr)
720 {
721   return pool_match_flags_evr(pool, pflags, pevr, flags, evr);
722 }
723
724 /* match two dependencies (d1 = provider) */
725
726 int
727 pool_match_dep(Pool *pool, Id d1, Id d2)
728 {
729   Reldep *rd1, *rd2;
730
731   if (d1 == d2)
732     return 1;
733   if (!ISRELDEP(d1))
734     {
735       if (!ISRELDEP(d2))
736         return 0;
737       rd2 = GETRELDEP(pool, d2);
738       return pool_match_dep(pool, d1, rd2->name);
739     }
740   rd1 = GETRELDEP(pool, d1);
741   if (!ISRELDEP(d2))
742     {
743       return pool_match_dep(pool, rd1->name, d2);
744     }
745   rd2 = GETRELDEP(pool, d2);
746   /* first match name */
747   if (!pool_match_dep(pool, rd1->name, rd2->name))
748     return 0;
749   /* name matches, check flags and evr */
750   return pool_intersect_evrs(pool, rd1->flags, rd1->evr, rd2->flags, rd2->evr);
751 }
752
753 Id
754 pool_searchlazywhatprovidesq(Pool *pool, Id d)
755 {
756   int start = 0;
757   int end = pool->lazywhatprovidesq.count;
758   Id *elements;
759   if (!end)
760     return 0;
761   elements = pool->lazywhatprovidesq.elements;
762   while (end - start > 16)
763     {
764       int mid = (start + end) / 2 & ~1;
765       if (elements[mid] == d)
766         return elements[mid + 1];
767       if (elements[mid] < d)
768         start = mid + 2;
769       else
770         end = mid;
771     }
772   for (; start < end; start += 2)
773     if (elements[start] == d)
774       return elements[start + 1];
775   return 0;
776 }
777
778 /*
779  * addstdproviders
780  *
781  * lazy populating of the whatprovides array, non relation case
782  */
783 static Id
784 pool_addstdproviders(Pool *pool, Id d)
785 {
786   const char *str;
787   Queue q;
788   Id qbuf[16];
789   Dataiterator di;
790   Id oldoffset;
791
792   if (pool->addedfileprovides == 2)
793     {
794       pool->whatprovides[d] = 1;
795       return 1;
796     }
797   str =  pool->ss.stringspace + pool->ss.strings[d];
798   if (*str != '/')
799     {
800       pool->whatprovides[d] = 1;
801       return 1;
802     }
803   queue_init_buffer(&q, qbuf, sizeof(qbuf)/sizeof(*qbuf));
804   dataiterator_init(&di, pool, 0, 0, SOLVABLE_FILELIST, str, SEARCH_STRING|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
805   for (; dataiterator_step(&di); dataiterator_skip_solvable(&di))
806     {
807       Solvable *s = pool->solvables + di.solvid;
808       /* XXX: maybe should add a provides dependency to the solvables
809        * OTOH this is only needed for rel deps that filter the provides,
810        * and those should not use filelist entries */
811       if (s->repo->disabled)
812         continue;
813       if (s->repo != pool->installed && !pool_installable(pool, s))
814         continue;
815       queue_push(&q, di.solvid);
816     }
817   dataiterator_free(&di);
818   oldoffset = pool_searchlazywhatprovidesq(pool, d);
819   if (!q.count)
820     pool->whatprovides[d] = oldoffset ? oldoffset : 1;
821   else
822     {
823       if (oldoffset)
824         {
825           Id *oo = pool->whatprovidesdata + oldoffset;
826           int i;
827           /* unify both queues. easy, as we know both are sorted */
828           for (i = 0; i < q.count; i++)
829             {
830               if (*oo > q.elements[i])
831                 continue;
832               if (*oo < q.elements[i])
833                 queue_insert(&q, i, *oo);
834               oo++;
835               if (!*oo)
836                 break;
837             }
838           while (*oo)
839             queue_push(&q, *oo++);
840           if (q.count == oo - (pool->whatprovidesdata + oldoffset))
841             {
842               /* end result has same size as oldoffset -> no new entries */
843               queue_free(&q);
844               pool->whatprovides[d] = oldoffset;
845               return oldoffset;
846             }
847         }
848       pool->whatprovides[d] = pool_queuetowhatprovides(pool, &q);
849     }
850   queue_free(&q);
851   return pool->whatprovides[d];
852 }
853
854
855 static inline int
856 pool_is_kind(Pool *pool, Id name, Id kind)
857 {
858   const char *n;
859   if (!kind)
860     return 1;
861   n = pool_id2str(pool, name);
862   if (kind != 1)
863     {
864       const char *kn = pool_id2str(pool, kind);
865       int knl = strlen(kn);
866       return !strncmp(n, kn, knl) && n[knl] == ':' ? 1 : 0;
867     }
868   else
869     {
870       if (*n == ':')
871         return 1;
872       while(*n >= 'a' && *n <= 'z')
873         n++;
874       return *n == ':' ? 0 : 1;
875     }
876 }
877
878 /*
879  * addrelproviders
880  *
881  * add packages fulfilling the relation to whatprovides array
882  *
883  * some words about REL_AND and REL_IF: we assume the best case
884  * here, so that you get a "potential" result if you ask for a match.
885  * E.g. if you ask for "whatrequires A" and package X contains
886  * "Requires: A & B", you'll get "X" as an answer.
887  */
888 Id
889 pool_addrelproviders(Pool *pool, Id d)
890 {
891   Reldep *rd;
892   Reldep *prd;
893   Queue plist;
894   Id buf[16];
895   Id name, evr, flags;
896   Id pid, *pidp;
897   Id p, *pp;
898
899   if (!ISRELDEP(d))
900     return pool_addstdproviders(pool, d);
901   rd = GETRELDEP(pool, d);
902   name = rd->name;
903   evr = rd->evr;
904   flags = rd->flags;
905   d = GETRELID(d);
906   queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
907
908   if (flags >= 8)
909     {
910       /* special relation */
911       Id wp = 0;
912       Id *pp2, *pp3;
913
914       switch (flags)
915         {
916         case REL_WITH:
917           wp = pool_whatprovides(pool, name);
918           pp2 = pool_whatprovides_ptr(pool, evr);
919           pp = pool->whatprovidesdata + wp;
920           while ((p = *pp++) != 0)
921             {
922               for (pp3 = pp2; *pp3; pp3++)
923                 if (*pp3 == p)
924                   break;
925               if (*pp3)
926                 queue_push(&plist, p);  /* found it */
927               else
928                 wp = 0;
929             }
930           break;
931
932         case REL_AND:
933         case REL_OR:
934           wp = pool_whatprovides(pool, name);
935           if (!pool->whatprovidesdata[wp])
936             wp = pool_whatprovides(pool, evr);
937           else
938             {
939               /* sorted merge */
940               pp2 = pool_whatprovides_ptr(pool, evr);
941               pp = pool->whatprovidesdata + wp;
942               while (*pp && *pp2)
943                 {
944                   if (*pp < *pp2)
945                     queue_push(&plist, *pp++);
946                   else
947                     {
948                       if (*pp == *pp2)
949                         pp++;
950                       queue_push(&plist, *pp2++);
951                     }
952                 }
953               while (*pp)
954                 queue_push(&plist, *pp++);
955               while (*pp2)
956                 queue_push(&plist, *pp2++);
957               /* if the number of elements did not change, we can reuse wp */
958               if (pp - (pool->whatprovidesdata + wp) != plist.count)
959                 wp = 0;
960             }
961           break;
962
963         case REL_COND:
964           /* assume the condition is true */
965           wp = pool_whatprovides(pool, name);
966           break;
967
968         case REL_NAMESPACE:
969           if (name == NAMESPACE_OTHERPROVIDERS)
970             {
971               wp = pool_whatprovides(pool, evr);
972               break;
973             }
974           if (pool->nscallback)
975             {
976               /* ask callback which packages provide the dependency
977                * 0:  none
978                * 1:  the system (aka SYSTEMSOLVABLE)
979                * >1: set of packages, stored as offset on whatprovidesdata
980                */
981               p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
982               if (p > 1)
983                 wp = p;
984               if (p == 1)
985                 queue_push(&plist, SYSTEMSOLVABLE);
986             }
987           break;
988         case REL_ARCH:
989           /* small hack: make it possible to match <pkg>.src
990            * we have to iterate over the solvables as src packages do not
991            * provide anything, thus they are not indexed in our
992            * whatprovides hash */
993           if (evr == ARCH_SRC || evr == ARCH_NOSRC)
994             {
995               Solvable *s;
996               for (p = 1, s = pool->solvables + p; p < pool->nsolvables; p++, s++)
997                 {
998                   if (!s->repo)
999                     continue;
1000                   if (s->arch != evr && s->arch != ARCH_NOSRC)
1001                     continue;
1002                   if (pool_disabled_solvable(pool, s))
1003                     continue;
1004                   if (!name || pool_match_nevr(pool, s, name))
1005                     queue_push(&plist, p);
1006                 }
1007               break;
1008             }
1009           if (!name)
1010             {
1011               FOR_POOL_SOLVABLES(p)
1012                 {
1013                   Solvable *s = pool->solvables + p;
1014                   if (s->repo != pool->installed && !pool_installable(pool, s))
1015                     continue;
1016                   if (s->arch == evr)
1017                     queue_push(&plist, p);
1018                 }
1019               break;
1020             }
1021           wp = pool_whatprovides(pool, name);
1022           pp = pool->whatprovidesdata + wp;
1023           while ((p = *pp++) != 0)
1024             {
1025               Solvable *s = pool->solvables + p;
1026               if (s->arch == evr)
1027                 queue_push(&plist, p);
1028               else
1029                 wp = 0;
1030             }
1031           break;
1032         case REL_MULTIARCH:
1033           if (evr != ARCH_ANY)
1034             break;
1035           /* XXX : need to check for Multi-Arch: allowed! */
1036           wp = pool_whatprovides(pool, name);
1037           break;
1038         case REL_KIND:
1039           /* package kind filtering */
1040           if (!name)
1041             {
1042               FOR_POOL_SOLVABLES(p)
1043                 {
1044                   Solvable *s = pool->solvables + p;
1045                   if (s->repo != pool->installed && !pool_installable(pool, s))
1046                     continue;
1047                   if (pool_is_kind(pool, s->name, evr))
1048                     queue_push(&plist, p);
1049                 }
1050               break;
1051             }
1052           wp = pool_whatprovides(pool, name);
1053           pp = pool->whatprovidesdata + wp;
1054           while ((p = *pp++) != 0)
1055             {
1056               Solvable *s = pool->solvables + p;
1057               if (pool_is_kind(pool, s->name, evr))
1058                 queue_push(&plist, p);
1059               else
1060                 wp = 0;
1061             }
1062           break;
1063         case REL_FILECONFLICT:
1064           pp = pool_whatprovides_ptr(pool, name);
1065           while ((p = *pp++) != 0)
1066             {
1067               Id origd = MAKERELDEP(d);
1068               Solvable *s = pool->solvables + p;
1069               if (!s->provides)
1070                 continue;
1071               pidp = s->repo->idarraydata + s->provides;
1072               while ((pid = *pidp++) != 0)
1073                 if (pid == origd)
1074                   break;
1075               if (pid)
1076                 queue_push(&plist, p);
1077             }
1078           break;
1079         default:
1080           break;
1081         }
1082       if (wp)
1083         {
1084           /* we can reuse an existing entry */
1085           queue_free(&plist);
1086           pool->whatprovides_rel[d] = wp;
1087           return wp;
1088         }
1089     }
1090   else if (flags)
1091     {
1092       /* simple version comparison relation */
1093 #if 0
1094       POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: what provides %s?\n", pool_dep2str(pool, name));
1095 #endif
1096       pp = pool_whatprovides_ptr(pool, name);
1097       while (ISRELDEP(name))
1098         {
1099           rd = GETRELDEP(pool, name);
1100           name = rd->name;
1101         }
1102       while ((p = *pp++) != 0)
1103         {
1104           Solvable *s = pool->solvables + p;
1105           if (!s->provides)
1106             {
1107               /* no provides - check nevr */
1108               if (pool_match_nevr_rel(pool, s, MAKERELDEP(d)))
1109                 queue_push(&plist, p);
1110               continue;
1111             }
1112           /* solvable p provides name in some rels */
1113           pidp = s->repo->idarraydata + s->provides;
1114           while ((pid = *pidp++) != 0)
1115             {
1116               if (!ISRELDEP(pid))
1117                 {
1118                   if (pid != name)
1119                     continue;           /* wrong provides name */
1120                   if (pool->disttype == DISTTYPE_DEB)
1121                     continue;           /* unversioned provides can never match versioned deps */
1122                   break;
1123                 }
1124               prd = GETRELDEP(pool, pid);
1125               if (prd->name != name)
1126                 continue;               /* wrong provides name */
1127               /* right package, both deps are rels. check flags/evr */
1128               if (pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
1129                 break;  /* matches */
1130             }
1131           if (!pid)
1132             continue;   /* none of the providers matched */
1133           queue_push(&plist, p);
1134         }
1135       /* make our system solvable provide all unknown rpmlib() stuff */
1136       if (plist.count == 0 && !strncmp(pool_id2str(pool, name), "rpmlib(", 7))
1137         queue_push(&plist, SYSTEMSOLVABLE);
1138     }
1139   /* add providers to whatprovides */
1140 #if 0
1141   POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
1142 #endif
1143   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
1144   queue_free(&plist);
1145
1146   return pool->whatprovides_rel[d];
1147 }
1148
1149 void
1150 pool_flush_namespaceproviders(Pool *pool, Id ns, Id evr)
1151 {
1152   int nrels = pool->nrels;
1153   Id d;
1154   Reldep *rd;
1155
1156   if (!pool->whatprovides_rel)
1157     return;
1158   for (d = 1, rd = pool->rels + d; d < nrels; d++, rd++)
1159     {
1160       if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
1161         continue;
1162       if (ns && rd->name != ns)
1163         continue;
1164       if (evr && rd->evr != evr)
1165         continue;
1166       pool->whatprovides_rel[d] = 0;
1167     }
1168 }
1169
1170 /* intersect dependencies in keyname with dep, return list of matching packages */
1171 void
1172 pool_whatmatchesdep(Pool *pool, Id keyname, Id dep, Queue *q, int marker)
1173 {
1174   Id p;
1175
1176   queue_empty(q);
1177   FOR_POOL_SOLVABLES(p)
1178     {
1179       Solvable *s = pool->solvables + p;
1180       if (s->repo->disabled)
1181         continue;
1182       if (s->repo != pool->installed && !pool_installable(pool, s))
1183         continue;
1184       if (solvable_matchesdep(s, keyname, dep, marker))
1185         queue_push(q, p);
1186     }
1187 }
1188
1189 /*************************************************************************/
1190
1191 void
1192 pool_debug(Pool *pool, int type, const char *format, ...)
1193 {
1194   va_list args;
1195   char buf[1024];
1196
1197   if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0)
1198     {
1199       if ((pool->debugmask & type) == 0)
1200         return;
1201     }
1202   va_start(args, format);
1203   if (!pool->debugcallback)
1204     {
1205       if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0 && !(pool->debugmask & SOLV_DEBUG_TO_STDERR))
1206         vprintf(format, args);
1207       else
1208         vfprintf(stderr, format, args);
1209       return;
1210     }
1211   vsnprintf(buf, sizeof(buf), format, args);
1212   va_end(args);
1213   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
1214 }
1215
1216 int
1217 pool_error(Pool *pool, int ret, const char *format, ...)
1218 {
1219   va_list args;
1220   int l;
1221   va_start(args, format);
1222   if (!pool->errstr)
1223     {
1224       pool->errstra = 1024;
1225       pool->errstr = solv_malloc(pool->errstra);
1226     }
1227   if (!*format)
1228     {
1229       *pool->errstr = 0;
1230       l = 0;
1231     }
1232   else
1233     l = vsnprintf(pool->errstr, pool->errstra, format, args);
1234   va_end(args);
1235   if (l >= 0 && l + 1 > pool->errstra)
1236     {
1237       pool->errstra = l + 256;
1238       pool->errstr = solv_realloc(pool->errstr, pool->errstra);
1239       va_start(args, format);
1240       l = vsnprintf(pool->errstr, pool->errstra, format, args);
1241       va_end(args);
1242     }
1243   if (l < 0)
1244     strcpy(pool->errstr, "unknown error");
1245   if (pool->debugmask & SOLV_ERROR)
1246     pool_debug(pool, SOLV_ERROR, "%s\n", pool->errstr);
1247   return ret;
1248 }
1249
1250 char *
1251 pool_errstr(Pool *pool)
1252 {
1253   return pool->errstr ? pool->errstr : "no error";
1254 }
1255
1256 void
1257 pool_setdebuglevel(Pool *pool, int level)
1258 {
1259   int mask = SOLV_DEBUG_RESULT;
1260   if (level > 0)
1261     mask |= SOLV_DEBUG_STATS|SOLV_DEBUG_ANALYZE|SOLV_DEBUG_UNSOLVABLE|SOLV_DEBUG_SOLVER|SOLV_DEBUG_TRANSACTION|SOLV_ERROR;
1262   if (level > 1)
1263     mask |= SOLV_DEBUG_JOB|SOLV_DEBUG_SOLUTIONS|SOLV_DEBUG_POLICY;
1264   if (level > 2)
1265     mask |= SOLV_DEBUG_PROPAGATE;
1266   if (level > 3)
1267     mask |= SOLV_DEBUG_RULE_CREATION;
1268   mask |= pool->debugmask & SOLV_DEBUG_TO_STDERR;       /* keep bit */
1269   pool->debugmask = mask;
1270 }
1271
1272 void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
1273 {
1274   pool->debugcallback = debugcallback;
1275   pool->debugcallbackdata = debugcallbackdata;
1276 }
1277
1278 void pool_setdebugmask(Pool *pool, int mask)
1279 {
1280   pool->debugmask = mask;
1281 }
1282
1283 void pool_setloadcallback(Pool *pool, int (*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
1284 {
1285   pool->loadcallback = cb;
1286   pool->loadcallbackdata = loadcbdata;
1287 }
1288
1289 void pool_setnamespacecallback(Pool *pool, Id (*cb)(struct _Pool *, void *, Id, Id), void *nscbdata)
1290 {
1291   pool->nscallback = cb;
1292   pool->nscallbackdata = nscbdata;
1293 }
1294
1295 /*************************************************************************/
1296
1297 struct searchfiles {
1298   Id *ids;
1299   int nfiles;
1300   Map seen;
1301 };
1302
1303 #define SEARCHFILES_BLOCK 127
1304
1305 static void
1306 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
1307 {
1308   Id dep, sid;
1309   const char *s;
1310   struct searchfiles *csf;
1311
1312   while ((dep = *ida++) != 0)
1313     {
1314       csf = sf;
1315       while (ISRELDEP(dep))
1316         {
1317           Reldep *rd;
1318           sid = pool->ss.nstrings + GETRELID(dep);
1319           if (MAPTST(&csf->seen, sid))
1320             {
1321               dep = 0;
1322               break;
1323             }
1324           MAPSET(&csf->seen, sid);
1325           rd = GETRELDEP(pool, dep);
1326           if (rd->flags < 8)
1327             dep = rd->name;
1328           else if (rd->flags == REL_NAMESPACE)
1329             {
1330               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
1331                 {
1332                   csf = isf;
1333                   if (!csf || MAPTST(&csf->seen, sid))
1334                     {
1335                       dep = 0;
1336                       break;
1337                     }
1338                   MAPSET(&csf->seen, sid);
1339                 }
1340               dep = rd->evr;
1341             }
1342           else if (rd->flags == REL_FILECONFLICT)
1343             {
1344               dep = 0;
1345               break;
1346             }
1347           else
1348             {
1349               Id ids[2];
1350               ids[0] = rd->name;
1351               ids[1] = 0;
1352               pool_addfileprovides_dep(pool, ids, csf, isf);
1353               dep = rd->evr;
1354             }
1355         }
1356       if (!dep)
1357         continue;
1358       if (MAPTST(&csf->seen, dep))
1359         continue;
1360       MAPSET(&csf->seen, dep);
1361       s = pool_id2str(pool, dep);
1362       if (*s != '/')
1363         continue;
1364       if (csf != isf && pool->addedfileprovides == 1 && !repodata_filelistfilter_matches(0, s))
1365         continue;       /* skip non-standard locations csf == isf: installed case */
1366       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1367       csf->ids[csf->nfiles++] = dep;
1368     }
1369 }
1370
1371 struct addfileprovides_cbdata {
1372   int nfiles;
1373   Id *ids;
1374   char **dirs;
1375   char **names;
1376
1377   Id *dids;
1378
1379   Map providedids;
1380
1381   Map useddirs;
1382 };
1383
1384 static int
1385 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1386 {
1387   struct addfileprovides_cbdata *cbd = cbdata;
1388   int i;
1389
1390   if (!cbd->useddirs.size)
1391     {
1392       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
1393       if (!cbd->dirs)
1394         {
1395           cbd->dirs = solv_malloc2(cbd->nfiles, sizeof(char *));
1396           cbd->names = solv_malloc2(cbd->nfiles, sizeof(char *));
1397           for (i = 0; i < cbd->nfiles; i++)
1398             {
1399               char *s = solv_strdup(pool_id2str(data->repo->pool, cbd->ids[i]));
1400               cbd->dirs[i] = s;
1401               s = strrchr(s, '/');
1402               *s = 0;
1403               cbd->names[i] = s + 1;
1404             }
1405         }
1406       for (i = 0; i < cbd->nfiles; i++)
1407         {
1408           Id did;
1409           if (MAPTST(&cbd->providedids, cbd->ids[i]))
1410             {
1411               cbd->dids[i] = 0;
1412               continue;
1413             }
1414           did = repodata_str2dir(data, cbd->dirs[i], 0);
1415           cbd->dids[i] = did;
1416           if (did)
1417             MAPSET(&cbd->useddirs, did);
1418         }
1419       repodata_free_dircache(data);
1420     }
1421   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
1422     return 0;
1423   for (i = 0; i < cbd->nfiles; i++)
1424     if (cbd->dids[i] != value->id && !strcmp(cbd->names[i], value->str))
1425       break;
1426   if (i < cbd->nfiles)
1427     s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
1428   return 0;
1429 }
1430
1431 static void
1432 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
1433 {
1434   Id p;
1435   Repodata *data;
1436   Repo *repo;
1437   Queue fileprovidesq;
1438   int i, j, repoid, repodataid;
1439   int provstart, provend;
1440   Map donemap;
1441   int ndone, incomplete;
1442
1443   if (!pool->urepos)
1444     return;
1445
1446   cbd->nfiles = sf->nfiles;
1447   cbd->ids = sf->ids;
1448   cbd->dirs = 0;
1449   cbd->names = 0;
1450   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
1451   map_init(&cbd->providedids, pool->ss.nstrings);
1452
1453   repoid = 1;
1454   repo = repoonly ? repoonly : pool->repos[repoid];
1455   map_init(&donemap, pool->nsolvables);
1456   queue_init(&fileprovidesq);
1457   provstart = provend = 0;
1458   for (;;)
1459     {
1460       if (!repo || repo->disabled)
1461         {
1462           if (repoonly || ++repoid == pool->nrepos)
1463             break;
1464           repo = pool->repos[repoid];
1465           continue;
1466         }
1467       ndone = 0;
1468       FOR_REPODATAS(repo, repodataid, data)
1469         {
1470           if (ndone >= repo->nsolvables)
1471             break;
1472
1473           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1474             {
1475               map_empty(&cbd->providedids);
1476               for (i = 0; i < fileprovidesq.count; i++)
1477                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1478               provstart = data->start;
1479               provend = data->end;
1480               for (i = 0; i < cbd->nfiles; i++)
1481                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1482                   break;
1483               if (i == cbd->nfiles)
1484                 {
1485                   /* great! no need to search files */
1486                   for (p = data->start; p < data->end; p++)
1487                     if (pool->solvables[p].repo == repo)
1488                       {
1489                         if (MAPTST(&donemap, p))
1490                           continue;
1491                         MAPSET(&donemap, p);
1492                         ndone++;
1493                       }
1494                   continue;
1495                 }
1496             }
1497
1498           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1499             continue;
1500
1501           if (data->start < provstart || data->end > provend)
1502             {
1503               map_empty(&cbd->providedids);
1504               provstart = provend = 0;
1505             }
1506
1507           /* check if the data is incomplete */
1508           incomplete = 0;
1509           if (data->state == REPODATA_AVAILABLE)
1510             {
1511               for (j = 1; j < data->nkeys; j++)
1512                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1513                   break;
1514               if (j < data->nkeys)
1515                 {
1516 #if 0
1517                   for (i = 0; i < cbd->nfiles; i++)
1518                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1519                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1520 #endif
1521                   for (i = 0; i < cbd->nfiles; i++)
1522                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1523                       break;
1524                   if (i < cbd->nfiles)
1525                     incomplete = 1;
1526                 }
1527             }
1528
1529           /* do the search */
1530           map_init(&cbd->useddirs, 0);
1531           for (p = data->start; p < data->end; p++)
1532             if (pool->solvables[p].repo == repo)
1533               {
1534                 if (MAPTST(&donemap, p))
1535                   continue;
1536                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1537                 if (!incomplete)
1538                   {
1539                     MAPSET(&donemap, p);
1540                     ndone++;
1541                   }
1542               }
1543           map_free(&cbd->useddirs);
1544         }
1545
1546       if (repoonly || ++repoid == pool->nrepos)
1547         break;
1548       repo = pool->repos[repoid];
1549     }
1550   map_free(&donemap);
1551   queue_free(&fileprovidesq);
1552   map_free(&cbd->providedids);
1553   if (cbd->dirs)
1554     {
1555       for (i = 0; i < cbd->nfiles; i++)
1556         solv_free(cbd->dirs[i]);
1557       cbd->dirs = solv_free(cbd->dirs);
1558       cbd->names = solv_free(cbd->names);
1559     }
1560 }
1561
1562 void
1563 pool_addfileprovides_queue(Pool *pool, Queue *idq, Queue *idqinst)
1564 {
1565   Solvable *s;
1566   Repo *installed, *repo;
1567   struct searchfiles sf, isf, *isfp;
1568   struct addfileprovides_cbdata cbd;
1569   int i;
1570   unsigned int now;
1571
1572   installed = pool->installed;
1573   now = solv_timems(0);
1574   memset(&sf, 0, sizeof(sf));
1575   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1576   memset(&isf, 0, sizeof(isf));
1577   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1578   pool->addedfileprovides = pool->addfileprovidesfiltered ? 1 : 2;
1579
1580   if (idq)
1581     queue_empty(idq);
1582   if (idqinst)
1583     queue_empty(idqinst);
1584   isfp = installed ? &isf : 0;
1585   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1586     {
1587       repo = s->repo;
1588       if (!repo)
1589         continue;
1590       if (s->obsoletes)
1591         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1592       if (s->conflicts)
1593         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1594       if (s->requires)
1595         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1596       if (s->recommends)
1597         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1598       if (s->suggests)
1599         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1600       if (s->supplements)
1601         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1602       if (s->enhances)
1603         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1604     }
1605   map_free(&sf.seen);
1606   map_free(&isf.seen);
1607   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1608   cbd.dids = 0;
1609   if (sf.nfiles)
1610     {
1611 #if 0
1612       for (i = 0; i < sf.nfiles; i++)
1613         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1614 #endif
1615       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1616       if (idq)
1617         for (i = 0; i < sf.nfiles; i++)
1618           queue_push(idq, sf.ids[i]);
1619       if (idqinst)
1620         for (i = 0; i < sf.nfiles; i++)
1621           queue_push(idqinst, sf.ids[i]);
1622       solv_free(sf.ids);
1623     }
1624   if (isf.nfiles)
1625     {
1626 #if 0
1627       for (i = 0; i < isf.nfiles; i++)
1628         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1629 #endif
1630       if (installed)
1631         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1632       if (installed && idqinst)
1633         for (i = 0; i < isf.nfiles; i++)
1634           queue_pushunique(idqinst, isf.ids[i]);
1635       solv_free(isf.ids);
1636     }
1637   solv_free(cbd.dids);
1638   pool_freewhatprovides(pool);  /* as we have added provides */
1639   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1640 }
1641
1642 void
1643 pool_addfileprovides(Pool *pool)
1644 {
1645   pool_addfileprovides_queue(pool, 0, 0);
1646 }
1647
1648 void
1649 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)
1650 {
1651   if (p)
1652     {
1653       if (pool->solvables[p].repo)
1654         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1655       return;
1656     }
1657   /* FIXME: obey callback return value! */
1658   for (p = 1; p < pool->nsolvables; p++)
1659     if (pool->solvables[p].repo)
1660       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1661 }
1662
1663 void
1664 pool_clear_pos(Pool *pool)
1665 {
1666   memset(&pool->pos, 0, sizeof(pool->pos));
1667 }
1668
1669
1670 void
1671 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1672 {
1673   int i;
1674
1675   pool->languagecache = solv_free(pool->languagecache);
1676   pool->languagecacheother = 0;
1677   for (i = 0; i < pool->nlanguages; i++)
1678     free((char *)pool->languages[i]);
1679   pool->languages = solv_free(pool->languages);
1680   pool->nlanguages = nlanguages;
1681   if (!nlanguages)
1682     return;
1683   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1684   for (i = 0; i < pool->nlanguages; i++)
1685     pool->languages[i] = solv_strdup(languages[i]);
1686 }
1687
1688 Id
1689 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1690 {
1691   const char *n;
1692   char buf[256], *p;
1693   int l;
1694
1695   if (!lang || !*lang)
1696     return id;
1697   n = pool_id2str(pool, id);
1698   l = strlen(n) + strlen(lang) + 2;
1699   if (l > sizeof(buf))
1700     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1701   else
1702     p = buf;
1703   sprintf(p, "%s:%s", n, lang);
1704   id = pool_str2id(pool, p, create);
1705   if (p != buf)
1706     free(p);
1707   return id;
1708 }
1709
1710 char *
1711 pool_alloctmpspace(Pool *pool, int len)
1712 {
1713   int n = pool->tmpspace.n;
1714   if (!len)
1715     return 0;
1716   if (len > pool->tmpspace.len[n])
1717     {
1718       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1719       pool->tmpspace.len[n] = len + 32;
1720     }
1721   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1722   return pool->tmpspace.buf[n];
1723 }
1724
1725 static char *
1726 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1727 {
1728   if (space)
1729     {
1730       int n, oldn;
1731       n = oldn = pool->tmpspace.n;
1732       for (;;)
1733         {
1734           if (!n--)
1735             n = POOL_TMPSPACEBUF - 1;
1736           if (n == oldn)
1737             break;
1738           if (pool->tmpspace.buf[n] != space)
1739             continue;
1740           if (len > pool->tmpspace.len[n])
1741             {
1742               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1743               pool->tmpspace.len[n] = len + 32;
1744             }
1745           return pool->tmpspace.buf[n];
1746         }
1747     }
1748   return 0;
1749 }
1750
1751 void
1752 pool_freetmpspace(Pool *pool, const char *space)
1753 {
1754   int n = pool->tmpspace.n;
1755   if (!space)
1756     return;
1757   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1758   if (pool->tmpspace.buf[n] == space)
1759     pool->tmpspace.n = n;
1760 }
1761
1762 char *
1763 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1764 {
1765   int l1, l2, l3;
1766   char *s, *str;
1767   l1 = str1 ? strlen(str1) : 0;
1768   l2 = str2 ? strlen(str2) : 0;
1769   l3 = str3 ? strlen(str3) : 0;
1770   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1771   if (l1)
1772     {
1773       strcpy(s, str1);
1774       s += l1;
1775     }
1776   if (l2)
1777     {
1778       strcpy(s, str2);
1779       s += l2;
1780     }
1781   if (l3)
1782     {
1783       strcpy(s, str3);
1784       s += l3;
1785     }
1786   *s = 0;
1787   return str;
1788 }
1789
1790 char *
1791 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1792 {
1793   int l1, l2, l3;
1794   char *s, *str;
1795
1796   l1 = str1 ? strlen(str1) : 0;
1797   l2 = str2 ? strlen(str2) : 0;
1798   l3 = str3 ? strlen(str3) : 0;
1799   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1800   if (str)
1801     str1 = str;
1802   else
1803     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1804   s = str;
1805   if (l1)
1806     {
1807       if (s != str1)
1808         strcpy(s, str1);
1809       s += l1;
1810     }
1811   if (l2)
1812     {
1813       strcpy(s, str2);
1814       s += l2;
1815     }
1816   if (l3)
1817     {
1818       strcpy(s, str3);
1819       s += l3;
1820     }
1821   *s = 0;
1822   return str;
1823 }
1824
1825 const char *
1826 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1827 {
1828   char *s;
1829   if (!len)
1830     return "";
1831   s = pool_alloctmpspace(pool, 2 * len + 1);
1832   solv_bin2hex(buf, len, s);
1833   return s;
1834 }
1835
1836 /*******************************************************************/
1837
1838 struct mptree {
1839   Id sibling;
1840   Id child;
1841   const char *comp;
1842   int compl;
1843   Id mountpoint;
1844 };
1845
1846 struct ducbdata {
1847   DUChanges *mps;
1848   struct mptree *mptree;
1849   int addsub;
1850   int hasdu;
1851
1852   Id *dirmap;
1853   int nmap;
1854   Repodata *olddata;
1855 };
1856
1857
1858 static int
1859 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1860 {
1861   struct ducbdata *cbd = cbdata;
1862   Id mp;
1863
1864   if (data != cbd->olddata)
1865     {
1866       Id dn, mp, comp, *dirmap, *dirs;
1867       int i, compl;
1868       const char *compstr;
1869       struct mptree *mptree;
1870
1871       /* create map from dir to mptree */
1872       cbd->dirmap = solv_free(cbd->dirmap);
1873       cbd->nmap = 0;
1874       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1875       mptree = cbd->mptree;
1876       mp = 0;
1877       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1878         {
1879           comp = *dirs++;
1880           if (comp <= 0)
1881             {
1882               mp = dirmap[-comp];
1883               continue;
1884             }
1885           if (mp < 0)
1886             {
1887               /* unconnected */
1888               dirmap[dn] = mp;
1889               continue;
1890             }
1891           if (!mptree[mp].child)
1892             {
1893               dirmap[dn] = -mp;
1894               continue;
1895             }
1896           if (data->localpool)
1897             compstr = stringpool_id2str(&data->spool, comp);
1898           else
1899             compstr = pool_id2str(data->repo->pool, comp);
1900           compl = strlen(compstr);
1901           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1902             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1903               break;
1904           dirmap[dn] = i ? i : -mp;
1905         }
1906       /* change dirmap to point to mountpoint instead of mptree */
1907       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1908         {
1909           mp = dirmap[dn];
1910           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1911         }
1912       cbd->dirmap = dirmap;
1913       cbd->nmap = data->dirpool.ndirs;
1914       cbd->olddata = data;
1915     }
1916   cbd->hasdu = 1;
1917   if (value->id < 0 || value->id >= cbd->nmap)
1918     return 0;
1919   mp = cbd->dirmap[value->id];
1920   if (mp < 0)
1921     return 0;
1922   if (cbd->addsub > 0)
1923     {
1924       cbd->mps[mp].kbytes += value->num;
1925       cbd->mps[mp].files += value->num2;
1926     }
1927   else
1928     {
1929       cbd->mps[mp].kbytes -= value->num;
1930       cbd->mps[mp].files -= value->num2;
1931     }
1932   return 0;
1933 }
1934
1935 static void
1936 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1937 {
1938   int i;
1939   if (mptree[pos].mountpoint == -1)
1940     mptree[pos].mountpoint = mountpoint;
1941   else
1942     mountpoint = mptree[pos].mountpoint;
1943   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1944     propagate_mountpoints(mptree, i, mountpoint);
1945 }
1946
1947 #define MPTREE_BLOCK 15
1948
1949 static struct mptree *
1950 create_mptree(DUChanges *mps, int nmps)
1951 {
1952   int i, nmptree;
1953   struct mptree *mptree;
1954   int pos, compl;
1955   int mp;
1956   const char *p, *path, *compstr;
1957
1958   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1959
1960   /* our root node */
1961   mptree[0].sibling = 0;
1962   mptree[0].child = 0;
1963   mptree[0].comp = 0;
1964   mptree[0].compl = 0;
1965   mptree[0].mountpoint = -1;
1966   nmptree = 1;
1967
1968   /* create component tree */
1969   for (mp = 0; mp < nmps; mp++)
1970     {
1971       mps[mp].kbytes = 0;
1972       mps[mp].files = 0;
1973       pos = 0;
1974       path = mps[mp].path;
1975       while(*path == '/')
1976         path++;
1977       while (*path)
1978         {
1979           if ((p = strchr(path, '/')) == 0)
1980             {
1981               compstr = path;
1982               compl = strlen(compstr);
1983               path += compl;
1984             }
1985           else
1986             {
1987               compstr = path;
1988               compl = p - path;
1989               path = p + 1;
1990               while(*path == '/')
1991                 path++;
1992             }
1993           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1994             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1995               break;
1996           if (!i)
1997             {
1998               /* create new node */
1999               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
2000               i = nmptree++;
2001               mptree[i].sibling = mptree[pos].child;
2002               mptree[i].child = 0;
2003               mptree[i].comp = compstr;
2004               mptree[i].compl = compl;
2005               mptree[i].mountpoint = -1;
2006               mptree[pos].child = i;
2007             }
2008           pos = i;
2009         }
2010       mptree[pos].mountpoint = mp;
2011     }
2012
2013   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
2014
2015 #if 0
2016   for (i = 0; i < nmptree; i++)
2017     {
2018       printf("#%d sibling: %d\n", i, mptree[i].sibling);
2019       printf("#%d child: %d\n", i, mptree[i].child);
2020       printf("#%d comp: %s\n", i, mptree[i].comp);
2021       printf("#%d compl: %d\n", i, mptree[i].compl);
2022       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
2023     }
2024 #endif
2025
2026   return mptree;
2027 }
2028
2029 void
2030 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
2031 {
2032   struct mptree *mptree;
2033   struct ducbdata cbd;
2034   Solvable *s;
2035   int sp;
2036   Map ignoredu;
2037   Repo *oldinstalled = pool->installed;
2038
2039   map_init(&ignoredu, 0);
2040   mptree = create_mptree(mps, nmps);
2041
2042   cbd.mps = mps;
2043   cbd.dirmap = 0;
2044   cbd.nmap = 0;
2045   cbd.olddata = 0;
2046   cbd.mptree = mptree;
2047   cbd.addsub = 1;
2048   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2049     {
2050       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2051         continue;
2052       if (!MAPTST(installedmap, sp))
2053         continue;
2054       cbd.hasdu = 0;
2055       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2056       if (!cbd.hasdu && oldinstalled)
2057         {
2058           Id op, opp;
2059           /* no du data available, ignore data of all installed solvables we obsolete */
2060           if (!ignoredu.size)
2061             map_grow(&ignoredu, oldinstalled->end - oldinstalled->start);
2062           if (s->obsoletes)
2063             {
2064               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2065               while ((obs = *obsp++) != 0)
2066                 FOR_PROVIDES(op, opp, obs)
2067                   if (op >= oldinstalled->start && op < oldinstalled->end)
2068                     MAPSET(&ignoredu, op - oldinstalled->start);
2069             }
2070           FOR_PROVIDES(op, opp, s->name)
2071             if (pool->solvables[op].name == s->name)
2072               if (op >= oldinstalled->start && op < oldinstalled->end)
2073                 MAPSET(&ignoredu, op - oldinstalled->start);
2074         }
2075     }
2076   cbd.addsub = -1;
2077   if (oldinstalled)
2078     {
2079       /* assumes we allways have du data for installed solvables */
2080       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2081         {
2082           if (MAPTST(installedmap, sp))
2083             continue;
2084           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
2085             continue;
2086           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2087         }
2088     }
2089   map_free(&ignoredu);
2090   solv_free(cbd.dirmap);
2091   solv_free(mptree);
2092 }
2093
2094 int
2095 pool_calc_installsizechange(Pool *pool, Map *installedmap)
2096 {
2097   Id sp;
2098   Solvable *s;
2099   int change = 0;
2100   Repo *oldinstalled = pool->installed;
2101
2102   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2103     {
2104       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2105         continue;
2106       if (!MAPTST(installedmap, sp))
2107         continue;
2108       change += solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2109     }
2110   if (oldinstalled)
2111     {
2112       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2113         {
2114           if (MAPTST(installedmap, sp))
2115             continue;
2116           change -= solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2117         }
2118     }
2119   return change;
2120 }
2121
2122 /* map:
2123  *  1: installed
2124  *  2: conflicts with installed
2125  *  8: interesting (only true if installed)
2126  * 16: undecided
2127  */
2128
2129 static inline Id dep2name(Pool *pool, Id dep)
2130 {
2131   while (ISRELDEP(dep))
2132     {
2133       Reldep *rd = GETRELDEP(pool, dep);
2134       dep = rd->name;
2135     }
2136   return dep;
2137 }
2138
2139 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con)
2140 {
2141   Id p, pp;
2142   Solvable *sn = pool->solvables + n;
2143
2144   FOR_PROVIDES(p, pp, sn->name)
2145     {
2146       Solvable *s = pool->solvables + p;
2147       if (s->name != sn->name || s->arch != sn->arch)
2148         continue;
2149       if ((map[p] & 9) != 9)
2150         continue;
2151       if (pool_match_nevr(pool, pool->solvables + p, con))
2152         continue;
2153       return 1;         /* found installed package that doesn't conflict */
2154     }
2155   return 0;
2156 }
2157
2158 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *multiversionmap)
2159 {
2160   Id p, pp;
2161   int r = 0;
2162   FOR_PROVIDES(p, pp, dep)
2163     {
2164       if (p == SYSTEMSOLVABLE)
2165         return 1;       /* always boring, as never constraining */
2166       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
2167         continue;
2168       if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
2169         if (providedbyinstalled_multiversion(pool, map, p, dep))
2170           continue;
2171       if ((map[p] & 9) == 9)
2172         return 9;
2173       r |= map[p] & 17;
2174     }
2175   return r;
2176 }
2177
2178 /*
2179  * pool_trivial_installable - calculate if a set of solvables is
2180  * trivial installable without any other installs/deinstalls of
2181  * packages not belonging to the set.
2182  *
2183  * the state is returned in the result queue:
2184  * 1:  solvable is installable without any other package changes
2185  * 0:  solvable is not installable
2186  * -1: solvable is installable, but doesn't constrain any installed packages
2187  */
2188
2189 void
2190 pool_trivial_installable_multiversionmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *multiversionmap)
2191 {
2192   int i, r, m, did;
2193   Id p, *dp, con, *conp, req, *reqp;
2194   unsigned char *map;
2195   Solvable *s;
2196
2197   map = solv_calloc(pool->nsolvables, 1);
2198   for (p = 1; p < pool->nsolvables; p++)
2199     {
2200       if (!MAPTST(installedmap, p))
2201         continue;
2202       map[p] |= 9;
2203       s = pool->solvables + p;
2204       if (!s->conflicts)
2205         continue;
2206       conp = s->repo->idarraydata + s->conflicts;
2207       while ((con = *conp++) != 0)
2208         {
2209           dp = pool_whatprovides_ptr(pool, con);
2210           for (; *dp; dp++)
2211             map[p] |= 2;        /* XXX: self conflict ? */
2212         }
2213     }
2214   for (i = 0; i < pkgs->count; i++)
2215     map[pkgs->elements[i]] = 16;
2216
2217   for (i = 0, did = 0; did < pkgs->count; i++, did++)
2218     {
2219       if (i == pkgs->count)
2220         i = 0;
2221       p = pkgs->elements[i];
2222       if ((map[p] & 16) == 0)
2223         continue;
2224       if ((map[p] & 2) != 0)
2225         {
2226           map[p] = 2;
2227           continue;
2228         }
2229       s = pool->solvables + p;
2230       m = 1;
2231       if (s->requires)
2232         {
2233           reqp = s->repo->idarraydata + s->requires;
2234           while ((req = *reqp++) != 0)
2235             {
2236               if (req == SOLVABLE_PREREQMARKER)
2237                 continue;
2238               r = providedbyinstalled(pool, map, req, 0, 0);
2239               if (!r)
2240                 {
2241                   /* decided and miss */
2242                   map[p] = 2;
2243                   did = 0;
2244                   break;
2245                 }
2246               if (r == 16)
2247                 break;  /* undecided */
2248               m |= r;   /* 1 | 9 | 17 */
2249             }
2250           if (req)
2251             continue;
2252           if ((m & 9) == 9)
2253             m = 9;
2254         }
2255       if (s->conflicts)
2256         {
2257           int ispatch = 0;      /* see solver.c patch handling */
2258
2259           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
2260             ispatch = 1;
2261           conp = s->repo->idarraydata + s->conflicts;
2262           while ((con = *conp++) != 0)
2263             {
2264               if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2265                 {
2266                   map[p] = 2;
2267                   did = 0;
2268                   break;
2269                 }
2270               if ((m == 1 || m == 17) && ISRELDEP(con))
2271                 {
2272                   con = dep2name(pool, con);
2273                   if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2274                     m = 9;
2275                 }
2276             }
2277           if (con)
2278             continue;   /* found a conflict */
2279         }
2280 #if 0
2281       if (s->repo && s->repo != oldinstalled)
2282         {
2283           Id p2, obs, *obsp, *pp;
2284           Solvable *s2;
2285           if (s->obsoletes)
2286             {
2287               obsp = s->repo->idarraydata + s->obsoletes;
2288               while ((obs = *obsp++) != 0)
2289                 {
2290                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
2291                     {
2292                       map[p] = 2;
2293                       break;
2294                     }
2295                 }
2296               if (obs)
2297                 continue;
2298             }
2299           FOR_PROVIDES(p2, pp, s->name)
2300             {
2301               s2 = pool->solvables + p2;
2302               if (s2->name == s->name && (map[p2] & 1) != 0)
2303                 {
2304                   map[p] = 2;
2305                   break;
2306                 }
2307             }
2308           if (p2)
2309             continue;
2310         }
2311 #endif
2312       if (m != map[p])
2313         {
2314           map[p] = m;
2315           did = 0;
2316         }
2317     }
2318   queue_free(res);
2319   queue_init_clone(res, pkgs);
2320   for (i = 0; i < pkgs->count; i++)
2321     {
2322       m = map[pkgs->elements[i]];
2323       if ((m & 9) == 9)
2324         r = 1;
2325       else if (m & 1)
2326         r = -1;
2327       else
2328         r = 0;
2329       res->elements[i] = r;
2330     }
2331   free(map);
2332 }
2333
2334 void
2335 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
2336 {
2337   pool_trivial_installable_multiversionmap(pool, installedmap, pkgs, res, 0);
2338 }
2339
2340 const char *
2341 pool_lookup_str(Pool *pool, Id entry, Id keyname)
2342 {
2343   if (entry == SOLVID_POS && pool->pos.repo)
2344     return repo_lookup_str(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2345   if (entry <= 0)
2346     return 0;
2347   return solvable_lookup_str(pool->solvables + entry, keyname);
2348 }
2349
2350 Id
2351 pool_lookup_id(Pool *pool, Id entry, Id keyname)
2352 {
2353   if (entry == SOLVID_POS && pool->pos.repo)
2354     return repo_lookup_id(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2355   if (entry <= 0)
2356     return 0;
2357   return solvable_lookup_id(pool->solvables + entry, keyname);
2358 }
2359
2360 unsigned long long
2361 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned long long notfound)
2362 {
2363   if (entry == SOLVID_POS && pool->pos.repo)
2364     return repo_lookup_num(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, notfound);
2365   if (entry <= 0)
2366     return notfound;
2367   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
2368 }
2369
2370 int
2371 pool_lookup_void(Pool *pool, Id entry, Id keyname)
2372 {
2373   if (entry == SOLVID_POS && pool->pos.repo)
2374     return repo_lookup_void(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2375   if (entry <= 0)
2376     return 0;
2377   return solvable_lookup_void(pool->solvables + entry, keyname);
2378 }
2379
2380 const unsigned char *
2381 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2382 {
2383   if (entry == SOLVID_POS && pool->pos.repo)
2384     return repo_lookup_bin_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2385   if (entry <= 0)
2386     return 0;
2387   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
2388 }
2389
2390 const char *
2391 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2392 {
2393   if (entry == SOLVID_POS && pool->pos.repo)
2394     return repo_lookup_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2395   if (entry <= 0)
2396     return 0;
2397   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2398 }
2399
2400 int
2401 pool_lookup_idarray(Pool *pool, Id entry, Id keyname, Queue *q)
2402 {
2403   if (entry == SOLVID_POS && pool->pos.repo)
2404     return repo_lookup_idarray(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, q);
2405   if (entry <= 0)
2406     return 0;
2407   return solvable_lookup_idarray(pool->solvables + entry, keyname, q);
2408 }
2409
2410 const char *
2411 pool_lookup_deltalocation(Pool *pool, Id entry, unsigned int *medianrp)
2412 {
2413   const char *loc;
2414   if (medianrp)
2415     *medianrp = 0;
2416   if (entry != SOLVID_POS)
2417     return 0;
2418   loc = pool_lookup_str(pool, entry, DELTA_LOCATION_DIR);
2419   loc = pool_tmpjoin(pool, loc, loc ? "/" : 0, pool_lookup_str(pool, entry, DELTA_LOCATION_NAME));
2420   loc = pool_tmpappend(pool, loc, "-", pool_lookup_str(pool, entry, DELTA_LOCATION_EVR));
2421   loc = pool_tmpappend(pool, loc, ".", pool_lookup_str(pool, entry, DELTA_LOCATION_SUFFIX));
2422   return loc;
2423 }
2424
2425 static void
2426 add_new_provider(Pool *pool, Id id, Id p)
2427 {
2428   Queue q;
2429   Id *pp;
2430
2431   while (ISRELDEP(id))
2432     {
2433       Reldep *rd = GETRELDEP(pool, id);
2434       id = rd->name;
2435     }
2436
2437   queue_init(&q);
2438   for (pp = pool->whatprovidesdata + pool->whatprovides[id]; *pp; pp++)
2439     {
2440       if (*pp == p)
2441         {
2442           queue_free(&q);
2443           return;
2444         }
2445       if (*pp > p)
2446         {
2447           queue_push(&q, p);
2448           p = 0;
2449         }
2450       queue_push(&q, *pp);
2451     }
2452   if (p)
2453     queue_push(&q, p);
2454   pool->whatprovides[id] = pool_queuetowhatprovides(pool, &q);
2455   queue_free(&q);
2456 }
2457
2458 void
2459 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2460 {
2461   int hadhashes = pool->relhashtbl ? 1 : 0;
2462   Solvable *s;
2463   Id fn, p, q, md5;
2464   Id id;
2465   int i;
2466
2467   if (!conflicts->count)
2468     return;
2469   for (i = 0; i < conflicts->count; i += 6)
2470     {
2471       fn = conflicts->elements[i];
2472       p = conflicts->elements[i + 1];
2473       md5 = conflicts->elements[i + 2];
2474       q = conflicts->elements[i + 4];
2475       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2476       s = pool->solvables + p;
2477       if (!s->repo)
2478         continue;
2479       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2480       if (pool->whatprovides)
2481         add_new_provider(pool, fn, p);
2482       if (pool->whatprovides_rel)
2483         pool->whatprovides_rel[GETRELID(id)] = 0;       /* clear cache */
2484       s = pool->solvables + q;
2485       if (!s->repo)
2486         continue;
2487       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2488     }
2489   if (!hadhashes)
2490     pool_freeidhashes(pool);
2491 }
2492
2493 char *
2494 pool_prepend_rootdir(Pool *pool, const char *path)
2495 {
2496   if (!path)
2497     return 0;
2498   if (!pool->rootdir)
2499     return solv_strdup(path);
2500   return solv_dupjoin(pool->rootdir, "/", *path == '/' ? path + 1 : path);
2501 }
2502
2503 const char *
2504 pool_prepend_rootdir_tmp(Pool *pool, const char *path)
2505 {
2506   if (!path)
2507     return 0;
2508   if (!pool->rootdir)
2509     return path;
2510   return pool_tmpjoin(pool, pool->rootdir, "/", *path == '/' ? path + 1 : path);
2511 }
2512
2513 void
2514 pool_set_rootdir(Pool *pool, const char *rootdir)
2515 {
2516   solv_free(pool->rootdir);
2517   pool->rootdir = solv_strdup(rootdir);
2518 }
2519
2520 const char *
2521 pool_get_rootdir(Pool *pool)
2522 {
2523   return pool->rootdir;
2524 }
2525
2526 /* only used in libzypp */
2527 void
2528 pool_set_custom_vendorcheck(Pool *pool, int (*vendorcheck)(Pool *, Solvable *, Solvable *))
2529 {
2530   pool->custom_vendorcheck = vendorcheck;
2531 }
2532
2533 /* EOF */