- add new flags for the load functions:
[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 #include "common_write.h"
25
26 static void
27 usage()
28 {
29   fprintf(stderr, "\nUsage:\n"
30           "mergesolv [file] [file] [...]\n"
31           "  merges multiple solv files into one and writes it to stdout\n"
32           );
33   exit(0);
34 }
35
36 static int
37 loadcallback (Pool *pool, Repodata *data, void *vdata)
38 {
39   FILE *fp;
40   const char *location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
41   int r;
42
43   if (!location)
44     return 0;
45   fprintf(stderr, "Loading SOLV file %s\n", location);
46   fp = fopen (location, "r");
47   if (!fp)
48     {
49       perror(location);
50       return 0;
51     }
52   r = repo_add_solv_flags(data->repo, fp, REPO_USE_LOADING|REPO_LOCALPOOL);
53   fclose(fp);
54   return r ? 0 : 1;
55 }
56
57 int
58 main(int argc, char **argv)
59 {
60   Pool *pool;
61   Repo *repo;
62   const char *basefile = 0;
63   int with_attr = 0;
64   int c;
65
66   pool = pool_create();
67   repo = repo_create(pool, "<mergesolv>");
68   
69   while ((c = getopt(argc, argv, "ahb:")) >= 0)
70     {
71       switch (c)
72       {
73         case 'h':
74           usage();
75           break;
76         case 'a':
77           with_attr = 1;
78           break;
79         case 'b':
80           basefile = optarg;
81           break;
82         default:
83           exit(1);
84       }
85     }
86   if (with_attr)
87     pool_setloadcallback(pool, loadcallback, 0);
88
89   for (; optind < argc; optind++)
90     {
91       FILE *fp;
92       if ((fp = fopen(argv[optind], "r")) == NULL)
93         {
94           perror(argv[optind]);
95           exit(1);
96         }
97       repo_add_solv(repo, fp);
98       fclose(fp);
99     }
100   tool_write(repo, basefile, 0);
101   pool_free(pool);
102   return 0;
103 }