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