Already use LOCALPOOL when converting filelist/language extensions
[platform/upstream/libsolv.git] / examples / solv.c
index 3c4e336..7ca9729 100644 (file)
@@ -57,6 +57,7 @@
 #include "solverdebug.h"
 #include "chksum.h"
 #include "repo_solv.h"
+#include "selection.h"
 
 #include "repo_write.h"
 #ifdef ENABLE_RPMDB
@@ -114,6 +115,7 @@ struct repoinfo {
 
   unsigned char cookie[32];
   unsigned char extcookie[32];
+  int incomplete;
 };
 
 #ifdef FEDORA
@@ -775,13 +777,13 @@ findmirrorlisturl(FILE *fp)
 static inline int
 iscompressed(const char *name)
 {
-  int l = strlen(name);
-  return l > 3 && !strcmp(name + l - 3, ".gz") ? 1 : 0;
+  return solv_xfopen_iscompressed(name) != 0;
 }
 
 FILE *
-curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int *badchecksump)
+curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsigned char *chksum, Id chksumtype, int markincomplete)
 {
+  FILE *fp;
   pid_t pid;
   int fd, l;
   int status;
@@ -794,8 +796,8 @@ curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsign
         return 0;
       if (file != cinfo->metalink && file != cinfo->mirrorlist)
        {
-         FILE *fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
          unsigned char mlchksum[32];
+         fp = curlfopen(cinfo, cinfo->metalink ? cinfo->metalink : cinfo->mirrorlist, 0, 0, 0, 0);
          Id mlchksumtype = 0;
          if (!fp)
            return 0;
@@ -819,7 +821,7 @@ curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsign
              chksumtype = mlchksumtype;
              chksum = mlchksum;
            }
-         return curlfopen(cinfo, file, uncompress, chksum, chksumtype, badchecksump);
+         return curlfopen(cinfo, file, uncompress, chksum, chksumtype, markincomplete);
        }
       snprintf(url, sizeof(url), "%s", file);
     }
@@ -862,22 +864,36 @@ curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsign
   if (status)
     {
       printf("%s: download error %d\n", file, status >> 8 ? status >> 8 : status);
-      if (badchecksump)
-       *badchecksump = 1;
+      if (markincomplete)
+       cinfo->incomplete = 1;
       close(fd);
       return 0;
     }
   if (chksumtype && !verify_checksum(fd, file, chksum, chksumtype))
     {
-      if (badchecksump)
-       *badchecksump = 1;
+      if (markincomplete)
+       cinfo->incomplete = 1;
       close(fd);
       return 0;
     }
-  if (uncompress)
-    return solv_xfopen_fd(".gz", fd, "r");
   fcntl(fd, F_SETFD, FD_CLOEXEC);
-  return fdopen(fd, "r");
+  if (uncompress)
+    {
+      if (solv_xfopen_iscompressed(file) < 0)
+       {
+         printf("%s: unsupported compression\n", file);
+         if (markincomplete)
+           cinfo->incomplete = 1;
+         close(fd);
+         return 0;
+       }
+      fp = solv_xfopen_fd(file, fd, "r");
+    }
+  else
+    fp = fdopen(fd, "r");
+  if (!fp)
+    close(fd);
+  return fp;
 }
 
 #ifndef DEBIAN
@@ -966,7 +982,7 @@ checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
 
 #else
 
-int
+static int
 checksig(Pool *sigpool, FILE *fp, FILE *sigfp)
 {
   char cmd[256];
@@ -983,6 +999,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
@@ -1000,10 +1049,12 @@ calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
 }
 
 void
-calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *out)
+calc_checksum_stat(struct stat *stb, Id chktype, unsigned char *cookie, unsigned char *out)
 {
   void *h = solv_chksum_create(chktype);
   solv_chksum_add(h, CHKSUM_IDENT, strlen(CHKSUM_IDENT));
+  if (cookie)
+    solv_chksum_add(h, cookie, 32);
   solv_chksum_add(h, &stb->st_dev, sizeof(stb->st_dev));
   solv_chksum_add(h, &stb->st_ino, sizeof(stb->st_ino));
   solv_chksum_add(h, &stb->st_size, sizeof(stb->st_size));
@@ -1108,6 +1159,8 @@ writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *
   int onepiece;
 
   cinfo = repo->appdata;
+  if (cinfo && cinfo->incomplete)
+    return;
   mkdir(SOLVCACHE_PATH, 0755);
   /* use dupjoin instead of tmpjoin because tmpl must survive repo_write */
   tmpl = solv_dupjoin(SOLVCACHE_PATH, "/", ".newsolv-XXXXXX");
@@ -1154,13 +1207,8 @@ writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *
          /* we just need some unique ID */
          struct stat stb;
          if (!fstat(fileno(fp), &stb))
-           {
-             int i;
-
-             calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cinfo->extcookie);
-             for (i = 0; i < 32; i++)
-               cinfo->extcookie[i] ^= cookie[i];
-           }
+           memset(&stb, 0, sizeof(stb));
+         calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, cookie, cinfo->extcookie);
          if (cinfo->extcookie[0] == 0)
            cinfo->extcookie[0] = 1;
        }
