rpms2solv failed to write out most solvable data (bnc #422338).
[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 int
27 main(int argc, char **argv)
28 {
29   const char **rpms = 0;
30   char *manifest = 0;
31   int c, nrpms = 0;
32   Pool *pool = pool_create();
33   Repo *repo;
34   Repodata *repodata;
35   FILE *fp;
36   char buf[4096], *p;
37   const char *basefile = 0;
38
39   while ((c = getopt(argc, argv, "b:m:")) >= 0)
40     {
41       switch(c)
42         {
43         case 'b':
44           basefile = optarg;
45           break;
46         case 'm':
47           manifest = optarg;
48           break;
49         default:
50           exit(1);
51         }
52     }
53   if (manifest)
54     {
55       if (!strcmp(manifest, "-"))
56         fp = stdin;
57       else if ((fp = fopen(manifest, "r")) == 0)
58         {
59           perror(manifest);
60           exit(1);
61         }
62       while(fgets(buf, sizeof(buf), fp))
63         {
64           if ((p = strchr(buf, '\n')) != 0)
65             *p = 0;
66           rpms = sat_extend(rpms, nrpms, 1, sizeof(char *), 15);
67           rpms[nrpms++] = strdup(buf);
68         }
69       if (fp != stdin)
70         fclose(fp);
71     }
72   while (optind < argc)
73     {
74       rpms = sat_extend(rpms, nrpms, 1, sizeof(char *), 15);
75       rpms[nrpms++] = strdup(argv[optind++]);
76     }
77   repo = repo_create(pool, "rpms2solv");
78   repodata = repo_add_repodata(repo, 0);
79   repo_add_rpms(repo, repodata, rpms, nrpms);
80   if (repodata)
81     repodata_internalize(repodata);
82   tool_write(repo, basefile, 0);
83   pool_free(pool);
84   for (c = 0; c < nrpms; c++)
85     free((char *)rpms[c]);
86   sat_free(rpms);
87   exit(0);
88 }
89