add repo_add_keyring() and repo_add_keydir() functions
[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 #include "common_write.h"
29
30 static char *
31 fgets0(char *s, int size, FILE *stream)
32 {
33   char *p = s;
34   int c;
35
36   while (--size > 0)
37     {
38       c = getc(stream);
39       if (c == EOF)
40         {
41           if (p == s)
42             return 0;
43           c = 0;
44         }
45       *p++ = c;
46       if (!c)
47         return s;
48     }
49   *p = 0;
50   return s;
51 }
52
53 int
54 main(int argc, char **argv)
55 {
56   const char **rpms = 0;
57   char *manifest = 0;
58   int manifest0 = 0;
59   int c, i, res, nrpms = 0;
60   Pool *pool = pool_create();
61   Repo *repo;
62   FILE *fp;
63   char buf[4096], *p;
64   const char *basefile = 0;
65 #ifdef ENABLE_PUBKEY
66   int pubkeys = 0;
67 #endif
68
69   while ((c = getopt(argc, argv, "0kKb:m:")) >= 0)
70     {
71       switch(c)
72         {
73         case 'b':
74           basefile = optarg;
75           break;
76         case 'm':
77           manifest = optarg;
78           break;
79         case '0':
80           manifest0 = 1;
81           break;
82 #ifdef ENABLE_PUBKEY
83         case 'k':
84           pubkeys = 1;
85           break;
86         case 'K':
87           pubkeys = 2;
88           break;
89 #endif
90         default:
91           exit(1);
92         }
93     }
94   if (manifest)
95     {
96       if (!strcmp(manifest, "-"))
97         fp = stdin;
98       else if ((fp = fopen(manifest, "r")) == 0)
99         {
100           perror(manifest);
101           exit(1);
102         }
103       for (;;)
104         {
105           if (manifest0)
106             {
107               if (!fgets0(buf, sizeof(buf), fp))
108                 break;
109             }
110           else
111             {
112               if (!fgets(buf, sizeof(buf), fp))
113                 break;
114               if ((p = strchr(buf, '\n')) != 0)
115                 *p = 0;
116             }
117           rpms = solv_extend(rpms, nrpms, 1, sizeof(char *), 15);
118           rpms[nrpms++] = strdup(buf);
119         }
120       if (fp != stdin)
121         fclose(fp);
122     }
123   while (optind < argc)
124     {
125       rpms = solv_extend(rpms, nrpms, 1, sizeof(char *), 15);
126       rpms[nrpms++] = strdup(argv[optind++]);
127     }
128   repo = repo_create(pool, "rpms2solv");
129   repo_add_repodata(repo, 0);
130   res = 0;
131   for (i = 0; i < nrpms; i++)
132     {
133 #ifdef ENABLE_PUBKEY
134       if (pubkeys == 2)
135         {
136           FILE *fp = solv_xfopen(rpms[i], "r");
137           if (!fp)
138             {
139               perror(rpms[i]);
140               res = 1;
141               continue;
142             }
143           if (repo_add_keyring(repo, fp, REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE))
144             {
145               fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
146               res = 1;
147             }
148           fclose(fp);
149           continue;
150         }
151       if (pubkeys)
152         {
153           if (repo_add_pubkey(repo, rpms[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE) == 0)
154             {
155               fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
156               res = 1;
157             }
158           continue;
159         }
160 #endif
161       if (repo_add_rpm(repo, rpms[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE) == 0)
162         {
163           fprintf(stderr, "rpms2solv: %s\n", pool_errstr(pool));
164           res = 1;
165         }
166     }
167   repo_internalize(repo);
168   tool_write(repo, basefile, 0);
169   pool_free(pool);
170   for (c = 0; c < nrpms; c++)
171     free((char *)rpms[c]);
172   solv_free(rpms);
173   exit(res);
174 }
175