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