Merge branch 'master' of git@git.opensuse.org:projects/zypp/sat-solver
[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   char *proddir = 0;
62   char *outfile = 0;
63
64   /*
65    * parse arguments
66    */
67   
68   while ((c = getopt(argc, argv, "Phnxb:r:p:o:")) >= 0)
69     switch (c)
70       {
71       case 'h':
72           usage(0);
73         break;
74       case 'r':
75         root = optarg;
76         break;
77       case 'b':
78         basefile = optarg;
79         break;
80       case 'n':
81         nopacks = 1;
82         break;
83       case 'P':
84         percent = 1;
85         break;
86       case 'p':
87         proddir = optarg;
88         break;
89       case 'x':
90         extrapool = 1;
91         break;
92       case 'o':
93         outfile = optarg;
94         break;
95       default:
96         usage(1);
97       }
98   
99   if (outfile && !freopen(outfile, "w", stdout))
100     {
101       perror(outfile);
102       exit(1);
103     }
104     
105   /*
106    * optional arg is old version of rpmdb solv file
107    * should make this a real option instead
108    */
109   
110   if (optind < argc)
111     refname = argv[optind];
112
113   if (refname)
114     {
115       FILE *fp;
116       if ((fp = fopen(refname, "r")) == NULL)
117         {
118           perror(refname);
119         }
120       else
121         {
122           if (extrapool)
123             refpool = pool_create();
124           else
125             refpool = pool;
126           ref = repo_create(refpool, "ref");
127           repo_add_solv(ref, fp);
128           repo_disable_paging(ref);
129           fclose(fp);
130         }
131     }
132
133   /*
134    * create 'installed' repository
135    * add products
136    * add rpmdb
137    * write .solv
138    */
139
140   repo = repo_create(pool, "installed");
141   data = repo_add_repodata(repo, 0);
142
143   if (!nopacks)
144     repo_add_rpmdb(repo, ref, root, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE | (percent ? RPMDB_REPORT_PROGRESS : 0));
145
146   if (proddir && *proddir)
147     {
148       char *buf = proddir;
149       /* if <root> given, not '/', and proddir does not start with <root> */
150       if (root && *root)
151         {
152           int rootlen = strlen(root);
153           if (strncmp(root, proddir, rootlen))
154             {
155               buf = (char *)sat_malloc(rootlen + strlen(proddir) + 2); /* + '/' + \0 */
156               strcpy(buf, root);
157               if (root[rootlen - 1] != '/' && *proddir != '/')
158                 buf[rootlen++] = '/';
159               strcpy(buf + rootlen, proddir);
160             }
161         }
162       repo_add_products(repo, buf, root, REPO_REUSE_REPODATA | REPO_NO_INTERNALIZE);
163       if (buf != proddir)
164         sat_free(buf);
165     }
166       
167   repodata_internalize(data);
168
169   if (ref)
170     {
171       if (ref->pool != pool)
172         pool_free(ref->pool);
173       else
174         repo_free(ref, 1);
175       ref = 0;
176     }
177
178   tool_write(repo, basefile, 0);
179   pool_free(pool);
180   exit(0);
181 }