Added instruction how to work around MMap error in MMap error message.
[ntk/apt.git] / apt-pkg / depcache.cc
index f5673dd..2411bfe 100644 (file)
@@ -8,9 +8,6 @@
    ##################################################################### */
                                                                        /*}}}*/
 // Include Files                                                       /*{{{*/
-#ifdef __GNUG__
-#pragma implementation "apt-pkg/depcache.h"
-#endif
 #include <apt-pkg/depcache.h>
 #include <apt-pkg/version.h>
 #include <apt-pkg/error.h>
 #include <sstream>    
 #include <set>
 
+#include <sys/stat.h>
+
 #include <apti18n.h>    
 
+// helper for Install-Recommends-Sections and Never-MarkAuto-Sections
+static bool 
+ConfigValueInSubTree(const char* SubTree, const char *needle)
+{
+   Configuration::Item const *Opts;
+   Opts = _config->Tree(SubTree);
+   if (Opts != 0 && Opts->Child != 0)
+   {
+      Opts = Opts->Child;
+      for (; Opts != 0; Opts = Opts->Next)
+      {
+        if (Opts->Value.empty() == true)
+           continue;
+        if (strcmp(needle, Opts->Value.c_str()) == 0)
+           return true;
+      }
+   }
+   return false;
+}
+
+
 pkgDepCache::ActionGroup::ActionGroup(pkgDepCache &cache) :
   cache(cache), released(false)
 {
@@ -179,7 +199,7 @@ bool pkgDepCache::readStateFile(OpProgress *Prog)
    return true;
 }
 
-bool pkgDepCache::writeStateFile(OpProgress *prog)
+bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly)
 {
    if(_config->FindB("Debug::pkgAutoRemove",false))
       std::clog << "pkgDepCache::writeStateFile()" << std::endl;
@@ -217,7 +237,6 @@ bool pkgDepCache::writeStateFile(OpProgress *prog)
         pkgCache::PkgIterator pkg=Cache->FindPkg(pkgname);
         if(pkg.end() || pkg.VersionList().end()) 
            continue;
-        bool oldAuto = section.FindI("Auto-Installed");
         bool newAuto = (PkgState[pkg->ID].Flags & Flag::Auto);
         if(_config->FindB("Debug::pkgAutoRemove",false))
            std::clog << "Update exisiting AutoInstall info: " 
@@ -241,20 +260,24 @@ bool pkgDepCache::writeStateFile(OpProgress *prog)
               std::clog << "Skipping already written " << pkg.Name() << std::endl;
            continue;
         }
+         // skip not installed ones if requested
+         if(InstalledOnly && pkg->CurrentVer == 0)
+            continue;
         if(_config->FindB("Debug::pkgAutoRemove",false))
            std::clog << "Writing new AutoInstall: " 
                      << pkg.Name() << std::endl;
         ostr.str(string(""));
         ostr << "Package: " << pkg.Name() 
              << "\nAuto-Installed: 1\n\n";
-        fprintf(OutFile,ostr.str().c_str());
+        fprintf(OutFile,"%s",ostr.str().c_str());
         fprintf(OutFile,"\n");
       }
    }
    fclose(OutFile);
 
-   // move the outfile over the real file
+   // move the outfile over the real file and set permissions
    rename(outfile.c_str(), state.c_str());
+   chmod(state.c_str(), 0644);
 
    return true;
 }
