8746ac60db82c825c2a01a9eca9bc2d78c2f8437
[platform/upstream/libsolv.git] / tools / mergesolv.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  * mergesolv
10  * 
11  */
12
13 #include <sys/types.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include <assert.h>
19
20 #include "pool.h"
21 #include "repo_solv.h"
22 #ifdef SUSE
23 #include "repo_autopattern.h"
24 #endif
25 #include "common_write.h"
26
27 static void
28 usage()
29 {
30   fprintf(stderr, "\nUsage:\n"
31           "mergesolv [file] [file] [...]\n"
32           "  merges multiple solv files into one and writes it to stdout\n"
33           );
34   exit(0);
35 }
36
37 static int
38 loadcallback (Pool *pool, Repodata *data, void *vdata)
39 {
40   FILE *fp;
41   const char *location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
42   int r;
43
44   if (!location)
45     return 0;
46   fprintf(stderr, "Loading SOLV file %s\n", location);
47   fp = fopen (location, "r");
48   if (!fp)
49     {
50       perror(location);
51       return 0;
52     }
53   r = repo_add_solv(data->repo, fp, REPO_USE_LOADING|REPO_LOCALPOOL);
54   fclose(fp);
55   return r ? 0 : 1;
56 }
57
58 int
59 main(int argc, char **argv)
60 {
61   Pool *pool;
62   Repo *repo;
63   int with_attr = 0;
64 #ifdef SUSE
65   int add_auto = 0;
66 #endif
67   int c;
68
69   pool = pool_create();
70   repo = repo_create(pool, "<mergesolv>");
71   
72   while ((c = getopt(argc, argv, "ahX")) >= 0)
73     {
74       switch (c)
75       {
76         case 'h':
77           usage();
78           break;
79         case 'a':
80           with_attr = 1;
81           break;
82         case 'X':
83 #ifdef SUSE
84           add_auto = 1;
85 #endif
86           break;
87         default:
88           usage();
89           exit(1);
90       }
91     }
92   if (with_attr)
93     pool_setloadcallback(pool, loadcallback, 0);
94
95   for (; optind < argc; optind++)
96     {
97       FILE *fp;
98       if ((fp = fopen(argv[optind], "r")) == NULL)
99         {
100           perror(argv[optind]);
101           exit(1);
102         }
103       if (repo_add_solv(repo, fp, 0))
104         {
105           fprintf(stderr, "repo %s: %s\n", argv[optind], pool_errstr(pool));
106           exit(1);
107         }
108       fclose(fp);
109     }
110 #ifdef SUSE
111   if (add_auto)
112     repo_add_autopattern(repo, 0);
113 #endif
114   tool_write(repo, stdout);
115   pool_free(pool);
116   return 0;
117 }