- write info block containing addedprovides
[platform/upstream/libsolv.git] / tools / dumpsolv.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 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <string.h>
12
13 static int with_attr = 0;
14
15 #include "pool.h"
16 #include "repo_solv.h"
17
18 static void
19 dump_repodata (Repo *repo)
20 {
21   unsigned i;
22   Repodata *data;
23   if (repo->nrepodata == 0)
24     return;
25   printf("repo refers to %d subfiles:\n", repo->nrepodata);
26   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
27     {
28       unsigned int j;
29       printf("%s has %d keys, %d schemata\n", data->location ? data->location : "**EMBED**", data->nkeys, data->nschemata);
30       for (j = 1; j < data->nkeys; j++)
31         printf("  %s (type %s size %d storage %d)\n", id2str(repo->pool, data->keys[j].name), id2str(repo->pool, data->keys[j].type), data->keys[j].size, data->keys[j].storage);
32       if (data->localpool)
33         printf("  localpool has %d strings, size is %d\n", data->spool.nstrings, data->spool.sstrings);
34       if (data->dirpool.ndirs)
35         printf("  localpool has %d directories\n", data->dirpool.ndirs);
36       if (data->addedfileprovides)
37         {
38           printf("  added file provides:\n");
39           for (j = 0; data->addedfileprovides[j]; j++)
40             printf("    %s\n", id2str(repo->pool, data->addedfileprovides[j]));
41         }
42       printf("\n");
43     }
44   printf("\n");
45 }
46
47 static void
48 printids(Repo *repo, char *kind, Offset ido)
49 {
50   Pool *pool = repo->pool;
51   Id id, *ids;
52   if (!ido)
53     return;
54   printf("%s:\n", kind);
55   ids = repo->idarraydata + ido;
56   while((id = *ids++) != 0)
57     printf("  %s\n", dep2str(pool, id));
58 }
59
60 int
61 dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
62 {
63   const char *keyname;
64
65   keyname = id2str(s->repo->pool, key->name);
66   switch(key->type)
67     {
68     case REPOKEY_TYPE_ID:
69       if (data && data->localpool)
70         kv->str = stringpool_id2str(&data->spool, kv->id);
71       else
72         kv->str = id2str(s->repo->pool, kv->id);
73       printf("%s: %s\n", keyname, kv->str);
74       break;
75     case REPOKEY_TYPE_CONSTANTID:
76       printf("%s: %s\n", keyname, dep2str(s->repo->pool, kv->id));
77       break;
78     case REPOKEY_TYPE_IDARRAY:
79       if (data && data->localpool)
80         printf("%s: %s\n", keyname, stringpool_id2str(&data->spool, kv->id));
81       else
82         printf("%s: %s\n", keyname, dep2str(s->repo->pool, kv->id));
83       break;
84     case REPOKEY_TYPE_STR:
85       printf("%s: %s\n", keyname, kv->str);
86       break;
87     case REPOKEY_TYPE_VOID:
88       printf("%s: (void)\n", keyname);
89       break;
90     case REPOKEY_TYPE_U32:
91     case REPOKEY_TYPE_NUM:
92     case REPOKEY_TYPE_CONSTANT:
93       printf("%s: %d\n", keyname, kv->num);
94       break;
95     case REPOKEY_TYPE_DIRNUMNUMARRAY:
96       printf("%s: %s %d %d\n", keyname, repodata_dir2str(data, kv->id, 0), kv->num, kv->num2);
97       break;
98     case REPOKEY_TYPE_DIRSTRARRAY:
99       printf("%s: %s\n", keyname, repodata_dir2str(data, kv->id, kv->str));
100       break;
101     default:
102       printf("%s: ?\n", keyname);
103       break;
104     }
105   return 0;
106 }
107
108 /*
109  * dump all attributes for Id <p>
110  */
111
112 void
113 dump_repoattrs(Repo *repo, Id p)
114 {
115 #if 1
116   repo_search(repo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE, dump_repoattrs_cb, 0);
117 #else
118   Dataiterator di;
119   dataiterator_init(&di, repo, p, 0, 0, SEARCH_NO_STORAGE_SOLVABLE);
120   while (dataiterator_step(&di))
121     dump_repoattrs_cb(0, repo->pool->solvables + di.solvid, di.data, di.key,
122                       &di.kv);
123 #endif
124 }
125
126 #if 0
127 void
128 dump_some_attrs(Repo *repo, Solvable *s)
129 {
130   const char *summary = 0;
131   unsigned int medianr = -1, downloadsize = -1;
132   unsigned int time = -1;
133   summary = repo_lookup_str(s, SOLVABLE_SUMMARY);
134   medianr = repo_lookup_num(s, SOLVABLE_MEDIANR);
135   downloadsize = repo_lookup_num (s, SOLVABLE_DOWNLOADSIZE);
136   time = repo_lookup_num(s, SOLVABLE_BUILDTIME);
137   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
138 }
139 #endif
140
141
142 static FILE *
143 loadcallback (Pool *pool, Repodata *data, void *vdata)
144 {
145   FILE *fp = 0;
146   if (data->location && with_attr)
147     {
148       fprintf (stderr, "Loading SOLV file %s\n", data->location);
149       fp = fopen (data->location, "r");
150       if (!fp)
151         perror(data->location);
152     }
153   return fp;
154 }
155
156
157 static void
158 usage( const char *err )
159 {
160   if (err)
161     fprintf (stderr, "\n** Error:\n  %s\n", err);
162   fprintf( stderr, "\nUsage:\n"
163            "dumpsolv [-a] [<solvfile>]\n"
164            "  -a  read attributes.\n"
165            );
166   exit(0);
167 }
168
169 #if 0
170 static void
171 tryme (Repo *repo, Id p, Id keyname, const char *match, int flags)
172 {
173   Dataiterator di;
174   dataiterator_init(&di, repo, p, keyname, match, flags);
175   while (dataiterator_step(&di))
176     {
177       switch (di.key->type)
178         {
179           case REPOKEY_TYPE_ID:
180           case REPOKEY_TYPE_IDARRAY:
181               if (di.data && di.data->localpool)
182                 di.kv.str = stringpool_id2str(&di.data->spool, di.kv.id);
183               else
184                 di.kv.str = id2str(repo->pool, di.kv.id);
185               break;
186           case REPOKEY_TYPE_STR:
187           case REPOKEY_TYPE_DIRSTRARRAY:
188               break;
189           default:
190               di.kv.str = 0;
191         }
192       fprintf (stdout, "found: %d:%s %d %s %d %d %d\n",
193                di.solvid,
194                id2str(repo->pool, di.key->name),
195                di.kv.id,
196                di.kv.str, di.kv.num, di.kv.num2, di.kv.eof);
197     }
198 }
199 #endif
200
201 int main(int argc, char **argv)
202 {
203   Repo *repo;
204   Pool *pool;
205   int i, n;
206   Solvable *s;
207   
208   argv++;
209   argc--;
210   while (argc--)
211     {
212       const char *s = argv[0];
213       if (*s++ == '-')
214         while (*s)
215           switch (*s++)
216             {
217               case 'h': usage(NULL); break;
218               case 'a': with_attr = 1; break;
219               default : break;
220             }
221       else
222         {
223           if (freopen (argv[0], "r", stdin) == 0)
224             {
225               perror(argv[0]);
226               exit(1);
227             }
228           break;
229         }
230       argv++;
231     }
232
233   pool = pool_create();
234   pool_setdebuglevel(pool, 1);
235   pool_setloadcallback(pool, loadcallback, 0);
236
237   repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
238   if (repo_add_solv(repo, stdin))
239     printf("could not read repository\n");
240   printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
241   dump_repodata(repo);
242   printf("repo contains %d solvables\n", repo->nsolvables);
243   for (i = repo->start, n = 1; i < repo->end; i++)
244     {
245       s = pool->solvables + i;
246       if (s->repo != repo)
247         continue;
248       printf("\n");
249       printf("solvable %d:\n", n);
250       if (s->name || s->evr || s->arch)
251         printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
252       if (s->vendor)
253         printf("vendor: %s\n", id2str(pool, s->vendor));
254       printids(repo, "provides", s->provides);
255       printids(repo, "obsoletes", s->obsoletes);
256       printids(repo, "conflicts", s->conflicts);
257       printids(repo, "requires", s->requires);
258       printids(repo, "recommends", s->recommends);
259       printids(repo, "suggests", s->suggests);
260       printids(repo, "supplements", s->supplements);
261       printids(repo, "enhances", s->enhances);
262       printids(repo, "freshens", s->freshens);
263       if (repo->rpmdbid)
264         printf("rpmdbid: %u\n", repo->rpmdbid[i - repo->start]);
265 #if 0
266       dump_attrs (repo, n - 1);
267 #endif
268       dump_repoattrs(repo, i);
269 #if 0
270       dump_some_attrs(repo, s);
271 #endif
272       n++;
273     }
274 #if 0
275   tryme(repo, 0, SOLVABLE_MEDIANR, 0, 0);
276   printf("\n");
277   tryme(repo, 0, 0, 0, 0);
278   printf("\n");
279   tryme(repo, 0, 0, "*y*e*", SEARCH_GLOB);
280 #endif
281   pool_free(pool);
282   exit(0);
283 }