Fix mergesolv for repos with holes.
[platform/upstream/libsolv.git] / tools / mergesolv.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  * mergesolv
10  * 
11  */
12
13 #include <sys/types.h>
14 #include <limits.h>
15 #include <fcntl.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <assert.h>
20
21 #include "pool.h"
22 #include "repo_solv.h"
23 #include "repo_write.h"
24
25 int
26 main(int argc, char **argv)
27 {
28   Pool *pool = pool_create();
29   int i;
30   int new_id_size;
31   Id *new_id;
32
33   while (argc-- > 1)
34     {
35       FILE *fp;
36       argv++;
37       if ((fp = fopen(*argv, "r")) == NULL)
38         {
39           perror(argv[1]);
40           exit(0);
41         }
42       repo_add_solv(repo_create(pool, ""), fp);
43       fclose(fp);
44     }
45   if (!pool->nrepos)
46     return 0;
47
48   new_id_size = 0;
49   for (i = 0; i < pool->nrepos; i++)
50     new_id_size += pool->repos[i]->idarraysize;
51   new_id = (Id*) malloc (sizeof (Id) * new_id_size);
52   new_id_size = 0;
53   for (i = 0; i < pool->nrepos; i++)
54     {
55       Repo *repo = pool->repos[i];
56       int si;
57       Solvable *s;
58       memcpy (new_id + new_id_size, repo->idarraydata,
59               repo->idarraysize * sizeof (new_id[0]));
60       FOR_REPO_SOLVABLES (repo, si, s)
61         {
62           if (s->provides)
63             s->provides += new_id_size;
64           if (s->obsoletes)
65             s->obsoletes += new_id_size;
66           if (s->conflicts)
67             s->conflicts += new_id_size;
68           if (s->requires)
69             s->requires += new_id_size;
70           if (s->recommends)
71             s->recommends += new_id_size;
72           if (s->suggests)
73             s->suggests+= new_id_size;
74           if (s->supplements)
75             s->supplements += new_id_size;
76           if (s->enhances)
77             s->enhances += new_id_size;
78           if (s->freshens)
79             s->freshens += new_id_size;
80           if (i > 0)
81             s->repo = pool->repos[0];
82         }
83       new_id_size += repo->idarraysize;
84       if (i > 0)
85         {
86           pool->repos[0]->nsolvables += repo->nsolvables;
87           if (pool->repos[0]->start > repo->start)
88             pool->repos[0]->start = repo->start;
89           if (pool->repos[0]->end < repo->end)
90             pool->repos[0]->end = repo->end;
91           repo->nsolvables = 0;
92           repo->start = pool->nsolvables;
93           repo->end = repo->start;
94           free (repo->idarraydata);
95           repo->idarraydata = 0;
96         }
97     }
98   while (pool->nrepos > 1)
99     {
100       repo_free(pool->repos[pool->nrepos - 1], 1);
101     }
102   free (pool->repos[0]->idarraydata);
103   pool->repos[0]->idarraydata = new_id;
104   pool->repos[0]->idarraysize = new_id_size;
105
106   repo_write(pool->repos[0], stdout);
107   pool_free(pool);
108
109   return 0;
110 }