@@ -1205,12 +1253,14 @@ writecachedrepo(Repo *repo, Repodata *info, const char *repoext, unsigned char *
            }
          else
            {
+             int flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
              /* make sure repodata contains complete repo */
              /* (this is how repodata_write saves it) */
              repodata_extend_block(info, repo->start, repo->end - repo->start);
              info->state = REPODATA_LOADING;
-             /* no need for LOCALPOOL as pool already contains ids */
-             repo_add_solv(repo, fp, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES);
+             if (strcmp(repoext, "DL") != 0)
+               flags |= REPO_LOCALPOOL;
+             repo_add_solv(repo, fp, flags);
              info->state = REPODATA_AVAILABLE; /* in case the load failed */
            }
          fclose(fp);
@@ -1222,18 +1272,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);
-#endif
-  return sigpool;
-}
-
-
 #ifdef ENABLE_RPMMD
 /* repomd helpers */
 
@@ -1330,7 +1368,7 @@ repomd_load_ext(Repo *repo, Repodata *data)
   if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 0)) == 0)
     return 0;
   if (!strcmp(ext, "FL"))
-    r = 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|REPO_LOCALPOOL);
   else if (!strcmp(ext, "DL"))
     r = repo_add_deltainfoxml(repo, fp, REPO_USE_LOADING);
   fclose(fp);
@@ -1450,6 +1488,7 @@ susetags_load_ext(Repo *repo, Repodata *data)
   struct repoinfo *cinfo;
   const unsigned char *filechksum;
   Id filechksumtype;
+  int flags;
 
   cinfo = repo->appdata;
   filename = repodata_lookup_str(data, SOLVID_META, SUSETAGS_FILE_NAME);
@@ -1478,7 +1517,10 @@ 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;
-  if (repo_add_susetags(repo, fp, defvendor, ext, REPO_USE_LOADING|REPO_EXTEND_SOLVABLES))
+  flags = REPO_USE_LOADING|REPO_EXTEND_SOLVABLES;
+  if (strcmp(ext, "DL") != 0)
+    flags |= REPO_LOCALPOOL;
+  if (repo_add_susetags(repo, fp, defvendor, ext, flags))
     {
       fclose(fp);
       printf("%s\n", pool_errstr(repo->pool));
@@ -1642,7 +1684,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;
@@ -1655,7 +1696,6 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
 #if defined(ENABLE_SUSEREPO) || defined(ENABLE_RPMMD)
   Repodata *data;
 #endif
-  int badchecksum;
   int dorefresh;
 #if defined(ENABLE_DEBIAN)
   FILE *fpr;
@@ -1677,7 +1717,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
   printf("no installed database:");
   memset(&stb, 0, sizeof(&stb));
 #endif
-  calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, installedcookie);
+  calc_checksum_stat(&stb, REPOKEY_TYPE_SHA256, 0, installedcookie);
   if (usecachedrepo(repo, 0, installedcookie, 0))
     printf(" cached\n");
   else
@@ -1715,7 +1755,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
         repo_free(ref, 1);
 #endif
 #if defined(ENABLE_DEBIAN) && defined(DEBIAN)
-      if (repo_add_debdb(repo, 0, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR) && !rootdir)
+      if (repo_add_debdb(repo, REPO_REUSE_REPODATA | REPO_USE_ROOTDIR))
        {
          fprintf(stderr, "installed db: %s\n", pool_errstr(pool));
          exit(1);
@@ -1748,7 +1788,6 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          printf(" cached\n");
          continue;
        }
