b29aaaa96e3fbe4ea161c6718c75fc481749830d
[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)          /* solvable already tacked at end ? */
337                 continue;              /* Y: skip, on to next provides */
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           /* ask callback which packages provide the dependency
457            * 0:  none
458            * 1:  the system (aka SYSTEMSOLVABLE)
459            * >1: a set of packages, stored as offset on whatprovidesdata
460            */
461           p = pool->nscallback(pool, pool->nscallbackdata, name, evr);
462           if (p > 1)
463             {
464               queue_free(&plist);
465               pool->whatprovides_rel[d] = p;
466               return pool->whatprovidesdata + p;
467             }
468           if (p == 1)
469             queue_push(&plist, SYSTEMSOLVABLE);
470         }
471       break;
472     case REL_ARCH:
473       pp = pp2 = pool_whatprovides(pool, name);
474       while ((p = *pp++) != 0)
475         {
476           Solvable *s = pool->solvables + p;
477           if (s->arch == evr)
478             queue_push(&plist, p);
479           else
480             pp2 = 0;
481         }
482       if (pp2)
483         return pp2;
484       break;
485     default:
486       break;
487     }
488
489   /* convert to whatprovides id */
490 #if 0
491   POOL_DEBUG(SAT_DEBUG_STATS, "addrelproviders: what provides %s?\n", dep2str(pool, name));
492 #endif
493   if (flags && flags < 8)
494     {
495       pp = pool_whatprovides(pool, name);
496       while (ISRELDEP(name))
497         {
498           rd = GETRELDEP(pool, name);
499           name = rd->name;
500         }
501       while ((p = *pp++) != 0)
502         {
503 #if 0
504           POOL_DEBUG(DEBUG_1, "addrelproviders: checking package %s\n", id2str(pool, pool->p[p].name));
505 #endif
506           /* solvable p provides name in some rels */
507           pidp = pool->solvables[p].repo->idarraydata + pool->solvables[p].provides;
508           while ((pid = *pidp++) != 0)
509             {
510               int pflags;
511               Id pevr;
512
513               if (pid == name)
514                 {
515 #ifdef DEBIAN_SEMANTICS
516                   continue;             /* unversioned provides can
517                                          * never match versioned deps */
518 #else
519                   break;                /* yes, provides all versions */
520 #endif
521                 }
522               if (!ISRELDEP(pid))
523                 continue;               /* wrong provides name */
524               prd = GETRELDEP(pool, pid);
525               if (prd->name != name)
526                 continue;               /* wrong provides name */
527               /* right package, both deps are rels */
528               pflags = prd->flags;
529               if (!pflags)
530                 continue;
531               if (flags == 7 || pflags == 7)
532                 break; /* included */
533               if ((pflags & flags & 5) != 0)
534                 break; /* same direction, match */
535               pevr = prd->evr;
536               if (pevr == evr)
537                 {
538                   if ((pflags & flags & 2) != 0)
539                     break; /* both have =, match */
540                 }
541               else
542                 {
543                   int f = flags == 5 ? 5 : flags == 2 ? pflags : (flags ^ 5) & (pflags | 5);
544                   if ((f & (1 << (1 + evrcmp(pool, pevr, evr, EVRCMP_MATCH_RELEASE)))) != 0)
545                     break;
546                 }
547             }
548           if (!pid)
549             continue;   /* no rel match */
550           queue_push(&plist, p);
551         }
552       /* make our system solvable provide all unknown rpmlib() stuff */
553       if (plist.count == 0 && !strncmp(id2str(pool, name), "rpmlib(", 7))
554         queue_push(&plist, SYSTEMSOLVABLE);
555     }
556   /* add providers to whatprovides */
557 #if 0
558   POOL_DEBUG(SAT_DEBUG_STATS, "addrelproviders: adding %d packages to %d\n", plist.count, d);
559 #endif
560   pool->whatprovides_rel[d] = pool_queuetowhatprovides(pool, &plist);
561   queue_free(&plist);
562
563   return pool->whatprovidesdata + pool->whatprovides_rel[d];
564 }
565
566 /*************************************************************************/
567
568 void
569 pool_debug(Pool *pool, int type, const char *format, ...)
570 {
571   va_list args;
572   char buf[1024];
573
574   if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
575     {
576       if ((pool->debugmask & type) == 0)
577         return;
578     }
579   va_start(args, format);
580   if (!pool->debugcallback)
581     {
582       if ((type & (SAT_FATAL|SAT_ERROR)) == 0)
583         vprintf(format, args);
584       else
585         vfprintf(stderr, format, args);
586       return;
587     }
588   vsnprintf(buf, sizeof(buf), format, args);
589   pool->debugcallback(pool, pool->debugcallbackdata, type, buf);
590 }
591
592 void
593 pool_setdebuglevel(Pool *pool, int level)
594 {
595   int mask = SAT_DEBUG_RESULT;
596   if (level > 0)
597     mask |= SAT_DEBUG_STATS|SAT_DEBUG_ANALYZE|SAT_DEBUG_UNSOLVABLE;
598   if (level > 1)
599     mask |= SAT_DEBUG_JOB|SAT_DEBUG_SOLUTIONS|SAT_DEBUG_POLICY;
600   if (level > 2)
601     mask |= SAT_DEBUG_PROPAGATE;
602   if (level > 3)
603     mask |= SAT_DEBUG_RULE_CREATION;
604   if (level > 4)
605     mask |= SAT_DEBUG_SCHUBI;
606   pool->debugmask = mask;
607 }
608
609 /*************************************************************************/
610
611 struct searchfiles {
612   Id *ids;
613   char **dirs;
614   char **names;
615   int nfiles;
616   Map seen;
617 };
618
619 #define SEARCHFILES_BLOCK 127
620
621 static void
622 pool_addfileprovides_dep(Pool *pool, Id *ida, struct searchfiles *sf, struct searchfiles *isf)
623 {
624   Id dep, sid;
625   const char *s, *sr;
626   struct searchfiles *csf;
627
628   while ((dep = *ida++) != 0)
629     {
630       csf = sf;
631       while (ISRELDEP(dep))
632         {
633           Reldep *rd;
634           sid = pool->ss.nstrings + GETRELID(dep);
635           if (MAPTST(&csf->seen, sid))
636             {
637               dep = 0;
638               break;
639             }
640           MAPSET(&csf->seen, sid);
641           rd = GETRELDEP(pool, dep);
642           if (rd->flags < 8)
643             dep = rd->name;
644           else if (rd->flags == REL_NAMESPACE)
645             {
646               if (rd->name == NAMESPACE_INSTALLED || rd->name == NAMESPACE_SPLITPROVIDES)
647                 {
648                   csf = isf;
649                   if (!csf || MAPTST(&csf->seen, sid))
650                     {
651                       dep = 0;
652                       break;
653                     }
654                   MAPSET(&csf->seen, sid);
655                 }
656               dep = rd->evr;
657             }
658           else
659             {
660               Id ids[2];
661               ids[0] = rd->name;
662               ids[1] = 0;
663               pool_addfileprovides_dep(pool, ids, csf, isf);
664               dep = rd->evr;
665             }
666         }
667       if (!dep)
668         continue;
669       if (MAPTST(&csf->seen, dep))
670         continue;
671       MAPSET(&csf->seen, dep);
672       s = id2str(pool, dep);
673       if (*s != '/')
674         continue;
675       csf->ids = sat_extend(csf->ids, csf->nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
676       csf->dirs = sat_extend(csf->dirs, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
677       csf->names = sat_extend(csf->names, csf->nfiles, 1, sizeof(const char *), SEARCHFILES_BLOCK);
678       csf->ids[csf->nfiles] = dep;
679       sr = strrchr(s, '/');
680       csf->names[csf->nfiles] = strdup(sr + 1);
681       csf->dirs[csf->nfiles] = sat_malloc(sr - s + 1);
682       if (sr != s)
683         strncpy(csf->dirs[csf->nfiles], s, sr - s);
684       csf->dirs[csf->nfiles][sr - s] = 0;
685       csf->nfiles++;
686     }
687 }
688
689 struct addfileprovides_cbdata {
690   int nfiles;
691   Id *ids;
692   char **dirs;
693   char **names;
694
695   Repodata *olddata;
696   Id *dids;
697   Map useddirs;
698 };
699
700 static int
701 addfileprovides_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
702 {
703   struct addfileprovides_cbdata *cbd = cbdata;
704   int i;
705
706   if (data != cbd->olddata)
707     {
708       map_free(&cbd->useddirs);
709       map_init(&cbd->useddirs, data->dirpool.ndirs);
710       for (i = 0; i < cbd->nfiles; i++)
711         {
712           Id did = repodata_str2dir(data, cbd->dirs[i], 0);
713           cbd->dids[i] = did;
714           if (did)
715             MAPSET(&cbd->useddirs, did);
716         }
717       cbd->olddata = data;
718     }
719   if (!MAPTST(&cbd->useddirs, value->id))
720     return 0;
721   for (i = 0; i < cbd->nfiles; i++)
722     {
723       if (cbd->dids[i] != value->id)
724         continue;
725       if (!strcmp(cbd->names[i], value->str))
726         break;
727     }
728   if (i == cbd->nfiles)
729     return 0;
730   s->provides = repo_addid_dep(s->repo, s->provides, cbd->ids[i], SOLVABLE_FILEMARKER);
731   return 0;
732 }
733
734
735 static void
736 pool_addfileprovides_search(Pool *pool, struct addfileprovides_cbdata *cbd, struct searchfiles *sf, Repo *repoonly)
737 {
738   Id p, start, end, *idp;
739   Solvable *s;
740   Repodata *data = 0, *nextdata;
741   Repo *oldrepo = 0;
742   int dataincludes = 0;
743   int i;
744   Map providedids;
745
746   cbd->nfiles = sf->nfiles;
747   cbd->ids = sf->ids;
748   cbd->dirs = sf->dirs;
749   cbd->names = sf->names;
750   cbd->olddata = 0;
751   cbd->dids = sat_realloc2(cbd->dids, sf->nfiles, sizeof(Id));
752   if (repoonly)
753     {
754       start = repoonly->start;
755       end = repoonly->end;
756     }
757   else
758     {
759       start = 2;        /* skip system solvable */
760       end = pool->nsolvables;
761     }
762   for (p = start, s = pool->solvables + p; p < end; p++, s++)
763     {
764       if (!s->repo || (repoonly && s->repo != repoonly))
765         continue;
766       if (s->repo != oldrepo || (data && p >= data->end))
767         {
768           data = 0;
769           oldrepo = 0;
770         }
771       if (oldrepo == 0)
772         {
773           nextdata = 0;
774           for (i = 0, data = s->repo->repodata; i < s->repo->nrepodata; i++, data++)
775             {
776               if (!data->addedfileprovides || p >= data->end)
777                 continue;
778               if (!nextdata || nextdata->start > data->start)
779                 nextdata = data;
780               if (p >= data->start)
781                 break;
782             }
783           if (i == s->repo->nrepodata)
784             data = nextdata;
785           if (data)
786             {
787               map_init(&providedids, pool->ss.nstrings);
788               for (idp = data->addedfileprovides; *idp; idp++)
789                 MAPSET(&providedids, *idp);
790               for (i = 0; i < cbd->nfiles; i++)
791                 if (!MAPTST(&providedids, cbd->ids[i]))
792                   {
793                     break;
794                   }
795               map_free(&providedids);
796               dataincludes = i == cbd->nfiles;
797             }
798           oldrepo = s->repo;
799         }
800       if (data && p >= data->start && dataincludes)
801         continue;
802       repo_search(s->repo, p, SOLVABLE_FILELIST, 0, 0, addfileprovides_cb, cbd);
803     }
804 }
805
806 void
807 pool_addfileprovides_ids(Pool *pool, Repo *installed, Id **idp)
808 {
809   Solvable *s;
810   Repo *repo;
811   struct searchfiles sf, isf, *isfp;
812   struct addfileprovides_cbdata cbd;
813   int i;
814
815   memset(&sf, 0, sizeof(sf));
816   map_init(&sf.seen, pool->ss.nstrings + pool->nrels);
817   memset(&isf, 0, sizeof(isf));
818   map_init(&isf.seen, pool->ss.nstrings + pool->nrels);
819
820   isfp = installed ? &isf : 0;
821   for (i = 1, s = pool->solvables + i; i < pool->nsolvables; i++, s++)
822     {
823       repo = s->repo;
824       if (!repo)
825         continue;
826       if (s->obsoletes)
827         pool_addfileprovides_dep(pool, repo->idarraydata + s->obsoletes, &sf, isfp);
828       if (s->conflicts)
829         pool_addfileprovides_dep(pool, repo->idarraydata + s->conflicts, &sf, isfp);
830       if (s->requires)
831         pool_addfileprovides_dep(pool, repo->idarraydata + s->requires, &sf, isfp);
832       if (s->recommends)
833         pool_addfileprovides_dep(pool, repo->idarraydata + s->recommends, &sf, isfp);
834       if (s->suggests)
835         pool_addfileprovides_dep(pool, repo->idarraydata + s->suggests, &sf, isfp);
836       if (s->supplements)
837         pool_addfileprovides_dep(pool, repo->idarraydata + s->supplements, &sf, isfp);
838       if (s->enhances)
839         pool_addfileprovides_dep(pool, repo->idarraydata + s->enhances, &sf, isfp);
840       if (s->freshens)
841         pool_addfileprovides_dep(pool, repo->idarraydata + s->freshens, &sf, isfp);
842     }
843   map_free(&sf.seen);
844   map_free(&isf.seen);
845   POOL_DEBUG(SAT_DEBUG_STATS, "found %d file dependencies\n", sf.nfiles);
846   POOL_DEBUG(SAT_DEBUG_STATS, "found %d installed file dependencies\n", isf.nfiles);
847   cbd.dids = 0;
848   map_init(&cbd.useddirs, 1);
849   if (idp)
850     *idp = 0;
851   if (sf.nfiles)
852     {
853 #if 0
854       for (i = 0; i < sf.nfiles; i++)
855         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in filelist\n", id2str(pool, sf.ids[i]));
856 #endif
857       pool_addfileprovides_search(pool, &cbd, &sf, 0);
858       if (idp)
859         {
860           sf.ids = sat_extend(sf.ids, sf.nfiles, 1, sizeof(Id), SEARCHFILES_BLOCK);
861           sf.ids[sf.nfiles] = 0;
862           *idp = sf.ids;
863           sf.ids = 0;
864         }
865       sat_free(sf.ids);
866       for (i = 0; i < sf.nfiles; i++)
867         {
868           sat_free(sf.dirs[i]);
869           sat_free(sf.names[i]);
870         }
871       sat_free(sf.dirs);
872       sat_free(sf.names);
873     }
874   if (isf.nfiles)
875     {
876 #if 0
877       for (i = 0; i < isf.nfiles; i++)
878         POOL_DEBUG(SAT_DEBUG_STATS, "looking up %s in installed filelist\n", id2str(pool, isf.ids[i]));
879 #endif
880       if (installed)
881         pool_addfileprovides_search(pool, &cbd, &isf, installed);
882       sat_free(isf.ids);
883       for (i = 0; i < isf.nfiles; i++)
884         {
885           sat_free(isf.dirs[i]);
886           sat_free(isf.names[i]);
887         }
888       sat_free(isf.dirs);
889       sat_free(isf.names);
890     }
891   map_free(&cbd.useddirs);
892   sat_free(cbd.dids);
893   pool_freewhatprovides(pool);  /* as we have added provides */
894 }
895
896 void
897 pool_addfileprovides(Pool *pool, Repo *installed)
898 {
899   pool_addfileprovides_ids(pool, installed, 0);
900 }
901
902 void
903 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)
904 {
905   if (p)
906     {
907       if (pool->solvables[p].repo)
908         repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
909       return;
910     }
911   /* FIXME: obey callback return value! */
912   for (p = 1; p < pool->nsolvables; p++)
913     if (pool->solvables[p].repo)
914       repo_search(pool->solvables[p].repo, p, key, match, flags, callback, cbdata);
915 }
916
917
918 void
919 pool_set_languages(Pool *pool, const char **languages, int nlanguages)
920 {
921   int i;
922
923   pool->languagecache = sat_free(pool->languagecache);
924   pool->languagecacheother = 0;
925   if (pool->nlanguages)
926     {
927       for (i = 0; i < pool->nlanguages; i++)
928         free((char *)pool->languages[i]);
929       free(pool->languages);
930     }
931   pool->nlanguages = nlanguages;
932   if (!nlanguages)
933     return;
934   pool->languages = sat_calloc(nlanguages, sizeof(const char **));
935   for (i = 0; i < pool->nlanguages; i++)
936     pool->languages[i] = strdup(languages[i]);
937 }
938
939 char *
940 pool_alloctmpspace(Pool *pool, int len)
941 {
942   int n = pool->tmpspacen;
943   if (!len)
944     return 0;
945   if (len > pool->tmpspacelen[n])
946     {
947       pool->tmpspacebuf[n] = sat_realloc(pool->tmpspacebuf[n], len + 32);
948       pool->tmpspacelen[n] = len + 32;
949     }
950   pool->tmpspacen = (n + 1) % POOL_TMPSPACEBUF;
951   return pool->tmpspacebuf[n];
952 }
953
954 /*******************************************************************/
955
956 struct mptree {
957   Id sibling;
958   Id child;
959   const char *comp;
960   int compl;
961   Id mountpoint;
962 };
963
964 struct ducbdata {
965   DUChanges *mps;
966   struct mptree *mptree;
967   int addsub;
968   int hasdu;
969
970   Id *dirmap;
971   int nmap;
972   Repodata *olddata;
973 };
974
975
976 static int
977 solver_fill_DU_cb(void *cbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *value)
978 {
979   struct ducbdata *cbd = cbdata;
980   Id mp;
981
982   if (data != cbd->olddata)
983     {
984       Id dn, mp, comp, *dirmap, *dirs;
985       int i, compl;
986       const char *compstr;
987       struct mptree *mptree;
988
989       /* create map from dir to mptree */
990       cbd->dirmap = sat_free(cbd->dirmap);
991       cbd->nmap = 0;
992       dirmap = sat_calloc(data->dirpool.ndirs, sizeof(Id));
993       mptree = cbd->mptree;
994       mp = 0;
995       for (dn = 2, dirs = data->dirpool.dirs + dn; dn < data->dirpool.ndirs; dn++)
996         {
997           comp = *dirs++;
998           if (comp <= 0)
999             {
1000               mp = dirmap[-comp];
1001               continue;
1002             }
1003           if (mp < 0)
1004             {
1005               /* unconnected */
1006               dirmap[dn] = mp;
1007               continue;
1008             }
1009           if (!mptree[mp].child)
1010             {
1011               dirmap[dn] = -mp;
1012               continue;
1013             }
1014           if (data->localpool)
1015             compstr = stringpool_id2str(&data->spool, comp);
1016           else
1017             compstr = id2str(data->repo->pool, comp);
1018           compl = strlen(compstr);
1019           for (i = mptree[mp].child; i; i = mptree[i].sibling)
1020             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1021               break;
1022           dirmap[dn] = i ? i : -mp;
1023         }
1024       /* change dirmap to point to mountpoint instead of mptree */
1025       for (dn = 0; dn < data->dirpool.ndirs; dn++)
1026         {
1027           mp = dirmap[dn];
1028           dirmap[dn] = mptree[mp > 0 ? mp : -mp].mountpoint;
1029         }
1030       cbd->dirmap = dirmap;
1031       cbd->nmap = data->dirpool.ndirs;
1032       cbd->olddata = data;
1033     }
1034   cbd->hasdu = 1;
1035   if (value->id < 0 || value->id >= cbd->nmap)
1036     return 0;
1037   mp = cbd->dirmap[value->id];
1038   if (mp < 0)
1039     return 0;
1040   if (cbd->addsub > 0)
1041     {
1042       cbd->mps[mp].kbytes += value->num;
1043       cbd->mps[mp].files += value->num2;
1044     }
1045   else
1046     {
1047       cbd->mps[mp].kbytes -= value->num;
1048       cbd->mps[mp].files -= value->num2;
1049     }
1050   return 0;
1051 }
1052
1053 static void
1054 propagate_mountpoints(struct mptree *mptree, int pos, Id mountpoint)
1055 {
1056   int i;
1057   if (mptree[pos].mountpoint == -1)
1058     mptree[pos].mountpoint = mountpoint;
1059   else
1060     mountpoint = mptree[pos].mountpoint;
1061   for (i = mptree[pos].child; i; i = mptree[i].sibling)
1062     propagate_mountpoints(mptree, i, mountpoint);
1063 }
1064
1065 #define MPTREE_BLOCK 15
1066
1067 void
1068 pool_calc_duchanges(Pool *pool, Repo *oldinstalled, Map *installedmap, DUChanges *mps, int nmps)
1069 {
1070   char *p;
1071   const char *path, *compstr;
1072   struct mptree *mptree;
1073   int i, nmptree;
1074   int pos, compl;
1075   int mp;
1076   struct ducbdata cbd;
1077   Solvable *s;
1078   Id sp;
1079   Map ignoredu;
1080
1081   memset(&ignoredu, 0, sizeof(ignoredu));
1082   cbd.mps = mps;
1083   cbd.addsub = 0;
1084   cbd.dirmap = 0;
1085   cbd.nmap = 0;
1086   cbd.olddata = 0;
1087
1088   mptree = sat_extend_resize(0, 1, sizeof(struct mptree), MPTREE_BLOCK);
1089
1090   /* our root node */
1091   mptree[0].sibling = 0;
1092   mptree[0].child = 0;
1093   mptree[0].comp = 0;
1094   mptree[0].compl = 0;
1095   mptree[0].mountpoint = -1;
1096   nmptree = 1;
1097   
1098   /* create component tree */
1099   for (mp = 0; mp < nmps; mp++)
1100     {
1101       mps[mp].kbytes = 0;
1102       mps[mp].files = 0;
1103       pos = 0;
1104       path = mps[mp].path;
1105       while(*path == '/')
1106         path++;
1107       while (*path)
1108         {
1109           if ((p = strchr(path, '/')) == 0)
1110             {
1111               compstr = path;
1112               compl = strlen(compstr);
1113               path += compl;
1114             }
1115           else
1116             {
1117               compstr = path;
1118               compl = p - path;
1119               path = p + 1;
1120               while(*path == '/')
1121                 path++;
1122             }
1123           for (i = mptree[pos].child; i; i = mptree[i].sibling)
1124             if (mptree[i].compl == compl && !strncmp(mptree[i].comp, compstr, compl))
1125               break;
1126           if (!i)
1127             {
1128               /* create new node */
1129               mptree = sat_extend(mptree, nmptree, 1, sizeof(struct mptree), MPTREE_BLOCK);
1130               i = nmptree++;
1131               mptree[i].sibling = mptree[pos].child;
1132               mptree[i].child = 0;
1133               mptree[i].comp = compstr;
1134               mptree[i].compl = compl;
1135               mptree[i].mountpoint = -1;
1136               mptree[pos].child = i;
1137             }
1138           pos = i;
1139         }
1140       mptree[pos].mountpoint = mp;
1141     }
1142
1143   propagate_mountpoints(mptree, 0, mptree[0].mountpoint);
1144
1145 #if 0
1146   for (i = 0; i < nmptree; i++)
1147     {
1148       printf("#%d sibling: %d\n", i, mptree[i].sibling);
1149       printf("#%d child: %d\n", i, mptree[i].child);
1150       printf("#%d comp: %s\n", i, mptree[i].comp);
1151       printf("#%d compl: %d\n", i, mptree[i].compl);
1152       printf("#%d mountpont: %d\n", i, mptree[i].mountpoint);
1153     }
1154 #endif
1155
1156   cbd.mptree = mptree;
1157   cbd.addsub = 1;
1158   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1159     {
1160       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1161         continue;
1162       if (!MAPTST(installedmap, sp))
1163         continue;
1164       cbd.hasdu = 0;
1165       repo_search(s->repo, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1166       if (!cbd.hasdu && oldinstalled)
1167         {
1168           Id op, *opp;
1169           /* no du data available, ignore data of all installed solvables we obsolete */
1170           if (!ignoredu.map)
1171             map_init(&ignoredu, oldinstalled->end - oldinstalled->start);
1172           if (s->obsoletes)
1173             {
1174               Id obs, *obsp = s->repo->idarraydata + s->obsoletes;
1175               while ((obs = *obsp++) != 0)
1176                 FOR_PROVIDES(op, opp, obs)
1177                   if (op >= oldinstalled->start && op < oldinstalled->end)
1178                     MAPSET(&ignoredu, op - oldinstalled->start);
1179             }
1180           FOR_PROVIDES(op, opp, s->name)
1181             if (pool->solvables[op].name == s->name)
1182               if (op >= oldinstalled->start && op < oldinstalled->end)
1183                 MAPSET(&ignoredu, op - oldinstalled->start);
1184         }
1185     }
1186   cbd.addsub = -1;
1187   if (oldinstalled)
1188     {
1189       /* assumes we allways have du data for installed solvables */
1190       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1191         {
1192           if (MAPTST(installedmap, sp))
1193             continue;
1194           if (ignoredu.map && MAPTST(&ignoredu, sp - oldinstalled->start))
1195             continue;
1196           repo_search(oldinstalled, sp, SOLVABLE_DISKUSAGE, 0, 0, solver_fill_DU_cb, &cbd);
1197         }
1198     }
1199   if (ignoredu.map)
1200     map_free(&ignoredu);
1201   sat_free(cbd.dirmap);
1202   sat_free(mptree);
1203 }
1204
1205 int
1206 pool_calc_installsizechange(Pool *pool, Repo *oldinstalled, Map *installedmap)
1207 {
1208   Id sp;
1209   Solvable *s;
1210   int change = 0;
1211
1212   for (sp = 1, s = pool->solvables + sp; sp < pool->nsolvables; sp++, s++)
1213     {
1214       if (!s->repo || (oldinstalled && s->repo == oldinstalled))
1215         continue;
1216       if (!MAPTST(installedmap, sp))
1217         continue;
1218       change += repo_lookup_num(s, SOLVABLE_INSTALLSIZE);
1219     }
1220   if (oldinstalled)
1221     {
1222       FOR_REPO_SOLVABLES(oldinstalled, sp, s)
1223         {
1224           if (MAPTST(installedmap, sp))
1225             continue;
1226           change -= repo_lookup_num(s, SOLVABLE_INSTALLSIZE);
1227         }
1228     }
1229   return change;
1230 }
1231
1232 // EOF