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