examples/solv: factor out downloadchecksig function
[platform/upstream/libsolv.git] / examples / solv.c
index 4baa6cf..55d7313 100644 (file)
@@ -81,7 +81,8 @@
 
 #ifdef FEDORA
 # define REPOINFO_PATH "/etc/yum.repos.d"
-#else
+#endif
+#ifdef SUSE
 # define REPOINFO_PATH "/etc/zypp/repos.d"
 # define PRODUCTS_PATH "/etc/products.d"
 # define SOFTLOCKS_PATH "/var/lib/zypp/SoftLocks"
@@ -139,14 +140,15 @@ yum_substitute(Pool *pool, char *line)
          if (!releaseevr)
            {
              Queue q;
+             const char *rootdir = pool_get_rootdir(pool);
        
              queue_init(&q);
-             rpm_installedrpmdbids(0, "Providename", "redhat-release", &q);
+             rpm_installedrpmdbids(rootdir, "Providename", "redhat-release", &q);
              if (q.count)
                {
                  void *handle, *state = 0;
                  char *p;
-                 handle = rpm_byrpmdbid(q.elements[0], 0, &state);
+                 handle = rpm_byrpmdbid(q.elements[0], rootdir, &state);
                  releaseevr = rpm_query(handle, SOLVABLE_EVR);
                  rpm_byrpmdbid(0, 0, &state);
                  if ((p = strchr(releaseevr, '-')) != 0)
@@ -154,7 +156,10 @@ yum_substitute(Pool *pool, char *line)
                }
              queue_free(&q);
              if (!releaseevr)
-               releaseevr = strdup("?");
+               {
+                 fprintf(stderr, "no installed package provides 'redhat-release', cannot determine $releasever\n");
+                 exit(1);
+               }
            }
          *p2 = 0;
          p = pool_tmpjoin(pool, line, releaseevr, p2 + 11);
@@ -196,6 +201,7 @@ yum_substitute(Pool *pool, char *line)
 #define TYPE_PLAINDIR  3
 #define TYPE_DEBIAN     4
 
+#ifndef NOSYSTEM
 static int
 read_repoinfos_sort(const void *ap, const void *bp)
 {
@@ -203,12 +209,14 @@ read_repoinfos_sort(const void *ap, const void *bp)
   const struct repoinfo *b = bp;
   return strcmp(a->alias, b->alias);
 }
+#endif
 
-#ifndef DEBIAN
+#if defined(SUSE) || defined(FEDORA)
 
 struct repoinfo *
-read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
+read_repoinfos(Pool *pool, int *nrepoinfosp)
 {
+  const char *reposdir = REPOINFO_PATH;
   char buf[4096];
   char buf2[4096], *kp, *vp, *kpe;
   DIR *dir;
@@ -337,10 +345,12 @@ read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
   return repoinfos;
 }
 
-#else
+#endif
+
+#ifdef DEBIAN
 
 struct repoinfo *
