- yet another incompatible change: as everybody seems to use repo_write_stdkeyfilter...
[platform/upstream/libsolv.git] / tools / mergesolv.c
index f466b9b..0d6075c 100644 (file)
@@ -1,9 +1,17 @@
 /*
+ * 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 <unistd.h>
 #include <limits.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <assert.h>
 
 #include "pool.h"
-#include "source_solv.h"
-#include "source_write.h"
+#include "repo_solv.h"
+#include "common_write.h"
 
 static void
-adjust (Id **val, Id * new_id, Source *source)
+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)
 {
-  if (!*val)
-    return;
-  assert (source->idarraydata <= *val);
-  assert (*val < source->idarraydata + source->idarraysize);
-  *val = new_id + (*val - source->idarraydata);
+  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;
+  const char *basefile = 0;
+  int with_attr = 0;
+  int c;
 
-  while (argc-- > 1)
+  pool = pool_create();
+  repo = repo_create(pool, "<mergesolv>");
+  
+  while ((c = getopt(argc, argv, "ahb:")) >= 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 'b':
+         basefile = optarg;
+         break;
+       default:
+         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;
-         adjust (&s->provides, new_id + new_id_size, source);
-         adjust (&s->obsoletes, new_id + new_id_size, source);
-         adjust (&s->conflicts, new_id + new_id_size, source);
-         adjust (&s->requires, new_id + new_id_size, source);
-         adjust (&s->recommends, new_id + new_id_size, source);
-         adjust (&s->suggests, new_id + new_id_size, source);
-         adjust (&s->supplements, new_id + new_id_size, source);
-         adjust (&s->enhances, new_id + new_id_size, source);
-         adjust (&s->freshens, new_id + new_id_size, source);
-       }
-      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;
+      FILE *fp;
+      if ((fp = fopen(argv[optind], "r")) == NULL)
+       {
+         perror(argv[optind]);
+         exit(1);
        }
+      repo_add_solv(repo, fp, 0);
+      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);
+  tool_write(repo, basefile, 0);
   pool_free(pool);
-
   return 0;
 }