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