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