-      badchecksum = 0;
       switch (cinfo->type)
        {
 #ifdef ENABLE_RPMMD
@@ -1769,25 +1808,10 @@ 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))
            {
@@ -1798,25 +1822,25 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          fclose(fp);
          printf(" fetching\n");
          filename = repomd_find(repo, "primary", &filechksum, &filechksumtype);
-         if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
+         if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
            {
              if (repo_add_rpmmd(repo, fp, 0, 0))
                {
                  printf("primary: %s\n", pool_errstr(pool));
-                 badchecksum = 1;
+                 cinfo->incomplete = 1;
                }
              fclose(fp);
            }
-         if (badchecksum)
+         if (cinfo->incomplete)
            break;      /* hopeless */
 
          filename = repomd_find(repo, "updateinfo", &filechksum, &filechksumtype);
-         if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
+         if (filename && (fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
            {
              if (repo_add_updateinfoxml(repo, fp, 0))
                {
                  printf("updateinfo: %s\n", pool_errstr(pool));
-                 badchecksum = 1;
+                 cinfo->incomplete = 1;
                }
              fclose(fp);
            }
@@ -1826,7 +1850,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
            repomd_add_ext(repo, data, "prestodelta");
          repomd_add_ext(repo, data, "filelists");
          repodata_internalize(data);
-         if (!badchecksum)
+         if (!cinfo->incomplete)
            writecachedrepo(repo, 0, 0, cinfo->cookie);
          repodata_create_stubs(repo_last_repodata(repo));
          break;
@@ -1852,28 +1876,10 @@ 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))
            {
@@ -1895,12 +1901,13 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
              break;
            }
          printf(" fetching\n");
-         if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) == 0)
+         if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) == 0)
            break;      /* hopeless */
          if (repo_add_susetags(repo, fp, defvendor, 0, REPO_NO_INTERNALIZE|SUSETAGS_RECORD_SHARES))
            {
              printf("packages: %s\n", pool_errstr(pool));
              fclose(fp);
+             cinfo->incomplete = 1;
              break;    /* hopeless */
            }
          fclose(fp);
@@ -1910,12 +1917,12 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
            filename = susetags_find(repo, "packages.en", &filechksum, &filechksumtype);
          if (filename)
            {
-             if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
+             if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
                {
                  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;
+                     cinfo->incomplete = 1;
                    }
                  fclose(fp);
                }
@@ -1923,7 +1930,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          filename = susetags_find(repo, "patterns", &filechksum, &filechksumtype);
          if (filename)
            {
-             if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
+             if ((fp = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
                {
                  char pbuf[256];
                  while (fgets(pbuf, sizeof(pbuf), fp))
@@ -1935,12 +1942,12 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
                      if (!*pbuf || *pbuf == '.' || strchr(pbuf, '/') != 0)
                        continue;
                      filename = susetags_find(repo, pbuf, &filechksum, &filechksumtype);
-                     if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
+                     if (filename && (fp2 = curlfopen(cinfo, pool_tmpjoin(pool, descrdir, "/", filename), iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
                        {
                          if (repo_add_susetags(repo, fp2, defvendor, 0, REPO_NO_INTERNALIZE))
                            {
                              printf("%s: %s\n", pbuf, pool_errstr(pool));
-                             badchecksum = 1;
+                             cinfo->incomplete = 1;
                            }
                          fclose(fp2);
                        }
@@ -1952,7 +1959,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          data = repo_add_repodata(repo, 0);
          susetags_add_ext(repo, data);
          repodata_internalize(data);
-         if (!badchecksum)
+         if (!cinfo->incomplete)
            writecachedrepo(repo, 0, 0, cinfo->cookie);
          repodata_create_stubs(repo_last_repodata(repo));
          break;
@@ -1975,24 +1982,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, fpr, 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))
@@ -2009,12 +2005,12 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
                  printf("[component %s not found]\n", cinfo->components[j]);
                  continue;
                }
-             if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, &badchecksum)) != 0)
+             if ((fp = curlfopen(cinfo, filename, iscompressed(filename), filechksum, filechksumtype, 1)) != 0)
                {
                  if (repo_add_debpackages(repo, fp, 0))
                    {
                      printf("component %s: %s\n", cinfo->components[j], pool_errstr(pool));
-                     badchecksum = 1;
+                     cinfo->incomplete = 1;
                    }
                  fclose(fp);
                }
@@ -2022,7 +2018,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
              solv_free((char *)filename);
            }
          fclose(fpr);
-         if (!badchecksum)
+         if (!cinfo->incomplete)
            writecachedrepo(repo, 0, 0, cinfo->cookie);
          break;
 #endif
@@ -2038,341 +2034,6 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
     pool_free(sigpool);
 }
 