@@ -273,7 +296,7 @@ bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
       we allow it anyhow because dpkg does. Technically it is a packaging
       bug. Conflicts may never self match */
    if (Dep.TargetPkg() != Dep.ParentPkg() || 
-       (Dep->Type != Dep::Conflicts && Dep->Type != Dep::Obsoletes))
+       (Dep->Type != Dep::Conflicts && Dep->Type != Dep::DpkgBreaks && Dep->Type != Dep::Obsoletes))
    {
       PkgIterator Pkg = Dep.TargetPkg();
       // Check the base package
@@ -303,7 +326,8 @@ bool pkgDepCache::CheckDep(DepIterator Dep,int Type,PkgIterator &Res)
    {
       /* Provides may never be applied against the same package if it is
          a conflicts. See the comment above. */
-      if (P.OwnerPkg() == Pkg && Dep->Type == Dep::Conflicts)
+      if (P.OwnerPkg() == Pkg &&
+         (Dep->Type == Dep::Conflicts || Dep->Type == Dep::DpkgBreaks))
         continue;
       
       // Check if the provides is a hit
@@ -399,9 +423,11 @@ void pkgDepCache::AddStates(const PkgIterator &Pkg,int Add)
 {
    StateCache &State = PkgState[Pkg->ID];
    
-   // The Package is broken
+   // The Package is broken (either minimal dep or policy dep)
    if ((State.DepState & DepInstMin) != DepInstMin)
       iBrokenCount += Add;
+   if ((State.DepState & DepInstPolicy) != DepInstPolicy)
+      iPolicyBrokenCount += Add;
    
    // Bad state
    if (Pkg.State() != PkgIterator::NeedsNothing)
@@ -455,7 +481,9 @@ void pkgDepCache::BuildGroupOrs(VerIterator const &V)
 
       /* Invert for Conflicts. We have to do this twice to get the
          right sense for a conflicts group */
-      if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
+      if (D->Type == Dep::Conflicts ||
+         D->Type == Dep::DpkgBreaks ||
+         D->Type == Dep::Obsoletes)
         State = ~State;
       
       // Add to the group if we are within an or..
@@ -466,7 +494,9 @@ void pkgDepCache::BuildGroupOrs(VerIterator const &V)
         Group = 0;
       
       // Invert for Conflicts
-      if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
+      if (D->Type == Dep::Conflicts ||
+         D->Type == Dep::DpkgBreaks ||
+         D->Type == Dep::Obsoletes)
         State = ~State;
    }    
 }
@@ -599,7 +629,9 @@ void pkgDepCache::Update(OpProgress *Prog)
               Group = 0;
 
            // Invert for Conflicts
-           if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
+           if (D->Type == Dep::Conflicts ||
+               D->Type == Dep::DpkgBreaks ||
+               D->Type == Dep::Obsoletes)
               State = ~State;
         }       
       }
@@ -629,7 +661,9 @@ void pkgDepCache::Update(DepIterator D)
       State = DependencyState(D);
     
       // Invert for Conflicts
-      if (D->Type == Dep::Conflicts || D->Type == Dep::Obsoletes)
+      if (D->Type == Dep::Conflicts ||
+         D->Type == Dep::DpkgBreaks ||
+         D->Type == Dep::Obsoletes)
         State = ~State;
 
       RemoveStates(D.ParentPkg());
@@ -701,7 +735,6 @@ void pkgDepCache::MarkKeep(PkgIterator const &Pkg, bool Soft, bool FromUser)
    // We dont even try to keep virtual packages..
    if (Pkg->VersionList == 0)
       return;
-
 #if 0 // reseting the autoflag here means we lose the 
       // auto-mark information if a user selects a package for removal
       // but changes  his mind then and sets it for keep again
@@ -772,7 +805,8 @@ void pkgDepCache::MarkDelete(PkgIterator const &Pkg, bool rPurge)
 // ---------------------------------------------------------------------
 /* */
 void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
