fix last commit
[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  */
884 Id
885 pool_addrelproviders(Pool *pool, Id d)
886 {
887   Reldep *rd;
888   Reldep *prd;
889   Queue plist;
890   Id buf[16];
891   Id name, evr, flags;
892   Id pid, *pidp;
893   Id p, *pp;
894
895   if (!ISRELDEP(d))
896     return pool_addstdproviders(pool, d);
897   rd = GETRELDEP(pool, d);
898   name = rd->name;
899   evr = rd->evr;
900   flags = rd->flags;
901   d = GETRELID(d);
902   queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
903
904   if (flags >= 8)
905     {
906       /* special relation */
907       Id wp = 0;
908       Id *pp2, *pp3;
909
910       switch (flags)
911         {
912         case REL_AND:
913         case REL_WITH:
914           wp = pool_whatprovides(pool, name);
915           pp2 = pool_whatprovides_ptr(pool, evr);
916           pp = pool->whatprovidesdata + wp;
917           while ((p = *pp++) != 0)
918             {
919               for (pp3 = pp2; *pp3; pp3++)
920                 if (*pp3 == p)
921                   break;
922               if (*pp3)
923                 queue_push(&plist, p);  /* found it */
924               else
925                 wp = 0;
926             }
927           break;
928         case REL_OR:
929           wp = pool_whatprovides(pool, name);
930           pp = pool->whatprovidesdata + wp;
931           if (!*pp)
932             wp = pool_whatprovides(pool, evr);
933           else
934             {
935               int cnt;
936               while ((p = *pp++) != 0)
937                 queue_push(&plist, p);
938               cnt = plist.count;
939               pp = pool_whatprovides_ptr(pool, evr);
940               while ((p = *pp++) != 0)
941                 queue_pushunique(&plist, p);
942               if (plist.count != cnt)
943                 wp = 0;
944             }
945           break;
946         case REL_NAMESPACE:
947           if (name == NAMESPACE_OTHERPROVIDERS)
948             {
949               wp = pool_whatprovides(pool, evr);
950               break;
951             }
952           if (pool->nscallback)
953             {
954               /* ask callback which packages provide the dependency
955                * 0:  none
956                * 1:  the system (aka SYSTEMSOLVABLE)
957                * >1: set of packages, stored as offset on whatprovidesdata
958                */
959               p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
960               if (p > 1)
961                 wp = p;
962               if (p == 1)
963                 queue_push(&plist, SYSTEMSOLVABLE);
964             }
965           break;
966         case REL_ARCH:
967           /* small hack: make it possible to match <pkg>.src
968            * we have to iterate over the solvables as src packages do not
969            * provide anything, thus they are not indexed in our
970            * whatprovides hash */
971           if (evr == ARCH_SRC || evr == ARCH_NOSRC)
972             {
973               Solvable *s;
974               for (p = 1, s = pool->solvables + p; p < pool->nsolvables; p++, s++)
975                 {
976                   if (!s->repo)
977                     continue;
978                   if (s->arch != evr && s->arch != ARCH_NOSRC)
979                     continue;
980                   if (pool_disabled_solvable(pool, s))
981                     continue;
982                   if (!name || pool_match_nevr(pool, s, name))
983                     queue_push(&plist, p);
984                 }
985               break;
986             }
987           if (!name)
988             {
989               FOR_POOL_SOLVABLES(p)
990                 {
991                   Solvable *s = pool->solvables + p;
992                   if (s->repo != pool->installed && !pool_installable(pool, s))
993                     continue;
994                   if (s->arch == evr)
995                     queue_push(&plist, p);
996                 }
997               break;
998             }
999           wp = pool_whatprovides(pool, name);
1000           pp = pool->whatprovidesdata + wp;
1001           while ((p = *pp++) != 0)
1002             {
1003               Solvable *s = pool->solvables + p;
1004               if (s->arch == evr)
1005                 queue_push(&plist, p);
1006               else
1007                 wp = 0;
1008             }
1009           break;
1010         case REL_MULTIARCH:
1011           if (evr != ARCH_ANY)
1012             break;
1013           /* XXX : need to check for Multi-Arch: allowed! */
1014           wp = pool_whatprovides(pool, name);
1015           break;
1016         case REL_KIND:
1017           /* package kind filtering */
1018           if (!name)
1019             {
1020               FOR_POOL_SOLVABLES(p)
1021                 {
1022                   Solvable *s = pool->solvables + p;
1023                   if (s->repo != pool->installed && !pool_installable(pool, s))
1024                     continue;
1025                   if (pool_is_kind(pool, s->name, evr))
1026                     queue_push(&plist, p);
1027                 }
1028               break;
1029             }
1030           wp = pool_whatprovides(pool, name);
1031           pp = pool->whatprovidesdata + wp;
1032           while ((p = *pp++) != 0)
1033             {
1034               Solvable *s = pool->solvables + p;
1035               if (pool_is_kind(pool, s->name, evr))
1036                 queue_push(&plist, p);
1037               else
1038                 wp = 0;
1039             }
1040           break;
1041         case REL_FILECONFLICT:
1042           pp = pool_whatprovides_ptr(pool, name);
1043           while ((p = *pp++) != 0)
1044             {
1045               Id origd = MAKERELDEP(d);
1046               Solvable *s = pool->solvables + p;
1047               if (!s->provides)
1048                 continue;
1049               pidp = s->repo->idarraydata + s->provides;
1050               while ((pid = *pidp++) != 0)
1051                 if (pid == origd)
1052                   break;
1053               if (pid)
1054                 queue_push(&plist, p);
1055             }
1056           break;
1057         default:
1058           break;
1059         }
1060       if (wp)
1061         {
1062           /* we can reuse an existing entry */
1063           queue_free(&plist);
1064           pool->whatprovides_rel[d] = wp;
1065           return wp;
1066         }
1067     }
1068   else if (flags)
1069     {
1070       /* simple version comparison relation */
1071 #if 0
1072       POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: what provides %s?\n", pool_dep2str(pool, name));
1073 #endif
1074       pp = pool_whatprovides_ptr(pool, name);
1075       while (ISRELDEP(name))
1076         {
1077           rd = GETRELDEP(pool, name);
1078           name = rd->name;
1079         }
1080       while ((p = *pp++) != 0)
1081         {
1082           Solvable *s = pool->solvables + p;
1083           if (!s->provides)
1084             {
1085               /* no provides - check nevr */
1086               if (pool_match_nevr_rel(pool, s, MAKERELDEP(d)))
1087                 queue_push(&plist, p);
1088               continue;
1089             }
1090           /* solvable p provides name in some rels */
1091           pidp = s->repo->idarraydata + s->provides;
1092           while ((pid = *pidp++) != 0)
1093             {
1094               if (!ISRELDEP(pid))
1095                 {
1096                   if (pid != name)
1097                     continue;           /* wrong provides name */
1098                   if (pool->disttype == DISTTYPE_DEB)
1099                     continue;           /* unversioned provides can never match versioned deps */
1100                   break;
1101                 }
1102               prd = GETRELDEP(pool, pid);
1103               if (prd->name != name)
1104                 continue;               /* wrong provides name */
1105               /* right package, both deps are rels. check flags/evr */
1106               if (pool_match_flags_evr(pool, prd->flags, prd->evr, flags, evr))
1107                 break;  /* matches */
1108             }
1109           if (!pid)
1110             continue;   /* none of the providers matched */
1111           queue_push(&plist, p);
1112         }
1113       /* make our system solvable provide all unknown rpmlib() stuff */
1114       if (plist.count == 0 && !strncmp(pool_id2str(pool, name), "rpmlib(", 7))
1115         queue_push(&plist, SYSTEMSOLVABLE);
1116     }
1117   /* add providers to whatprovides */
1118 #if 0
1119   POOL_DEBUG(SOLV_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
1120 #endif
1121   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
1122   queue_free(&plist);
1123
1124   return pool->whatprovides_rel[d];
1125 }
1126
1127 void
1128 pool_flush_namespaceproviders(Pool *pool, Id ns, Id evr)
1129 {
1130   int nrels = pool->nrels;
1131   Id d;
1132   Reldep *rd;
1133
1134   if (!pool->whatprovides_rel)
1135     return;
1136   for (d = 1, rd = pool->rels + d; d < nrels; d++, rd++)
1137     {
1138       if (rd->flags != REL_NAMESPACE || rd->name == NAMESPACE_OTHERPROVIDERS)
1139         continue;
1140       if (ns && rd->name != ns)
1141         continue;
1142       if (evr && rd->evr != evr)
1143         continue;
1144       pool->whatprovides_rel[d] = 0;
1145     }
1146 }
1147
1148 /* intersect dependencies in keyname with dep, return list of matching packages */
1149 void
1150 pool_whatmatchesdep(Pool *pool, Id keyname, Id dep, Queue *q, int marker)
1151 {
1152   Id p;
1153
1154   queue_empty(q);
1155   FOR_POOL_SOLVABLES(p)
1156     {
1157       Solvable *s = pool->solvables + p;
1158       if (s->repo->disabled)
1159         continue;
1160       if (s->repo != pool->installed && !pool_installable(pool, s))
1161         continue;
1162       if (solvable_matchesdep(s, keyname, dep, marker))
1163         queue_push(q, p);
1164     }
1165 }
1166
1167 /*************************************************************************/
1168
1169 void
1170 pool_debug(Pool *pool, int type, const char *format, ...)
1171 {
1172   va_list args;
1173   char buf[1024];
1174
1175   if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0)
1176     {
1177       if ((pool->debugmask & type) == 0)
1178         return;
1179     }
1180   va_start(args, format);
1181   if (!pool->debugcallback)
1182     {
1183       if ((type & (SOLV_FATAL|SOLV_ERROR)) == 0 && !(pool->debugmask & SOLV_DEBUG_TO_STDERR))
1184         vprintf(format, args);
1185       else
1186         vfprintf(stderr, format, args);
1187       return;
1188     }
1189   vsnprintf(buf, sizeof(buf), format, args);
1190   va_end(args);
1191   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
1192 }
1193
1194 int
1195 pool_error(Pool *pool, int ret, const char *format, ...)
1196 {
1197   va_list args;
1198   int l;
1199   va_start(args, format);
1200   if (!pool->errstr)
1201     {
1202       pool->errstra = 1024;
1203       pool->errstr = solv_malloc(pool->errstra);
1204     }
1205   if (!*format)
1206     {
1207       *pool->errstr = 0;
1208       l = 0;
1209     }
1210   else
1211     l = vsnprintf(pool->errstr, pool->errstra, format, args);
1212   va_end(args);
1213   if (l >= 0 && l + 1 > pool->errstra)
1214     {
1215       pool->errstra = l + 256;
1216       pool->errstr = solv_realloc(pool->errstr, pool->errstra);
1217       va_start(args, format);
1218       l = vsnprintf(pool->errstr, pool->errstra, format, args);
1219       va_end(args);
1220     }
1221   if (l < 0)
1222     strcpy(pool->errstr, "unknown error");
1223   if (pool->debugmask & SOLV_ERROR)
1224     pool_debug(pool, SOLV_ERROR, "%s\n", pool->errstr);
1225   return ret;
1226 }
1227
1228 char *
1229 pool_errstr(Pool *pool)
1230 {
1231   return pool->errstr ? pool->errstr : "no error";
1232 }
1233
1234 void
1235 pool_setdebuglevel(Pool *pool, int level)
1236 {
1237   int mask = SOLV_DEBUG_RESULT;
1238   if (level > 0)
1239     mask |= SOLV_DEBUG_STATS|SOLV_DEBUG_ANALYZE|SOLV_DEBUG_UNSOLVABLE|SOLV_DEBUG_SOLVER|SOLV_DEBUG_TRANSACTION|SOLV_ERROR;
1240   if (level > 1)
1241     mask |= SOLV_DEBUG_JOB|SOLV_DEBUG_SOLUTIONS|SOLV_DEBUG_POLICY;
1242   if (level > 2)
1243     mask |= SOLV_DEBUG_PROPAGATE;
1244   if (level > 3)
1245     mask |= SOLV_DEBUG_RULE_CREATION;
1246   mask |= pool->debugmask & SOLV_DEBUG_TO_STDERR;       /* keep bit */
1247   pool->debugmask = mask;
1248 }
1249
1250 void pool_setdebugcallback(Pool *pool, void (*debugcallback)(struct _Pool *, void *data, int type, const char *str), void *debugcallbackdata)
1251 {
1252   pool->debugcallback = debugcallback;
1253   pool->debugcallbackdata = debugcallbackdata;
1254 }
1255
1256 void pool_setdebugmask(Pool *pool, int mask)
1257 {
1258   pool->debugmask = mask;
1259 }
1260
1261 void pool_setloadcallback(Pool *pool, int (*cb)(struct _Pool *, struct _Repodata *, void *), void *loadcbdata)
1262 {
1263   pool->loadcallback = cb;
1264   pool->loadcallbackdata = loadcbdata;
1265 }
1266
1267 void pool_setnamespacecallback(Pool *pool, Id (*cb)(struct _Pool *, void *, Id, Id), void *nscbdata)
1268 {
1269   pool->nscallback = cb;
1270   pool->nscallbackdata = nscbdata;
1271 }
1272
1273 /*************************************************************************/
1274
1275 struct searchfiles {
1276   Id *ids;
1277   char **dirs;
1278   char **names;
1279   int nfiles;
1280   Map seen;
1281 };
1282
1283 #define SEARCHFILES_BLOCK 127
1284
1285 static void
1286 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
1287 {
1288   Id dep, sid;
1289   const char *s, *sr;
1290   struct searchfiles *csf;
1291
1292   while ((dep = *ida++) != 0)
1293     {
1294       csf = sf;
1295       while (ISRELDEP(dep))
1296         {
1297           Reldep *rd;
1298           sid = pool->ss.nstrings + GETRELID(dep);
1299           if (MAPTST(&csf->seen, sid))
1300             {
1301               dep = 0;
1302               break;
1303             }
1304           MAPSET(&csf->seen, sid);
1305           rd = GETRELDEP(pool, dep);
1306           if (rd->flags < 8)
1307             dep = rd->name;
1308           else if (rd->flags == REL_NAMESPACE)
1309             {
1310               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
1311                 {
1312                   csf = isf;
1313                   if (!csf || MAPTST(&csf->seen, sid))
1314                     {
1315                       dep = 0;
1316                       break;
1317                     }
1318                   MAPSET(&csf->seen, sid);
1319                 }
1320               dep = rd->evr;
1321             }
1322           else if (rd->flags == REL_FILECONFLICT)
1323             {
1324               dep = 0;
1325               break;
1326             }
1327           else
1328             {
1329               Id ids[2];
1330               ids[0] = rd->name;
1331               ids[1] = 0;
1332               pool_addfileprovides_dep(pool, ids, csf, isf);
1333               dep = rd->evr;
1334             }
1335         }
1336       if (!dep)
1337         continue;
1338       if (MAPTST(&csf->seen, dep))
1339         continue;
1340       MAPSET(&csf->seen, dep);
1341       s = pool_id2str(pool, dep);
1342       if (*s != '/')
1343         continue;
1344       if (csf != isf && pool->addedfileprovides == 1 && !repodata_filelistfilter_matches(0, s))
1345         continue;       /* skip non-standard locations csf == isf: installed case */
1346       csf->ids = solv_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
1347       csf->dirs = solv_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1348       csf->names = solv_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
1349       csf->ids[csf->nfiles] = dep;
1350       sr = strrchr(s, '/');
1351       csf->names[csf->nfiles] = solv_strdup(sr + 1);
1352       csf->dirs[csf->nfiles] = solv_malloc(sr - s + 1);
1353       if (sr != s)
1354         strncpy(csf->dirs[csf->nfiles], s, sr - s);
1355       csf->dirs[csf->nfiles][sr - s] = 0;
1356       csf->nfiles++;
1357     }
1358 }
1359
1360 struct addfileprovides_cbdata {
1361   int nfiles;
1362   Id *ids;
1363   char **dirs;
1364   char **names;
1365
1366   Id *dids;
1367
1368   Map providedids;
1369
1370   Map useddirs;
1371 };
1372
1373 static int
1374 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1375 {
1376   struct addfileprovides_cbdata *cbd = cbdata;
1377   int i;
1378
1379   if (!cbd->useddirs.size)
1380     {
1381       map_init(&cbd->useddirs, data->dirpool.ndirs + 1);
1382       for (i = 0; i < cbd->nfiles; i++)
1383         {
1384           Id did;
1385           if (MAPTST(&cbd->providedids, cbd->ids[i]))
1386             {
1387               cbd->dids[i] = 0;
1388               continue;
1389             }
1390           did = repodata_str2dir(data, cbd->dirs[i], 0);
1391           cbd->dids[i] = did;
1392           if (did)
1393             MAPSET(&cbd->useddirs, did);
1394         }
1395       repodata_free_dircache(data);
1396     }
1397   if (value->id >= data->dirpool.ndirs || !MAPTST(&cbd->useddirs, value->id))
1398     return 0;
1399   for (i = 0; i < cbd->nfiles; i++)
1400     {
1401       if (cbd->dids[i] != value->id)
1402         continue;
1403       if (!strcmp(cbd->names[i], value->str))
1404         break;
1405     }
1406   if (i == cbd->nfiles)
1407     return 0;
1408   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
1409   return 0;
1410 }
1411
1412 static void
1413 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
1414 {
1415   Id p;
1416   Repodata *data;
1417   Repo *repo;
1418   Queue fileprovidesq;
1419   int i, j, repoid, repodataid;
1420   int provstart, provend;
1421   Map donemap;
1422   int ndone, incomplete;
1423
1424   if (!pool->urepos)
1425     return;
1426
1427   cbd->nfiles = sf->nfiles;
1428   cbd->ids = sf->ids;
1429   cbd->dirs = sf->dirs;
1430   cbd->names = sf->names;
1431   cbd->dids = solv_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
1432   map_init(&cbd->providedids, pool->ss.nstrings);
1433
1434   repoid = 1;
1435   repo = repoonly ? repoonly : pool->repos[repoid];
1436   map_init(&donemap, pool->nsolvables);
1437   queue_init(&fileprovidesq);
1438   provstart = provend = 0;
1439   for (;;)
1440     {
1441       if (!repo || repo->disabled)
1442         {
1443           if (repoonly || ++repoid == pool->nrepos)
1444             break;
1445           repo = pool->repos[repoid];
1446           continue;
1447         }
1448       ndone = 0;
1449       FOR_REPODATAS(repo, repodataid, data)
1450         {
1451           if (ndone >= repo->nsolvables)
1452             break;
1453
1454           if (repodata_lookup_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, &fileprovidesq))
1455             {
1456               map_empty(&cbd->providedids);
1457               for (i = 0; i < fileprovidesq.count; i++)
1458                 MAPSET(&cbd->providedids, fileprovidesq.elements[i]);
1459               provstart = data->start;
1460               provend = data->end;
1461               for (i = 0; i < cbd->nfiles; i++)
1462                 if (!MAPTST(&cbd->providedids, cbd->ids[i]))
1463                   break;
1464               if (i == cbd->nfiles)
1465                 {
1466                   /* great! no need to search files */
1467                   for (p = data->start; p < data->end; p++)
1468                     if (pool->solvables[p].repo == repo)
1469                       {
1470                         if (MAPTST(&donemap, p))
1471                           continue;
1472                         MAPSET(&donemap, p);
1473                         ndone++;
1474                       }
1475                   continue;
1476                 }
1477             }
1478
1479           if (!repodata_has_keyname(data, SOLVABLE_FILELIST))
1480             continue;
1481
1482           if (data->start < provstart || data->end > provend)
1483             {
1484               map_empty(&cbd->providedids);
1485               provstart = provend = 0;
1486             }
1487
1488           /* check if the data is incomplete */
1489           incomplete = 0;
1490           if (data->state == REPODATA_AVAILABLE)
1491             {
1492               for (j = 1; j < data->nkeys; j++)
1493                 if (data->keys[j].name != REPOSITORY_SOLVABLES && data->keys[j].name != SOLVABLE_FILELIST)
1494                   break;
1495               if (j < data->nkeys)
1496                 {
1497 #if 0
1498                   for (i = 0; i < cbd->nfiles; i++)
1499                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1500                       printf("need complete filelist because of %s\n", pool_id2str(pool, cbd->ids[i]));
1501 #endif
1502                   for (i = 0; i < cbd->nfiles; i++)
1503                     if (!MAPTST(&cbd->providedids, cbd->ids[i]) && !repodata_filelistfilter_matches(data, pool_id2str(pool, cbd->ids[i])))
1504                       break;
1505                   if (i < cbd->nfiles)
1506                     incomplete = 1;
1507                 }
1508             }
1509
1510           /* do the search */
1511           map_init(&cbd->useddirs, 0);
1512           for (p = data->start; p < data->end; p++)
1513             if (pool->solvables[p].repo == repo)
1514               {
1515                 if (MAPTST(&donemap, p))
1516                   continue;
1517                 repodata_search(data, p, SOLVABLE_FILELIST, 0, addfileprovides_cb, cbd);
1518                 if (!incomplete)
1519                   {
1520                     MAPSET(&donemap, p);
1521                     ndone++;
1522                   }
1523               }
1524           map_free(&cbd->useddirs);
1525         }
1526
1527       if (repoonly || ++repoid == pool->nrepos)
1528         break;
1529       repo = pool->repos[repoid];
1530     }
1531   map_free(&donemap);
1532   queue_free(&fileprovidesq);
1533   map_free(&cbd->providedids);
1534 }
1535
1536 void
1537 pool_addfileprovides_queue(Pool *pool, Queue *idq, Queue *idqinst)
1538 {
1539   Solvable *s;
1540   Repo *installed, *repo;
1541   struct searchfiles sf, isf, *isfp;
1542   struct addfileprovides_cbdata cbd;
1543   int i;
1544   unsigned int now;
1545
1546   installed = pool->installed;
1547   now = solv_timems(0);
1548   memset(&sf, 0, sizeof(sf));
1549   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
1550   memset(&isf, 0, sizeof(isf));
1551   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
1552   pool->addedfileprovides = pool->addfileprovidesfiltered ? 1 : 2;
1553
1554   if (idq)
1555     queue_empty(idq);
1556   if (idqinst)
1557     queue_empty(idqinst);
1558   isfp = installed ? &isf : 0;
1559   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
1560     {
1561       repo = s->repo;
1562       if (!repo)
1563         continue;
1564       if (s->obsoletes)
1565         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
1566       if (s->conflicts)
1567         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
1568       if (s->requires)
1569         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
1570       if (s->recommends)
1571         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
1572       if (s->suggests)
1573         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
1574       if (s->supplements)
1575         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
1576       if (s->enhances)
1577         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
1578     }
1579   map_free(&sf.seen);
1580   map_free(&isf.seen);
1581   POOL_DEBUG(SOLV_DEBUG_STATS, "found %d file dependencies, %d installed file dependencies\n", sf.nfiles, isf.nfiles);
1582   cbd.dids = 0;
1583   if (sf.nfiles)
1584     {
1585 #if 0
1586       for (i = 0; i < sf.nfiles; i++)
1587         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in filelist\n", pool_id2str(pool, sf.ids[i]));
1588 #endif
1589       pool_addfileprovides_search(pool, &cbd, &sf, 0);
1590       if (idq)
1591         for (i = 0; i < sf.nfiles; i++)
1592           queue_push(idq, sf.ids[i]);
1593       if (idqinst)
1594         for (i = 0; i < sf.nfiles; i++)
1595           queue_push(idqinst, sf.ids[i]);
1596       solv_free(sf.ids);
1597       for (i = 0; i < sf.nfiles; i++)
1598         {
1599           solv_free(sf.dirs[i]);
1600           solv_free(sf.names[i]);
1601         }
1602       solv_free(sf.dirs);
1603       solv_free(sf.names);
1604     }
1605   if (isf.nfiles)
1606     {
1607 #if 0
1608       for (i = 0; i < isf.nfiles; i++)
1609         POOL_DEBUG(SOLV_DEBUG_STATS, "looking up %s in installed filelist\n", pool_id2str(pool, isf.ids[i]));
1610 #endif
1611       if (installed)
1612         pool_addfileprovides_search(pool, &cbd, &isf, installed);
1613       if (installed && idqinst)
1614         for (i = 0; i < isf.nfiles; i++)
1615           queue_pushunique(idqinst, isf.ids[i]);
1616       solv_free(isf.ids);
1617       for (i = 0; i < isf.nfiles; i++)
1618         {
1619           solv_free(isf.dirs[i]);
1620           solv_free(isf.names[i]);
1621         }
1622       solv_free(isf.dirs);
1623       solv_free(isf.names);
1624     }
1625   solv_free(cbd.dids);
1626   pool_freewhatprovides(pool);  /* as we have added provides */
1627   POOL_DEBUG(SOLV_DEBUG_STATS, "addfileprovides took %d ms\n", solv_timems(now));
1628 }
1629
1630 void
1631 pool_addfileprovides(Pool *pool)
1632 {
1633   pool_addfileprovides_queue(pool, 0, 0);
1634 }
1635
1636 void
1637 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)
1638 {
1639   if (p)
1640     {
1641       if (pool->solvables[p].repo)
1642         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1643       return;
1644     }
1645   /* FIXME: obey callback return value! */
1646   for (p = 1; p < pool->nsolvables; p++)
1647     if (pool->solvables[p].repo)
1648       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
1649 }
1650
1651 void
1652 pool_clear_pos(Pool *pool)
1653 {
1654   memset(&pool->pos, 0, sizeof(pool->pos));
1655 }
1656
1657
1658 void
1659 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
1660 {
1661   int i;
1662
1663   pool->languagecache = solv_free(pool->languagecache);
1664   pool->languagecacheother = 0;
1665   for (i = 0; i < pool->nlanguages; i++)
1666     free((char *)pool->languages[i]);
1667   pool->languages = solv_free(pool->languages);
1668   pool->nlanguages = nlanguages;
1669   if (!nlanguages)
1670     return;
1671   pool->languages = solv_calloc(nlanguages, sizeof(const char **));
1672   for (i = 0; i < pool->nlanguages; i++)
1673     pool->languages[i] = solv_strdup(languages[i]);
1674 }
1675
1676 Id
1677 pool_id2langid(Pool *pool, Id id, const char *lang, int create)
1678 {
1679   const char *n;
1680   char buf[256], *p;
1681   int l;
1682
1683   if (!lang || !*lang)
1684     return id;
1685   n = pool_id2str(pool, id);
1686   l = strlen(n) + strlen(lang) + 2;
1687   if (l > sizeof(buf))
1688     p = solv_malloc(strlen(n) + strlen(lang) + 2);
1689   else
1690     p = buf;
1691   sprintf(p, "%s:%s", n, lang);
1692   id = pool_str2id(pool, p, create);
1693   if (p != buf)
1694     free(p);
1695   return id;
1696 }
1697
1698 char *
1699 pool_alloctmpspace(Pool *pool, int len)
1700 {
1701   int n = pool->tmpspace.n;
1702   if (!len)
1703     return 0;
1704   if (len > pool->tmpspace.len[n])
1705     {
1706       pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1707       pool->tmpspace.len[n] = len + 32;
1708     }
1709   pool->tmpspace.n = (n + 1) % POOL_TMPSPACEBUF;
1710   return pool->tmpspace.buf[n];
1711 }
1712
1713 static char *
1714 pool_alloctmpspace_free(Pool *pool, const char *space, int len)
1715 {
1716   if (space)
1717     {
1718       int n, oldn;
1719       n = oldn = pool->tmpspace.n;
1720       for (;;)
1721         {
1722           if (!n--)
1723             n = POOL_TMPSPACEBUF - 1;
1724           if (n == oldn)
1725             break;
1726           if (pool->tmpspace.buf[n] != space)
1727             continue;
1728           if (len > pool->tmpspace.len[n])
1729             {
1730               pool->tmpspace.buf[n] = solv_realloc(pool->tmpspace.buf[n], len + 32);
1731               pool->tmpspace.len[n] = len + 32;
1732             }
1733           return pool->tmpspace.buf[n];
1734         }
1735     }
1736   return 0;
1737 }
1738
1739 void
1740 pool_freetmpspace(Pool *pool, const char *space)
1741 {
1742   int n = pool->tmpspace.n;
1743   if (!space)
1744     return;
1745   n = (n + (POOL_TMPSPACEBUF - 1)) % POOL_TMPSPACEBUF;
1746   if (pool->tmpspace.buf[n] == space)
1747     pool->tmpspace.n = n;
1748 }
1749
1750 char *
1751 pool_tmpjoin(Pool *pool, const char *str1, const char *str2, const char *str3)
1752 {
1753   int l1, l2, l3;
1754   char *s, *str;
1755   l1 = str1 ? strlen(str1) : 0;
1756   l2 = str2 ? strlen(str2) : 0;
1757   l3 = str3 ? strlen(str3) : 0;
1758   s = str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1759   if (l1)
1760     {
1761       strcpy(s, str1);
1762       s += l1;
1763     }
1764   if (l2)
1765     {
1766       strcpy(s, str2);
1767       s += l2;
1768     }
1769   if (l3)
1770     {
1771       strcpy(s, str3);
1772       s += l3;
1773     }
1774   *s = 0;
1775   return str;
1776 }
1777
1778 char *
1779 pool_tmpappend(Pool *pool, const char *str1, const char *str2, const char *str3)
1780 {
1781   int l1, l2, l3;
1782   char *s, *str;
1783
1784   l1 = str1 ? strlen(str1) : 0;
1785   l2 = str2 ? strlen(str2) : 0;
1786   l3 = str3 ? strlen(str3) : 0;
1787   str = pool_alloctmpspace_free(pool, str1, l1 + l2 + l3 + 1);
1788   if (str)
1789     str1 = str;
1790   else
1791     str = pool_alloctmpspace(pool, l1 + l2 + l3 + 1);
1792   s = str;
1793   if (l1)
1794     {
1795       if (s != str1)
1796         strcpy(s, str1);
1797       s += l1;
1798     }
1799   if (l2)
1800     {
1801       strcpy(s, str2);
1802       s += l2;
1803     }
1804   if (l3)
1805     {
1806       strcpy(s, str3);
1807       s += l3;
1808     }
1809   *s = 0;
1810   return str;
1811 }
1812
1813 const char *
1814 pool_bin2hex(Pool *pool, const unsigned char *buf, int len)
1815 {
1816   char *s;
1817   if (!len)
1818     return "";
1819   s = pool_alloctmpspace(pool, 2 * len + 1);
1820   solv_bin2hex(buf, len, s);
1821   return s;
1822 }
1823
1824 /*******************************************************************/
1825
1826 struct mptree {
1827   Id sibling;
1828   Id child;
1829   const char *comp;
1830   int compl;
1831   Id mountpoint;
1832 };
1833
1834 struct ducbdata {
1835   DUChanges *mps;
1836   struct mptree *mptree;
1837   int addsub;
1838   int hasdu;
1839
1840   Id *dirmap;
1841   int nmap;
1842   Repodata *olddata;
1843 };
1844
1845
1846 static int
1847 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
1848 {
1849   struct ducbdata *cbd = cbdata;
1850   Id mp;
1851
1852   if (data != cbd->olddata)
1853     {
1854       Id dn, mp, comp, *dirmap, *dirs;
1855       int i, compl;
1856       const char *compstr;
1857       struct mptree *mptree;
1858
1859       /* create map from dir to mptree */
1860       cbd->dirmap = solv_free(cbd->dirmap);
1861       cbd->nmap = 0;
1862       dirmap = solv_calloc(data->dirpool.ndirs, sizeof(Id));
1863       mptree = cbd->mptree;
1864       mp = 0;
1865       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
1866         {
1867           comp = *dirs++;
1868           if (comp <= 0)
1869             {
1870               mp = dirmap[-comp];
1871               continue;
1872             }
1873           if (mp < 0)
1874             {
1875               /* unconnected */
1876               dirmap[dn] = mp;
1877               continue;
1878             }
1879           if (!mptree[mp].child)
1880             {
1881               dirmap[dn] = -mp;
1882               continue;
1883             }
1884           if (data->localpool)
1885             compstr = stringpool_id2str(&data->spool, comp);
1886           else
1887             compstr = pool_id2str(data->repo->pool, comp);
1888           compl = strlen(compstr);
1889           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1890             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1891               break;
1892           dirmap[dn] = i ? i : -mp;
1893         }
1894       /* change dirmap to point to mountpoint instead of mptree */
1895       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1896         {
1897           mp = dirmap[dn];
1898           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1899         }
1900       cbd->dirmap = dirmap;
1901       cbd->nmap = data->dirpool.ndirs;
1902       cbd->olddata = data;
1903     }
1904   cbd->hasdu = 1;
1905   if (value->id < 0 || value->id >= cbd->nmap)
1906     return 0;
1907   mp = cbd->dirmap[value->id];
1908   if (mp < 0)
1909     return 0;
1910   if (cbd->addsub > 0)
1911     {
1912       cbd->mps[mp].kbytes += value->num;
1913       cbd->mps[mp].files += value->num2;
1914     }
1915   else
1916     {
1917       cbd->mps[mp].kbytes -= value->num;
1918       cbd->mps[mp].files -= value->num2;
1919     }
1920   return 0;
1921 }
1922
1923 static void
1924 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1925 {
1926   int i;
1927   if (mptree[pos].mountpoint == -1)
1928     mptree[pos].mountpoint = mountpoint;
1929   else
1930     mountpoint = mptree[pos].mountpoint;
1931   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1932     propagate_mountpoints(mptree, i, mountpoint);
1933 }
1934
1935 #define MPTREE_BLOCK 15
1936
1937 static struct mptree *
1938 create_mptree(DUChanges *mps, int nmps)
1939 {
1940   int i, nmptree;
1941   struct mptree *mptree;
1942   int pos, compl;
1943   int mp;
1944   const char *p, *path, *compstr;
1945
1946   mptree = solv_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1947
1948   /* our root node */
1949   mptree[0].sibling = 0;
1950   mptree[0].child = 0;
1951   mptree[0].comp = 0;
1952   mptree[0].compl = 0;
1953   mptree[0].mountpoint = -1;
1954   nmptree = 1;
1955
1956   /* create component tree */
1957   for (mp = 0; mp < nmps; mp++)
1958     {
1959       mps[mp].kbytes = 0;
1960       mps[mp].files = 0;
1961       pos = 0;
1962       path = mps[mp].path;
1963       while(*path == '/')
1964         path++;
1965       while (*path)
1966         {
1967           if ((p = strchr(path, '/')) == 0)
1968             {
1969               compstr = path;
1970               compl = strlen(compstr);
1971               path += compl;
1972             }
1973           else
1974             {
1975               compstr = path;
1976               compl = p - path;
1977               path = p + 1;
1978               while(*path == '/')
1979                 path++;
1980             }
1981           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1982             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1983               break;
1984           if (!i)
1985             {
1986               /* create new node */
1987               mptree = solv_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1988               i = nmptree++;
1989               mptree[i].sibling = mptree[pos].child;
1990               mptree[i].child = 0;
1991               mptree[i].comp = compstr;
1992               mptree[i].compl = compl;
1993               mptree[i].mountpoint = -1;
1994               mptree[pos].child = i;
1995             }
1996           pos = i;
1997         }
1998       mptree[pos].mountpoint = mp;
1999     }
2000
2001   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
2002
2003 #if 0
2004   for (i = 0; i < nmptree; i++)
2005     {
2006       printf("#%d sibling: %d\n", i, mptree[i].sibling);
2007       printf("#%d child: %d\n", i, mptree[i].child);
2008       printf("#%d comp: %s\n", i, mptree[i].comp);
2009       printf("#%d compl: %d\n", i, mptree[i].compl);
2010       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
2011     }
2012 #endif
2013
2014   return mptree;
2015 }
2016
2017 void
2018 pool_calc_duchanges(Pool *pool, Map *installedmap, DUChanges *mps, int nmps)
2019 {
2020   struct mptree *mptree;
2021   struct ducbdata cbd;
2022   Solvable *s;
2023   int sp;
2024   Map ignoredu;
2025   Repo *oldinstalled = pool->installed;
2026
2027   map_init(&ignoredu, 0);
2028   mptree = create_mptree(mps, nmps);
2029
2030   cbd.mps = mps;
2031   cbd.dirmap = 0;
2032   cbd.nmap = 0;
2033   cbd.olddata = 0;
2034   cbd.mptree = mptree;
2035   cbd.addsub = 1;
2036   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2037     {
2038       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2039         continue;
2040       if (!MAPTST(installedmap, sp))
2041         continue;
2042       cbd.hasdu = 0;
2043       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2044       if (!cbd.hasdu && oldinstalled)
2045         {
2046           Id op, opp;
2047           /* no du data available, ignore data of all installed solvables we obsolete */
2048           if (!ignoredu.size)
2049             map_grow(&ignoredu, oldinstalled->end - oldinstalled->start);
2050           if (s->obsoletes)
2051             {
2052               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
2053               while ((obs = *obsp++) != 0)
2054                 FOR_PROVIDES(op, opp, obs)
2055                   if (op >= oldinstalled->start && op < oldinstalled->end)
2056                     MAPSET(&ignoredu, op - oldinstalled->start);
2057             }
2058           FOR_PROVIDES(op, opp, s->name)
2059             if (pool->solvables[op].name == s->name)
2060               if (op >= oldinstalled->start && op < oldinstalled->end)
2061                 MAPSET(&ignoredu, op - oldinstalled->start);
2062         }
2063     }
2064   cbd.addsub = -1;
2065   if (oldinstalled)
2066     {
2067       /* assumes we allways have du data for installed solvables */
2068       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2069         {
2070           if (MAPTST(installedmap, sp))
2071             continue;
2072           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
2073             continue;
2074           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
2075         }
2076     }
2077   map_free(&ignoredu);
2078   solv_free(cbd.dirmap);
2079   solv_free(mptree);
2080 }
2081
2082 int
2083 pool_calc_installsizechange(Pool *pool, Map *installedmap)
2084 {
2085   Id sp;
2086   Solvable *s;
2087   int change = 0;
2088   Repo *oldinstalled = pool->installed;
2089
2090   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
2091     {
2092       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
2093         continue;
2094       if (!MAPTST(installedmap, sp))
2095         continue;
2096       change += solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2097     }
2098   if (oldinstalled)
2099     {
2100       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
2101         {
2102           if (MAPTST(installedmap, sp))
2103             continue;
2104           change -= solvable_lookup_sizek(s, SOLVABLE_INSTALLSIZE, 0);
2105         }
2106     }
2107   return change;
2108 }
2109
2110 /* map:
2111  *  1: installed
2112  *  2: conflicts with installed
2113  *  8: interesting (only true if installed)
2114  * 16: undecided
2115  */
2116
2117 static inline Id dep2name(Pool *pool, Id dep)
2118 {
2119   while (ISRELDEP(dep))
2120     {
2121       Reldep *rd = GETRELDEP(pool, dep);
2122       dep = rd->name;
2123     }
2124   return dep;
2125 }
2126
2127 static int providedbyinstalled_multiversion(Pool *pool, unsigned char *map, Id n, Id con)
2128 {
2129   Id p, pp;
2130   Solvable *sn = pool->solvables + n;
2131
2132   FOR_PROVIDES(p, pp, sn->name)
2133     {
2134       Solvable *s = pool->solvables + p;
2135       if (s->name != sn->name || s->arch != sn->arch)
2136         continue;
2137       if ((map[p] & 9) != 9)
2138         continue;
2139       if (pool_match_nevr(pool, pool->solvables + p, con))
2140         continue;
2141       return 1;         /* found installed package that doesn't conflict */
2142     }
2143   return 0;
2144 }
2145
2146 static inline int providedbyinstalled(Pool *pool, unsigned char *map, Id dep, int ispatch, Map *multiversionmap)
2147 {
2148   Id p, pp;
2149   int r = 0;
2150   FOR_PROVIDES(p, pp, dep)
2151     {
2152       if (p == SYSTEMSOLVABLE)
2153         return 1;       /* always boring, as never constraining */
2154       if (ispatch && !pool_match_nevr(pool, pool->solvables + p, dep))
2155         continue;
2156       if (ispatch && multiversionmap && multiversionmap->size && MAPTST(multiversionmap, p) && ISRELDEP(dep))
2157         if (providedbyinstalled_multiversion(pool, map, p, dep))
2158           continue;
2159       if ((map[p] & 9) == 9)
2160         return 9;
2161       r |= map[p] & 17;
2162     }
2163   return r;
2164 }
2165
2166 /*
2167  * pool_trivial_installable - calculate if a set of solvables is
2168  * trivial installable without any other installs/deinstalls of
2169  * packages not belonging to the set.
2170  *
2171  * the state is returned in the result queue:
2172  * 1:  solvable is installable without any other package changes
2173  * 0:  solvable is not installable
2174  * -1: solvable is installable, but doesn't constrain any installed packages
2175  */
2176
2177 void
2178 pool_trivial_installable_multiversionmap(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res, Map *multiversionmap)
2179 {
2180   int i, r, m, did;
2181   Id p, *dp, con, *conp, req, *reqp;
2182   unsigned char *map;
2183   Solvable *s;
2184
2185   map = solv_calloc(pool->nsolvables, 1);
2186   for (p = 1; p < pool->nsolvables; p++)
2187     {
2188       if (!MAPTST(installedmap, p))
2189         continue;
2190       map[p] |= 9;
2191       s = pool->solvables + p;
2192       if (!s->conflicts)
2193         continue;
2194       conp = s->repo->idarraydata + s->conflicts;
2195       while ((con = *conp++) != 0)
2196         {
2197           dp = pool_whatprovides_ptr(pool, con);
2198           for (; *dp; dp++)
2199             map[p] |= 2;        /* XXX: self conflict ? */
2200         }
2201     }
2202   for (i = 0; i < pkgs->count; i++)
2203     map[pkgs->elements[i]] = 16;
2204
2205   for (i = 0, did = 0; did < pkgs->count; i++, did++)
2206     {
2207       if (i == pkgs->count)
2208         i = 0;
2209       p = pkgs->elements[i];
2210       if ((map[p] & 16) == 0)
2211         continue;
2212       if ((map[p] & 2) != 0)
2213         {
2214           map[p] = 2;
2215           continue;
2216         }
2217       s = pool->solvables + p;
2218       m = 1;
2219       if (s->requires)
2220         {
2221           reqp = s->repo->idarraydata + s->requires;
2222           while ((req = *reqp++) != 0)
2223             {
2224               if (req == SOLVABLE_PREREQMARKER)
2225                 continue;
2226               r = providedbyinstalled(pool, map, req, 0, 0);
2227               if (!r)
2228                 {
2229                   /* decided and miss */
2230                   map[p] = 2;
2231                   did = 0;
2232                   break;
2233                 }
2234               if (r == 16)
2235                 break;  /* undecided */
2236               m |= r;   /* 1 | 9 | 17 */
2237             }
2238           if (req)
2239             continue;
2240           if ((m & 9) == 9)
2241             m = 9;
2242         }
2243       if (s->conflicts)
2244         {
2245           int ispatch = 0;      /* see solver.c patch handling */
2246
2247           if (!strncmp("patch:", pool_id2str(pool, s->name), 6))
2248             ispatch = 1;
2249           conp = s->repo->idarraydata + s->conflicts;
2250           while ((con = *conp++) != 0)
2251             {
2252               if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2253                 {
2254                   map[p] = 2;
2255                   did = 0;
2256                   break;
2257                 }
2258               if ((m == 1 || m == 17) && ISRELDEP(con))
2259                 {
2260                   con = dep2name(pool, con);
2261                   if ((providedbyinstalled(pool, map, con, ispatch, multiversionmap) & 1) != 0)
2262                     m = 9;
2263                 }
2264             }
2265           if (con)
2266             continue;   /* found a conflict */
2267         }
2268 #if 0
2269       if (s->repo && s->repo != oldinstalled)
2270         {
2271           Id p2, obs, *obsp, *pp;
2272           Solvable *s2;
2273           if (s->obsoletes)
2274             {
2275               obsp = s->repo->idarraydata + s->obsoletes;
2276               while ((obs = *obsp++) != 0)
2277                 {
2278                   if ((providedbyinstalled(pool, map, obs, 0, 0) & 1) != 0)
2279                     {
2280                       map[p] = 2;
2281                       break;
2282                     }
2283                 }
2284               if (obs)
2285                 continue;
2286             }
2287           FOR_PROVIDES(p2, pp, s->name)
2288             {
2289               s2 = pool->solvables + p2;
2290               if (s2->name == s->name && (map[p2] & 1) != 0)
2291                 {
2292                   map[p] = 2;
2293                   break;
2294                 }
2295             }
2296           if (p2)
2297             continue;
2298         }
2299 #endif
2300       if (m != map[p])
2301         {
2302           map[p] = m;
2303           did = 0;
2304         }
2305     }
2306   queue_free(res);
2307   queue_init_clone(res, pkgs);
2308   for (i = 0; i < pkgs->count; i++)
2309     {
2310       m = map[pkgs->elements[i]];
2311       if ((m & 9) == 9)
2312         r = 1;
2313       else if (m & 1)
2314         r = -1;
2315       else
2316         r = 0;
2317       res->elements[i] = r;
2318     }
2319   free(map);
2320 }
2321
2322 void
2323 pool_trivial_installable(Pool *pool, Map *installedmap, Queue *pkgs, Queue *res)
2324 {
2325   pool_trivial_installable_multiversionmap(pool, installedmap, pkgs, res, 0);
2326 }
2327
2328 const char *
2329 pool_lookup_str(Pool *pool, Id entry, Id keyname)
2330 {
2331   if (entry == SOLVID_POS && pool->pos.repo)
2332     return repo_lookup_str(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2333   if (entry <= 0)
2334     return 0;
2335   return solvable_lookup_str(pool->solvables + entry, keyname);
2336 }
2337
2338 Id
2339 pool_lookup_id(Pool *pool, Id entry, Id keyname)
2340 {
2341   if (entry == SOLVID_POS && pool->pos.repo)
2342     return repo_lookup_id(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2343   if (entry <= 0)
2344     return 0;
2345   return solvable_lookup_id(pool->solvables + entry, keyname);
2346 }
2347
2348 unsigned long long
2349 pool_lookup_num(Pool *pool, Id entry, Id keyname, unsigned long long notfound)
2350 {
2351   if (entry == SOLVID_POS && pool->pos.repo)
2352     return repo_lookup_num(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, notfound);
2353   if (entry <= 0)
2354     return notfound;
2355   return solvable_lookup_num(pool->solvables + entry, keyname, notfound);
2356 }
2357
2358 int
2359 pool_lookup_void(Pool *pool, Id entry, Id keyname)
2360 {
2361   if (entry == SOLVID_POS && pool->pos.repo)
2362     return repo_lookup_void(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname);
2363   if (entry <= 0)
2364     return 0;
2365   return solvable_lookup_void(pool->solvables + entry, keyname);
2366 }
2367
2368 const unsigned char *
2369 pool_lookup_bin_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2370 {
2371   if (entry == SOLVID_POS && pool->pos.repo)
2372     return repo_lookup_bin_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2373   if (entry <= 0)
2374     return 0;
2375   return solvable_lookup_bin_checksum(pool->solvables + entry, keyname, typep);
2376 }
2377
2378 const char *
2379 pool_lookup_checksum(Pool *pool, Id entry, Id keyname, Id *typep)
2380 {
2381   if (entry == SOLVID_POS && pool->pos.repo)
2382     return repo_lookup_checksum(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, typep);
2383   if (entry <= 0)
2384     return 0;
2385   return solvable_lookup_checksum(pool->solvables + entry, keyname, typep);
2386 }
2387
2388 int
2389 pool_lookup_idarray(Pool *pool, Id entry, Id keyname, Queue *q)
2390 {
2391   if (entry == SOLVID_POS && pool->pos.repo)
2392     return repo_lookup_idarray(pool->pos.repo, pool->pos.repodataid ? entry : pool->pos.solvid, keyname, q);
2393   if (entry <= 0)
2394     return 0;
2395   return solvable_lookup_idarray(pool->solvables + entry, keyname, q);
2396 }
2397
2398 const char *
2399 pool_lookup_deltalocation(Pool *pool, Id entry, unsigned int *medianrp)
2400 {
2401   const char *loc;
2402   if (medianrp)
2403     *medianrp = 0;
2404   if (entry != SOLVID_POS)
2405     return 0;
2406   loc = pool_lookup_str(pool, entry, DELTA_LOCATION_DIR);
2407   loc = pool_tmpjoin(pool, loc, loc ? "/" : 0, pool_lookup_str(pool, entry, DELTA_LOCATION_NAME));
2408   loc = pool_tmpappend(pool, loc, "-", pool_lookup_str(pool, entry, DELTA_LOCATION_EVR));
2409   loc = pool_tmpappend(pool, loc, ".", pool_lookup_str(pool, entry, DELTA_LOCATION_SUFFIX));
2410   return loc;
2411 }
2412
2413 static void
2414 add_new_provider(Pool *pool, Id id, Id p)
2415 {
2416   Queue q;
2417   Id *pp;
2418
2419   while (ISRELDEP(id))
2420     {
2421       Reldep *rd = GETRELDEP(pool, id);
2422       id = rd->name;
2423     }
2424
2425   queue_init(&q);
2426   for (pp = pool->whatprovidesdata + pool->whatprovides[id]; *pp; pp++)
2427     {
2428       if (*pp == p)
2429         {
2430           queue_free(&q);
2431           return;
2432         }
2433       if (*pp > p)
2434         {
2435           queue_push(&q, p);
2436           p = 0;
2437         }
2438       queue_push(&q, *pp);
2439     }
2440   if (p)
2441     queue_push(&q, p);
2442   pool->whatprovides[id] = pool_queuetowhatprovides(pool, &q);
2443   queue_free(&q);
2444 }
2445
2446 void
2447 pool_add_fileconflicts_deps(Pool *pool, Queue *conflicts)
2448 {
2449   int hadhashes = pool->relhashtbl ? 1 : 0;
2450   Solvable *s;
2451   Id fn, p, q, md5;
2452   Id id;
2453   int i;
2454
2455   if (!conflicts->count)
2456     return;
2457   for (i = 0; i < conflicts->count; i += 6)
2458     {
2459       fn = conflicts->elements[i];
2460       p = conflicts->elements[i + 1];
2461       md5 = conflicts->elements[i + 2];
2462       q = conflicts->elements[i + 4];
2463       id = pool_rel2id(pool, fn, md5, REL_FILECONFLICT, 1);
2464       s = pool->solvables + p;
2465       if (!s->repo)
2466         continue;
2467       s->provides = repo_addid_dep(s->repo, s->provides, id, SOLVABLE_FILEMARKER);
2468       if (pool->whatprovides)
2469         add_new_provider(pool, fn, p);
2470       if (pool->whatprovides_rel)
2471         pool->whatprovides_rel[GETRELID(id)] = 0;       /* clear cache */
2472       s = pool->solvables + q;
2473       if (!s->repo)
2474         continue;
2475       s->conflicts = repo_addid_dep(s->repo, s->conflicts, id, 0);
2476     }
2477   if (!hadhashes)
2478     pool_freeidhashes(pool);
2479 }
2480
2481 char *
2482 pool_prepend_rootdir(Pool *pool, const char *path)
2483 {
2484   if (!path)
2485     return 0;
2486   if (!pool->rootdir)
2487     return solv_strdup(path);
2488   return solv_dupjoin(pool->rootdir, "/", *path == '/' ? path + 1 : path);
2489 }
2490
2491 const char *
2492 pool_prepend_rootdir_tmp(Pool *pool, const char *path)
2493 {
2494   if (!path)
2495     return 0;
2496   if (!pool->rootdir)
2497     return path;
2498   return pool_tmpjoin(pool, pool->rootdir, "/", *path == '/' ? path + 1 : path);
2499 }
2500
2501 void
2502 pool_set_rootdir(Pool *pool, const char *rootdir)
2503 {
2504   solv_free(pool->rootdir);
2505   pool->rootdir = solv_strdup(rootdir);
2506 }
2507
2508 const char *
2509 pool_get_rootdir(Pool *pool)
2510 {
2511   return pool->rootdir;
2512 }
2513
2514 /* only used in libzypp */
2515 void
2516 pool_set_custom_vendorcheck(Pool *pool, int (*vendorcheck)(Pool *, Solvable *, Solvable *))
2517 {
2518   pool->custom_vendorcheck = vendorcheck;
2519 }
2520
2521 /* EOF */