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