Imported Upstream version 0.7.22
[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 #ifdef ENABLE_PUBKEY
68   int pubkeys = 0;
69 #endif
70 #ifdef SUSE
71   int add_auto = 0;
72 #endif
73   int filtered_filelist = 0;
74
75   while ((c = getopt(argc, argv, "0XkKm:F")) >= 0)
76     {
77       switch(c)
78         {
79         case 'm':
80           manifest = optarg;
81           break;
82         case '0':
83           manifest0 = 1;
84           break;
85         case 'F':
86           filtered_filelist = 1;
87           break;
88 #ifdef ENABLE_PUBKEY
89         case 'k':
90           pubkeys = 1;
91           break;
92         case 'K':
93           pubkeys = 2;
94           break;
95 #endif
96         case 'X':
97 #ifdef SUSE
98           add_auto = 1;
99 #endif
100           break;
101         default:
102           exit(1);
103         }
104     }
105   if (manifest)
106     {
107       if (!strcmp(manifest, "-"))
108         fp = stdin;
109       else if ((fp = fopen(manifest, "r")) == 0)
110         {
111           perror(manifest);
112           exit(1);
113         }
114       for (;;)
115         {
116           if (manifest0)
117             {
118               if (!fgets0(buf, sizeof(buf), fp))
119                 break;
120             }
121           else
122             {
123               if (!fgets(buf, sizeof(buf), fp))
124                 break;
125               if ((p = strchr(buf, '\n')) != 0)
126                 *p = 0;
127             }
128           rpms = solv_extend(rpms, nrpms, 1, sizeof(char *), 15);
129           rpms[nrpms++] = strdup(buf);
130         }
131       if (fp != stdin)
132         fclose(fp);
133     }
134   while (optind < argc)
135     {
136       rpms = solv_extend(rpms, nrpms, 1, sizeof(char *), 15);
137       rpms[nrpms++] = strdup(argv[optind++]);
138     }
139   repo = repo_create(pool, "rpms2solv");
140   repo_add_repodata(repo, 0);
141   res = 0;
142   for (i = 0; i < nrpms; i++)
143     {
144 #ifdef ENABLE_PUBKEY
145       if (pubkeys == 2)
146         {
147           FILE *fp = solv_xfopen(rpms[i], "r");
148           if (!fp)
149             {
150               perror(rpms[i]);
151               res = 1;
152               continue;
153             }
154           if (repo_add_keyring(repo, fp, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|ADD_WITH_KEYSIGNATURES))
155             {
156               fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
157               res = 1;
158             }
159           fclose(fp);
160           continue;
161         }
162       if (pubkeys)
163         {
164           if (repo_add_pubkey(repo, rpms[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|ADD_WITH_KEYSIGNATURES) == 0)
165             {
166               fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
167               res = 1;
168             }
169           continue;
170         }
171 #endif
172       if (repo_add_rpm(repo, rpms[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE|(filtered_filelist ? RPM_ADD_FILTERED_FILELIST : 0)) == 0)
173         {
174           fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
175           res = 1;
176         }
177     }
178   repo_internalize(repo);
179 #ifdef SUSE
180   if (add_auto)
181     repo_add_autopattern(repo, 0);
182 #endif
183   tool_write(repo, stdout);
184   pool_free(pool);
185   for (c = 0; c < nrpms; c++)
186     free((char *)rpms[c]);
187   solv_free(rpms);
188   exit(res);
189 }
190