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