-
-int
-str2archid(Pool *pool, char *arch)
-{
-  Id id;
-  if (!*arch)
-    return 0;
-  id = pool_str2id(pool, arch, 0);
-  if (id == ARCH_SRC || id == ARCH_NOSRC || id == ARCH_NOARCH)
-    return id;
-  if (pool->id2arch && (id > pool->lastarch || !pool->id2arch[id]))
-    return 0;
-  return id;
-}
-
-
-#define DEPGLOB_NAME     1
-#define DEPGLOB_DEP      2
-#define DEPGLOB_NAMEDEP  3
-
-int
-depglob(Pool *pool, char *name, Queue *job, int what)
-{
-  Id p, pp;
-  Id id = pool_str2id(pool, name, 0);
-  int i, match = 0;
-
-  if (id)
-    {
-      FOR_PROVIDES(p, pp, id)
-       {
-         Solvable *s = pool->solvables + p;
-         match = 1;
-         if (s->name == id && (what & DEPGLOB_NAME) != 0)
-           {
-             queue_push2(job, SOLVER_SOLVABLE_NAME, id);
-             return 1;
-           }
-       }
-      if (match)
-       {
-         if (what == DEPGLOB_NAMEDEP)
-           printf("[using capability match for '%s']\n", name);
-         queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
-         return 1;
-       }
-    }
-
-  if (strpbrk(name, "[*?") == 0)
-    return 0;
-
-  if ((what & DEPGLOB_NAME) != 0)
-    {
-      /* looks like a name glob. hard work. */
-      for (p = 1; p < pool->nsolvables; p++)
-       {
-         Solvable *s = pool->solvables + p;
-         if (!s->repo || !pool_installable(pool, s))
-           continue;
-         id = s->name;
-         if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
-           {
-             for (i = 0; i < job->count; i += 2)
-               if (job->elements[i] == SOLVER_SOLVABLE_NAME && job->elements[i + 1] == id)
-                 break;
-             if (i == job->count)
-               queue_push2(job, SOLVER_SOLVABLE_NAME, id);
-             match = 1;
-           }
-       }
-      if (match)
-       return 1;
-    }
-  if ((what & DEPGLOB_DEP))
-    {
-      /* looks like a dep glob. really hard work. */
-      for (id = 1; id < pool->ss.nstrings; id++)
-       {
-         if (!pool->whatprovides[id])
-           continue;
-         if (fnmatch(name, pool_id2str(pool, id), 0) == 0)
-           {
-             if (!match && what == DEPGLOB_NAMEDEP)
-               printf("[using capability match for '%s']\n", name);
-             for (i = 0; i < job->count; i += 2)
-               if (job->elements[i] == SOLVER_SOLVABLE_PROVIDES && job->elements[i + 1] == id)
-                 break;
-             if (i == job->count)
-               queue_push2(job, SOLVER_SOLVABLE_PROVIDES, id);
-             match = 1;
-           }
-       }
-      if (match)
-       return 1;
-    }
-  return 0;
-}
-
-int
-limitrelation(Pool *pool, Queue *job, int flags, Id evr)
-{
-  int i, j;
-  Id p, pp;
-  for (i = j = 0; i < job->count; i += 2)
-    {
-      Id select = job->elements[i] & SOLVER_SELECTMASK;
-      if (select != SOLVER_SOLVABLE_NAME && select != SOLVER_SOLVABLE_PROVIDES)
-       {
-         fprintf(stderr, "limitrelation only works on name/provides jobs\n");
-         exit(1);
-       }
-      job->elements[i + 1] = pool_rel2id(pool, job->elements[i + 1], evr, flags, 1);
-      if (flags == REL_ARCH)
-       job->elements[i] |= SOLVER_SETARCH;
-      if (flags == REL_EQ && select == SOLVER_SOLVABLE_NAME && job->elements[i])
-       {
-#ifndef DEBIAN
-         const char *evrstr = pool_id2str(pool, evr);
-         if (!strchr(evrstr, '-'))
-           job->elements[i] |= SOLVER_SETEV;
-         else
-#endif
-           job->elements[i] |= SOLVER_SETEVR;
-       }
-      /* make sure we still have matches */
-      FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
-       break;
-      if (p)
-       {
-         job->elements[j] = job->elements[i];
-         job->elements[j + 1] = job->elements[i + 1];
-         j += 2;
-       }
-    }
-  queue_truncate(job, j);
-  return j / 2;
-}
-
-int
-limitrelation_arch(Pool *pool, Queue *job, int flags, char *evr)
-{
-  char *r;
-  Id archid;
-  if ((r = strrchr(evr, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
-    {
-      *r = 0;
-      limitrelation(pool, job, REL_ARCH, archid);
-      limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
-      *r = '.';
-    }
-  else
-    limitrelation(pool, job, flags, pool_str2id(pool, evr, 1));
-  return job->count / 2;
-}
-
-int
-limitrepo(Pool *pool, Id repofilter, Queue *job)
-{
-  Queue mq;
-  Id p, pp;
-  int i, j;
-  Solvable *s;
-
-  queue_init(&mq);
-  for (i = j = 0; i < job->count; i += 2)
-    {
-      queue_empty(&mq);
-      FOR_JOB_SELECT(p, pp, job->elements[i], job->elements[i + 1])
-       {
-         s = pool_id2solvable(pool, p);
-         if (s->repo && s->repo->repoid == repofilter)
-            queue_push(&mq, p);
-       }
-      if (mq.count)
-       {
-         /* here we assume that repo == vendor, so we also set SOLVER_SETVENDOR */
-         Id flags = (job->elements[i] & SOLVER_SETMASK) | SOLVER_SETVENDOR | SOLVER_SETREPO;
-         if ((job->elements[i] & SOLVER_SELECTMASK) == SOLVER_SOLVABLE_NAME)
-           flags |= SOLVER_SETNAME;
-         if (mq.count == 1)
-           {
-             job->elements[j] = SOLVER_SOLVABLE | SOLVER_NOAUTOSET | flags;
-             job->elements[j + 1] = mq.elements[0];
-           }
-         else
-           {
-             job->elements[j] = SOLVER_SOLVABLE_ONE_OF | flags;
-             job->elements[j + 1] = pool_queuetowhatprovides(pool, &mq);
-           }
-         j += 2;
-       }
-    }
-  queue_truncate(job, j);
-  queue_free(&mq);
-  return j / 2;
-}
-
-int
-mkselect(Pool *pool, int mode, char *name, Queue *job)
-{
-  char *r, *r2;
-  Id archid;
-
-  if (*name == '/')
-    {
-      Dataiterator di;
-      int type = strpbrk(name, "[*?") == 0 ? SEARCH_STRING : SEARCH_GLOB;
-      Queue q;
-      queue_init(&q);
-      dataiterator_init(&di, pool, mode == SOLVER_ERASE ? pool->installed : 0, 0, SOLVABLE_FILELIST, name, type|SEARCH_FILES|SEARCH_COMPLETE_FILELIST);
-      while (dataiterator_step(&di))
-       {
-         Solvable *s = pool->solvables + di.solvid;
-         if (!s->repo || !pool_installable(pool, s))
-           continue;
-         queue_push(&q, di.solvid);
-         dataiterator_skip_solvable(&di);
-       }
-      dataiterator_free(&di);
-      if (q.count)
-       {
-         printf("[using file list match for '%s']\n", name);
-         if (q.count > 1)
-           queue_push2(job, SOLVER_SOLVABLE_ONE_OF, pool_queuetowhatprovides(pool, &q));
-         else
-           queue_push2(job, SOLVER_SOLVABLE | SOLVER_NOAUTOSET, q.elements[0]);
-         queue_free(&q);
-         return job->count / 2;
-       }
-    }
-  if ((r = strpbrk(name, "<=>")) != 0)
-    {
-      /* relation case, support:
-       * depglob rel
-       * depglob.arch rel
-       */
-      int rflags = 0;
-      int nend = r - name;
-      char oldnend;
-      for (; *r; r++)
-       {
-         if (*r == '<')
-           rflags |= REL_LT;
-         else if (*r == '=')
-           rflags |= REL_EQ;
-         else if (*r == '>')
-           rflags |= REL_GT;
-         else
-           break;
-       }
-      while (*r && *r == ' ' && *r == '\t')
-       r++;
-      while (nend && (name[nend - 1] == ' ' || name[nend -1 ] == '\t'))
-       nend--;
-      if (!*name || !*r)
-       {
-         fprintf(stderr, "bad relation\n");
-         exit(1);
-       }
-      oldnend = name[nend];
-      name[nend] = 0;
-      if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
-       {
-         name[nend] = oldnend;
-         limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
-         return job->count / 2;
-       }
-      if ((r2 = strrchr(name, '.')) != 0 && r2[1] && (archid = str2archid(pool, r2 + 1)) != 0)
-       {
-         *r2 = 0;
-         if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
-           {
-             name[nend] = oldnend;
-             *r2 = '.';
-             limitrelation(pool, job, REL_ARCH, archid);
-             limitrelation(pool, job, rflags, pool_str2id(pool, r, 1));
-             return job->count / 2;
-           }
-         *r2 = '.';
-       }
-      name[nend] = oldnend;
-    }
-  else
-    {
-      /* no relation case, support:
-       * depglob
-       * depglob.arch
-       * nameglob-version
-       * nameglob-version.arch
-       * nameglob-version-release
-       * nameglob-version-release.arch
-       */
-      if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
-       return job->count / 2;
-      if ((r = strrchr(name, '.')) != 0 && r[1] && (archid = str2archid(pool, r + 1)) != 0)
-       {
-         *r = 0;
-         if (depglob(pool, name, job, DEPGLOB_NAMEDEP))
-           {
-             *r = '.';
-             limitrelation(pool, job, REL_ARCH, archid);
-             return job->count / 2;
-           }
-         *r = '.';
-       }
-      if ((r = strrchr(name, '-')) != 0)
-       {
-         *r = 0;
-         if (depglob(pool, name, job, DEPGLOB_NAME))
-           {
-             /* have just the version */
-             limitrelation_arch(pool, job, REL_EQ, r + 1);
-             *r = '-';
-             return job->count / 2;
-           }
-         if ((r2 = strrchr(name, '-')) != 0)
-           {
-             *r = '-';
-             *r2 = 0;
-             r = r2;
-             if (depglob(pool, name, job, DEPGLOB_NAME))
-               {
-                 /* have version-release */
-                 limitrelation_arch(pool, job, REL_EQ, r + 1);
-                 *r = '-';
-                 return job->count / 2;
-               }
-           }
-         *r = '-';
-       }
-    }
-  return 0;
-}
-
-
 int
 yesno(const char *str)
 {
@@ -2619,6 +2280,9 @@ rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_ins
       /* make sure all repodatas but the first are extensions */
       if (repo->nrepodata < 2)
        continue;
+      cinfo = repo->appdata;
+      if (cinfo && cinfo->incomplete)
+       continue;
       data = repo_id2repodata(repo, 1);
       if (data->loadcallback)
         continue;
@@ -2657,7 +2321,6 @@ rewrite_repos(Pool *pool, Queue *addedfileprovides, Queue *addedfileprovides_ins
        }
       repodata_set_idarray(data, SOLVID_META, REPOSITORY_ADDEDFILEPROVIDES, repo == pool->installed && addedfileprovides_inst ? addedfileprovides_inst : addedfileprovides);
       repodata_internalize(data);
-      cinfo = repo->appdata;
       writecachedrepo(repo, data, 0, cinfo ? cinfo->cookie : installedcookie);
     }
   queue_free(&fileprovidesq);
@@ -2764,12 +2427,12 @@ main(int argc, char **argv)
   Solver *solv = 0;
   Transaction *trans;
   char inbuf[128], *ip;
-  int allpkgs = 0;
   FILE **newpkgsfps;
   Queue addedfileprovides;
   Queue addedfileprovides_inst;
-  Id repofilter = 0;
+  Queue repofilter;
   int cleandeps = 0;
+  int forcebest = 0;
   char *rootdir = 0;
 
   argc--;
@@ -2819,7 +2482,7 @@ main(int argc, char **argv)
   else if (!strcmp(argv[0], "dist-upgrade") || !strcmp(argv[0], "dup"))
     {
       mainmode = MODE_DISTUPGRADE;
-      mode = SOLVER_UPDATE;
+      mode = SOLVER_DISTUPGRADE;
     }
   else if (!strcmp(argv[0], "repos") || !strcmp(argv[0], "repolist") || !strcmp(argv[0], "lr"))
     {
@@ -2844,6 +2507,12 @@ main(int argc, char **argv)
          argc--;
          argv++;
        }
+      else if (argc > 1 && !strcmp(argv[1], "--best"))
+       {
+         forcebest = 1;
+         argc--;
+         argv++;
+       }
       else
        break;
     }
@@ -2879,9 +2548,11 @@ main(int argc, char **argv)
 
   read_repos(pool, repoinfos, nrepoinfos);
 
-  if (argc > 2 && !strcmp(argv[1], "-r"))
+  queue_init(&repofilter);
+  while (argc > 2 && !strcmp(argv[1], "-r"))
     {
       const char *rname = argv[2], *rp;
+      Id repoid = 0;
       for (rp = rname; *rp; rp++)
        if (*rp <= '0' || *rp >= '9')
          break;
@@ -2895,7 +2566,7 @@ main(int argc, char **argv)
              if (!cinfo->enabled)
                continue;
              if (--rnum == 0)
-               repofilter = cinfo->repo->repoid;
+               repoid = cinfo->repo->repoid;
            }
        }
       else
@@ -2905,14 +2576,16 @@ main(int argc, char **argv)
          FOR_REPOS(i, repo)
            {
              if (!strcasecmp(rname, repo->name))
-               repofilter = repo->repoid;
+               repoid = repo->repoid;
            }
        }
