Imported Upstream version 0.6.27
[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   const char *basefile = 0;
64   int with_attr = 0;
65 #ifdef SUSE
66   int add_auto = 0;
67 #endif
68   int c;
69
70   pool = pool_create();
71   repo = repo_create(pool, "<mergesolv>");
72   
73   while ((c = getopt(argc, argv, "ahb:X")) >= 0)
74     {
75       switch (c)
76       {
77         case 'h':
78           usage();
79           break;
80         case 'a':
81           with_attr = 1;
82           break;
83         case 'b':
84           basefile = optarg;
85           break;
86         case 'X':
87 #ifdef SUSE
88           add_auto = 1;
89 #endif
90           break;
91         default:
92           usage();
93           exit(1);
94       }
95     }
96   if (with_attr)
97     pool_setloadcallback(pool, loadcallback, 0);
98
99   for (; optind < argc; optind++)
100     {
101       FILE *fp;
102       if ((fp = fopen(argv[optind], "r")) == NULL)
103         {
104           perror(argv[optind]);
105           exit(1);
106         }
107       if (repo_add_solv(repo, fp, 0))
108         {
109           fprintf(stderr, "repo %s: %s\n", argv[optind], pool_errstr(pool));
110           exit(1);
111         }
112       fclose(fp);
113     }
114 #ifdef SUSE
115   if (add_auto)
116     repo_add_autopattern(repo, 0);
117 #endif
118   tool_write(repo, basefile, 0);
119   pool_free(pool);
120   return 0;
121 }