- add '-o <outfile>' support
[platform/upstream/libsolv.git] / tools / rpmdb2solv.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  * rpmdb2solv
10  * 
11  * Reads rpm database (and evtl. more, like product metadata) to build
12  * a .solv file of 'installed' solvables.
13  * Writes .solv to stdout
14  * 
15  */
16
17 #include <sys/types.h>
18 #include <limits.h>
19 #include <fcntl.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "pool.h"
26 #include "repo.h"
27 #include "repo_rpmdb.h"
28 #include "repo_products.h"
29 #include "repo_solv.h"
30 #include "common_write.h"
31
32 static void
33 usage(int status)
34 {
35   fprintf(stderr, "\nUsage:\n"
36           "rpmdb2solv [-n] [-x] [-b <basefile>] [-p <productsdir>] [-r <root>]\n"
37           " -a <attr> : Only print this attribute, no .solv generation. E.g. '-a distribution.target'\n"
38           " -n : No packages, do not read rpmdb, useful to only parse products\n"
39           " -x : use extrapool\n"
40           " -b <basefile> : Write .solv to <basefile>.solv instead of stdout\n"
41           " -p <productsdir> : Scan <productsdir> for .prod files, representing installed products\n"
42           " -r <root> : Prefix rpmdb path and <productsdir> with <root>\n"
43          );
44   exit(status);
45 }
46
47
48 int
49 main(int argc, char **argv)
50 {
51   Pool *pool = pool_create();
52   Repo *repo, *ref = 0;
53   Repodata *data;
54   FILE *fp;
55   Pool *refpool;
56   int c, percent = 0;
57   int extrapool = 0;
58   int nopacks = 0;
59   const char *root = 0;
60   const char *basefile = 0;
61   char *proddir = 0;
62   const char *attribute = 0;
63   char *outfile = 0;
64
65   /*
66    * parse arguments
67    */
68   
69   while ((c = getopt(argc, argv, "Pa:hnxb:r:p:o:")) >= 0)
70     switch (c)
71       {
72       case 'h':
73           usage(0);
74         break;
75       case 'a':
76         attribute = optarg;
77         break;
78       case 'r':
79         root = optarg;
80         break;
81       case 'b':
82         basefile = optarg;
83         break;
84       case 'n':
85         nopacks = 1;
86         break;
87       case 'P':
88         percent = 1;
89         break;
90       case 'p':
91         proddir = optarg;
92         break;
93       case 'x':
94         extrapool = 1;
95         break;
96       case 'o':
97         outfile = optarg;
98         break;
99       default:
100         usage(1);
101       }
102   
103   if (outfile && !freopen(outfile, "w", stdout))
104     {
105       perror(outfile);
106       exit(1);
107     }
108     
109   /*
110    * ???
111    */
112   
113   if (optind < argc)
114     {
115       if (extrapool)
116         refpool = pool_create();
117       else
118         refpool = pool;
119       if ((fp = fopen(argv[optind], "r")) == NULL)
120         {
121           perror(argv[optind]);
122           exit(1);
123         }
124       ref = repo_create(refpool, "ref");
125       repo_add_solv(ref, fp);
126       repo_disable_paging(ref);
127       fclose(fp);
128     }
129
130   /*
131    * create 'installed' repository
132    * add products
133    * add rpmdb
134    * write .solv
135    */
136
137   repo = repo_create(pool, "installed");
138   data = repo_add_repodata(repo, 0);
139
140   if (!nopacks)
141     repo_add_rpmdb(repo, ref, root, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | (percent ? RPMDB_REPORT_PROGRESS : 0));
142
143   if (proddir && *proddir)
144     {
145       char *buf = proddir;
146       /* if <root> given, not '/', and proddir does not start with <root> */
147       if (root && *root)
148         {
149           int rootlen = strlen(root);
150           if (strncmp(root, proddir, rootlen))
151             {
152               buf = (char *)sat_malloc(rootlen + strlen(proddir) + 2); /* + '/' + \0 */
153               strcpy(buf, root);
154               if (root[rootlen - 1] != '/' && *proddir != '/')
155                 buf[rootlen++] = '/';
156               strcpy(buf + rootlen, proddir);
157             }
158         }
159       repo_add_products(repo, proddir, root, attribute, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
160       if (buf != proddir)
161         sat_free(buf);
162     }
163       
164   repodata_internalize(data);
165
166   if (ref)
167     {
168       if (ref->pool != pool)
169         pool_free(ref->pool);
170       else
171         repo_free(ref, 1);
172       ref = 0;
173     }
174
175   if (!attribute)
176     tool_write(repo, basefile, 0);
177
178   pool_free(pool);
179
180   exit(0);
181 }