- also disable update rules for "keep installed" jobs.
[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_DIRNUMNUMARRAY:
106       if (!kv->entry)
107         printf("%s:\n%*s", keyname, indent, "");
108       printf("  %s %d %d\n", repodata_dir2str(data, kv->id, 0), kv->num, kv->num2);
109       break;
110     case REPOKEY_TYPE_DIRSTRARRAY:
111       if (!kv->entry)
112         printf("%s:\n%*s", keyname, indent, "");
113       printf("  %s\n", repodata_dir2str(data, kv->id, kv->str));
114       break;
115     case REPOKEY_TYPE_FIXARRAY:
116     case REPOKEY_TYPE_FLEXARRAY:
117       if (!kv->entry)
118         printf("%s:\n", keyname);
119       else
120         printf("\n");
121       break;
122     default:
123       printf("%s: ?\n", keyname);
124       break;
125     }
126   return 0;
127 }
128
129 #if 1
130 static int
131 dump_repoattrs_cb(void *vcbdata, Solvable *s, Repodata *data, Repokey *key, KeyValue *kv)
132 {
133   if (key->name == REPOSITORY_SOLVABLES)
134     return SEARCH_NEXT_SOLVABLE;
135   return dump_attr(data->repo, data, key, kv);
136 }
137 #endif
138
139 /*
140  * dump all attributes for Id <p>
141  */
142
143 void
144 dump_repoattrs(Repo *repo, Id p)
145 {
146 #if 0
147   repo_search(repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB, dump_repoattrs_cb, 0);
148 #else
149   Dataiterator di;
150   dataiterator_init(&di, repo->pool, repo, p, 0, 0, SEARCH_ARRAYSENTINEL|SEARCH_SUB);
151   while (dataiterator_step(&di))
152     dump_attr(repo, di.data, di.key, &di.kv);
153 #endif
154 }
155
156 #if 0
157 void
158 dump_some_attrs(Repo *repo, Solvable *s)
159 {
160   const char *summary = 0;
161   unsigned int medianr = -1, downloadsize = -1;
162   unsigned int time = -1;
163   summary = repo_lookup_str(s, SOLVABLE_SUMMARY);
164   medianr = repo_lookup_num(s, SOLVABLE_MEDIANR);
165   downloadsize = repo_lookup_num (s, SOLVABLE_DOWNLOADSIZE);
166   time = repo_lookup_num(s, SOLVABLE_BUILDTIME);
167   printf ("  XXX %d %d %u %s\n", medianr, downloadsize, time, summary);
168 }
169 #endif
170
171
172 static FILE *
173 loadcallback (Pool *pool, Repodata *data, void *vdata)
174 {
175   FILE *fp = 0;
176 printf("LOADCALLBACK\n");
177   const char *location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
178 printf("loc %s\n", location);
179   if (location && with_attr)
180     {
181       fprintf (stderr, "Loading SOLV file %s\n", location);
182       fp = fopen (location, "r");
183       if (!fp)
184         perror(location);
185     }
186   return fp;
187 }
188
189
190 static void
191 usage( const char *err )
192 {
193   if (err)
194     fprintf (stderr, "\n** Error:\n  %s\n", err);
195   fprintf( stderr, "\nUsage:\n"
196            "dumpsolv [-a] [<solvfile>]\n"
197            "  -a  read attributes.\n"
198            );
199   exit(0);
200 }
201
202 #if 0
203 static void
204 tryme (Repo *repo, Id p, Id keyname, const char *match, int flags)
205 {
206   Dataiterator di;
207   dataiterator_init(&di, repo, p, keyname, match, flags);
208   while (dataiterator_step(&di))
209     {
210       switch (di.key->type)
211         {
212           case REPOKEY_TYPE_ID:
213           case REPOKEY_TYPE_IDARRAY:
214               if (di.data && di.data->localpool)
215                 di.kv.str = stringpool_id2str(&di.data->spool, di.kv.id);
216               else
217                 di.kv.str = id2str(repo->pool, di.kv.id);
218               break;
219           case REPOKEY_TYPE_STR:
220           case REPOKEY_TYPE_DIRSTRARRAY:
221               break;
222           default:
223               di.kv.str = 0;
224         }
225       fprintf (stdout, "found: %d:%s %d %s %d %d %d\n",
226                di.solvid,
227                id2str(repo->pool, di.key->name),
228                di.kv.id,
229                di.kv.str, di.kv.num, di.kv.num2, di.kv.eof);
230     }
231 }
232 #endif
233
234 int main(int argc, char **argv)
235 {
236   Repo *repo;
237   Pool *pool;
238   int i, j, n;
239   Solvable *s;
240   
241   pool = pool_create();
242   pool_setdebuglevel(pool, 1);
243   pool_setloadcallback(pool, loadcallback, 0);
244
245   argv++;
246   argc--;
247   while (argc--)
248     {
249       const char *s = argv[0];
250       if (*s++ == '-')
251         while (*s)
252           switch (*s++)
253             {
254               case 'h': usage(NULL); break;
255               case 'a': with_attr = 1; break;
256               default : break;
257             }
258       else
259         {
260           if (freopen (argv[0], "r", stdin) == 0)
261             {
262               perror(argv[0]);
263               exit(1);
264             }
265           repo = repo_create(pool, argv[0]);
266           if (repo_add_solv(repo, stdin))
267             printf("could not read repository\n");
268         }
269       argv++;
270     }
271
272   if (!pool->nrepos)
273     {
274       repo = repo_create(pool, argc != 1 ? argv[1] : "<stdin>");
275       if (repo_add_solv(repo, stdin))
276         printf("could not read repository\n");
277     }
278   printf("pool contains %d strings, %d rels, string size is %d\n", pool->ss.nstrings, pool->nrels, pool->ss.sstrings);
279
280 #if 0
281 {
282   Dataiterator di;
283   dataiterator_init(&di, repo, -1, 0, "oo", DI_SEARCHSUB|SEARCH_SUBSTRING);
284   while (dataiterator_step(&di))
285     dump_attr(di.repo, di.data, di.key, &di.kv);
286   exit(0);
287 }
288 #endif
289
290   for (j = 0; 1 && j < pool->nrepos; j++)
291     {
292       repo = pool->repos[j];
293       dump_repodata(repo);
294
295       printf("repo %d contains %d solvables\n", j, repo->nsolvables);
296       printf("repo start: %d end: %d\n", repo->start, repo->end);
297       for (i = repo->start, n = 1; i < repo->end; i++)
298         {
299           s = pool->solvables + i;
300           if (s->repo != repo)
301             continue;
302           printf("\n");
303           printf("solvable %d (%d):\n", n, i);
304 #if 0
305           if (s->name || s->evr || s->arch)
306             printf("name: %s %s %s\n", id2str(pool, s->name), id2str(pool, s->evr), id2str(pool, s->arch));
307           if (s->vendor)
308             printf("vendor: %s\n", id2str(pool, s->vendor));
309           printids(repo, "provides", s->provides);
310           printids(repo, "obsoletes", s->obsoletes);
311           printids(repo, "conflicts", s->conflicts);
312           printids(repo, "requires", s->requires);
313           printids(repo, "recommends", s->recommends);
314           printids(repo, "suggests", s->suggests);
315           printids(repo, "supplements", s->supplements);
316           printids(repo, "enhances", s->enhances);
317           if (repo->rpmdbid)
318             printf("rpmdbid: %u\n", repo->rpmdbid[i - repo->start]);
319 #if 0
320           dump_attrs (repo, n - 1);
321 #endif
322 #endif
323           dump_repoattrs(repo, i);
324 #if 0
325           dump_some_attrs(repo, s);
326 #endif
327           n++;
328         }
329 #if 0
330       tryme(repo, 0, SOLVABLE_MEDIANR, 0, 0);
331       printf("\n");
332       tryme(repo, 0, 0, 0, 0);
333       printf("\n");
334       tryme(repo, 0, 0, "*y*e*", SEARCH_GLOB);
335 #endif
336     }
337 #if 0
338   printf ("\nSearchresults:\n");
339   Dataiterator di;
340   dataiterator_init(&di, pool, 0, 0, 0, "3", SEARCH_SUB | SEARCH_SUBSTRING | SEARCH_FILES);
341   //int count = 0;
342   while (dataiterator_step(&di))
343     {
344       printf("%d:", di.solvid);
345       dump_attr(repo, di.data, di.key, &di.kv);
346       /*if (di.solvid == 4 && count++ == 0)
347         dataiterator_jump_to_solvable(&di, pool->solvables + 3);*/
348       //dataiterator_skip_attribute(&di);
349       //dataiterator_skip_solvable(&di);
350       //dataiterator_skip_repo(&di);
351     }
352 #endif
353   pool_free(pool);
354   exit(0);
355 }