- expand mergesolv a bit
[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 FILE *
37 loadcallback (Pool *pool, Repodata *data, void *vdata)
38 {
39   FILE *fp = 0;
40   if (data->location)
41     {
42       fprintf(stderr, "Loading SOLV file %s\n", data->location);
43       fp = fopen (data->location, "r");
44       if (!fp)
45         perror(data->location);
46     }
47   return fp;
48 }
49
50 int
51 main(int argc, char **argv)
52 {
53   Pool *pool;
54   Repo *repo;
55   const char *basefile = 0;
56   int with_attr = 0;
57   int c;
58
59   pool = pool_create();
60   repo = repo_create(pool, "<mergesolv>");
61   
62   while ((c = getopt(argc, argv, "ahb:")) >= 0)
63     {
64       switch (c)
65       {
66         case 'h':
67           usage();
68           break;
69         case 'a':
70           with_attr = 1;
71           break;
72         case 'b':
73           basefile = optarg;
74           break;
75         default:
76           exit(1);
77       }
78     }
79   if (with_attr)
80     pool_setloadcallback(pool, loadcallback, 0);
81
82   for (; optind < argc; optind++)
83     {
84       FILE *fp;
85       if ((fp = fopen(argv[optind], "r")) == NULL)
86         {
87           perror(argv[optind]);
88           exit(0);
89         }
90       repo_add_solv(repo, fp);
91       fclose(fp);
92     }
93   tool_write(repo, basefile, 0);
94   pool_free(pool);
95   return 0;
96 }