make repodata_stringify return the result string
[platform/upstream/libsolv.git] / tools / rpmmd2solv.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 #define _GNU_SOURCE
9
10 #include <sys/types.h>
11 #include <limits.h>
12 #include <fcntl.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <zlib.h>
18
19 #include "pool.h"
20 #include "repo.h"
21 #include "repo_rpmmd.h"
22 #include "common_write.h"
23 #include "solv_xfopen.h"
24
25
26 static void
27 usage(int status)
28 {
29   fprintf(stderr, "\nUsage:\n"
30           "rpmmd2solv [-a][-h][-n <attrname>][-l <locale>]\n"
31           "  reads 'primary' from a 'rpmmd' repository from <stdin> and writes a .solv file to <stdout>\n"
32           "  -h : print help & exit\n"
33           "  -n <name>: save attributes as <name>.attr\n"
34           "  -l <locale>: parse localization data for <locale>\n"
35          );
36    exit(status);
37 }
38
39 int
40 main(int argc, char **argv)
41 {
42   int c, flags = 0;
43   const char *attrname = 0;
44   const char *basefile = 0;
45   const char *dir = 0;
46   const char *locale = 0;
47   
48   Pool *pool = pool_create();
49   Repo *repo = repo_create(pool, "<stdin>");
50
51   while ((c = getopt (argc, argv, "hn:b:d:l:")) >= 0)
52     {
53       switch(c)
54         {
55         case 'h':
56           usage(0);
57           break;
58         case 'n':
59           attrname = optarg;
60           break;
61         case 'b':
62           basefile = optarg;
63           break;
64         case 'd':
65           dir = optarg;
66           break;
67         case 'l':
68           locale = optarg;
69           break;
70         default:
71           usage(1);
72           break;
73         }
74     }
75   if (dir)
76     {
77       FILE *fp;
78       int l;
79       char *fnp;
80       l = strlen(dir) + 128;
81       fnp = solv_malloc(l+1);
82       snprintf(fnp, l, "%s/primary.xml.gz", dir);
83       if (!(fp = solv_xfopen(fnp, 0)))
84         {
85           perror(fnp);
86           exit(1);
87         }
88       if (repo_add_rpmmd(repo, fp, 0, flags))
89         {
90           fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
91           exit(1);
92         }
93       fclose(fp);
94       snprintf(fnp, l, "%s/diskusagedata.xml.gz", dir);
95       if ((fp = solv_xfopen(fnp, 0)))
96         {
97           if (repo_add_rpmmd(repo, fp, 0, flags))
98             {
99               fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
100               exit(1);
101             }
102           fclose(fp);
103         }
104       if (locale)
105         {
106           if (snprintf(fnp, l, "%s/translation-%s.xml.gz", dir, locale) >= l)
107             {
108               fprintf(stderr, "-l parameter too long\n");
109               exit(1);
110             }
111           while (!(fp = solv_xfopen(fnp, 0)))
112             {
113               fprintf(stderr, "not opened %s\n", fnp);
114               if (strlen(locale) > 2)
115                 {
116                   if (snprintf(fnp, l, "%s/translation-%.2s.xml.gz", dir, locale) >= l)
117                     {
118                       fprintf(stderr, "-l parameter too long\n");
119                       exit(1);
120                     }
121                   if ((fp = solv_xfopen(fnp, 0)))
122                     break;
123                 }
124               perror(fnp);
125               exit(1);
126             }
127           fprintf(stderr, "opened %s\n", fnp);
128           if (repo_add_rpmmd(repo, fp, 0, flags))
129             {
130               fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
131               exit(1);
132             }
133           fclose(fp);
134         }
135       solv_free(fnp);
136     }
137   else
138     {
139       if (repo_add_rpmmd(repo, stdin, 0, flags))
140         {
141           fprintf(stderr, "rpmmd2solv: %s\n", pool_errstr(pool));
142           exit(1);
143         }
144     }
145   tool_write(repo, basefile, attrname);
146   pool_free(pool);
147   exit(0);
148 }