document usage
[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  * Usage:
16  * rpmdb2solv [-n] [-x] [-b <basefile>] [-p <productsdir>] [-r <root>] 
17  * -n : No packages, do not read rpmdb, useful to only parse products
18  * -x : use extrapool
19  * -b <basefile> : Write .solv to <basefile>.solv instead of stdout
20  * -p <productsdir> : Scan <productsdir> for .prod files, representing installed products
21  * -r <root> : Prefix rpmdb path and <productsdir> with <root>
22  * 
23  */
24
25 #include <sys/types.h>
26 #include <limits.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <unistd.h>
32
33 #include "pool.h"
34 #include "repo.h"
35 #include "repo_rpmdb.h"
36 #include "repo_products.h"
37 #include "repo_solv.h"
38 #include "common_write.h"
39
40 int
41 main(int argc, char **argv)
42 {
43   Pool *pool = pool_create();
44   Repo *repo, *ref = 0;
45   Repodata *repodata;
46   FILE *fp;
47   Pool *refpool;
48   int c;
49   int extrapool = 0;
50   int nopacks = 0;
51   const char *root = 0;
52   const char *basefile = 0;
53   const char *proddir = 0;
54
55   /*
56    * parse arguments
57    */
58   
59   while ((c = getopt (argc, argv, "nxb:r:p:")) >= 0)
60     switch (c)
61       {
62       case 'r':
63         root = optarg;
64         break;
65       case 'b':
66         basefile = optarg;
67         break;
68       case 'n':
69         nopacks = 1;
70         break;
71       case 'p':
72         proddir = optarg;
73         break;
74       case 'x':
75         extrapool = 1;
76         break;
77       default:
78         exit(1);
79       }
80   
81   /*
82    * ???
83    */
84   
85   if (optind < argc)
86     {
87       if (extrapool)
88         refpool = pool_create();
89       else
90         refpool = pool;
91       if ((fp = fopen(argv[optind], "r")) == NULL)
92         {
93           perror(argv[optind]);
94           exit(1);
95         }
96       ref = repo_create(refpool, "ref");
97       repo_add_solv(ref, fp);
98       repo_disable_paging(ref);
99       fclose(fp);
100     }
101
102   /*
103    * create 'installed' repository
104    * add products
105    * add rpmdb
106    * write .solv
107    */
108
109   repo = repo_create(pool, "installed");
110   repodata = repo_add_repodata(repo, 0);
111
112   if (!nopacks)
113     repo_add_rpmdb(repo, repodata, ref, root);
114
115   if (proddir && *proddir)
116     {
117       /* if <root> given, not '/', and proddir does not start with <root> */
118       if (root && *root)
119         {
120           int rootlen = strlen(root);
121           if (strncmp(root, proddir, rootlen))
122             {
123               char *buf;
124               buf = (char *)sat_malloc(rootlen + strlen(proddir) + 2); /* + '/' + \0 */
125               strcpy(buf, root);
126               if (root[rootlen-1] != '/'
127                   && *proddir != '/')
128                 {
129                   strcpy(buf+rootlen, "/");
130                   ++rootlen;
131                 }
132               strcpy(buf+rootlen, proddir);
133               proddir = buf;
134             }
135         }
136       
137       repo_add_products(repo, repodata, proddir);
138     }
139       
140   if (repodata)
141     repodata_internalize(repodata);
142
143   if (ref)
144     {
145       if (ref->pool != pool)
146         pool_free(ref->pool);
147       else
148         repo_free(ref, 1);
149       ref = 0;
150     }
151
152   tool_write(repo, basefile, 0);
153   pool_free(pool);
154
155   exit(0);
156 }