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