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