-read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
+read_repoinfos(Pool *pool, int *nrepoinfosp)
 {
   FILE *fp;
   char buf[4096];
@@ -453,6 +463,16 @@ read_repoinfos(Pool *pool, const char *reposdir, int *nrepoinfosp)
 
 #endif
 
+#ifdef NOSYSTEM
+struct repoinfo *
+read_repoinfos(Pool *pool, int *nrepoinfosp)
+{
+  *nrepoinfosp = 0;
+  return 0;
+}
+#endif
+
+
 void
 free_repoinfos(struct repoinfo *repoinfos, int nrepoinfos)
 {
@@ -946,7 +966,7 @@ checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
 
 #else
 
-int
+static int
 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
 {
   char cmd[256];
@@ -963,6 +983,39 @@ checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
 
 #endif
 
+static Pool *
+read_sigs()
+{
+  Pool *sigpool = pool_create();
+#if defined(ENABLE_RPMDB_PUBKEY)
+  Repo *repo = repo_create(sigpool, "rpmdbkeys");
+  repo_add_rpmdb_pubkeys(repo, 0);
+#endif
+  return sigpool;
+}
+
+static int
+downloadchecksig(struct repoinfo *cinfo, FILE *fp, const char *sigurl, Pool **sigpool)
+{
+  FILE *sigfp;
+  sigfp = curlfopen(cinfo, sigurl, 0, 0, 0, 0);
+  if (!sigfp)
+    {
+      printf(" unsigned, skipped\n");
+      return 0;
+    }
+  if (!*sigpool)
+    *sigpool = read_sigs();
+  if (!checksig(*sigpool, fp, sigfp))
+    {
+      printf(" checksig failed, skipped\n");
+      fclose(sigfp);
+      return 0;
+    }
+  fclose(sigfp);
+  return 1;
+}
+
 #define CHKSUM_IDENT "1.1"
 
 void
@@ -1202,18 +1255,6 @@ writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *
 }
 
 
-static Pool *
-read_sigs()
-{
-  Pool *sigpool = pool_create();
-#if defined(ENABLE_RPMDB_PUBKEY)
-  Repo *repo = repo_create(sigpool, "rpmdbkeys");
-  repo_add_rpmdb_pubkeys(repo, 0, 0);
-#endif
-  return sigpool;
-}
-
-
 #ifdef ENABLE_RPMMD
 /* repomd helpers */
 
@@ -1283,6 +1324,7 @@ repomd_load_ext(Repo *repo, Repodata *data)
   struct repoinfo *cinfo;
   const unsigned char *filechksum;
   Id filechksumtype;
+  int r = 0;
 
   cinfo = repo->appdata;
   repomdtype = repodata_lookup_str(data, SOLVID_META, REPOSITORY_REPOMD_TYPE);
@@ -1309,10 +1351,15 @@ repomd_load_ext(Repo *repo, Repodata *data)
   if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
     return 0;
   if (!strcmp(ext, "FL"))
-    repo_add_rpmmd(repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
+    r = repo_add_rpmmd(repo, fp, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
   else if (!strcmp(ext, "DL"))
-    repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
+    r = repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
   fclose(fp);
+  if (r)
+    {
+      printf("%s\n", pool_errstr(repo->pool));
+      return 0;
+    }
   writecachedrepo(repo, data, ext, cinfo->extcookie);
   return 1;
 }
@@ -1452,7 +1499,12 @@ susetags_load_ext(Repo *repo, Repodata *data)
   filechksum = repodata_lookup_bin_checksum(data, SOLVID_META, SUSETAGS_FILE_CHECKSUM, &filechksumtype);
   if ((fp = curlfopen(cinfo, pool_tmpjoin(repo->pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
     return 0;
-  repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
+  if (repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES))
+    {
+      fclose(fp);
+      printf("%s\n", pool_errstr(repo->pool));
+      return 0;
+    }
   fclose(fp);
   writecachedrepo(repo, data, ext, cinfo->extcookie);
   return 1;
@@ -1467,15 +1519,19 @@ int
 load_stub(Pool *pool, Repodata *data, void *dp)
 {
   struct repoinfo *cinfo = data->repo->appdata;
+  switch (cinfo->type)
+    {
 #ifdef ENABLE_SUSEREPO
-  if (cinfo->type == TYPE_SUSETAGS)
-    return susetags_load_ext(data->repo, data);
+    case TYPE_SUSETAGS:
+      return susetags_load_ext(data->repo, data);
 #endif
 #ifdef ENABLE_RPMMD
-  if (cinfo->type == TYPE_RPMMD)
-    return repomd_load_ext(data->repo, data);
+    case TYPE_RPMMD:
+      return repomd_load_ext(data->repo, data);
 #endif
-  return 0;
+    default:
+      return 0;
+    }
 }
 
 static unsigned char installedcookie[32];
@@ -1607,7 +1663,6 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
   struct repoinfo *cinfo;
   int i;
   FILE *fp;
-  FILE *sigfp;
   const char *filename;
   const unsigned char *filechksum;
   Id filechksumtype;
@@ -1617,57 +1672,74 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
 #endif
   struct stat stb;
   Pool *sigpool = 0;
+#if defined(ENABLE_SUSEREPO) || defined(ENABLE_RPMMD)
   Repodata *data;
+#endif
   int badchecksum;
   int dorefresh;
-#if defined(ENABLE_DEBIAN) && defined(DEBIAN)
+#if defined(ENABLE_DEBIAN)
   FILE *fpr;
   int j;
 #endif
 
   repo = repo_create(pool, "@System");
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
   printf("rpm database:");
-  if (stat("/var/lib/rpm/Packages", &stb))
+  if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/rpm/Packages"), &stb))
     memset(&stb, 0, sizeof(&stb));
 #endif
 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
   printf("dpgk database:");
-  if (stat("/var/lib/dpkg/status", &stb))
+  if (stat(pool_prepend_rootdir_tmp(pool, "/var/lib/dpkg/status"), &stb))
     memset(&stb, 0, sizeof(&stb));
 #endif
+#ifdef NOSYSTEM
+  printf("no installed database:");
+  memset(&stb, 0, sizeof(&stb));
+#endif
   calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
   if (usecachedrepo(repo, 0, installedcookie, 0))
     printf(" cached\n");
   else
     {
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
       FILE *ofp;
-      int done = 0;
+      Repo *ref = 0;
 #endif
       printf(" reading\n");
 
-#if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
-      repo_add_products(repo, PRODUCTS_PATH, 0, REPO_NO_INTERNALIZE);
-#endif
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
+# if defined(ENABLE_SUSEREPO) && defined(PRODUCTS_PATH)
+      if (repo_add_products(repo, PRODUCTS_PATH, REPO_NO_INTERNALIZE | REPO_USE_ROOTDIR))
+       {
+         fprintf(stderr, "product reading failed: %s\n", pool_errstr(pool));
+         exit(1);
+       }
+# endif
       if ((ofp = fopen(calccachepath(repo, 0), "r")) != 0)
        {
-         Repo *ref = repo_create(pool, "@System.old");
-         if (!repo_add_solv(ref, ofp, 0))
+         ref = repo_create(pool, "@System.old");
+         if (repo_add_solv(ref, ofp, 0))
            {
-             repo_add_rpmdb(repo, ref, 0, REPO_REUSE_REPODATA);
-             done = 1;
+             repo_free(ref, 1);
+             ref = 0;
            }
          fclose(ofp);
-         repo_free(ref, 1);
        }
-      if (!done)
-        repo_add_rpmdb(repo, 0, 0, REPO_REUSE_REPODATA);
-#else
-# if defined(ENABLE_DEBIAN) && defined(DEBIAN)
-        repo_add_debdb(repo, 0, REPO_REUSE_REPODATA);
-# endif
+      if (repo_add_rpmdb(repo, ref, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
+       {
+         fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
+         exit(1);
+       }
+      if (ref)
+        repo_free(ref, 1);
+#endif
+#if defined(ENABLE_DEBIAN) && defined(DEBIAN)
+      if (repo_add_debdb(repo, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
+       {
+         fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
+         exit(1);
+       }
 #endif
       writecachedrepo(repo, 0, 0, installedcookie);
     }
@@ -1717,33 +1789,27 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
               fclose(fp);
              break;
            }
-         if (cinfo->repo_gpgcheck)
+         if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "repodata/repomd.xml.asc", &sigpool))
            {
-             sigfp = curlfopen(cinfo, "repodata/repomd.xml.asc", 0, 0, 0, 0);
-             if (!sigfp)
-               {
-                 printf(" unsigned, skipped\n");
-                 fclose(fp);
-                 break;
-               }
-             if (!sigpool)
-               sigpool = read_sigs();
-             if (!checksig(sigpool, fp, sigfp))
-               {
-                 printf(" checksig failed, skipped\n");
-                 fclose(sigfp);
-                 fclose(fp);
-                 break;
-               }
-             fclose(sigfp);
+             fclose(fp);
+             break;
+           }
+         if (repo_add_repomdxml(repo, fp, 0))
+           {
+             printf("repomd.xml: %s\n", pool_errstr(pool));
+             fclose(fp);
+             break;    /* hopeless */
            }
-         repo_add_repomdxml(repo, fp, 0);
          fclose(fp);
          printf(" fetching\n");
          filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
          if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
            {
-             repo_add_rpmmd(repo, fp, 0, 0);
+             if (repo_add_rpmmd(repo, fp, 0, 0))
+               {
+                 printf("primary: %s\n", pool_errstr(pool));
+                 badchecksum = 1;
+               }
              fclose(fp);
            }
          if (badchecksum)
@@ -1752,7 +1818,11 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
          if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
            {
-             repo_add_updateinfoxml(repo, fp, 0);
+             if (repo_add_updateinfoxml(repo, fp, 0))
+               {
+                 printf("updateinfo: %s\n", pool_errstr(pool));
+                 badchecksum = 1;
+               }
              fclose(fp);
            }
 
@@ -1787,30 +1857,17 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
              fclose(fp);
              break;
            }
-         if (cinfo->repo_gpgcheck)
+         if (cinfo->repo_gpgcheck && !downloadchecksig(cinfo, fp, "content.asc", &sigpool))
            {
-             sigfp = curlfopen(cinfo, "content.asc", 0, 0, 0, 0);
-             if (!sigfp)
-               {
-                 printf(" unsigned, skipped\n");
-                 fclose(fp);
-                 break;
-               }
-             if (sigfp)
-               {
-                 if (!sigpool)
-                   sigpool = read_sigs();
-                 if (!checksig(sigpool, fp, sigfp))
-                   {
-                     printf(" checksig failed, skipped\n");
-                     fclose(sigfp);
-                     fclose(fp);
-                     break;
-                   }
-                 fclose(sigfp);
-               }
+             fclose(fp);
+             break;
+           }
+         if (repo_add_content(repo, fp, 0))
+           {
+             printf("content: %s\n", pool_errstr(pool));
+             fclose(fp);
+             break;    /* hopeless */
            }
-         repo_add_content(repo, fp, 0);
          fclose(fp);
          defvendor = repo_lookup_id(repo, SOLVID_META, SUSETAGS_DEFAULTVENDOR);
          descrdir = repo_lookup_str(repo, SOLVID_META, SUSETAGS_DESCRDIR);
@@ -1827,7 +1884,12 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          printf(" fetching\n");
          if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
            break;      /* hopeless */
-         repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES);
+         if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
+           {
+             printf("packages: %s\n", pool_errstr(pool));
+             fclose(fp);
+             break;    /* hopeless */
+           }
          fclose(fp);
          /* add default language */
          filename = susetags_find(repo, "packages.en.gz", &filechksum, &filechksumtype);
@@ -1837,7 +1899,11 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
            {
              if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
                {
-                 repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES);
+                 if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|REPO_REUSE_REPODATA|REPO_EXTEND_SOLVABLES))
+                   {
+                     printf("packages.en: %s\n", pool_errstr(pool));
+                     badchecksum = 1;
+                   }
                  fclose(fp);
                }
            }
@@ -1858,7 +1924,11 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
                      filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
                      if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
                        {
-                         repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE);
+                         if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
+                           {
+                             printf("%s: %s\n", pbuf, pool_errstr(pool));
+                             badchecksum = 1;
+                           }
                          fclose(fp2);
                        }
                    }
