Imported Upstream version 0.6.29
[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 <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_rpmmd.h"
19 #ifdef SUSE
20 #include "repo_autopattern.h"
21 #endif
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 #ifdef SUSE
48   int add_auto = 0;
49 #endif
50   
51   Pool *pool = pool_create();
52   Repo *repo = repo_create(pool, "<stdin>");
53
54   while ((c = getopt (argc, argv, "hn:b:d:l:X")) >= 0)
55     {
56       switch(c)
57         {
58         case 'h':
59           usage(0);
60           break;
61         case 'n':
62           attrname = optarg;
63           break;
64         case 'b':
65           basefile = optarg;
66           break;
67         case 'd':
68           dir = optarg;
69           break;
70         case 'l':
71           locale = optarg;
72           break;
73         case 'X':
74 #ifdef SUSE
75           add_auto = 1;
76 #endif
77           break;
78         default:
79           usage(1);
80           break;
81         }
82     }
83   if (dir)
84     {
85       FILE *fp;
86       int l;
87       char *fnp;
88       l = strlen(dir) + 128;
89       fnp = solv_malloc(l+1);
90       snprintf(fnp, l, "%s/primary.xml.gz", dir);
91       if (!(fp = solv_xfopen(fnp, 0)))
92         {
93           perror(fnp);
94           exit(1);
95         }
96       if (repo_add_rpmmd(repo, fp, 0, flags))
97         {
98           fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
99           exit(1);
100         }
101       fclose(fp);
102       snprintf(fnp, l, "%s/diskusagedata.xml.gz", dir);
103       if ((fp = solv_xfopen(fnp, 0)))
104         {
105           if (repo_add_rpmmd(repo, fp, 0, flags))
106             {
107               fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
108               exit(1);
109             }
110           fclose(fp);
111         }
112       if (locale)
113         {
114           if (snprintf(fnp, l, "%s/translation-%s.xml.gz", dir, locale) >= l)
115             {
116               fprintf(stderr, "-l parameter too long\n");
117               exit(1);
118             }
119           while (!(fp = solv_xfopen(fnp, 0)))
120             {
121               fprintf(stderr, "not opened %s\n", fnp);
122               if (strlen(locale) > 2)
123                 {
124                   if (snprintf(fnp, l, "%s/translation-%.2s.xml.gz", dir, locale) >= l)
125                     {
126                       fprintf(stderr, "-l parameter too long\n");
127                       exit(1);
128                     }
129                   if ((fp = solv_xfopen(fnp, 0)))
130                     break;
131                 }
132               perror(fnp);
133               exit(1);
134             }
135           fprintf(stderr, "opened %s\n", fnp);
136           if (repo_add_rpmmd(repo, fp, 0, flags))
137             {
138               fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
139               exit(1);
140             }
141           fclose(fp);
142         }
143       solv_free(fnp);
144     }
145   else
146     {
147       if (repo_add_rpmmd(repo, stdin, 0, flags))
148         {
149           fprintf(stderr, "rpmmd2solv: %s\n", pool_errstr(pool));
150           exit(1);
151         }
152     }
153 #ifdef SUSE
154   if (add_auto)
155     repo_add_autopattern(repo, 0);
156 #endif
157   tool_write(repo, basefile, attrname);
158   pool_free(pool);
159   exit(0);
160 }