-      if (!repofilter)
+      if (!repoid)
        {
          fprintf(stderr, "%s: no such repo\n", rname);
          exit(1);
        }
+      /* SETVENDOR is actually wrong but useful */
+      queue_push2(&repofilter, SOLVER_SOLVABLE_REPO | SOLVER_SETREPO | SOLVER_SETVENDOR, repoid);
       argc -= 2;
       argv += 2;
     }
@@ -3006,7 +2679,7 @@ main(int argc, char **argv)
   for (i = 1; i < argc; i++)
     {
       Queue job2;
-      int j;
+      int j, flags, rflags;
 
       if (commandlinepkgs && commandlinepkgs[i])
        {
@@ -3014,39 +2687,64 @@ main(int argc, char **argv)
          continue;
        }
       queue_init(&job2);
-      if (!mkselect(pool, mode, argv[i], &job2))
+      flags = SELECTION_NAME|SELECTION_PROVIDES|SELECTION_GLOB;
+      if (mode == MODE_LIST)
+       flags |= SELECTION_WITH_SOURCE;
+      if (argv[i][0] == '/')
+       flags |= SELECTION_FILELIST | (mode == MODE_ERASE ? SELECTION_INSTALLED_ONLY : 0);
+      rflags = selection_make(pool, &job2, argv[i], flags);
+      if (repofilter.count)
+       selection_limit(pool, &job2, &repofilter);
+      if (!job2.count)
+       {
+         flags |= SELECTION_NOCASE;
+          rflags = selection_make(pool, &job2, argv[i], flags);
+         if (repofilter.count)
+           selection_limit(pool, &job2, &repofilter);
+         if (job2.count)
+           printf("[ignoring case for '%s']\n", argv[i]);
+       }
+      if (!job2.count)
        {
          fprintf(stderr, "nothing matches '%s'\n", argv[i]);
          exit(1);
        }
-      if (repofilter && !limitrepo(pool, repofilter, &job2))
-        {
-         fprintf(stderr, "nothing in repo matches '%s'\n", argv[i]);
-         exit(1);
-        }
+      if (rflags & SELECTION_FILELIST)
+        printf("[using file list match for '%s']\n", argv[i]);
+      if (rflags & SELECTION_PROVIDES)
+       printf("[using capability match for '%s']\n", argv[i]);
       for (j = 0; j < job2.count; j++)
        queue_push(&job, job2.elements[j]);
       queue_free(&job2);
     }
 
