/*
+ * Copyright (c) 2007, Novell Inc.
+ *
+ * This program is licensed under the BSD license, read LICENSE.BSD
+ * for further information
+ */
+
+/*
* mergesolv
*
*/
#include <sys/types.h>
-#include <limits.h>
-#include <fcntl.h>
+#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
+#include <getopt.h>
#include "pool.h"
-#include "source_solv.h"
-#include "source_write.h"
+#include "repo_solv.h"
+#ifdef SUSE
+#include "repo_autopattern.h"
+#endif
+#include "common_write.h"
+
+static void
+usage()
+{
+ fprintf(stderr, "\nUsage:\n"
+ "mergesolv [file] [file] [...]\n"
+ " merges multiple solv files into one and writes it to stdout\n"
+ );
+ exit(0);
+}
+
+static int
+loadcallback (Pool *pool, Repodata *data, void *vdata)
+{
+ FILE *fp;
+ const char *location = repodata_lookup_str(data, SOLVID_META, REPOSITORY_LOCATION);
+ int r;
+
+ if (!location)
+ return 0;
+ fprintf(stderr, "Loading SOLV file %s\n", location);
+ fp = fopen (location, "r");
+ if (!fp)
+ {
+ perror(location);
+ return 0;
+ }
+ r = repo_add_solv(data->repo, fp, REPO_USE_LOADING|REPO_LOCALPOOL);
+ fclose(fp);
+ return r ? 0 : 1;
+}
int
main(int argc, char **argv)
{
- Pool *pool = pool_create();
- int i;
- int new_id_size;
- Id *new_id;
+ Pool *pool;
+ Repo *repo;
+ int with_attr = 0;
+#ifdef SUSE
+ int add_auto = 0;
+#endif
+ int c;
- while (argc-- > 1)
+ pool = pool_create();
+ repo = repo_create(pool, "<mergesolv>");
+
+ while ((c = getopt(argc, argv, "ahX")) >= 0)
{
- FILE *fp;
- argv++;
- if ((fp = fopen(*argv, "r")) == NULL)
- {
- perror(argv[1]);
- exit(0);
- }
- pool_addsource_solv(pool, fp, "");
- fclose(fp);
+ switch (c)
+ {
+ case 'h':
+ usage();
+ break;
+ case 'a':
+ with_attr = 1;
+ break;
+ case 'X':
+#ifdef SUSE
+ add_auto = 1;
+#endif
+ break;
+ default:
+ usage();
+ exit(1);
+ }
}
- if (!pool->nsources)
- return 0;
+ if (with_attr)
+ pool_setloadcallback(pool, loadcallback, 0);
- new_id_size = 0;
- for (i = 0; i < pool->nsources; i++)
- new_id_size += pool->sources[i]->idarraysize;
- new_id = (Id*) malloc (sizeof (Id) * new_id_size);
- new_id_size = 0;
- for (i = 0; i < pool->nsources; i++)
+ for (; optind < argc; optind++)
{
- Source *source = pool->sources[i];
- int si;
- memcpy (new_id + new_id_size, source->idarraydata,
- source->idarraysize * sizeof (new_id[0]));
- for (si = source->start; si < source->start + source->nsolvables; si++)
- {
- Solvable *s = pool->solvables + si;
- if (s->provides)
- s->provides += new_id_size;
- if (s->obsoletes)
- s->obsoletes += new_id_size;
- if (s->conflicts)
- s->conflicts += new_id_size;
- if (s->requires)
- s->requires += new_id_size;
- if (s->recommends)
- s->recommends += new_id_size;
- if (s->suggests)
- s->suggests+= new_id_size;
- if (s->supplements)
- s->supplements += new_id_size;
- if (s->enhances)
- s->enhances += new_id_size;
- if (s->freshens)
- s->freshens += new_id_size;
+ FILE *fp;
+ if ((fp = fopen(argv[optind], "r")) == NULL)
+ {
+ perror(argv[optind]);
+ exit(1);
}
- new_id_size += source->idarraysize;
- if (i > 0)
- {
- pool->sources[0]->nsolvables += source->nsolvables;
- source->nsolvables = 0;
- source->start = pool->nsolvables;
- free (source->idarraydata);
- source->idarraydata = 0;
+ if (repo_add_solv(repo, fp, 0))
+ {
+ fprintf(stderr, "repo %s: %s\n", argv[optind], pool_errstr(pool));
+ exit(1);
}
+ fclose(fp);
}
- while (pool->nsources > 1)
- {
- pool_freesource (pool, pool->sources[1]);
- }
- free (pool->sources[0]->idarraydata);
- pool->sources[0]->idarraydata = new_id;
- pool->sources[0]->idarraysize = new_id_size;
-
- pool_writesource(pool, pool->sources[0], stdout);
+#ifdef SUSE
+ if (add_auto)
+ repo_add_autopattern(repo, 0);
+#endif
+ tool_write(repo, stdout);
pool_free(pool);
-
return 0;
}