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