support -X option in rpmmd2solv, make add_autopattern available in bindings
[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 #ifdef SUSE
23 #include "repo_autopattern.h"
24 #endif
25 #include "common_write.h"
26 #include "solv_xfopen.h"
27
28
29 static void
30 usage(int status)
31 {
32   fprintf(stderr, "\nUsage:\n"
33           "rpmmd2solv [-a][-h][-n <attrname>][-l <locale>]\n"
34           "  reads 'primary' from a 'rpmmd' repository from <stdin> and writes a .solv file to <stdout>\n"
35           "  -h : print help & exit\n"
36           "  -n <name>: save attributes as <name>.attr\n"
37           "  -l <locale>: parse localization data for <locale>\n"
38          );
39    exit(status);
40 }
41
42 int
43 main(int argc, char **argv)
44 {
45   int c, flags = 0;
46   const char *attrname = 0;
47   const char *basefile = 0;
48   const char *dir = 0;
49   const char *locale = 0;
50 #ifdef SUSE
51   int add_auto = 0;
52 #endif
53   
54   Pool *pool = pool_create();
55   Repo *repo = repo_create(pool, "<stdin>");
56
57   while ((c = getopt (argc, argv, "hn:b:d:l:X")) >= 0)
58     {
59       switch(c)
60         {
61         case 'h':
62           usage(0);
63           break;
64         case 'n':
65           attrname = optarg;
66           break;
67         case 'b':
68           basefile = optarg;
69           break;
70         case 'd':
71           dir = optarg;
72           break;
73         case 'l':
74           locale = optarg;
75           break;
76         case 'X':
77 #ifdef SUSE
78           add_auto = 1;
79 #endif
80           break;
81         default:
82           usage(1);
83           break;
84         }
85     }
86   if (dir)
87     {
88       FILE *fp;
89       int l;
90       char *fnp;
91       l = strlen(dir) + 128;
92       fnp = solv_malloc(l+1);
93       snprintf(fnp, l, "%s/primary.xml.gz", dir);
94       if (!(fp = solv_xfopen(fnp, 0)))
95         {
96           perror(fnp);
97           exit(1);
98         }
99       if (repo_add_rpmmd(repo, fp, 0, flags))
100         {
101           fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
102           exit(1);
103         }
104       fclose(fp);
105       snprintf(fnp, l, "%s/diskusagedata.xml.gz", dir);
106       if ((fp = solv_xfopen(fnp, 0)))
107         {
108           if (repo_add_rpmmd(repo, fp, 0, flags))
109             {
110               fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
111               exit(1);
112             }
113           fclose(fp);
114         }
115       if (locale)
116         {
117           if (snprintf(fnp, l, "%s/translation-%s.xml.gz", dir, locale) >= l)
118             {
119               fprintf(stderr, "-l parameter too long\n");
120               exit(1);
121             }
122           while (!(fp = solv_xfopen(fnp, 0)))
123             {
124               fprintf(stderr, "not opened %s\n", fnp);
125               if (strlen(locale) > 2)
126                 {
127                   if (snprintf(fnp, l, "%s/translation-%.2s.xml.gz", dir, locale) >= l)
128                     {
129                       fprintf(stderr, "-l parameter too long\n");
130                       exit(1);
131                     }
132                   if ((fp = solv_xfopen(fnp, 0)))
133                     break;
134                 }
135               perror(fnp);
136               exit(1);
137             }
138           fprintf(stderr, "opened %s\n", fnp);
139           if (repo_add_rpmmd(repo, fp, 0, flags))
140             {
141               fprintf(stderr, "rpmmd2solv: %s: %s\n", fnp, pool_errstr(pool));
142               exit(1);
143             }
144           fclose(fp);
145         }
146       solv_free(fnp);
147     }
148   else
149     {
150       if (repo_add_rpmmd(repo, stdin, 0, flags))
151         {
152           fprintf(stderr, "rpmmd2solv: %s\n", pool_errstr(pool));
153           exit(1);
154         }
155     }
156 #ifdef SUSE
157   if (add_auto)
158     repo_add_autopattern(repo, 0);
159 #endif
160   tool_write(repo, basefile, attrname);
161   pool_free(pool);
162   exit(0);
163 }