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