Already use LOCALPOOL when converting filelist/language extensions
[platform/upstream/libsolv.git] / examples / solv.c
index 139529f..7ca9729 100644 (file)
@@ -115,6 +115,7 @@ struct repoinfo {
 
   unsigned char cookie[32];
   unsigned char extcookie[32];
+  int incomplete;
 };
 
 #ifdef FEDORA
@@ -780,7 +781,7 @@ iscompressed(const char *name)
 }
 
 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;
@@ -820,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);
     }
@@ -863,15 +864,15 @@ 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;
     }
@@ -881,8 +882,8 @@ curlfopen(struct repoinfo *cinfo, const char *file, int uncompress, const unsign
       if (solv_xfopen_iscompressed(file) < 0)
        {
          printf("%s: unsupported compression\n", file);
-         if (badchecksump)
-           *badchecksump = 1;
+         if (markincomplete)
+           cinfo->incomplete = 1;
          close(fd);
          return 0;
        }
@@ -1048,7 +1049,7 @@ calc_checksum_fp(FILE *fp, Id chktype, unsigned char *out)
 }
 
 void
-calc_checksum_stat(struct stat *stb, Id chktype, unsigned *cookie, 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));
@@ -1158,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");
@@ -1250,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);
@@ -1363,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);
@@ -1483,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);
@@ -1511,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));
@@ -1687,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;
@@ -1780,7 +1788,6 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          printf(" cached\n");
          continue;
        }
-      badchecksum = 0;
       switch (cinfo->type)
        {
 #ifdef ENABLE_RPMMD
@@ -1815,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);
            }
@@ -1843,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;
@@ -1894,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);
@@ -1909,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);
                }
@@ -1922,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))
@@ -1934,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);
                        }
@@ -1951,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;
@@ -1974,7 +1982,7 @@ read_repos(Pool *pool, struct repoinfo *repoinfos, int nrepoinfos)
          if (cinfo->repo_gpgcheck)
            {
              filename = solv_dupjoin("dists/", cinfo->name, "/Release.gpg");
-             if (!downloadchecksig(cinfo, fp, filename, &sigpool))
+             if (!downloadchecksig(cinfo, fpr, filename, &sigpool))
                {
                  fclose(fpr);
                  solv_free((char *)filename);
@@ -1997,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);
                }
@@ -2010,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
@@ -2272,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;
@@ -2310,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);
@@ -2422,6 +2432,7 @@ main(int argc, char **argv)
   Queue addedfileprovides_inst;
   Queue repofilter;
   int cleandeps = 0;
+  int forcebest = 0;
   char *rootdir = 0;
 
   argc--;
@@ -2471,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"))
     {
@@ -2496,6 +2507,12 @@ main(int argc, char **argv)
          argc--;
          argv++;
        }
+      else if (argc > 1 && !strcmp(argv[1], "--best"))
+       {
+         forcebest = 1;
+         argc--;
+         argv++;
+       }
       else
        break;
     }
@@ -2671,6 +2688,8 @@ main(int argc, char **argv)
        }
       queue_init(&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);
@@ -2771,23 +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 (forcebest)
+        job.elements[i] |= SOLVER_FORCEBEST;
     }
 
   // multiversion test
@@ -2811,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;
@@ -2949,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);
@@ -2964,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;
 
@@ -3025,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 */