-  if (!job.count && mainmode != MODE_UPDATE && mainmode != MODE_DISTUPGRADE && mainmode != MODE_VERIFY && mainmode != MODE_PATCH)
+  if (!job.count && (mainmode == MODE_UPDATE || mainmode == MODE_DISTUPGRADE || mainmode == MODE_VERIFY || repofilter.count))
+    {
+      queue_push2(&job, SOLVER_SOLVABLE_ALL, 0);
+      if (repofilter.count)
+       selection_limit(pool, &job, &repofilter);
+    }
+  queue_free(&repofilter);
+
+  if (!job.count && MODE_PATCH)
     {
       printf("no package matched\n");
       exit(1);
     }
 
-  if (!job.count)
-    allpkgs = 1;
-
   if (mainmode == MODE_LIST || mainmode == MODE_INFO)
     {
       /* list mode, no solver needed */
+      Queue q;
+      queue_init(&q);
       for (i = 0; i < job.count; i += 2)
        {
-         Id how = job.elements[i] & SOLVER_SELECTMASK;
-         FOR_JOB_SELECT(p, pp, how, job.elements[i + 1])
+         int j;
+         queue_empty(&q);
+         pool_job2solvables(pool, &q, job.elements[i], job.elements[i + 1]);
+         for (j = 0; j < q.count; j++)
            {
-             Solvable *s = pool_id2solvable(pool, p);
+             Solvable *s = pool_id2solvable(pool, q.elements[j]);
              if (mainmode == MODE_INFO)
                {
                  const char *str;
@@ -3075,6 +2773,7 @@ main(int argc, char **argv)
                }
            }
        }
+      queue_free(&q);
       queue_free(&job);
       pool_free(pool);
       free_repoinfos(repoinfos, nrepoinfos);
@@ -3091,45 +2790,13 @@ main(int argc, char **argv)
   // add mode
   for (i = 0; i < job.count; i += 2)
     {
-      if (mode == SOLVER_UPDATE)
-       {
-         /* make update of not installed packages an install */
-         FOR_JOB_SELECT(p, pp, job.elements[i], job.elements[i + 1])
-           if (pool->installed && pool->solvables[p].repo == pool->installed)
-             break;
-         if (!p)
-           {
-             job.elements[i] |= SOLVER_INSTALL;
-             if (cleandeps)
-               job.elements[i] |= SOLVER_CLEANDEPS;
-             continue;
-           }
-       }
       job.elements[i] |= mode;
+      if (mode == SOLVER_UPDATE && pool_isemptyupdatejob(pool, job.elements[i], job.elements[i + 1]))
+       job.elements[i] ^= SOLVER_UPDATE ^ SOLVER_INSTALL;
       if (cleandeps)
         job.elements[i] |= SOLVER_CLEANDEPS;
-    }
-
-  if (mainmode == MODE_DISTUPGRADE && allpkgs)
-    {
-      if (repofilter)
-        queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_REPO, repofilter);
-      else
-        queue_push2(&job, SOLVER_DISTUPGRADE|SOLVER_SOLVABLE_ALL, 0);
-    }
-  if (mainmode == MODE_UPDATE && allpkgs)
-    {
-      if (repofilter)
-        queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_REPO, repofilter);
-      else
-        queue_push2(&job, SOLVER_UPDATE|SOLVER_SOLVABLE_ALL, 0);
-    }
-  if (mainmode == MODE_VERIFY && allpkgs)
-    {
-      if (repofilter)
-        queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_REPO, repofilter);
-      else
-        queue_push2(&job, SOLVER_VERIFY|SOLVER_SOLVABLE_ALL, 0);
+      if (forcebest)
+        job.elements[i] |= SOLVER_FORCEBEST;
     }
 
   // multiversion test
