- move known id definitions to one file, lets see if g++ likes it
[platform/upstream/libsolv.git] / src / pool.c
1 /*
2  * Copyright (c) 2007, 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 *)sat_calloc(1, sizeof(*pool));
44
45   stringpool_init (&pool->ss, initpool_data);
46
47   /* alloc space for RelDep 0 */
48   pool->rels = sat_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 = sat_extend_resize(0, 2, sizeof(Solvable), SOLVABLE_BLOCK);
54   pool->nsolvables = 2;
55   memset(pool->solvables, 0, 2 * sizeof(Solvable));
56   s = pool->solvables + SYSTEMSOLVABLE;
57   s->name = SYSTEM_SYSTEM;
58   s->arch = ARCH_NOARCH;
59   s->evr = ID_EMPTY;
60
61   queue_init(&pool->vendormap);
62
63   pool->debugmask = SAT_DEBUG_RESULT;   /* FIXME */
64   return pool;
65 }
66
67
68 /* free all the resources of our pool */
69 void
70 pool_free(Pool *pool)
71 {
72   int i;
73
74   pool_freewhatprovides(pool);
75   pool_freeidhashes(pool);
76   repo_freeallrepos(pool, 1);
77   sat_free(pool->id2arch);
78   sat_free(pool->solvables);
79   sat_free(pool->ss.stringspace);
80   sat_free(pool->ss.strings);
81   sat_free(pool->rels);
82   queue_free(&pool->vendormap);
83   for (i = 0; i < POOL_TMPSPACEBUF; i++)
84     sat_free(pool->tmpspacebuf[i]);
85   for (i = 0; i < pool->nlanguages; i++)
86     free((char *)pool->languages[i]);
87   sat_free(pool->languages);
88   sat_free(pool->languagecache);
89   sat_free(pool);
90 }
91
92 Id
93 pool_add_solvable(Pool *pool)
94 {
95   pool->solvables = sat_extend(pool->solvables, pool->nsolvables, 1, sizeof(Solvable), SOLVABLE_BLOCK);
96   memset(pool->solvables + pool->nsolvables, 0, sizeof(Solvable));
97   return pool->nsolvables++;
98 }
99
100 Id
101 pool_add_solvable_block(Pool *pool, int count)
102 {
103   Id nsolvables = pool->nsolvables;
104   if (!count)
105     return nsolvables;
106   pool->solvables = sat_extend(pool->solvables, pool->nsolvables, count, sizeof(Solvable), SOLVABLE_BLOCK);
107   memset(pool->solvables + nsolvables, 0, sizeof(Solvable) * count);
108   pool->nsolvables += count;
109   return nsolvables;
110 }
111
112 void
113 pool_free_solvable_block(Pool *pool, Id start, int count, int reuseids)
114 {
115   if (!count)
116     return;
117   if (reuseids && start + count == pool->nsolvables)
118     {
119       /* might want to shrink solvable array */
120       pool->nsolvables = start;
121       return;
122     }
123   memset(pool->solvables + start, 0, sizeof(Solvable) * count);
124 }
125
126
127 static Pool *pool_shrink_whatprovides_sortcmp_data;
128
129 static int
130 pool_shrink_whatprovides_sortcmp(const void *ap, const void *bp)
131 {
132   int r;
133   Pool *pool = pool_shrink_whatprovides_sortcmp_data;
134   Id oa, ob, *da, *db;
135   oa = pool->whatprovides[*(Id *)ap];
136   ob = pool->whatprovides[*(Id *)bp];
137   if (oa == ob)
138     return *(Id *)ap - *(Id *)bp;
139   if (!oa)
140     return -1;
141   if (!ob)
142     return 1;
143   da = pool->whatprovidesdata + oa;
144   db = pool->whatprovidesdata + ob;
145   while (*db)
146     if ((r = (*da++ - *db++)) != 0)
147       return r;
148   if (*da)
149     return *da;
150   return *(Id *)ap - *(Id *)bp;
151 }
152
153 /*
154  * pool_shrink_whatprovides  - unify whatprovides data
155  *
156  * whatprovides_rel must be empty for this to work!
157  *
158  */
159 static void
160 pool_shrink_whatprovides(Pool *pool)
161 {
162   Id i, id;
163   Id *sorted;
164   Id lastid, *last, *dp, *lp;
165   Offset o;
166   int r;
167
168   if (pool->ss.nstrings < 3)
169     return;
170   sorted = sat_malloc2(pool->ss.nstrings, sizeof(Id));
171   for (id = 0; id < pool->ss.nstrings; id++)
172     sorted[id] = id;
173   pool_shrink_whatprovides_sortcmp_data = pool;
174   qsort(sorted + 1, pool->ss.nstrings - 1, sizeof(Id), pool_shrink_whatprovides_sortcmp);
175   last = 0;
176   lastid = 0;
177   for (i = 1; i < pool->ss.nstrings; i++)
178     {
179       id = sorted[i];
180       o = pool->whatprovides[id];
181       if (o == 0 || o == 1)
182         continue;
183       dp = pool->whatprovidesdata + o;
184       if (last)
185         {
186           lp = last;
187           while (*dp)   
188             if (*dp++ != *lp++)
189               {
190                 last = 0;
191                 break;
192               }
193           if (last && *lp)
194             last = 0;
195           if (last)
196             {
197               pool->whatprovides[id] = -lastid;
198               continue;
199             }
200         }
201       last = pool->whatprovidesdata + o;
202       lastid = id;
203     }
204   sat_free(sorted);
205   dp = pool->whatprovidesdata + 2;
206   for (id = 1; id < pool->ss.nstrings; id++)
207     {
208       o = pool->whatprovides[id];
209       if (o == 0 || o == 1)
210         continue;
211       if ((Id)o < 0)
212         {
213           i = -(Id)o;
214           if (i >= id)
215             abort();
216           pool->whatprovides[id] = pool->whatprovides[i];
217           continue;
218         }
219       lp = pool->whatprovidesdata + o;
220       if (lp < dp)
221         abort();
222       pool->whatprovides[id] = dp - pool->whatprovidesdata;
223       while ((*dp++ = *lp++) != 0)
224         ;
225     }
226   o = dp - pool->whatprovidesdata;
227   POOL_DEBUG(SAT_DEBUG_STATS, "shrunk whatprovidesdata from %d to %d\n", pool->whatprovidesdataoff, o);
228   if (pool->whatprovidesdataoff == o)
229     return;
230   r = pool->whatprovidesdataoff - o;
231   pool->whatprovidesdataoff = o;
232   pool->whatprovidesdata = sat_realloc(pool->whatprovidesdata, (o + pool->whatprovidesdataleft) * sizeof(Id));
233   if (r > pool->whatprovidesdataleft)
234     r = pool->whatprovidesdataleft;
235   memset(pool->whatprovidesdata + o, 0, r * sizeof(Id));
236 }
237
238
239 /*
240  * pool_createwhatprovides()
241  * 
242  * create hashes over pool of solvables to ease provide lookups
243  * 
244  */
245 void
246 pool_createwhatprovides(Pool *pool)
247 {
248   int i, num, np, extra;
249   Offset off;
250   Solvable *s;
251   Id id;
252   Offset *idp, n;
253   Offset *whatprovides;
254   Id *whatprovidesdata, *d;
255
256   POOL_DEBUG(SAT_DEBUG_STATS, "number of solvables: %d\n", pool->nsolvables);
257   POOL_DEBUG(SAT_DEBUG_STATS, "number of ids: %d + %d\n", pool->ss.nstrings, pool->nrels);
258
259   pool_freeidhashes(pool);      /* XXX: should not be here! */
260   pool_freewhatprovides(pool);
261   num = pool->ss.nstrings;
262   pool->whatprovides = whatprovides = sat_calloc_block(num, sizeof(Offset), WHATPROVIDES_BLOCK);
263   pool->whatprovides_rel = sat_calloc_block(pool->nrels, sizeof(Offset), WHATPROVIDES_BLOCK);
264
265   /* count providers for each name */
266   for (i = 1; i < pool->nsolvables; i++)
267     {
268       Id *pp;
269       s = pool->solvables + i;
270       if (!s->provides)
271         continue;
272       if (!pool_installable(pool, s))
273         continue;
274       pp = s->repo->idarraydata + s->provides;
275       while ((id = *pp++) != ID_NULL)
276         {
277           while (ISRELDEP(id))
278             {
279               Reldep *rd = GETRELDEP(pool, id);
280               id = rd->name;
281             }
282           whatprovides[id]++;          /* inc count of providers */
283         }
284     }
285
286   off = 2;      /* first entry is undef, second is empty list */
287   idp = whatprovides;
288   np = 0;                              /* number of names provided */
289   for (i = 0; i < num; i++, idp++)
290     {
291       n = *idp;
292       if (!n)                          /* no providers */
293         continue;
294       *idp = off;                      /* move from counts to offsets into whatprovidesdata */
295       off += n + 1;                    /* make space for all providers + terminating ID_NULL */
296       np++;                            /* inc # of provider 'slots' */
297     }
298
299   POOL_DEBUG(SAT_DEBUG_STATS, "provide ids: %d\n", np);
300
301   /* reserve some space for relation data */
302   extra = 2 * pool->nrels;
303   if (extra < 256)
304     extra = 256;
305
306   POOL_DEBUG(SAT_DEBUG_STATS, "provide space needed: %d + %d\n", off, extra);
307
308   /* alloc space for all providers + extra */
309   whatprovidesdata = sat_calloc(off + extra, sizeof(Id));
310
311   /* now fill data for all provides */
312   for (i = 1; i < pool->nsolvables; i++)
313     {
314       Id *pp;
315       s = pool->solvables + i;
316       if (!s->provides)
317         continue;
318       if (!pool_installable(pool, s))
319         continue;
320
321       /* for all provides of this solvable */
322       pp = s->repo->idarraydata + s->provides;
323       while ((id = *pp++) != 0)
324         {
325           while (ISRELDEP(id))
326             {
327               Reldep *rd = GETRELDEP(pool, id);
328               id = rd->name;
329             }
330           d = whatprovidesdata + whatprovides[id];   /* offset into whatprovidesdata */
331           if (*d)
332             {
333               d++;
334               while (*d)               /* find free slot */
335                 d++;
336               if (d[-1] == i)
337                 continue;
338             }
339           *d = i;                      /* put solvable Id into data */
340         }
341     }
342   pool->whatprovidesdata = whatprovidesdata;
343   pool->whatprovidesdataoff = off;
344   pool->whatprovidesdataleft = extra;
345   pool_shrink_whatprovides(pool);
346 }
347
348 /*
349  * free all of our whatprovides data
350  * be careful, everything internalized with pool_queuetowhatprovides is gone, too
351  */
352 void
353 pool_freewhatprovides(Pool *pool)
354 {
355   pool->whatprovides = sat_free(pool->whatprovides);
356   pool->whatprovides_rel = sat_free(pool->whatprovides_rel);
357   pool->whatprovidesdata = sat_free(pool->whatprovidesdata);
358   pool->whatprovidesdataoff = 0;
359   pool->whatprovidesdataleft = 0;
360 }
361
362
363 /******************************************************************************/
364
365 /*
366  * pool_queuetowhatprovides  - add queue contents to whatprovidesdata
367  * 
368  * on-demand filling of provider information
369  * move queue data into whatprovidesdata
370  * q: queue of Ids
371  * returns: Offset into whatprovides
372  *
373  */
374 Id
375 pool_queuetowhatprovides(Pool *pool, Queue *q)
376 {
377   Offset off;
378   int count = q->count;
379
380   if (count == 0)                      /* queue empty -> ID_EMPTY */
381     return ID_EMPTY;
382
383   /* extend whatprovidesdata if needed, +1 for ID_NULL-termination */
384   if (pool->whatprovidesdataleft < count + 1)
385     {
386       POOL_DEBUG(SAT_DEBUG_STATS, "growing provides hash data...\n");
387       pool->whatprovidesdata = sat_realloc(pool->whatprovidesdata, (pool->whatprovidesdataoff + count + 4096) * sizeof(Id));
388       pool->whatprovidesdataleft = count + 4096;
389     }
390
391   /* copy queue to next free slot */
392   off = pool->whatprovidesdataoff;
393   memcpy(pool->whatprovidesdata + pool->whatprovidesdataoff, q->elements, count * sizeof(Id));
394
395   /* adapt count and ID_NULL-terminate */
396   pool->whatprovidesdataoff += count;
397   pool->whatprovidesdata[pool->whatprovidesdataoff++] = ID_NULL;
398   pool->whatprovidesdataleft -= count + 1;
399
400   return (Id)off;
401 }
402
403
404 /*************************************************************************/
405
406 /*
407  * addrelproviders
408  * 
409  * add packages fulfilling the relation to whatprovides array
410  * no exact providers, do range match
411  * 
412  */
413
414 Id *
415 pool_addrelproviders(Pool *pool, Id d)
416 {
417   Reldep *rd = GETRELDEP(pool, d);
418   Reldep *prd;
419   Queue plist;
420   Id buf[16];
421   Id name = rd->name;
422   Id evr = rd->evr;
423   int flags = rd->flags;
424   Id pid, *pidp;
425   Id p, *pp, *pp2, *pp3;
426
427   d = GETRELID(d);
428   queue_init_buffer(&plist, buf, sizeof(buf)/sizeof(*buf));
429   switch (flags)
430     {
431     case REL_AND:
432     case REL_WITH:
433       pp = pool_whatprovides(pool, name);
434       pp2 = pool_whatprovides(pool, evr);
435       while ((p = *pp++) != 0)
436         {
437           for (pp3 = pp2; *pp3;)
438             if (*pp3++ == p)
439               {
440                 queue_push(&plist, p);
441                 break;
442               }
443         }
444       break;
445     case REL_OR:
446       pp = pool_whatprovides(pool, name);
447       while ((p = *pp++) != 0)
448         queue_push(&plist, p);
449       pp = pool_whatprovides(pool, evr);
450       while ((p = *pp++) != 0)
451         queue_pushunique(&plist, p);
452       break;
453     case REL_NAMESPACE:
454       if (pool->nscallback)
455         {
456           p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
457           if (p > 1)
458             {
459               queue_free(&plist);
460               pool->whatprovides_rel[d] = p;
461               return pool->whatprovidesdata + p;
462             }
463           if (p == 1)
464             queue_push(&plist, SYSTEMSOLVABLE);
465         }
466       break;
467     default:
468       break;
469     }
470
471   /* convert to whatprovides id */
472 #if 0
473   POOL_DEBUG(SAT_DEBUG_STATS, "addrelproviders: what provides %s?\n", dep2str(pool, name));
474 #endif
475   if (flags && flags < 8)
476     {
477       FOR_PROVIDES(p, pp, name)
478         {
479 #if 0
480           POOL_DEBUG(DEBUG_1, "addrelproviders: checking package %s\n", id2str(pool, pool->p[p].name));
481 #endif
482           /* solvable p provides name in some rels */
483           pidp = pool->solvables[p].repo->idarraydata + pool->solvables[p].provides;
484           while ((pid = *pidp++) != 0)
485             {
486               int pflags;
487               Id pevr;
488
489               if (pid == name)
490                 {
491 #ifdef DEBIAN_SEMANTICS
492                   continue;             /* unversioned provides can
493                                          * never match versioned deps */
494 #else
495                   break;                /* yes, provides all versions */
496 #endif
497                 }
498               if (!ISRELDEP(pid))
499                 continue;               /* wrong provides name */
500               prd = GETRELDEP(pool, pid);
501               if (prd->name != name)
502                 continue;               /* wrong provides name */
503               /* right package, both deps are rels */
504               pflags = prd->flags;
505               if (!pflags)
506                 continue;
507               if (flags == 7 || pflags == 7)
508                 break; /* included */
509               if ((pflags & flags & 5) != 0)
510                 break; /* same direction, match */
511               pevr = prd->evr;
512               if (pevr == evr)
513                 {
514                   if ((pflags & flags & 2) != 0)
515                     break; /* both have =, match */
516                 }
517               else
518                 {
519                   int f = flags == 5 ? 5 : flags == 2 ? pflags : (flags ^ 5) & (pflags | 5);
520                   if ((f & (1 << (1 + evrcmp(pool, pevr, evr, EVRCMP_MATCH_RELEASE)))) != 0)
521                     break;
522                 }
523             }
524           if (!pid)
525             continue;   /* no rel match */
526           queue_push(&plist, p);
527         }
528       /* make our system solvable provide all unknown rpmlib() stuff */
529       if (plist.count == 0 && !strncmp(id2str(pool, name), "rpmlib(", 7))
530         queue_push(&plist, SYSTEMSOLVABLE);
531     }
532   /* add providers to whatprovides */
533 #if 0
534   POOL_DEBUG(SAT_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
535 #endif
536   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
537   queue_free(&plist);
538
539   return pool->whatprovidesdata + pool->whatprovides_rel[d];
540 }
541
542 /*************************************************************************/
543
544 void
545 pool_debug(Pool *pool, int type, const char *format, ...)
546 {
547   va_list args;
548   char buf[1024];
549
550   if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
551     {
552       if ((pool->debugmask & type) == 0)
553         return;
554     }
555   va_start(args, format);
556   if (!pool->debugcallback)
557     {
558       if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
559         vprintf(format, args);
560       else
561         vfprintf(stderr, format, args);
562       return;
563     }
564   vsnprintf(buf, sizeof(buf), format, args);
565   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
566 }
567
568 void
569 pool_setdebuglevel(Pool *pool, int level)
570 {
571   int mask = SAT_DEBUG_RESULT;
572   if (level > 0)
573     mask |= SAT_DEBUG_STATS|SAT_DEBUG_ANALYZE|SAT_DEBUG_UNSOLVABLE;
574   if (level > 1)
575     mask |= SAT_DEBUG_JOB|SAT_DEBUG_SOLUTIONS|SAT_DEBUG_POLICY;
576   if (level > 2)
577     mask |= SAT_DEBUG_PROPAGATE;
578   if (level > 3)
579     mask |= SAT_DEBUG_RULE_CREATION;
580   if (level > 4)
581     mask |= SAT_DEBUG_SCHUBI;
582   pool->debugmask = mask;
583 }
584
585 /*************************************************************************/
586
587 struct searchfiles {
588   Id *ids;
589   char **dirs;
590   char **names;
591   int nfiles;
592   Map seen;
593 };
594
595 #define SEARCHFILES_BLOCK 127
596
597 static void
598 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
599 {
600   Id dep, sid;
601   const char *s, *sr;
602
603   while ((dep = *ida++) != 0)
604     {
605       while (ISRELDEP(dep))
606         {
607           Reldep *rd;
608           sid = pool->ss.nstrings + GETRELID(dep);
609           if (MAPTST(&sf->seen, sid))
610             {
611               dep = 0;
612               break;
613             }
614           MAPSET(&sf->seen, sid);
615           rd = GETRELDEP(pool, dep);
616           if (rd->flags < 8)
617             dep = rd->name;
618           else if (rd->flags == REL_NAMESPACE)
619             {
620               if (isf && (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES))
621                 {
622                   sf = isf;
623                   isf = 0;
624                   if (MAPTST(&sf->seen, sid))
625                     {
626                       dep = 0;
627                       break;
628                     }
629                   MAPSET(&sf->seen, sid);
630                 }
631               dep = rd->evr;
632             }
633           else
634             {
635               Id ids[2];
636               ids[0] = rd->name;
637               ids[1] = 0;
638               pool_addfileprovides_dep(pool, ids, sf, isf);
639               dep = rd->evr;
640             }
641         }
642       if (!dep)
643         continue;
644       if (MAPTST(&sf->seen, dep))
645         continue;
646       MAPSET(&sf->seen, dep);
647       s = id2str(pool, dep);
648       if (*s != '/')
649         continue;
650       sf->ids = sat_extend(sf->ids, sf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
651       sf->dirs = sat_extend(sf->dirs, sf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
652       sf->names = sat_extend(sf->names, sf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
653       sf->ids[sf->nfiles] = dep;
654       sr = strrchr(s, '/');
655       sf->names[sf->nfiles] = strdup(sr + 1);
656       sf->dirs[sf->nfiles] = sat_malloc(sr - s + 1);
657       if (sr != s)
658         strncpy(sf->dirs[sf->nfiles], s, sr - s);
659       sf->dirs[sf->nfiles][sr - s] = 0;
660       sf->nfiles++;
661     }
662 }
663
664 struct addfileprovides_cbdata {
665   int nfiles;
666   Id *ids;
667   char **dirs;
668   char **names;
669
670   Repodata *olddata;
671   Id *dids;
672   Map useddirs;
673 };
674
675 static int
676 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
677 {
678   struct addfileprovides_cbdata *cbd = cbdata;
679   int i;
680
681   if (data != cbd->olddata)
682     {
683       map_free(&cbd->useddirs);
684       map_init(&cbd->useddirs, data->dirpool.ndirs);
685       for (i = 0; i < cbd->nfiles; i++)
686         {
687           Id did = repodata_str2dir(data, cbd->dirs[i], 0);
688           cbd->dids[i] = did;
689           if (did)
690             MAPSET(&cbd->useddirs, did);
691         }
692       cbd->olddata = data;
693     }
694   if (!MAPTST(&cbd->useddirs, value->id))
695     return 0;
696   for (i = 0; i < cbd->nfiles; i++)
697     {
698       if (cbd->dids[i] != value->id)
699         continue;
700       if (!strcmp(cbd->names[i], value->str))
701         break;
702     }
703   if (i == cbd->nfiles)
704     return 0;
705   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
706   return 0;
707 }
708
709 void
710 pool_addfileprovides(Pool *pool, Repo *installed)
711 {
712   Solvable *s;
713   Repo *repo;
714   struct searchfiles sf, isf;
715   struct addfileprovides_cbdata cbd;
716   int i;
717
718   memset(&sf, 0, sizeof(sf));
719   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
720   memset(&isf, 0, sizeof(isf));
721   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
722
723   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
724     {
725       repo = s->repo;
726       if (!repo)
727         continue;
728       if (s->obsoletes)
729         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, &isf);
730       if (s->conflicts)
731         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, &isf);
732       if (s->requires)
733         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, &isf);
734       if (s->recommends)
735         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, &isf);
736       if (s->suggests)
737         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, &isf);
738       if (s->supplements)
739         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, &isf);
740       if (s->enhances)
741         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, &isf);
742       if (s->freshens)
743         pool_addfileprovides_dep(pool, repo->idarraydata + s->freshens, &sf, &isf);
744     }
745   map_free(&sf.seen);
746   map_free(&isf.seen);
747   POOL_DEBUG(SAT_DEBUG_STATS, "found %d file dependencies\n", sf.nfiles);
748   POOL_DEBUG(SAT_DEBUG_STATS, "found %d installed file dependencies\n", isf.nfiles);
749   cbd.dids = 0;
750   map_init(&cbd.useddirs, 1);
751   if (sf.nfiles)
752     {
753 #if 0
754       for (i = 0; i < sf.nfiles; i++)
755         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in filelist\n", id2str(pool, sf.ids[i]));
756 #endif
757       cbd.nfiles = sf.nfiles;
758       cbd.ids = sf.ids;
759       cbd.dirs = sf.dirs;
760       cbd.names = sf.names;
761       cbd.olddata = 0;
762       cbd.dids = sat_realloc2(cbd.dids, sf.nfiles, sizeof(Id));
763       pool_search(pool, 0, SOLVABLE_FILELIST, 0, 0, addfileprovides_cb, &cbd);
764       sat_free(sf.ids);
765       for (i = 0; i < sf.nfiles; i++)
766         {
767           sat_free(sf.dirs[i]);
768           sat_free(sf.names[i]);
769         }
770       sat_free(sf.dirs);
771       sat_free(sf.names);
772     }
773   if (isf.nfiles && installed)
774     {
775 #if 0
776       for (i = 0; i < isf.nfiles; i++)
777         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in installed filelist\n", id2str(pool, isf.ids[i]));
778 #endif
779       cbd.nfiles = isf.nfiles;
780       cbd.ids = isf.ids;
781       cbd.dirs = isf.dirs;
782       cbd.names = isf.names;
783       cbd.olddata = 0;
784       cbd.dids = sat_realloc2(cbd.dids, isf.nfiles, sizeof(Id));
785       repo_search(installed, 0, SOLVABLE_FILELIST, 0, 0, addfileprovides_cb, &cbd);
786       sat_free(isf.ids);
787       for (i = 0; i < isf.nfiles; i++)
788         {
789           sat_free(isf.dirs[i]);
790           sat_free(isf.names[i]);
791         }
792       sat_free(isf.dirs);
793       sat_free(isf.names);
794     }
795   map_free(&cbd.useddirs);
796   sat_free(cbd.dids);
797   pool_freewhatprovides(pool);  /* as we have added provides */
798 }
799
800 void
801 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)
802 {
803   if (p)
804     {
805       if (pool->solvables[p].repo)
806         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
807       return;
808     }
809   /* FIXME: obey callback return value! */
810   for (p = 1; p < pool->nsolvables; p++)
811     if (pool->solvables[p].repo)
812       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
813 }
814
815
816 void
817 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
818 {
819   int i;
820
821   pool->languagecache = sat_free(pool->languagecache);
822   pool->languagecacheother = 0;
823   if (pool->nlanguages)
824     {
825       for (i = 0; i < pool->nlanguages; i++)
826         free((char *)pool->languages[i]);
827       free(pool->languages);
828     }
829   pool->nlanguages = nlanguages;
830   if (!nlanguages)
831     return;
832   pool->languages = sat_calloc(nlanguages, sizeof(const char **));
833   for (i = 0; i < pool->nlanguages; i++)
834     pool->languages[i] = strdup(languages[i]);
835 }
836
837 char *
838 pool_alloctmpspace(Pool *pool, int len)
839 {
840   int n = pool->tmpspacen;
841   if (!len)
842     return 0;
843   if (len > pool->tmpspacelen[n])
844     {
845       pool->tmpspacebuf[n] = sat_realloc(pool->tmpspacebuf[n], len + 32);
846       pool->tmpspacelen[n] = len + 32;
847     }
848   pool->tmpspacen = (n + 1) % POOL_TMPSPACEBUF;
849   return pool->tmpspacebuf[n];
850 }
851
852 /*******************************************************************/
853
854 struct mptree {
855   Id sibling;
856   Id child;
857   const char *comp;
858   int compl;
859   Id mountpoint;
860 };
861
862 struct ducbdata {
863   DUChanges *mps;
864   struct mptree *mptree;
865   int addsub;
866
867   Id *dirmap;
868   int nmap;
869   Repodata *olddata;
870 };
871
872
873 static int
874 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
875 {
876   struct ducbdata *cbd = cbdata;
877   Id mp;
878
879   if (data != cbd->olddata)
880     {
881       Id dn, mp, comp, *dirmap, *dirs;
882       int i, compl;
883       const char *compstr;
884       struct mptree *mptree;
885
886       /* create map from dir to mptree */
887       cbd->dirmap = sat_free(cbd->dirmap);
888       cbd->nmap = 0;
889       dirmap = sat_calloc(data->dirpool.ndirs, sizeof(Id));
890       mptree = cbd->mptree;
891       mp = 0;
892       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
893         {
894           comp = *dirs++;
895           if (comp <= 0)
896             {
897               mp = dirmap[-comp];
898               continue;
899             }
900           if (mp < 0)
901             {
902               /* unconnected */
903               dirmap[dn] = mp;
904               continue;
905             }
906           if (!mptree[mp].child)
907             {
908               dirmap[dn] = -mp;
909               continue;
910             }
911           if (data->localpool)
912             compstr = stringpool_id2str(&data->spool, comp);
913           else
914             compstr = id2str(data->repo->pool, comp);
915           compl = strlen(compstr);
916           for (i = mptree[mp].child; i; i = mptree[i].sibling)
917             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
918               break;
919           dirmap[dn] = i ? i : -mp;
920         }
921       /* change dirmap to point to mountpoint instead of mptree */
922       for (dn = 0; dn < data->dirpool.ndirs; dn++)
923         {
924           mp = dirmap[dn];
925           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
926         }
927       cbd->dirmap = dirmap;
928       cbd->nmap = data->dirpool.ndirs;
929       cbd->olddata = data;
930     }
931   if (value->id < 0 || value->id >= cbd->nmap)
932     return 0;
933   mp = cbd->dirmap[value->id];
934   if (mp < 0)
935     return 0;
936   if (cbd->addsub > 0)
937     {
938       cbd->mps[mp].kbytes += value->num;
939       cbd->mps[mp].files += value->num2;
940     }
941   else
942     {
943       cbd->mps[mp].kbytes -= value->num;
944       cbd->mps[mp].files -= value->num2;
945     }
946   return 0;
947 }
948
949 static void
950 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
951 {
952   int i;
953   if (mptree[pos].mountpoint == -1)
954     mptree[pos].mountpoint = mountpoint;
955   else
956     mountpoint = mptree[pos].mountpoint;
957   for (i = mptree[pos].child; i; i = mptree[i].sibling)
958     propagate_mountpoints(mptree, i, mountpoint);
959 }
960
961 #define MPTREE_BLOCK 15
962
963 void
964 pool_calc_duchanges(Pool *pool, Queue *pkgs, DUChanges *mps, int nmps)
965 {
966   char *p;
967   const char *path, *compstr;
968   struct mptree *mptree;
969   int i, nmptree;
970   int pos, compl;
971   int mp;
972   struct ducbdata cbd;
973
974   cbd.mps = mps;
975   cbd.addsub = 0;
976   cbd.dirmap = 0;
977   cbd.nmap = 0;
978   cbd.olddata = 0;
979
980   mptree = sat_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
981
982   /* our root node */
983   mptree[0].sibling = 0;
984   mptree[0].child = 0;
985   mptree[0].comp = 0;
986   mptree[0].compl = 0;
987   mptree[0].mountpoint = -1;
988   nmptree = 1;
989   
990   /* create component tree */
991   for (mp = 0; mp < nmps; mp++)
992     {
993       mps[mp].kbytes = 0;
994       mps[mp].files = 0;
995       pos = 0;
996       path = mps[mp].path;
997       while(*path == '/')
998         path++;
999       while (*path)
1000         {
1001           if ((p = strchr(path, '/')) == 0)
1002             {
1003               compstr = path;
1004               compl = strlen(compstr);
1005               path += compl;
1006             }
1007           else
1008             {
1009               compstr = path;
1010               compl = p - path;
1011               path = p + 1;
1012               while(*path == '/')
1013                 path++;
1014             }
1015           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1016             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1017               break;
1018           if (!i)
1019             {
1020               /* create new node */
1021               mptree = sat_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1022               i = nmptree++;
1023               mptree[i].sibling = mptree[pos].child;
1024               mptree[i].child = 0;
1025               mptree[i].comp = compstr;
1026               mptree[i].compl = compl;
1027               mptree[i].mountpoint = -1;
1028               mptree[pos].child = i;
1029             }
1030           pos = i;
1031         }
1032       mptree[pos].mountpoint = mp;
1033     }
1034
1035   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1036
1037 #if 0
1038   for (i = 0; i < nmptree; i++)
1039     {
1040       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1041       printf("#%d child: %d\n", i, mptree[i].child);
1042       printf("#%d comp: %s\n", i, mptree[i].comp);
1043       printf("#%d compl: %d\n", i, mptree[i].compl);
1044       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1045     }
1046 #endif
1047
1048   cbd.mptree = mptree;
1049   cbd.addsub = 1;
1050   for (i = 0; i < pkgs->count; i++)
1051     {
1052       Id sp = pkgs->elements[i];
1053       if (sp > 0)
1054         repo_search(pool->solvables[sp].repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1055     }
1056   cbd.addsub = -1;
1057   for (i = 0; i < pkgs->count; i++)
1058     {
1059       Id sp = pkgs->elements[i];
1060       if (sp < 0)
1061         repo_search(pool->solvables[-sp].repo, -sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1062     }
1063   sat_free(mptree);
1064 }
1065
1066 int
1067 pool_calc_installsizechange(Pool *pool, Queue *pkgs)
1068 {
1069   int i, change;
1070
1071   change = 0;
1072   for (i = 0; i < pkgs->count; i++)
1073     {
1074       Id sp = pkgs->elements[i];
1075       if (sp > 0)
1076         change += repo_lookup_num(pool->solvables + sp, SOLVABLE_INSTALLSIZE);
1077       else if (sp < 0)
1078         change -= repo_lookup_num(pool->solvables - sp, SOLVABLE_INSTALLSIZE);
1079     }
1080   return change;
1081 }
1082
1083 // EOF