@@ -1875,7 +1945,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          break;
 #endif
 
-#if defined(ENABLE_DEBIAN) && defined(DEBIAN)
+#if defined(ENABLE_DEBIAN)
         case TYPE_DEBIAN:
          printf("debian repo '%s':", cinfo->alias);
          fflush(stdout);
@@ -1892,24 +1962,13 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          if (cinfo->repo_gpgcheck)
            {
              filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
-             sigfp = curlfopen(cinfo, filename, 0, 0, 0, 0);
-             solv_free((char *)filename);
-             if (!sigfp)
-               {
-                 printf(" unsigned, skipped\n");
-                 fclose(fpr);
-                 break;
-               }
-             if (!sigpool)
-               sigpool = read_sigs();
-             if (!checksig(sigpool, fpr, sigfp))
+             if (!downloadchecksig(cinfo, fp, filename, &sigpool))
                {
-                 printf(" checksig failed, skipped\n");
-                 fclose(sigfp);
                  fclose(fpr);
+                 solv_free((char *)filename);
                  break;
                }
-             fclose(sigfp);
+             solv_free((char *)filename);
            }
          calc_checksum_fp(fpr, REPOKEY_TYPE_SHA256, cinfo->cookie);
          if (usecachedrepo(repo, 0, cinfo->cookie, 1))