@@ -3153,6 +2820,7 @@ rerunsolver:
       solver_set_flag(solv, SOLVER_FLAG_SPLITPROVIDES, 1);
       if (mainmode == MODE_ERASE)
         solver_set_flag(solv, SOLVER_FLAG_ALLOW_UNINSTALL, 1); /* don't nag */
+      solver_set_flag(solv, SOLVER_FLAG_BEST_OBEY_POLICY, 1);
 
       if (!solver_solve(solv, &job))
        break;
@@ -3291,7 +2959,7 @@ rerunsolver:
          s = pool_id2solvable(pool, p);
          if (s->repo == commandlinerepo)
            {
-             loc = solvable_get_location(s, &medianr);
+             loc = solvable_lookup_location(s, &medianr);
              if (!(newpkgsfps[i] = fopen(loc, "r")))
                {
                  perror(loc);
@@ -3306,7 +2974,7 @@ rerunsolver:
              printf("%s: no repository information\n", s->repo->name);
              exit(1);
            }
-         loc = solvable_get_location(s, &medianr);
+         loc = solvable_lookup_location(s, &medianr);
          if (!loc)
             continue;
 
@@ -3367,10 +3035,14 @@ rerunsolver:
                      chksum = pool_lookup_bin_checksum(pool, SOLVID_POS, DELTA_CHECKSUM, &chksumtype);
                      if (!chksumtype)
                        continue;       /* no way! */
-                     dloc = pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_DIR);
-                     dloc = pool_tmpappend(pool, dloc, "/", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_NAME));
-                     dloc = pool_tmpappend(pool, dloc, "-", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_EVR));
-                     dloc = pool_tmpappend(pool, dloc, ".", pool_lookup_str(pool, SOLVID_POS, DELTA_LOCATION_SUFFIX));
+                     dloc = pool_lookup_deltalocation(pool, SOLVID_POS, 0);
+                     if (!dloc)
+                       continue;
+                     if (cinfo->type == TYPE_SUSETAGS)
+                       {
+                         const char *datadir = repo_lookup_str(cinfo->repo, SOLVID_META, SUSETAGS_DATADIR);
+                         dloc = pool_tmpjoin(pool, datadir ? datadir : "suse", "/", dloc);
+                       }
                      if ((fp = curlfopen(cinfo, dloc, 0, chksum, chksumtype, 0)) == 0)
                        continue;
                      /* got it, now reconstruct */