support -X option in rpmmd2solv, make add_autopattern available in bindings
[platform/upstream/libsolv.git] / tools / rpms2solv.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 /*
9  * rpms2solv - create a solv file from multiple rpms
10  * 
11  */
12
13 #include <sys/types.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <unistd.h>
17 #include <string.h>
18
19 #include "util.h"
20 #include "pool.h"
21 #include "repo.h"
22 #include "repo_rpmdb.h"
23 #ifdef ENABLE_PUBKEY
24 #include "repo_pubkey.h"
25 #include "solv_xfopen.h"
26 #endif
27 #include "repo_solv.h"
28 #ifdef SUSE
29 #include "repo_autopattern.h"
30 #endif
31 #include "common_write.h"
32
33 static char *
34 fgets0(char *s, int size, FILE *stream)
35 {
36   char *p = s;
37   int c;
38
39   while (--size > 0)
40     {
41       c = getc(stream);
42       if (c == EOF)
43         {
44           if (p == s)
45             return 0;
46           c = 0;
47         }
48       *p++ = c;
49       if (!c)
50         return s;
51     }
52   *p = 0;
53   return s;
54 }
55
56 int
57 main(int argc, char **argv)
58 {
59   const char **rpms = 0;
60   char *manifest = 0;
61   int manifest0 = 0;
62   int c, i, res, nrpms = 0;
63   Pool *pool = pool_create();
64   Repo *repo;
65   FILE *fp;
66   char buf[4096], *p;
67   const char *basefile = 0;
68 #ifdef ENABLE_PUBKEY
69   int pubkeys = 0;
70 #endif
71 #ifdef SUSE
72   int add_auto = 0;
73 #endif
74   int filtered_filelist = 0;
75
76   while ((c = getopt(argc, argv, "0XkKb:m:F")) >= 0)
77     {
78       switch(c)
79         {
80         case 'b':
81           basefile = optarg;
82           break;
83         case 'm':
84           manifest = optarg;
85           break;
86         case '0':
87           manifest0 = 1;
88           break;
89         case 'F':
90           filtered_filelist = 1;
91           break;
92 #ifdef ENABLE_PUBKEY
93         case 'k':
94           pubkeys = 1;
95           break;
96         case 'K':
97           pubkeys = 2;
98           break;
99 #endif
100         case 'X':
101 #ifdef SUSE
102           add_auto = 1;
103 #endif
104           break;
105         default:
106           exit(1);
107         }
108     }
109   if (manifest)
110     {
111       if (!strcmp(manifest, "-"))
112         fp = stdin;
113       else if ((fp = fopen(manifest, "r")) == 0)
114         {
115           perror(manifest);
116           exit(1);
117         }
118       for (;;)
119         {
120           if (manifest0)
121             {
122               if (!fgets0(buf, sizeof(buf), fp))
123                 break;
124             }
125           else
126             {
127               if (!fgets(buf, sizeof(buf), fp))
128                 break;
129               if ((p = strchr(buf, '\n')) != 0)
130                 *p = 0;
131             }
132           rpms = solv_extend(rpms, nrpms, 1, sizeof(char *), 15);
133           rpms[nrpms++] = strdup(buf);
134         }
135       if (fp != stdin)
136         fclose(fp);
137     }
138   while (optind < argc)
139     {
140       rpms = solv_extend(rpms, nrpms, 1, sizeof(char *), 15);
141       rpms[nrpms++] = strdup(argv[optind++]);
142     }
143   repo = repo_create(pool, "rpms2solv");
144   repo_add_repodata(repo, 0);
145   res = 0;
146   for (i = 0; i < nrpms; i++)
147     {
148 #ifdef ENABLE_PUBKEY
149       if (pubkeys == 2)
150         {
151           FILE *fp = solv_xfopen(rpms[i], "r");
152           if (!fp)
153             {
154               perror(rpms[i]);
155               res = 1;
156               continue;
157             }
158           if (repo_add_keyring(repo, fp, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|ADD_WITH_KEYSIGNATURES))
159             {
160               fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
161               res = 1;
162             }
163           fclose(fp);
164           continue;
165         }
166       if (pubkeys)
167         {
168           if (repo_add_pubkey(repo, rpms[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|ADD_WITH_KEYSIGNATURES) == 0)
169             {
170               fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
171               res = 1;
172             }
173           continue;
174         }
175 #endif
176       if (repo_add_rpm(repo, rpms[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|(filtered_filelist ? RPM_ADD_FILTERED_FILELIST : 0)) == 0)
177         {
178           fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
179           res = 1;
180         }
181     }
182   repo_internalize(repo);
183 #ifdef SUSE
184   if (add_auto)
185     repo_add_autopattern(repo, 0);
186 #endif
187   tool_write(repo, basefile, 0);
188   pool_free(pool);
189   for (c = 0; c < nrpms; c++)
190     free((char *)rpms[c]);
191   solv_free(rpms);
192   exit(res);
193 }
194