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