-                             unsigned long Depth, bool FromUser)
+                             unsigned long Depth, bool FromUser,
+                             bool ForceImportantDeps)
 {
    if (Depth > 100)
       return;
@@ -787,7 +821,8 @@ void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
       installed */
    StateCache &P = PkgState[Pkg->ID];
    P.iFlags &= ~AutoKept;
-   if (P.InstBroken() == false && (P.Mode == ModeInstall ||
+   if ((P.InstPolicyBroken() == false && P.InstBroken() == false) && 
+       (P.Mode == ModeInstall ||
        P.CandidateVer == (Version *)Pkg.CurrentVer()))
    {
       if (P.CandidateVer == (Version *)Pkg.CurrentVer() && P.InstallVer == 0)
@@ -798,11 +833,9 @@ void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
    // See if there is even any possible instalation candidate
    if (P.CandidateVer == 0)
       return;
-   
    // We dont even try to install virtual packages..
    if (Pkg->VersionList == 0)
       return;
-   
    /* Target the candidate version and remove the autoflag. We reset the
       autoflag below if this was called recursively. Otherwise the user
       should have the ability to de-auto a package by changing its state */
@@ -856,10 +889,66 @@ void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
 
       /* Check if this dep should be consider for install. If it is a user
          defined important dep and we are installed a new package then 
-        it will be installed. Otherwise we only worry about critical deps */
+        it will be installed. Otherwise we only check for important
+         deps that have changed from the installed version
+      */
       if (IsImportantDep(Start) == false)
         continue;
-      if (Pkg->CurrentVer != 0 && Start.IsCritical() == false)
+      
+      /* Check if any ImportantDep() (but not Critical) were added
+       * since we installed the package.  Also check for deps that
+       * were satisfied in the past: for instance, if a version
+       * restriction in a Recommends was tightened, upgrading the
+       * package should follow that Recommends rather than causing the
+       * dependency to be removed. (bug #470115)
+       */
+      bool isNewImportantDep = false;
+      bool isPreviouslySatisfiedImportantDep = false;
+      if(!ForceImportantDeps && !Start.IsCritical())
+      {
+        bool found=false;
+        VerIterator instVer = Pkg.CurrentVer();
+        if(!instVer.end())
+        {
+          for (DepIterator D = instVer.DependsList(); D.end() != true; D++)
+            {
+              //FIXME: deal better with or-groups(?)
+              DepIterator LocalStart = D;
+
+              if(IsImportantDep(D) && !D.IsCritical() &&
+                 Start.TargetPkg() == D.TargetPkg())
+                {
+                  if(!isPreviouslySatisfiedImportantDep)
+                    {
+                      DepIterator D2 = D;
+                      while((D2->CompareOp & Dep::Or) != 0)
+                        ++D2;
+
+                      isPreviouslySatisfiedImportantDep =
+                        (((*this)[D2] & DepGNow) != 0);
+                    }
+
+                  found=true;
+                }
+            }
+           // this is a new dep if it was not found to be already
+           // a important dep of the installed pacakge
+           isNewImportantDep = !found;
+        }
+      }
+      if(isNewImportantDep)
+        if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
+           std::clog << "new important dependency: " 
+                     << Start.TargetPkg().Name() << std::endl;
+      if(isPreviouslySatisfiedImportantDep)
+       if(_config->FindB("Debug::pkgDepCache::AutoInstall", false) == true)
+         std::clog << "previously satisfied important dependency on "
+                   << Start.TargetPkg().Name() << std::endl;
+
+      // skip important deps if the package is already installed
+      if (Pkg->CurrentVer != 0 && Start.IsCritical() == false 
+         && !isNewImportantDep && !isPreviouslySatisfiedImportantDep
+         && !ForceImportantDeps)
         continue;
       
       /* If we are in an or group locate the first or that can 
@@ -870,7 +959,8 @@ void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
       /* This bit is for processing the possibilty of an install/upgrade
          fixing the problem */
       SPtrArray<Version *> List = Start.AllTargets();
-      if ((DepState[Start->ID] & DepCVer) == DepCVer)
+      if (Start->Type != Dep::DpkgBreaks &&
+         (DepState[Start->ID] & DepCVer) == DepCVer)
       {
         // Right, find the best version to install..
         Version **Cur = List;
@@ -908,17 +998,16 @@ void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
                         << " as dep of " << Pkg.Name() 
                         << std::endl;
            // now check if we should consider it a automatic dependency or not
-           string sec = _config->Find("APT::Never-MarkAuto-Section","");
-           if(Pkg.Section() && (string(Pkg.Section()) ==  sec))
+           if(Pkg.Section() && ConfigValueInSubTree("APT::Never-MarkAuto-Sections", Pkg.Section()))
            {
               if(_config->FindB("Debug::pkgDepCache::AutoInstall",false) == true)
-                 std::clog << "Setting NOT as auto-installed because its a direct dep of a package in section " << sec << std::endl;
+                 std::clog << "Setting NOT as auto-installed (direct dep of pkg in APT::Never-MarkAuto-Sections)" << std::endl;
               MarkInstall(InstPkg,true,Depth + 1, true);
            }
            else 
            {
               // mark automatic dependency
-              MarkInstall(InstPkg,true,Depth + 1, false);
+              MarkInstall(InstPkg,true,Depth + 1, false, ForceImportantDeps);
               // Set the autoflag, after MarkInstall because MarkInstall unsets it
               if (P->CurrentVer == 0)
                  PkgState[InstPkg->ID].Flags |= Flag::Auto;
@@ -926,17 +1015,23 @@ void pkgDepCache::MarkInstall(PkgIterator const &Pkg,bool AutoInst,
         }
         continue;
       }
-      
+
       /* For conflicts we just de-install the package and mark as auto,
-         Conflicts may not have or groups */
-      if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes)
+         Conflicts may not have or groups.  For dpkg's Breaks we try to
+         upgrade the package. */
+      if (Start->Type == Dep::Conflicts || Start->Type == Dep::Obsoletes ||
+         Start->Type == Dep::DpkgBreaks)
       {
         for (Version **I = List; *I != 0; I++)
         {
            VerIterator Ver(*this,*I);
            PkgIterator Pkg = Ver.ParentPkg();
-      
-           MarkDelete(Pkg);
+
+           if (Start->Type != Dep::DpkgBreaks)
+              MarkDelete(Pkg);
+           else
+              if (PkgState[Pkg->ID].CandidateVer != *I)
+                 MarkInstall(Pkg,true,Depth + 1, false, ForceImportantDeps);
         }
         continue;
       }      
@@ -1079,12 +1174,30 @@ pkgCache::VerIterator pkgDepCache::Policy::GetCandidateVer(PkgIterator Pkg)
    return Last;
 }
                                                                        /*}}}*/
+
 // Policy::IsImportantDep - True if the dependency is important                /*{{{*/
 // ---------------------------------------------------------------------
 /* */
 bool pkgDepCache::Policy::IsImportantDep(DepIterator Dep)
 {
-   return Dep.IsCritical();
+   if(Dep.IsCritical())
+      return true;
+   else if(Dep->Type == pkgCache::Dep::Recommends) 
+   {
+      if ( _config->FindB("APT::Install-Recommends", false))
+        return true;
+      // we suport a special mode to only install-recommends for certain
+      // sections
+      // FIXME: this is a meant as a temporarly solution until the 
+      //        recommends are cleaned up
+      const char *sec = Dep.ParentVer().Section();
+      if (sec && ConfigValueInSubTree("APT::Install-Recommends-Sections", sec))
+        return true;
+   }
+   else if(Dep->Type == pkgCache::Dep::Suggests)
+     return _config->FindB("APT::Install-Suggests", false);
+
+   return false;
 }
                                                                        /*}}}*/
 
@@ -1193,7 +1306,7 @@ bool pkgDepCache::MarkRequired(InRootSetFunc &userFunc)
       {
         // the package is installed (and set to keep)
         if(PkgState[p->ID].Keep() && !p.CurrentVer().end())
-           MarkPackage(p, p.CurrentVer(),
+           MarkPackage(p, p.CurrentVer(),
                        follow_recommends, follow_suggests);
         // the package is to be installed 
         else if(PkgState[p->ID].Install())
@@ -1212,6 +1325,7 @@ void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
                              bool follow_suggests)
 {
    pkgDepCache::StateCache &state = PkgState[pkg->ID];
+   VerIterator currver            = pkg.CurrentVer();
    VerIterator candver            = state.CandidateVerIter(*this);
    VerIterator instver            = state.InstVerIter(*this);
 
@@ -1232,16 +1346,29 @@ void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
    }
 #endif
 
-   // Ignore versions other than the InstVer, and ignore packages
-   // that are already going to be removed or just left uninstalled.
-   if(!(ver == instver && !instver.end()))
+   // For packages that are not going to be removed, ignore versions
+   // other than the InstVer.  For packages that are going to be
+   // removed, ignore versions other than the current version.
+   if(!(ver == instver && !instver.end()) &&
+      !(ver == currver && instver.end() && !ver.end()))
       return;
 
    // if we are marked already we are done
    if(state.Marked)
       return;
 
-   //std::cout << "Setting Marked for: " << pkg.Name() << std::endl;
+   if(_config->FindB("Debug::pkgAutoRemove",false))
+     {
+       std::clog << "Marking: " << pkg.Name();
+       if(!ver.end())
+        std::clog << " " << ver.VerStr();
+       if(!currver.end())
+        std::clog << ", Curr=" << currver.VerStr();
+       if(!instver.end())
+        std::clog << ", Inst=" << instver.VerStr();
+       std::clog << std::endl;
+     }
+
    state.Marked=true;
 
    if(!ver.end())
@@ -1261,6 +1388,19 @@ void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
           {
              if(_system->VS->CheckDep(V.VerStr(), d->CompareOp, d.TargetVer()))
              {
+               if(_config->FindB("Debug::pkgAutoRemove",false))
+                 {
+                   std::clog << "Following dep: " << d.ParentPkg().Name()
+                             << " " << d.ParentVer().VerStr() << " "
+                             << d.DepType() << " "
+                             << d.TargetPkg().Name();
+                   if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
+                     {
+                       std::clog << " (" << d.CompType() << " "
+                                 << d.TargetVer() << ")";
+                     }
+                   std::clog << std::endl;
+                 }
                 MarkPackage(V.ParentPkg(), V, 
                             follow_recommends, follow_suggests);
              }
@@ -1272,6 +1412,23 @@ void pkgDepCache::MarkPackage(const pkgCache::PkgIterator &pkg,
              if(_system->VS->CheckDep(prv.ProvideVersion(), d->CompareOp, 
                                       d.TargetVer()))
              {
+               if(_config->FindB("Debug::pkgAutoRemove",false))
+                 {
+                   std::clog << "Following dep: " << d.ParentPkg().Name()
+                             << " " << d.ParentVer().VerStr() << " "
+                             << d.DepType() << " "
+                             << d.TargetPkg().Name();
+                   if((d->CompareOp & ~pkgCache::Dep::Or) != pkgCache::Dep::NoOp)
+                     {
+                       std::clog << " (" << d.CompType() << " "
+                                 << d.TargetVer() << ")";
+                     }
+                   std::clog << ", provided by "
+                             << prv.OwnerPkg().Name() << " "
+                             << prv.OwnerVer().VerStr()
+                             << std::endl;
+                 }
+
                 MarkPackage(prv.OwnerPkg(), prv.OwnerVer(),
                             follow_recommends, follow_suggests);
              }