- do not use obsolete interface in libsolv library itself
[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 "sat_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 = sat_malloc(l+1);
82       snprintf(fnp, l, "%s/primary.xml.gz", dir);
83       if (!(fp = sat_xfopen(fnp, 0)))
84         {
85           perror(fnp);
86           exit(1);
87         }
88       repo_add_rpmmd(repo, fp, 0, flags);
89       fclose(fp);
90       snprintf(fnp, l, "%s/diskusagedata.xml.gz", dir);
91       if ((fp = sat_xfopen(fnp, 0)))
92         {
93           repo_add_rpmmd(repo, fp, 0, flags);
94           fclose(fp);
95         }
96       if (locale)
97         {
98           if (snprintf(fnp, l, "%s/translation-%s.xml.gz", dir, locale) >= l)
99             {
100               fprintf(stderr, "-l parameter too long\n");
101               exit(1);
102             }
103           while (!(fp = sat_xfopen(fnp, 0)))
104             {
105               fprintf(stderr, "not opened %s\n", fnp);
106               if (strlen(locale) > 2)
107                 {
108                   if (snprintf(fnp, l, "%s/translation-%.2s.xml.gz", dir, locale) >= l)
109                     {
110                       fprintf(stderr, "-l parameter too long\n");
111                       exit(1);
112                     }
113                   if ((fp = sat_xfopen(fnp, 0)))
114                     break;
115                 }
116               perror(fnp);
117               exit(1);
118             }
119           fprintf(stderr, "opened %s\n", fnp);
120           repo_add_rpmmd(repo, fp, 0, flags);
121           fclose(fp);
122         }
123       sat_free(fnp);
124     }
125   else
126     repo_add_rpmmd(repo, stdin, 0, flags);
127   tool_write(repo, basefile, attrname);
128   pool_free(pool);
129   exit(0);
130 }