@@ -1928,7 +1987,11 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
                }
              if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
                {
-                 repo_add_debpackages(repo, fp, 0);
+                 if (repo_add_debpackages(repo, fp, 0))
+                   {
+                     printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
+                     badchecksum = 1;
+                   }
                  fclose(fp);
                }
              solv_free((char *)filechksum);
@@ -2313,7 +2376,7 @@ yesno(const char *str)
     }
 }
 
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
 
 struct fcstate {
   FILE **newpkgsfps;
@@ -2360,7 +2423,7 @@ fileconflict_cb(Pool *pool, Id p, void *cbdata)
 
 
 void
-runrpm(const char *arg, const char *name, int dupfd3)
+runrpm(const char *arg, const char *name, int dupfd3, const char *rootdir)
 {
   pid_t pid;
   int status;
@@ -2372,6 +2435,8 @@ runrpm(const char *arg, const char *name, int dupfd3)
     }
   if (pid == 0)
     {
+      if (!rootdir)
+       rootdir = "/";
       if (dupfd3 != -1 && dupfd3 != 3)
        {
          dup2(dupfd3, 3);
@@ -2380,9 +2445,9 @@ runrpm(const char *arg, const char *name, int dupfd3)
       if (dupfd3 != -1)
        fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
       if (strcmp(arg, "-e") == 0)
-        execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
+        execlp("rpm", "rpm", arg, "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
       else
-        execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", name, (char *)0);
+        execlp("rpm", "rpm", arg, "--force", "--nodeps", "--nodigest", "--nosignature", "--root", rootdir, name, (char *)0);
       perror("rpm");
       _exit(0);
     }
@@ -2400,7 +2465,7 @@ runrpm(const char *arg, const char *name, int dupfd3)
 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
 
 void
-rundpkg(const char *arg, const char *name, int dupfd3)
+rundpkg(const char *arg, const char *name, int dupfd3, const char *rootdir)
 {
   pid_t pid;
   int status;
@@ -2412,6 +2477,8 @@ rundpkg(const char *arg, const char *name, int dupfd3)
     }
   if (pid == 0)
     {
+      if (!rootdir)
+       rootdir = "/";
       if (dupfd3 != -1 && dupfd3 != 3)
        {
          dup2(dupfd3, 3);
@@ -2420,9 +2487,9 @@ rundpkg(const char *arg, const char *name, int dupfd3)
       if (dupfd3 != -1)
        fcntl(3, F_SETFD, 0);   /* clear CLOEXEC */
       if (strcmp(arg, "--install") == 0)
-        execlp("dpkg", "dpkg", "--install", "--force", "all", name, (char *)0);
+        execlp("dpkg", "dpkg", "--install", "--root", rootdir, "--force", "all", name, (char *)0);
       else
-        execlp("dpkg", "dpkg", "--remove", "--force", "all", name, (char *)0);
+        execlp("dpkg", "dpkg", "--remove", "--root", rootdir, "--force", "all", name, (char *)0);
       perror("dpkg");
       _exit(0);
     }
@@ -2679,6 +2746,7 @@ main(int argc, char **argv)
   Queue addedfileprovides_inst;
   Id repofilter = 0;
   int cleandeps = 0;
+  char *rootdir = 0;
 
   argc--;
   argv++;
@@ -2737,14 +2805,27 @@ main(int argc, char **argv)
   else
     usage(1);
 
-  if (argc > 1 && !strcmp(argv[1], "--clean"))
+  for (;;)
     {
-      cleandeps = 1;
-      argc--;
-      argv++;
+      if (argc > 2 && !strcmp(argv[1], "--root"))
+       {
+         rootdir = argv[2];
+         argc -= 2;
+         argv += 2;
+       }
+
+      else if (argc > 1 && !strcmp(argv[1], "--clean"))
+       {
+         cleandeps = 1;
+         argc--;
+         argv++;
+       }
+      else
+       break;
     }
 
   pool = pool_create();
+  pool_set_rootdir(pool, rootdir);
 
 #if 0
   {
@@ -2757,7 +2838,7 @@ main(int argc, char **argv)
   pool->nscallback = nscallback;
   // pool_setdebuglevel(pool, 2);
   setarch(pool);
-  repoinfos = read_repoinfos(pool, REPOINFO_PATH, &nrepoinfos);
+  repoinfos = read_repoinfos(pool, &nrepoinfos);
 
   if (mainmode == MODE_REPOLIST)
     {
@@ -2851,10 +2932,11 @@ main(int argc, char **argv)
        {
          int l;
           l = strlen(argv[i]);
-#ifndef DEBIAN
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
          if (l <= 4 || strcmp(argv[i] + l - 4, ".rpm"))
            continue;
-#else
+#endif
+#if defined(ENABLE_DEBIAN) && defined(DEBIAN)
          if (l <= 4 || strcmp(argv[i] + l - 4, ".deb"))
            continue;
 #endif
@@ -2868,7 +2950,7 @@ main(int argc, char **argv)
          if (!commandlinerepo)
            commandlinerepo = repo_create(pool, "@commandline");
          p = 0;
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
          p = repo_add_rpm(commandlinerepo, (const char *)argv[i], REPO_REUSE_REPODATA|REPO_NO_INTERNALIZE);
 #endif
 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
@@ -3035,7 +3117,7 @@ main(int argc, char **argv)
   addsoftlocks(pool, &job);
 #endif
 
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
 rerunsolver:
 #endif
   for (;;)
@@ -3121,7 +3203,7 @@ rerunsolver:
   printf("Transaction summary:\n\n");
   transaction_print(trans);
 
-#if !defined(FEDORA) && !defined(DEBIAN)
+#if defined(SUSE)
   if (1)
     {
       DUChanges duc[4];
@@ -3167,14 +3249,14 @@ rerunsolver:
 
          p = checkq.elements[i];
          s = pool_id2solvable(pool, p);
-         downloadsize += solvable_lookup_num(s, SOLVABLE_DOWNLOADSIZE, 0);
+         downloadsize += solvable_lookup_sizek(s, SOLVABLE_DOWNLOADSIZE, 0);
        }
       printf("Downloading %d packages, %d K\n", newpkgs, downloadsize);
       newpkgsfps = solv_calloc(newpkgs, sizeof(*newpkgsfps));
       for (i = 0; i < newpkgs; i++)
        {
          unsigned int medianr;
-         char *loc;
+         const char *loc;
          Solvable *s;
          struct repoinfo *cinfo;
          const unsigned char *chksum;
@@ -3233,21 +3315,28 @@ rerunsolver:
                      const char *seqnum;
                      const char *seq;
                      const char *dloc;
+                     const char *archstr;
                      FILE *fp;
                      char cmd[128];
                      int newfd;
 
+                     archstr = pool_id2str(pool, s->arch);
+                     if (strlen(archstr) > 10 || strchr(archstr, '\'') != 0)
+                       continue;
+
                      seqname = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NAME);
                      seqevr = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_EVR);
                      seqnum = pool_lookup_str(pool, SOLVID_POS, DELTA_SEQ_NUM);
                      seq = pool_tmpjoin(pool, seqname, "-", seqevr);
                      seq = pool_tmpappend(pool, seq, "-", seqnum);
+                     if (strchr(seq, '\'') != 0)
+                       continue;
 #ifdef FEDORA
-                     sprintf(cmd, "/usr/bin/applydeltarpm -a %s -c -s ", pool_id2str(pool, s->arch));
+                     sprintf(cmd, "/usr/bin/applydeltarpm -a '%s' -c -s '", archstr);
 #else
-                     sprintf(cmd, "/usr/bin/applydeltarpm -c -s ");
+                     sprintf(cmd, "/usr/bin/applydeltarpm -c -s '");
 #endif
-                     if (system(pool_tmpjoin(pool, cmd, seq, 0)) != 0)
+                     if (system(pool_tmpjoin(pool, cmd, seq, "'")) != 0)
                        continue;       /* didn't match */
                      /* looks good, download delta */
                      chksumtype = 0;
@@ -3263,7 +3352,7 @@ rerunsolver:
                      /* got it, now reconstruct */
                      newfd = opentmpfile();
 #ifdef FEDORA
-                     sprintf(cmd, "applydeltarpm -a %s /dev/fd/%d /dev/fd/%d", pool_id2str(pool, s->arch), fileno(fp), newfd);
+                     sprintf(cmd, "applydeltarpm -a '%s' /dev/fd/%d /dev/fd/%d", archstr, fileno(fp), newfd);
 #else
                      sprintf(cmd, "applydeltarpm /dev/fd/%d /dev/fd/%d", fileno(fp), newfd);
 #endif
@@ -3316,7 +3405,7 @@ rerunsolver:
       putchar('\n');
     }
 
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
   if (newpkgs)
     {
       Queue conflicts;
@@ -3355,7 +3444,7 @@ rerunsolver:
   transaction_order(trans, 0);
   for (i = 0; i < trans->steps.count; i++)
     {
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
       const char *evr, *evrp, *nvra;
 #endif
       Solvable *s;
@@ -3369,7 +3458,7 @@ rerunsolver:
        {
        case SOLVER_TRANSACTION_ERASE:
          printf("erase %s\n", pool_solvid2str(pool, p));
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
          if (!s->repo->rpmdbid || !s->repo->rpmdbid[p - s->repo->start])
            continue;
          /* strip epoch from evr */
@@ -3380,10 +3469,10 @@ rerunsolver:
            evr = evrp + 1;
          nvra = pool_tmpjoin(pool, pool_id2str(pool, s->name), "-", evr);
          nvra = pool_tmpappend(pool, nvra, ".", pool_id2str(pool, s->arch));
-         runrpm("-e", nvra, -1);       /* too bad that --querybynumber doesn't work */
+         runrpm("-e", nvra, -1, rootdir);      /* too bad that --querybynumber doesn't work */
 #endif
 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
-         rundpkg("--remove", pool_id2str(pool, s->name), 0);
+         rundpkg("--remove", pool_id2str(pool, s->name), 0, rootdir);
 #endif
          break;
        case SOLVER_TRANSACTION_INSTALL:
@@ -3397,11 +3486,11 @@ rerunsolver:
            continue;
          rewind(fp);
          lseek(fileno(fp), 0, SEEK_SET);
-#if defined(ENABLE_RPMDB) && !defined(DEBIAN)
-         runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp));
+#if defined(ENABLE_RPMDB) && (defined(SUSE) || defined(FEDORA))
+         runrpm(type == SOLVER_TRANSACTION_MULTIINSTALL ? "-i" : "-U", "/dev/fd/3", fileno(fp), rootdir);
 #endif
 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
-         rundpkg("--install", "/dev/fd/3", fileno(fp));
+         rundpkg("--install", "/dev/fd/3", fileno(fp), rootdir);
 #endif
          fclose(fp);
          newpkgsfps[j] = 0;