Merge pull request #4 from akozumpl/req
[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           " -n : No packages, do not read rpmdb, useful to only parse products\n"
38           " -x : use extrapool\n"
39           " -b <basefile> : Write .solv to <basefile>.solv instead of stdout\n"
40           " -p <productsdir> : Scan <productsdir> for .prod files, representing installed products\n"
41           " -r <root> : Prefix rpmdb path and <productsdir> with <root>\n"
42           " -o <solv> : Write .solv to file instead of stdout\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   Pool *refpool;
55   int c, percent = 0;
56   int extrapool = 0;
57   int nopacks = 0;
58   const char *root = 0;
59   const char *basefile = 0;
60   const char *refname = 0;
61 #ifdef ENABLE_SUSEREPO
62   char *proddir = 0;
63 #endif
64   char *outfile = 0;
65
66   /*
67    * parse arguments
68    */
69   
70   while ((c = getopt(argc, argv, "Phnxb:r:p:o:")) >= 0)
71     switch (c)
72       {
73       case 'h':
74           usage(0);
75         break;
76       case 'r':
77         root = optarg;
78         break;
79       case 'b':
80         basefile = optarg;
81         break;
82       case 'n':
83         nopacks = 1;
84         break;
85       case 'P':
86         percent = 1;
87         break;
88       case 'p':
89 #ifdef ENABLE_SUSEREPO
90         proddir = optarg;
91 #endif
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    * optional arg is old version of rpmdb solv file
111    * should make this a real option instead
112    */
113   
114   if (optind < argc)
115     refname = argv[optind];
116
117   if (refname)
118     {
119       FILE *fp;
120       if ((fp = fopen(refname, "r")) == NULL)
121         {
122           perror(refname);
123         }
124       else
125         {
126           if (extrapool)
127             refpool = pool_create();
128           else
129             refpool = pool;
130           ref = repo_create(refpool, "ref");
131           repo_add_solv(ref, fp);
132           repo_disable_paging(ref);
133           fclose(fp);
134         }
135     }
136
137   /*
138    * create 'installed' repository
139    * add products
140    * add rpmdb
141    * write .solv
142    */
143
144   repo = repo_create(pool, "installed");
145   data = repo_add_repodata(repo, 0);
146
147   if (!nopacks)
148     repo_add_rpmdb(repo, ref, root, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | (percent ? RPMDB_REPORT_PROGRESS : 0));
149
150 #ifdef ENABLE_SUSEREPO
151   if (proddir && *proddir)
152     {
153       char *buf = proddir;
154       /* if <root> given, not '/', and proddir does not start with <root> */
155       if (root && *root)
156         {
157           int rootlen = strlen(root);
158           if (strncmp(root, proddir, rootlen))
159             {
160               buf = (char *)solv_malloc(rootlen + strlen(proddir) + 2); /* + '/' + \0 */
161               strcpy(buf, root);
162               if (root[rootlen - 1] != '/' && *proddir != '/')
163                 buf[rootlen++] = '/';
164               strcpy(buf + rootlen, proddir);
165             }
166         }
167       repo_add_products(repo, buf, root, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
168       if (buf != proddir)
169         solv_free(buf);
170     }
171 #endif
172   repodata_internalize(data);
173
174   if (ref)
175     {
176       if (ref->pool != pool)
177         pool_free(ref->pool);
178       else
179         repo_free(ref, 1);
180       ref = 0;
181     }
182
183   tool_write(repo, basefile, 0);
184   pool_free(pool);
185   exit(0);
186 }