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