* cmdline/apt-get.cc
[ntk/apt.git] / cmdline / apt-get.cc
index 7325bbf..849401b 100644 (file)
@@ -1098,7 +1098,17 @@ bool TryToInstall(pkgCache::PkgIterator Pkg,pkgDepCache &Cache,
                  Pkg.Name());
       return true;
    }
-   
+
+   // Ignore request for install if package would be new
+   if (_config->FindB("APT::Get::Only-Upgrade", false) == true &&
+       Pkg->CurrentVer == 0)
+   {
+      if (AllowFail == true)
+        ioprintf(c1out,_("Skipping %s, it is not installed and only upgrades are requested.\n"),
+                 Pkg.Name());
+      return true;
+   }
+
    // Check if there is something at all to install
    pkgDepCache::StateCache &State = Cache[Pkg];
    if (Remove == true && Pkg->CurrentVer == 0)
@@ -1247,131 +1257,153 @@ pkgSrcRecords::Parser *FindSrc(const char *Name,pkgRecords &Recs,
                               pkgSrcRecords &SrcRecs,string &Src,
                               pkgDepCache &Cache)
 {
-       string VerTag;
-       string DefRel = _config->Find("APT::Default-Release");
-       string TmpSrc = Name;
-       const size_t found = TmpSrc.find_last_of("/=");
-
-       // extract the version/release from the pkgname
-       if (found != string::npos) {
-               if (TmpSrc[found] == '/')
-                       DefRel = TmpSrc.substr(found+1);
-               else
-                       VerTag = TmpSrc.substr(found+1);
-               TmpSrc = TmpSrc.substr(0,found);
-       }
-
-       /* Lookup the version of the package we would install if we were to
-          install a version and determine the source package name, then look
-          in the archive for a source package of the same name. */
-       bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
-       const pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
-       if (MatchSrcOnly == false && Pkg.end() == false) {
-               if(VerTag.empty() == false || DefRel.empty() == false) {
-                       // we have a default release, try to locate the pkg. we do it like
-                       // this because GetCandidateVer() will not "downgrade", that means
-                       // "apt-get source -t stable apt" won't work on a unstable system
-                       for (pkgCache::VerIterator Ver = Pkg.VersionList();
-                            Ver.end() == false; Ver++) {
-                               for (pkgCache::VerFileIterator VF = Ver.FileList();
-                                    VF.end() == false; VF++) {
-                                       /* If this is the status file, and the current version is not the
-                                          version in the status file (ie it is not installed, or somesuch)
-                                          then it is not a candidate for installation, ever. This weeds
-                                          out bogus entries that may be due to config-file states, or
-                                          other. */
-                                       if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
-                                           pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
-                                               continue;
-
-                                       // We match against a concrete version (or a part of this version)
-                                       if (VerTag.empty() == false && strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)
-                                               continue;
-
-                                       // or we match against a release
-                                       if(VerTag.empty() == false ||
-                                          (VF.File().Archive() != 0 && VF.File().Archive() == DefRel) ||
-                                          (VF.File().Codename() != 0 && VF.File().Codename() == DefRel)) {
-                                               pkgRecords::Parser &Parse = Recs.Lookup(VF);
-                                               Src = Parse.SourcePkg();
-                                               if (VerTag.empty() == true)
-                                                       VerTag = Parse.SourceVer();
-                                               break;
-                                       }
-                               }
-                       }
-                       if (Src.empty() == true) {
-                               if (VerTag.empty() == false)
-                                       _error->Warning(_("Ignore unavailable version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
-                               else
-                                       _error->Warning(_("Ignore unavailable target release '%s' of package '%s'"), DefRel.c_str(), TmpSrc.c_str());
-                               VerTag.clear();
-                               DefRel.clear();
-                       }
-               }
-               if (VerTag.empty() == true && DefRel.empty() == true) {
-                       // if we don't have a version or default release, use the CandidateVer to find the Source
-                       pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
-                       if (Ver.end() == false) {
-                               pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
-                               Src = Parse.SourcePkg();
-                               VerTag = Parse.SourceVer();
-                       }
-               }
-       }
-
-       if (Src.empty() == true)
-               Src = TmpSrc;
-       else {
-               /* if we have a source pkg name, make sure to only search
-                  for srcpkg names, otherwise apt gets confused if there
-                  is a binary package "pkg1" and a source package "pkg1"
-                  with the same name but that comes from different packages */
-               MatchSrcOnly = true;
-               if (Src != TmpSrc) {
-                       ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
-               }
-       }
-
-       // The best hit
-       pkgSrcRecords::Parser *Last = 0;
-       unsigned long Offset = 0;
-       string Version;
-
-       /* Iterate over all of the hits, which includes the resulting
-          binary packages in the search */
-       pkgSrcRecords::Parser *Parse;
-       while (true) {
-               SrcRecs.Restart();
-               while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0) {
-                       const string Ver = Parse->Version();
-
-                       // Ignore all versions which doesn't fit
-                       if (VerTag.empty() == false && strncmp(VerTag.c_str(), Ver.c_str(), VerTag.size()) != 0)
-                               continue;
-
-                       // Newer version or an exact match? Save the hit
-                       if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0) {
-                               Last = Parse;
-                               Offset = Parse->Offset();
-                               Version = Ver;
-                       }
-
-                       // was the version check above an exact match? If so, we don't need to look further
-                       if (VerTag.empty() == false && VerTag.size() == Ver.size())
-                               break;
-               }
-               if (Last != 0 || VerTag.empty() == true)
-                       break;
-               //if (VerTag.empty() == false && Last == 0)
-               _error->Warning(_("Ignore unavailable version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
-               VerTag.clear();
-       }
-
-       if (Last == 0 || Last->Jump(Offset) == false)
-               return 0;
-
-       return Last;
+   string VerTag;
+   string DefRel = _config->Find("APT::Default-Release");
+   string TmpSrc = Name;
+
+   // extract the version/release from the pkgname
+   const size_t found = TmpSrc.find_last_of("/=");
+   if (found != string::npos) {
+      if (TmpSrc[found] == '/')
+        DefRel = TmpSrc.substr(found+1);
+      else
+        VerTag = TmpSrc.substr(found+1);
+      TmpSrc = TmpSrc.substr(0,found);
+   }
+
+   /* Lookup the version of the package we would install if we were to
+      install a version and determine the source package name, then look
+      in the archive for a source package of the same name. */
+   bool MatchSrcOnly = _config->FindB("APT::Get::Only-Source");
+   const pkgCache::PkgIterator Pkg = Cache.FindPkg(TmpSrc);
+   if (MatchSrcOnly == false && Pkg.end() == false) 
+   {
+      if(VerTag.empty() == false || DefRel.empty() == false) 
+      {
+        // we have a default release, try to locate the pkg. we do it like
+        // this because GetCandidateVer() will not "downgrade", that means
+        // "apt-get source -t stable apt" won't work on a unstable system
+        for (pkgCache::VerIterator Ver = Pkg.VersionList();
+             Ver.end() == false; Ver++) 
+        {
+           for (pkgCache::VerFileIterator VF = Ver.FileList();
+                VF.end() == false; VF++) 
+           {
+              /* If this is the status file, and the current version is not the
+                 version in the status file (ie it is not installed, or somesuch)
+                 then it is not a candidate for installation, ever. This weeds
+                 out bogus entries that may be due to config-file states, or
+                 other. */
+              if ((VF.File()->Flags & pkgCache::Flag::NotSource) ==
+                  pkgCache::Flag::NotSource && Pkg.CurrentVer() != Ver)
+                 continue;
+
+              // We match against a concrete version (or a part of this version)
+              if (VerTag.empty() == false && strncmp(VerTag.c_str(), Ver.VerStr(), VerTag.size()) != 0)
+                 continue;
+
+              // or we match against a release
+              if(VerTag.empty() == false ||
+                 (VF.File().Archive() != 0 && VF.File().Archive() == DefRel) ||
+                 (VF.File().Codename() != 0 && VF.File().Codename() == DefRel)) 
+              {
+                 pkgRecords::Parser &Parse = Recs.Lookup(VF);
+                 Src = Parse.SourcePkg();
+                 // no SourcePkg name, so it is the "binary" name
+                 if (Src.empty() == true)
+                    Src = TmpSrc;
+                 // no Version, so we try the Version of the SourcePkg -
+                 // and after that the version of the binary package
+                 if (VerTag.empty() == true)
+                    VerTag = Parse.SourceVer();
+                 if (VerTag.empty() == true)
+                    VerTag = Ver.VerStr();
+                 break;
+              }
+           }
+           if (Src.empty() == false)
+              break;
+        }
+        if (Src.empty() == true) 
+        {
+           // Sources files have no codename information
+           if (VerTag.empty() == true && DefRel.empty() == false)
+              _error->Warning(_("Ignore unavailable target release '%s' of package '%s'"), DefRel.c_str(), TmpSrc.c_str());
+           DefRel.clear();
+        }
+      }
+      if (Src.empty() == true)
+      {
+        // if we don't have found a fitting package yet so we will
+        // choose a good candidate and proceed with that.
+        // Maybe we will find a source later on with the right VerTag
+        pkgCache::VerIterator Ver = Cache.GetCandidateVer(Pkg);
+        if (Ver.end() == false) 
+        {
+           pkgRecords::Parser &Parse = Recs.Lookup(Ver.FileList());
+           Src = Parse.SourcePkg();
+           if (VerTag.empty() == true)
+              VerTag = Parse.SourceVer();
+        }
+      }
+   }
+
+   if (Src.empty() == true)
+      Src = TmpSrc;
+   else 
+   {
+      /* if we have a source pkg name, make sure to only search
+        for srcpkg names, otherwise apt gets confused if there
+        is a binary package "pkg1" and a source package "pkg1"
+        with the same name but that comes from different packages */
+      MatchSrcOnly = true;
+      if (Src != TmpSrc) 
+      {
+        ioprintf(c1out, _("Picking '%s' as source package instead of '%s'\n"), Src.c_str(), TmpSrc.c_str());
+      }
+   }
+
+   // The best hit
+   pkgSrcRecords::Parser *Last = 0;
+   unsigned long Offset = 0;
+   string Version;
+
+   /* Iterate over all of the hits, which includes the resulting
+      binary packages in the search */
+   pkgSrcRecords::Parser *Parse;
+   while (true) 
+   {
+      SrcRecs.Restart();
+      while ((Parse = SrcRecs.Find(Src.c_str(), MatchSrcOnly)) != 0) 
+      {
+        const string Ver = Parse->Version();
+
+        // Ignore all versions which doesn't fit
+        if (VerTag.empty() == false && strncmp(VerTag.c_str(), Ver.c_str(), VerTag.size()) != 0)
+           continue;
+
+        // Newer version or an exact match? Save the hit
+        if (Last == 0 || Cache.VS().CmpVersion(Version,Ver) < 0) {
+           Last = Parse;
+           Offset = Parse->Offset();
+           Version = Ver;
+        }
+
+        // was the version check above an exact match? If so, we don't need to look further
+        if (VerTag.empty() == false && VerTag.size() == Ver.size())
+           break;
+      }
+      if (Last != 0 || VerTag.empty() == true)
+        break;
+      //if (VerTag.empty() == false && Last == 0)
+      _error->Warning(_("Ignore unavailable version '%s' of package '%s'"), VerTag.c_str(), TmpSrc.c_str());
+      VerTag.clear();
+   }
+
+   if (Last == 0 || Last->Jump(Offset) == false)
+      return 0;
+
+   return Last;
 }
                                                                        /*}}}*/
 // DoUpdate - Update the package lists                                 /*{{{*/
@@ -1482,10 +1514,9 @@ bool DoAutomaticRemove(CacheFile &Cache)
            // only show stuff in the list that is not yet marked for removal
            if(Cache[Pkg].Delete() == false) 
            {
+              ++autoRemoveCount;
               // we don't need to fill the strings if we don't need them
-              if (smallList == true)
-                 ++autoRemoveCount;
-              else
+              if (smallList == false)
               {
                 autoremovelist += string(Pkg.Name()) + " ";
                 autoremoveversions += string(Cache[Pkg].CandVersion) + "\n";
@@ -1498,9 +1529,12 @@ bool DoAutomaticRemove(CacheFile &Cache)
    if (doAutoRemove == false && (autoremovelist.empty() == false || autoRemoveCount != 0))
    {
       if (smallList == false)
-        ShowList(c1out, _("The following packages were automatically installed and are no longer required:"), autoremovelist, autoremoveversions);
+        ShowList(c1out, P_("The following package is automatically installed and is no longer required:",
+                 "The following packages were automatically installed and are no longer required:",
+                 autoRemoveCount), autoremovelist, autoremoveversions);
       else
-        ioprintf(c1out, _("%lu packages were automatically installed and are no longer required.\n"), autoRemoveCount);
+        ioprintf(c1out, P_("%lu package was automatically installed and is no longer required.\n",
+                 "%lu packages were automatically installed and are no longer required.\n", autoRemoveCount), autoRemoveCount);
       c1out << _("Use 'apt-get autoremove' to remove them.") << std::endl;
    }
    // Now see if we had destroyed anything (if we had done anything)
@@ -1754,7 +1788,9 @@ bool DoInstall(CommandLine &CmdL)
            if(!Remove && 
               Cache[Pkg].Install() == false && 
               (Cache[Pkg].Flags & pkgCache::Flag::Auto) &&
-              _config->FindB("APT::Get::ReInstall",false) == false)
+              _config->FindB("APT::Get::ReInstall",false) == false &&
+              _config->FindB("APT::Get::Only-Upgrade",false) == false &&
+              _config->FindB("APT::Get::Download-Only",false) == false)
            {
               ioprintf(c1out,_("%s set to manually installed.\n"),
                        Pkg.Name());
@@ -2263,6 +2299,7 @@ bool DoSource(CommandLine &CmdL)
    {
       for (unsigned I = 0; I != J; I++)
         ioprintf(cout,_("Fetch source %s\n"),Dsc[I].Package.c_str());
+      delete[] Dsc;
       return true;
    }
    
@@ -2273,6 +2310,7 @@ bool DoSource(CommandLine &CmdL)
       for (; I != Fetcher.UriEnd(); I++)
         cout << '\'' << I->URI << "' " << flNotDir(I->Owner->DestFile) << ' ' << 
               I->Owner->FileSize << ' ' << I->Owner->HashSum() << endl;
+      delete[] Dsc;
       return true;
    }
    
@@ -2298,6 +2336,7 @@ bool DoSource(CommandLine &CmdL)
    if (_config->FindB("APT::Get::Download-only",false) == true)
    {
       c1out << _("Download complete and in download only mode") << endl;
+      delete[] Dsc;
       return true;
    }
 
@@ -2359,7 +2398,8 @@ bool DoSource(CommandLine &CmdL)
       
       _exit(0);
    }
-   
+   delete[] Dsc;
+
    // Wait for the subprocess
    int Status = 0;
    while (waitpid(Process,&Status,0) != Process)
@@ -2796,6 +2836,7 @@ int main(int argc,const char *argv[])                                     /*{{{*/
       {0,"fix-missing","APT::Get::Fix-Missing",0},
       {0,"ignore-hold","APT::Ignore-Hold",0},      
       {0,"upgrade","APT::Get::upgrade",0},
+      {0,"only-upgrade","APT::Get::Only-Upgrade",0},
       {0,"force-yes","APT::Get::force-yes",0},
       {0,"print-uris","APT::Get::Print-URIs",0},
       {0,"diff-only","APT::Get::Diff-Only",0},