- put sat_xfopen in libsatsolverext
[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 <limits.h>
10 #include <fcntl.h>
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 #include "pool.h"
17 #include "repo.h"
18 #include "repo_repomdxml.h"
19 #include "common_write.h"
20
21 static void
22 usage(int status)
23 {
24   fprintf(stderr, "\nUsage:\n"
25           "repomdxml2solv [-q query]\n"
26           "  reads a 'repomd.xml' file from <stdin> and writes a .solv file to <stdout>\n"
27           "  -q : query a repomd data entry\n"
28           "  -h : print help & exit\n"
29          );
30    exit(status);
31 }
32
33 static void
34 doquery(Pool *pool, Repo *repo, const char *query)
35 {
36   Id id, type = 0;
37   char qbuf[256];
38   const char *qp;
39   Dataiterator di;
40
41   qp = strchr(query, ':');
42   if (qp)
43     {
44       type = strn2id(pool, query, qp - query, 0);
45       if (!type)
46         exit(0);
47       qp++;
48     }
49   else
50     qp = query;
51   snprintf(qbuf, sizeof(qbuf), "repository:repomd:%s", qp);
52   id = str2id(pool, qbuf, 0);
53   if (!id)
54     exit(0);
55   dataiterator_init(&di, pool, repo, SOLVID_META, id, 0, 0);
56   dataiterator_prepend_keyname(&di, REPOSITORY_REPOMD);
57   while (dataiterator_step(&di))
58     {
59       if (type)
60         {
61           dataiterator_setpos_parent(&di);
62           if (pool_lookup_id(pool, SOLVID_POS, REPOSITORY_REPOMD_TYPE) != type)
63             continue;
64         }
65       switch (di.key->type)
66         {
67         case REPOKEY_TYPE_ID:
68         case REPOKEY_TYPE_CONSTANTID:
69           printf("%s\n", id2str(pool, di.kv.id));
70           break;
71         case REPOKEY_TYPE_STR:
72           printf("%s\n", di.kv.str);
73           break;
74         case REPOKEY_TYPE_NUM:
75           printf("%d\n", di.kv.num);
76           break;
77         case REPOKEY_TYPE_SHA1:
78           printf("sha1:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str));
79           break;
80         case REPOKEY_TYPE_SHA256:
81           printf("sha256:%s\n", repodata_chk2str(di.data, di.key->type, (unsigned char *)di.kv.str));
82           break;
83         default:
84           break;
85         }
86     }
87   dataiterator_free(&di);
88 }
89
90 int
91 main(int argc, char **argv)
92 {
93   int c, flags = 0;
94   const char *query = 0;
95   
96   Pool *pool = pool_create();
97   Repo *repo = repo_create(pool, "<stdin>");
98
99   while ((c = getopt (argc, argv, "hq:")) >= 0)
100     {
101       switch(c)
102         {
103         case 'h':
104           usage(0);
105           break;
106         case 'q':
107           query = optarg;
108           break;
109         default:
110           usage(1);
111           break;
112         }
113     }
114   repo_add_repomdxml(repo, stdin, flags);
115   if (query)
116     doquery(pool, repo, query);
117   else
118     tool_write(repo, 0, 0);
119   pool_free(pool);
120   exit(0);
121 }