- kill stillborn KINDS_SEPARATELY, use getopt() in tools
[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 int dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv);
19
20 static void
21 dump_repodata(Repo *repo)
22 {
23   unsigned i;
24   Repodata *data;
25   if (repo->nrepodata == 0)
26     return;
27   printf("repo contains %d repodata sections:\n", repo->nrepodata);
28   for (i = 0, data = repo->repodata; i < repo->nrepodata; i++, data++)
29     {
30       unsigned int j;
31       printf("\nrepodata %d has %d keys, %d schemata\n", i + 1, data->nkeys - 1, data->nschemata - 1);
32       for (j = 1; j < data->nkeys; j++)
33         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);
34       if (data->localpool)
35         printf("  localpool has %d strings, size is %d\n", data->spool.nstrings, data->spool.sstrings);
36       if (data->dirpool.ndirs)
37         printf("  localpool has %d directories\n", data->dirpool.ndirs);
38       printf("\n");
39       repodata_search(data, SOLVID_META, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repoattrs_cb, 0);
40     }
41   printf("\n");
42 }
43
44 #if 0
45 static void
46 printids(Repo *repo, char *kind, Offset ido)
47 {
48   Pool *pool = repo->pool;
49   Id id, *ids;
50   if (!ido)
51     return;
52   printf("%s:\n", kind);
53   ids = repo->idarraydata + ido;
54   while((id = *ids++) != 0)
55     printf("  %s\n", dep2str(pool, id));
56 }
57 #endif
58
59 int
60 dump_attr(Repo *repo, Repodata *data, Repokey *key, KeyValue *kv)
61 {
62   const char *keyname;
63   KeyValue *kvp;
64   int indent = 0;
65
66   keyname = id2str(repo->pool, key->name);
67   for (kvp = kv; (kvp = kvp->parent) != 0; indent += 2)
68     printf("  ");
69   switch(key->type)
70     {
71     case REPOKEY_TYPE_ID:
72       if (data && data->localpool)
73         kv->str = stringpool_id2str(&data->spool, kv->id);
74       else
75         kv->str = id2str(repo->pool, kv->id);
76       printf("%s: %s\n", keyname, kv->str);
77       break;
78     case REPOKEY_TYPE_CONSTANTID:
79       printf("%s: %s\n", keyname, dep2str(repo->pool, kv->id));
80       break;
81     case REPOKEY_TYPE_IDARRAY:
82       if (!kv->entry)
83         printf("%s:\n%*s", keyname, indent, "");
84       if (data && data->localpool)
85         printf("  %s\n", stringpool_id2str(&data->spool, kv->id));
86       else
87         printf("  %s\n", dep2str(repo->pool, kv->id));
88       break;
89     case REPOKEY_TYPE_STR:
90       printf("%s: %s\n", keyname, kv->str);
91       break;
92     case REPOKEY_TYPE_MD5:
93     case REPOKEY_TYPE_SHA1:
94     case REPOKEY_TYPE_SHA256:
95       printf("%s: %s (%s)\n", keyname, repodata_chk2str(data, key->type, (unsigned char *)kv->str), id2str(repo->pool, key->type));
96       break;
97     case REPOKEY_TYPE_VOID:
98       printf("%s: (void)\n", keyname);
99       break;
100     case REPOKEY_TYPE_U32:
101     case REPOKEY_TYPE_NUM:
102     case REPOKEY_TYPE_CONSTANT:
103       printf("%s: %d\n", keyname, kv->num);
104       break;
105     case REPOKEY_TYPE_BINARY:
106       if (kv->num)
107         printf("%s: %02x..%02x len %d\n", keyname, (unsigned char)kv->str[0], (unsigned char)kv->str[kv->num - 1], kv->num);
108       else
109         printf("%s: len 0\n", keyname);
110       break;
111     case REPOKEY_TYPE_DIRNUMNUMARRAY:
112       if (!kv->entry)
113         printf("%s:\n%*s", keyname, indent, "");
114       printf("  %s %d %d\n", repodata_dir2str(data, kv->id, 0), kv->num, kv->num2);
115       break;
116     case REPOKEY_TYPE_DIRSTRARRAY:
117       if (!kv->entry)
118         printf("%s:\n%*s", keyname, indent, "");
119       printf("  %s\n", repodata_dir2str(data, kv->id, kv->str));
120       break;
121     case REPOKEY_TYPE_FIXARRAY:
122     case REPOKEY_TYPE_FLEXARRAY:
123       if (!kv->entry)
124         printf("%s:\n", keyname);
125       else
126         printf("\n");
127       break;
128     default:
129       printf("%s: ?\n", keyname);
130       break;
131     }
132   return 0;
133 }
134
135 #if 1
136 static int
137 dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
138 {
139   if (key->name == REPOSITORY_SOLVABLES)
140     return SEARCH_NEXT_SOLVABLE;
141   return dump_attr(data->repo, data, key, kv);
142 }
143 #endif
144
145 /*
146  * dump all attributes for Id <p>
147  */
148
149 void
150 dump_repoattrs(Repo *repo, Id p)
151 {
152 #if 0
153   repo_search(repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repoattrs_cb, 0);
154 #else
155   Dataiterator di;
156   dataiterator_init(&di, repo->pool, repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB);
157   while (dataiterator_step(&di))
158     dump_attr(repo, di.data, di.key, &di.kv);
159 #endif
160 }
161
162 #if 0
163 void
164 dump_some_attrs(Repo *repo, Solvable *s)
165 {
166   const char *summary = 0;
167   unsigned int medianr = -1, downloadsize = -1;
168   unsigned int time = -1;
169   summary = repo_lookup_str(s, SOLVABLE_SUMMARY);
170   medianr = repo_lookup_num(s, SOLVABLE_MEDIANR);
171   downloadsize = repo_lookup_num (s, SOLVABLE_DOWNLOADSIZE);
172   time = repo_lookup_num(s, SOLVABLE_BUILDTIME);
173   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
174 }
175 #endif
176
177
178 static int
179 loadcallback (Pool *pool, Repodata *data, void *vdata)
180 {
181   FILE *fp = 0;
182   int r;
183
184 printf("LOADCALLBACK\n");
185   const char *location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
186 printf("loc %s\n", location);
187   if (!location || !with_attr)
188     return 0;
189   fprintf (stderr, "Loading SOLV file %s\n", location);
190   fp = fopen (location, "r");
191   if (!fp)
192     {
193       perror(location);
194       return 0;
195     }
196   r = repo_add_solv_flags(data->repo, fp, REPO_USE_LOADING|REPO_LOCALPOOL);
197   fclose(fp);
198   return !r ? 1 : 0;
199 }
200
201
202 static void
203 usage(int status)
204 {
205   fprintf( stderr, "\nUsage:\n"
206            "dumpsolv [-a] [<solvfile>]\n"
207            "  -a  read attributes.\n"
208            );
209   exit(status);
210 }
211
212 #if 0
213 static void
214 tryme (Repo *repo, Id p, Id keyname, const char *match, int flags)
215 {
216   Dataiterator di;
217   dataiterator_init(&di, repo, p, keyname, match, flags);
218   while (dataiterator_step(&di))
219     {
220       switch (di.key->type)
221         {
222           case REPOKEY_TYPE_ID:
223           case REPOKEY_TYPE_IDARRAY:
224               if (di.data && di.data->localpool)
225                 di.kv.str = stringpool_id2str(&di.data->spool, di.kv.id);
226               else
227                 di.kv.str = id2str(repo->pool, di.kv.id);
228               break;
229           case REPOKEY_TYPE_STR:
230           case REPOKEY_TYPE_DIRSTRARRAY:
231               break;
232           default:
233               di.kv.str = 0;
234         }
235       fprintf (stdout, "found: %d:%s %d %s %d %d %d\n",
236                di.solvid,
237                id2str(repo->pool, di.key->name),
238                di.kv.id,
239                di.kv.str, di.kv.num, di.kv.num2, di.kv.eof);
240     }
241 }
242 #endif
243
244 int main(int argc, char **argv)
245 {
246   Repo *repo;
247   Pool *pool;
248   int c, i, j, n;
249   Solvable *s;
250   
251   pool = pool_create();
252   pool_setdebuglevel(pool, 1);
253   pool_setloadcallback(pool, loadcallback, 0);
254
255   while ((c = getopt(argc, argv, "ha")) >= 0)
256     {
257       switch(c)
258         {
259         case 'h':
260           usage(0);
261           break;
262         case 'a':
263           with_attr = 1;
264           break;
265         default:
266           usage(1);
267           break;
268         }
269     }
270   for (; optind < argc; optind++)
271     {
272       if (freopen(argv[optind], "r", stdin) == 0)
273         {
274           perror(argv[optind]);
275           exit(1);
276         }
277       repo = repo_create(pool, argv[optind]);
278       if (repo_add_solv(repo, stdin))
279         printf("could not read repository\n");
280     }
281   if (!pool->nrepos)
282     {
283       repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
284       if (repo_add_solv(repo, stdin))
285         printf("could not read repository\n");
286     }
287   printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
288
289 #if 0
290 {
291   Dataiterator di;
292   dataiterator_init(&di, repo, -1, 0, "oo", DI_SEARCHSUB|SEARCH_SUBSTRING);
293   while (dataiterator_step(&di))
294     dump_attr(di.repo, di.data, di.key, &di.kv);
295   exit(0);
296 }
297 #endif
298
299   n = 0;
300   FOR_REPOS(j, repo)
301     {
302       dump_repodata(repo);
303
304       printf("repo %d contains %d solvables\n", j, repo->nsolvables);
305       printf("repo start: %d end: %d\n", repo->start, repo->end);
306       FOR_REPO_SOLVABLES(repo, i, s)
307         {
308           n++;
309           printf("\n");
310           printf("solvable %d (%d):\n", n, i);
311 #if 0
312           if (s->name || s->evr || s->arch)
313             printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
314           if (s->vendor)
315             printf("vendor: %s\n", id2str(pool, s->vendor));
316           printids(repo, "provides", s->provides);
317           printids(repo, "obsoletes", s->obsoletes);
318           printids(repo, "conflicts", s->conflicts);
319           printids(repo, "requires", s->requires);
320           printids(repo, "recommends", s->recommends);
321           printids(repo, "suggests", s->suggests);
322           printids(repo, "supplements", s->supplements);
323           printids(repo, "enhances", s->enhances);
324           if (repo->rpmdbid)
325             printf("rpmdbid: %u\n", repo->rpmdbid[i - repo->start]);
326 #endif
327           dump_repoattrs(repo, i);
328 #if 0
329           dump_some_attrs(repo, s);
330 #endif
331         }
332 #if 0
333       tryme(repo, 0, SOLVABLE_MEDIANR, 0, 0);
334       printf("\n");
335       tryme(repo, 0, 0, 0, 0);
336       printf("\n");
337       tryme(repo, 0, 0, "*y*e*", SEARCH_GLOB);
338 #endif
339     }
340 #if 0
341   printf ("\nSearchresults:\n");
342   Dataiterator di;
343   dataiterator_init(&di, pool, 0, 0, 0, "3", SEARCH_SUB | SEARCH_SUBSTRING | SEARCH_FILES);
344   //int count = 0;
345   while (dataiterator_step(&di))
346     {
347       printf("%d:", di.solvid);
348       dump_attr(repo, di.data, di.key, &di.kv);
349       /*if (di.solvid == 4 && count++ == 0)
350         dataiterator_jump_to_solvable(&di, pool->solvables + 3);*/
351       //dataiterator_skip_attribute(&di);
352       //dataiterator_skip_solvable(&di);
353       //dataiterator_skip_repo(&di);
354     }
355 #endif
356   pool_free(pool);
357   exit(0);
358 }