Imported Upstream version 0.6.29
[platform/upstream/libsolv.git] / tools / repomdxml2solv.c
1 /*
2  * Copyright (c) 2007-2009, Novell Inc.
3  *
4  * This program is licensed under the BSD license, read LICENSE.BSD
5  * for further information
6  */
7
8 #include <sys/types.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <unistd.h>
13
14 #include "pool.h"
15 #include "repo.h"
16 #include "chksum.h"
17 #include "repo_repomdxml.h"
18 #include "common_write.h"
19
20 static void
21 usage(int status)
22 {
23   fprintf(stderr, "\nUsage:\n"
24           "repomdxml2solv [-q query]\n"
25           "  reads a 'repomd.xml' file from <stdin> and writes a .solv file to <stdout>\n"
26           "  -q : query a repomd data entry\n"
27           "  -h : print help & exit\n"
28          );
29    exit(status);
30 }
31
32 static void
33 doquery(Pool *pool, Repo *repo, const char *query)
34 {
35   Id id, type = 0;
36   char qbuf[256];
37   const char *qp;
38   Dataiterator di;
39
40   qp = strchr(query, ':');
41   if (qp)
42     {
43       type = pool_strn2id(pool, query, qp - query, 0);
44       if (!type)
45         exit(0);
46       qp++;
47     }
48   else
49     qp = query;
50   snprintf(qbuf, sizeof(qbuf), "repository:repomd:%s", qp);
51   id = pool_str2id(pool, qbuf, 0);
52   if (!id)
53     exit(0);
54   dataiterator_init(&di, pool, repo, SOLVID_META, id, 0, 0);
55   dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
56   while (dataiterator_step(&di))
57     {
58       if (type)
59         {
60           dataiterator_setpos_parent(&di);
61           if (pool_lookup_id(pool, SOLVID_POS, REPOSITORY_REPOMD_TYPE) != type)
62             continue;
63         }
64       switch (di.key->type)
65         {
66         case REPOKEY_TYPE_ID:
67         case REPOKEY_TYPE_CONSTANTID:
68           printf("%s\n", pool_id2str(pool, di.kv.id));
69           break;
70         case REPOKEY_TYPE_STR:
71           printf("%s\n", di.kv.str);
72           break;
73         case REPOKEY_TYPE_NUM:
74         case REPOKEY_TYPE_CONSTANT:
75           printf("%llu\n", SOLV_KV_NUM64(&di.kv));
76           break;
77         default:
78           if (solv_chksum_len(di.key->type))
79             printf("%s:%s\n", solv_chksum_type2str(di.key->type), repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str));
80           break;
81         }
82     }
83   dataiterator_free(&di);
84 }
85
86 int
87 main(int argc, char **argv)
88 {
89   int c, flags = 0;
90   const char *query = 0;
91   
92   Pool *pool = pool_create();
93   Repo *repo = repo_create(pool, "<stdin>");
94
95   while ((c = getopt (argc, argv, "hq:")) >= 0)
96     {
97       switch(c)
98         {
99         case 'h':
100           usage(0);
101           break;
102         case 'q':
103           query = optarg;
104           break;
105         default:
106           usage(1);
107           break;
108         }
109     }
110   if (repo_add_repomdxml(repo, stdin, flags))
111     {
112       fprintf(stderr, "repomdxml2solv: %s\n", pool_errstr(pool));
113       exit(1);
114     }
115   if (query)
116     doquery(pool, repo, query);
117   else
118     tool_write(repo, 0, 0);
119   pool_free(pool);
120   exit(0);
121 }