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