merge from the debian-sid branch
[ntk/apt.git] / apt-pkg / depcache.cc
index 45c614c..4d1a08e 100644 (file)
@@ -192,7 +192,7 @@ bool pkgDepCache::readStateFile(OpProgress *Prog)                   /*{{{*/
         {
            PkgState[pkg->ID].Flags |= Flag::Auto;
            if (unlikely(debug_autoremove))
-              std::cout << "Auto-Installed : " << pkg.FullName() << std::endl;
+              std::clog << "Auto-Installed : " << pkg.FullName() << std::endl;
            if (pkgarch == "any")
            {
               pkgCache::GrpIterator G = pkg.Group();
@@ -257,7 +257,22 @@ bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly)     /*{{{*/
         pkgCache::PkgIterator pkg = Cache->FindPkg(pkgname, pkgarch);
         if(pkg.end() || pkg.VersionList().end()) 
            continue;
-        bool const newAuto = (PkgState[pkg->ID].Flags & Flag::Auto);
+        StateCache const &P = PkgState[pkg->ID];
+        bool newAuto = (P.Flags & Flag::Auto);
+        // skip not installed or now-removed ones if requested
+        if (InstalledOnly && (
+            (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
+            (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
+        {
+           // The section is obsolete if it contains no other tag
+           unsigned int const count = section.Count();
+           if (count < 2 ||
+               (count == 2 && section.Exists("Auto-Installed")) ||
+               (count == 3 && section.Exists("Auto-Installed") && section.Exists("Architecture")))
+              continue;
+           else
+              newAuto = false;
+        }
         if(_config->FindB("Debug::pkgAutoRemove",false))
            std::clog << "Update existing AutoInstall info: " 
                      << pkg.FullName() << std::endl;
@@ -277,14 +292,17 @@ bool pkgDepCache::writeStateFile(OpProgress *prog, bool InstalledOnly)    /*{{{*/
    // then write the ones we have not seen yet
    std::ostringstream ostr;
    for(pkgCache::PkgIterator pkg=Cache->PkgBegin(); !pkg.end(); pkg++) {
-      if(PkgState[pkg->ID].Flags & Flag::Auto) {
+      StateCache const &P = PkgState[pkg->ID];
+      if(P.Flags & Flag::Auto) {
         if (pkgs_seen.find(pkg.FullName()) != pkgs_seen.end()) {
            if(debug_autoremove)
               std::clog << "Skipping already written " << pkg.FullName() << std::endl;
            continue;
         }
         // skip not installed ones if requested
-        if(InstalledOnly && pkg->CurrentVer == 0)
+        if (InstalledOnly && (
+            (pkg->CurrentVer == 0 && P.Mode != ModeInstall) ||
+            (pkg->CurrentVer != 0 && P.Mode == ModeDelete)))
            continue;
         const char* const pkgarch = pkg.Arch();
         if (strcmp(pkgarch, "all") == 0)
@@ -630,11 +648,43 @@ bool pkgDepCache::RemovePseudoInstalledPkg(PkgIterator &Pkg, std::set<unsigned l
    if (V->MultiArch != Version::All)
       return false;
 
-   unsigned char const DepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
-   if ((DepState & DepInstMin) == DepInstMin)
+   // Never ever kill an "all" package - they have no dependency so they can't be broken
+   if (strcmp(Pkg.Arch(),"all") == 0)
       return false;
 
-   // Dependencies for this arch all are not statisfied
+   unsigned char const CurDepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
+   if ((CurDepState & DepInstMin) == DepInstMin) {
+      // okay, the package isn't broken, but is the package also required?
+      // If it has no real dependencies, no installed rdepends and doesn't
+      // provide something of value, we will kill it as not required.
+      // These pseudopackages have otherwise interesting effects if they get
+      // a new dependency in a newer version…
+      for (pkgCache::DepIterator D = V.DependsList();
+          D.end() != true; ++D)
+        if (D.IsCritical() == true && D.ParentPkg()->Group != Pkg->Group)
+           return false;
+      for (DepIterator D = Pkg.RevDependsList(); D.end() != true; ++D)
+      {
+        if (D.IsCritical() == false)
+           continue;
+        PkgIterator const P = D.ParentPkg();
+        if (P->Group == Pkg->Group)
+           continue;
+        if (P->CurrentVer != 0)
+           return false;
+      }
+      for (PrvIterator Prv = V.ProvidesList(); Prv.end() != true; Prv++)
+        for (DepIterator d = Prv.ParentPkg().RevDependsList();
+             d.end() != true; ++d)
+        {
+           PkgIterator const P = d.ParentPkg();
+           if (P->CurrentVer != 0 &&
+               P->Group != Pkg->Group)
+              return false;
+        }
+   }
+
+   // Dependencies for this arch all package are not statisfied
    // so we installed it only for our convenience: get right of it now.
    RemoveSizes(Pkg);
    RemoveStates(Pkg);
@@ -655,15 +705,33 @@ bool pkgDepCache::RemovePseudoInstalledPkg(PkgIterator &Pkg, std::set<unsigned l
         recheck.insert(P.Index());
    }
 
-   if (V.end() != true)
-      for (PrvIterator Prv = V.ProvidesList(); Prv.end() != true; Prv++)
-        for (DepIterator d = Prv.ParentPkg().RevDependsList();
-             d.end() != true; ++d)
-        {
-           PkgIterator const P = d.ParentPkg();
-           if (P->CurrentVer != 0)
-              recheck.insert(P.Index());
-        }
+   for (DepIterator d = V.DependsList(); d.end() != true; ++d)
+   {
+      PkgIterator const P = d.TargetPkg();
+      for (PrvIterator Prv = P.ProvidesList(); Prv.end() != true; ++Prv)
+      {
+        PkgIterator const O = Prv.OwnerPkg();
+        if (O->CurrentVer != 0)
+           recheck.insert(O.Index());
+      }
+
+      if (P->CurrentVer != 0)
+        recheck.insert(P.Index());
+   }
+
+   for (PrvIterator Prv = V.ProvidesList(); Prv.end() != true; Prv++)
+   {
+      for (DepIterator d = Prv.ParentPkg().RevDependsList();
+          d.end() != true; ++d)
+      {
+        PkgIterator const P = d.ParentPkg();
+        if (P->CurrentVer == 0)
+           continue;
+
+           recheck.insert(P.Index());
+      }
+   }
+
 
    return true;
 }
@@ -747,6 +815,41 @@ void pkgDepCache::Update(OpProgress *Prog)
         }
         recheck.erase(p);
       }
+
+      /* Okay, we have killed a great amount of pseudopackages -
+        we have killed so many that we have now arch "all" packages
+        without an installed pseudo package, but we NEED an installed
+        pseudo package, so we will search now for a pseudo package
+        we can install without breaking everything. */
+      for (GrpIterator G = Cache->GrpBegin(); G.end() != true; ++G)
+      {
+        PkgIterator P = G.FindPkg("all");
+        if (P.end() == true)
+           continue;
+        if (P->CurrentVer == 0)
+           continue;
+        bool installed = false;
+        for (P = G.FindPkg("any"); P.end() != true; P = G.NextPkg(P))
+        {
+           if (strcmp(P.Arch(), "all") == 0)
+              continue;
+           if (P->CurrentVer == 0)
+              continue;
+           installed = true;
+           break;
+        }
+        if (installed == false)
+           recheck.insert(G.Index());
+      }
+
+      while (recheck.empty() != true)
+      {
+        std::set<unsigned long>::const_iterator g = recheck.begin();
+        unsigned long const G = *g;
+        recheck.erase(g);
+        if (unlikely(ReInstallPseudoForGroup(G, recheck) == false))
+           _error->Warning(_("Internal error, group '%s' has no installable pseudo package"), GrpIterator(*Cache, Cache->GrpP + *g).Name());
+      }
    }
 
    if (Prog != 0)
@@ -755,6 +858,80 @@ void pkgDepCache::Update(OpProgress *Prog)
    readStateFile(Prog);
 }
                                                                        /*}}}*/
+// DepCache::ReInstallPseudoForGroup - MultiArch helper for Update()   /*{{{*/
+// ---------------------------------------------------------------------
+/* RemovePseudoInstalledPkg() is very successful. It even kills packages
+   to an amount that no pseudo package is left, but we need a pseudo package
+   for upgrading senarios so we need to reinstall one pseudopackage which
+   doesn't break everything. Thankfully we can't have architecture depending
+   negative dependencies so this problem is already eliminated */
+bool pkgDepCache::ReInstallPseudoForGroup(pkgCache::PkgIterator const &P, std::set<unsigned long> &recheck)
+{
+   if (P->CurrentVer != 0)
+      return true;
+   // recursive call for packages which provide this package
+   for (pkgCache::PrvIterator Prv = P.ProvidesList(); Prv.end() != true; ++Prv)
+      ReInstallPseudoForGroup(Prv.OwnerPkg(), recheck);
+   // check if we actually need to look at this group
+   unsigned long const G = P->Group;
+   std::set<unsigned long>::const_iterator Pi = recheck.find(G);
+   if (Pi == recheck.end())
+      return true;
+   recheck.erase(Pi); // remove here, so we can't fall into an endless loop
+   if (unlikely(ReInstallPseudoForGroup(G, recheck) == false))
+   {
+      recheck.insert(G);
+      return false;
+   }
+   return true;
+}
+bool pkgDepCache::ReInstallPseudoForGroup(unsigned long const &G, std::set<unsigned long> &recheck)
+{
+   std::vector<std::string> static const Archs = APT::Configuration::getArchitectures();
+   pkgCache::GrpIterator Grp(*Cache, Cache->GrpP + G);
+   if (unlikely(Grp.end() == true))
+      return false;
+   for (std::vector<std::string>::const_iterator a = Archs.begin();
+        a != Archs.end(); ++a)
+   {
+      pkgCache::PkgIterator P = Grp.FindPkg(*a);
+      if (P.end() == true)
+        continue;
+      pkgCache::VerIterator allV = Grp.FindPkg("all").CurrentVer();
+      for (VerIterator V = P.VersionList(); V.end() != true; ++V)
+      {
+        // search for the same version as the all package
+        if (allV->Hash != V->Hash || strcmp(allV.VerStr(),V.VerStr()) != 0)
+           continue;
+        unsigned char const CurDepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
+        // If it is broken, try to install dependencies first before retry
+        if ((CurDepState & DepInstMin) != DepInstMin)
+        {
+           for (pkgCache::DepIterator D = V.DependsList(); D.end() != true; ++D)
+           {
+              if (D->Type != pkgCache::Dep::PreDepends && D->Type != pkgCache::Dep::Depends)
+                 continue;
+              ReInstallPseudoForGroup(D.TargetPkg(), recheck);
+           }
+           unsigned char const CurDepState = VersionState(V.DependsList(),DepInstall,DepInstMin,DepInstPolicy);
+           // if package ist still broken… try another arch
+           if ((CurDepState & DepInstMin) != DepInstMin)
+              break;
+        }
+        // dependencies satisfied: reinstall the package
+        RemoveSizes(P);
+        RemoveStates(P);
+        P->CurrentVer = V.Index();
+        PkgState[P->ID].InstallVer = V;
+        AddStates(P);
+        Update(P);
+        AddSizes(P);
+        return true;
+      }
+   }
+   return false;
+}
+                                                                       /*}}}*/
 // DepCache::Update - Update the deps list of a package                        /*{{{*/
 // ---------------------------------------------------------------------
 /* This is a helper for update that only does the dep portion of the scan. 
@@ -1642,7 +1819,7 @@ bool pkgDepCache::Sweep()                                         /*{{{*/
      {
        state.Garbage=true;
        if(debug_autoremove)
-          std::cout << "Garbage: " << p.FullName() << std::endl;
+          std::clog << "Garbage: " << p.FullName() << std::endl;
      }
   }