fix a bunch of cppcheck warnings/errors based on a patch by
[ntk/apt.git] / cmdline / apt-get.cc
index 6a30c5a..38003e4 100644 (file)
@@ -764,7 +764,7 @@ struct TryToInstall {
    unsigned long AutoMarkChanged;
    APT::PackageSet doAutoInstallLater;
 
-   TryToInstall(pkgCacheFile &Cache, pkgProblemResolver &PM, bool const &FixBroken) : Cache(&Cache), Fix(&PM),
+   TryToInstall(pkgCacheFile &Cache, pkgProblemResolver *PM, bool const &FixBroken) : Cache(&Cache), Fix(PM),
                        FixBroken(FixBroken), AutoMarkChanged(0) {};
 
    void operator() (pkgCache::VerIterator const &Ver) {
@@ -782,8 +782,10 @@ struct TryToInstall {
         ioprintf(c1out,_("Skipping %s, it is not installed and only upgrades are requested.\n"),
                  Pkg.FullName(true).c_str());
       else {
-        Fix->Clear(Pkg);
-        Fix->Protect(Pkg);
+        if (Fix != NULL) {
+           Fix->Clear(Pkg);
+           Fix->Protect(Pkg);
+        }
         Cache->GetDepCache()->MarkInstall(Pkg,false);
 
         if (State.Install() == false) {
@@ -871,16 +873,19 @@ struct TryToRemove {
    bool PurgePkgs;
    unsigned long AutoMarkChanged;
 
-   TryToRemove(pkgCacheFile &Cache, pkgProblemResolver &PM) : Cache(&Cache), Fix(&PM),
+   TryToRemove(pkgCacheFile &Cache, pkgProblemResolver *PM) : Cache(&Cache), Fix(PM),
                                PurgePkgs(_config->FindB("APT::Get::Purge", false)) {};
 
    void operator() (pkgCache::VerIterator const &Ver)
    {
       pkgCache::PkgIterator Pkg = Ver.ParentPkg();
 
-      Fix->Clear(Pkg);
-      Fix->Protect(Pkg);
-      Fix->Remove(Pkg);
+      if (Fix != NULL)
+      {
+        Fix->Clear(Pkg);
+        Fix->Protect(Pkg);
+        Fix->Remove(Pkg);
+      }
 
       if ((Pkg->CurrentVer == 0 && PurgePkgs == false) ||
          (PurgePkgs == true && Pkg->CurrentState == pkgCache::State::NotInstalled))
@@ -1385,10 +1390,10 @@ bool TryToInstallBuildDep(pkgCache::PkgIterator Pkg,pkgCacheFile &Cache,
 
    if (Remove == true)
    {
-      TryToRemove RemoveAction(Cache, Fix);
+      TryToRemove RemoveAction(Cache, &Fix);
       RemoveAction(Pkg.VersionList());
    } else if (Cache[Pkg].CandidateVer != 0) {
-      TryToInstall InstallAction(Cache, Fix, BrokenFix);
+      TryToInstall InstallAction(Cache, &Fix, BrokenFix);
       InstallAction(Cache[Pkg].CandidateVerIter(Cache));
       InstallAction.doAutoInstall();
    } else
@@ -1791,8 +1796,10 @@ bool DoInstall(CommandLine &CmdL)
    bool BrokenFix = false;
    if (Cache->BrokenCount() != 0)
       BrokenFix = true;
-   
-   pkgProblemResolver Fix(Cache);
+
+   pkgProblemResolver* Fix = NULL;
+   if (_config->FindB("APT::Get::CallResolver", true) == true)
+      Fix = new pkgProblemResolver(Cache);
 
    static const unsigned short MOD_REMOVE = 1;
    static const unsigned short MOD_INSTALL = 2;
@@ -1823,6 +1830,8 @@ bool DoInstall(CommandLine &CmdL)
    if (_error->PendingError() == true)
    {
       helper.showVirtualPackageErrors(Cache);
+      if (Fix != NULL)
+        delete Fix;
       return false;
    }
 
@@ -1837,17 +1846,29 @@ bool DoInstall(CommandLine &CmdL)
 
       for (unsigned short i = 0; order[i] != 0; ++i)
       {
-        if (order[i] == MOD_INSTALL) {
+        if (order[i] == MOD_INSTALL)
            InstallAction = std::for_each(verset[MOD_INSTALL].begin(), verset[MOD_INSTALL].end(), InstallAction);
+        else if (order[i] == MOD_REMOVE)
+           RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction);
+      }
+
+      if (Fix != NULL && _config->FindB("APT::Get::AutoSolving", true) == true)
+      {
+         for (unsigned short i = 0; order[i] != 0; ++i)
+         {
+           if (order[i] != MOD_INSTALL)
+              continue;
            InstallAction.propergateReleaseCandiateSwitching(helper.selectedByRelease, c0out);
            InstallAction.doAutoInstall();
         }
-        else if (order[i] == MOD_REMOVE)
-           RemoveAction = std::for_each(verset[MOD_REMOVE].begin(), verset[MOD_REMOVE].end(), RemoveAction);
       }
 
       if (_error->PendingError() == true)
+      {
+        if (Fix != NULL)
+           delete Fix;
         return false;
+      }
 
       /* If we are in the Broken fixing mode we do not attempt to fix the
         problems. This is if the user invoked install without -f and gave
@@ -1856,14 +1877,19 @@ bool DoInstall(CommandLine &CmdL)
       {
         c1out << _("You might want to run 'apt-get -f install' to correct these:") << endl;
         ShowBroken(c1out,Cache,false);
-
+        if (Fix != NULL)
+           delete Fix;
         return _error->Error(_("Unmet dependencies. Try 'apt-get -f install' with no packages (or specify a solution)."));
       }
-   
-      // Call the scored problem resolver
-      Fix.InstallProtect();
-      if (Fix.Resolve(true) == false)
-        _error->Discard();
+
+      if (Fix != NULL)
+      {
+        // Call the scored problem resolver
+        Fix->InstallProtect();
+        if (Fix->Resolve(true) == false)
+           _error->Discard();
+        delete Fix;
+      }
 
       // Now we check the state of the packages,
       if (Cache->BrokenCount() != 0)
@@ -2360,8 +2386,10 @@ bool DoSource(CommandLine &CmdL)
       string Src;
       pkgSrcRecords::Parser *Last = FindSrc(*I,Recs,SrcRecs,Src,*Cache);
       
-      if (Last == 0)
+      if (Last == 0) {
+        delete[] Dsc;
         return _error->Error(_("Unable to find a source package for %s"),Src.c_str());
+      }
       
       string srec = Last->AsStr();
       string::size_type pos = srec.find("\nVcs-");
@@ -2392,8 +2420,10 @@ bool DoSource(CommandLine &CmdL)
 
       // Back track
       vector<pkgSrcRecords::File> Lst;
-      if (Last->Files(Lst) == false)
+      if (Last->Files(Lst) == false) {
+        delete[] Dsc;
         return false;
+      }
 
       // Load them into the fetcher
       for (vector<pkgSrcRecords::File>::const_iterator I = Lst.begin();
@@ -2454,6 +2484,7 @@ bool DoSource(CommandLine &CmdL)
    struct statvfs Buf;
    string OutputDir = ".";
    if (statvfs(OutputDir.c_str(),&Buf) != 0) {
+      delete[] Dsc;
       if (errno == EOVERFLOW)
         return _error->WarningE("statvfs",_("Couldn't determine free space in %s"),
                                OutputDir.c_str());
@@ -2467,10 +2498,12 @@ bool DoSource(CommandLine &CmdL)
 #if HAVE_STRUCT_STATFS_F_TYPE
            || unsigned(Stat.f_type) != RAMFS_MAGIC
 #endif
-           ) 
+           )  {
+        delete[] Dsc;
           return _error->Error(_("You don't have enough free space in %s"),
               OutputDir.c_str());
-      }
+       }
+     }
    
    // Number of bytes
    if (DebBytes != FetchBytes)
@@ -2505,7 +2538,10 @@ bool DoSource(CommandLine &CmdL)
    
    // Run it
    if (Fetcher.Run() == pkgAcquire::Failed)
+   {
+      delete[] Dsc;
       return false;
+   }
 
    // Print error messages
    bool Failed = false;
@@ -2520,8 +2556,11 @@ bool DoSource(CommandLine &CmdL)
       Failed = true;
    }
    if (Failed == true)
+   {
+      delete[] Dsc;
       return _error->Error(_("Failed to fetch some archives."));
-   
+   }
+
    if (_config->FindB("APT::Get::Download-only",false) == true)
    {
       c1out << _("Download complete and in download only mode") << endl;
@@ -2612,6 +2651,9 @@ bool DoSource(CommandLine &CmdL)
 bool DoBuildDep(CommandLine &CmdL)
 {
    CacheFile Cache;
+
+   _config->Set("APT::Install-Recommends", false);
+   
    if (Cache.Open(true) == false)
       return false;
 
@@ -3278,7 +3320,10 @@ int main(int argc,const char *argv[])                                    /*{{{*/
    }
 
    // simulate user-friendly if apt-get has no root privileges
-   if (getuid() != 0 && _config->FindB("APT::Get::Simulate") == true)
+   if (getuid() != 0 && _config->FindB("APT::Get::Simulate") == true &&
+       (CmdL.FileSize() == 0 ||
+        (strcmp(CmdL.FileList[0], "source") != 0 && strcmp(CmdL.FileList[0], "download") != 0 &&
+         strcmp(CmdL.FileList[0], "changelog") != 0)))
    {
       if (_config->FindB("APT::Get::Show-User-Simulation-Note",true) == true)
         cout << _("NOTE: This is only a simulation!\n"