X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/88a8975f156e452d9f3ebe76822b236e8962ebba..63c7141275c8c5c0f6e60f5242785e50cabaf2a0:/apt-pkg/pkgcachegen.cc

diff --git a/apt-pkg/pkgcachegen.cc b/apt-pkg/pkgcachegen.cc
index 6daebbc2a..69c8fd105 100644
--- a/apt-pkg/pkgcachegen.cc
+++ b/apt-pkg/pkgcachegen.cc
@@ -57,14 +57,15 @@ pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
 		    Map(*pMap), Cache(pMap,false), Progress(Prog),
 		     CurrentRlsFile(NULL), CurrentFile(NULL), d(NULL)
 {
-   if (_error->PendingError() == true)
-      return;
-
    if (Map.Size() == 0)
    {
       // Setup the map interface..
       Cache.HeaderP = (pkgCache::Header *)Map.Data();
-      if (Map.RawAllocate(sizeof(pkgCache::Header)) == 0 && _error->PendingError() == true)
+      _error->PushToStack();
+      Map.RawAllocate(sizeof(pkgCache::Header));
+      bool const newError = _error->PendingError();
+      _error->MergeWithStack();
+      if (newError)
 	 return;
 
       Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
@@ -382,7 +383,7 @@ bool pkgCacheGenerator::MergeListVersion(ListParser &List, pkgCache::PkgIterator
 
    // Add a new version
    map_pointer_t const verindex = NewVersion(Ver, Version, Pkg.Index(), Hash, *LastVer);
-   if (verindex == 0 && _error->PendingError())
+   if (unlikely(verindex == 0))
       return _error->Error(_("Error occurred while processing %s (%s%d)"),
 			   Pkg.Name(), "NewVersion", 1);
 
@@ -470,7 +471,7 @@ bool pkgCacheGenerator::AddNewDescription(ListParser &List, pkgCache::VerIterato
    Dynamic<pkgCache::DescIterator> DynDesc(Desc);
 
    map_pointer_t const descindex = NewDescription(Desc, lang, CurMd5, md5idx);
-   if (unlikely(descindex == 0 && _error->PendingError()))
+   if (unlikely(descindex == 0))
       return _error->Error(_("Error occurred while processing %s (%s%d)"),
 	    Ver.ParentPkg().Name(), "NewDescription", 1);
 
@@ -572,21 +573,31 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name
       if (APT::Configuration::checkArchitecture(Pkg.Arch()) == true)
       {
 	 pkgCache::PkgIterator const M = Grp.FindPreferredPkg(false); // native or any foreign pkg will do
-	 if (M.end() == false)
-	    for (pkgCache::PrvIterator Prv = M.ProvidesList(); Prv.end() == false; ++Prv)
+	 if (M.end() == false) {
+	    pkgCache::PrvIterator Prv;
+	    Dynamic<pkgCache::PrvIterator> DynPrv(Prv);
+	    for (Prv = M.ProvidesList(); Prv.end() == false; ++Prv)
 	    {
 	       if ((Prv->Flags & pkgCache::Flag::ArchSpecific) != 0)
 		  continue;
 	       pkgCache::VerIterator Ver = Prv.OwnerVer();
+	       Dynamic<pkgCache::VerIterator> DynVer(Ver);
 	       if ((Ver->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed ||
 	           ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign &&
 			(Prv->Flags & pkgCache::Flag::MultiArchImplicit) == 0))
 		  if (NewProvides(Ver, Pkg, Prv->ProvideVersion, Prv->Flags) == false)
 		     return false;
 	    }
+	 }
+
 
-	 for (pkgCache::PkgIterator P = Grp.PackageList(); P.end() == false;  P = Grp.NextPkg(P))
-	    for (pkgCache::VerIterator Ver = P.VersionList(); Ver.end() == false; ++Ver)
+	 pkgCache::PkgIterator P;
+	 pkgCache::VerIterator Ver;
+	 Dynamic<pkgCache::PkgIterator> DynP(P);
+	 Dynamic<pkgCache::VerIterator> DynVer(Ver);
+
+	 for (P = Grp.PackageList(); P.end() == false;  P = Grp.NextPkg(P))
+	    for (Ver = P.VersionList(); Ver.end() == false; ++Ver)
 	       if ((Ver->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
 		  if (NewProvides(Ver, Pkg, Ver->VerStr, pkgCache::Flag::MultiArchImplicit) == false)
 		     return false;
@@ -594,8 +605,10 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name
       // and negative dependencies, don't forget negative dependencies
       {
 	 pkgCache::PkgIterator const M = Grp.FindPreferredPkg(false);
-	 if (M.end() == false)
-	    for (pkgCache::DepIterator Dep = M.RevDependsList(); Dep.end() == false; ++Dep)
+	 if (M.end() == false) {
+	    pkgCache::DepIterator Dep;
+	    Dynamic<pkgCache::DepIterator> DynDep(Dep);
+	    for (Dep = M.RevDependsList(); Dep.end() == false; ++Dep)
 	    {
 	       if ((Dep->CompareOp & (pkgCache::Dep::ArchSpecific | pkgCache::Dep::MultiArchImplicit)) != 0)
 		  continue;
@@ -603,10 +616,12 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name
 		     Dep->Type != pkgCache::Dep::Replaces)
 		  continue;
 	       pkgCache::VerIterator Ver = Dep.ParentVer();
+	       Dynamic<pkgCache::VerIterator> DynVer(Ver);
 	       map_pointer_t * unused = NULL;
 	       if (NewDepends(Pkg, Ver, Dep->Version, Dep->CompareOp, Dep->Type, unused) == false)
 		  return false;
 	    }
+	 }
       }
 
       // this package is the new last package
@@ -615,6 +630,37 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name
       LastPkg->NextPackage = Package;
    }
    Grp->LastPackage = Package;
+
+   // lazy-create foo (of amd64) provides foo:amd64 at the time we first need it
+   if (Arch == "any")
+   {
+      size_t const found = Name.find(':');
+      std::string const NameA = Name.substr(0, found);
+      std::string const ArchA = Name.substr(found + 1);
+      pkgCache::PkgIterator PkgA = Cache.FindPkg(NameA, ArchA);
+      if (PkgA.end() == false)
+      {
+	 Dynamic<pkgCache::PkgIterator> DynPkgA(PkgA);
+	 pkgCache::PrvIterator Prv = PkgA.ProvidesList();
+	 for (; Prv.end() == false; ++Prv)
+	 {
+	    if (Prv.IsMultiArchImplicit())
+	       continue;
+	    pkgCache::VerIterator V = Prv.OwnerVer();
+	    if (ArchA != V.ParentPkg().Arch())
+	       continue;
+	    if (NewProvides(V, Pkg, V->VerStr, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
+	       return false;
+	 }
+	 pkgCache::VerIterator V = PkgA.VersionList();
+	 Dynamic<pkgCache::VerIterator> DynV(V);
+	 for (; V.end() == false; ++V)
+	 {
+	    if (NewProvides(V, Pkg, V->VerStr, pkgCache::Flag::MultiArchImplicit | pkgCache::Flag::ArchSpecific) == false)
+	       return false;
+	 }
+      }
+   }
    return true;
 }
 									/*}}}*/
@@ -1024,7 +1070,8 @@ bool pkgCacheListParser::NewProvides(pkgCache::VerIterator &Ver,
 
    // We do not add self referencing provides
    if (Ver.ParentPkg().Name() == PkgName && (PkgArch == Ver.ParentPkg().Arch() ||
-	(PkgArch == "all" && strcmp((Cache.StrP + Cache.HeaderP->Architecture), Ver.ParentPkg().Arch()) == 0)))
+	(PkgArch == "all" && strcmp((Cache.StrP + Cache.HeaderP->Architecture), Ver.ParentPkg().Arch()) == 0)) &&
+	 (Version.empty() || Version == Ver.VerStr()))
       return true;
 
    // Locate the target package
@@ -1071,7 +1118,9 @@ bool pkgCacheGenerator::NewProvides(pkgCache::VerIterator &Ver,
 bool pkgCacheListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string const &Package,
 				string const &Version, uint8_t const Flags) {
    pkgCache &Cache = Owner->Cache;
-   pkgCache::GrpIterator const Grp = Cache.FindGrp(Package);
+   pkgCache::GrpIterator Grp = Cache.FindGrp(Package);
+   Dynamic<pkgCache::GrpIterator> DynGrp(Grp);
+
    if (Grp.end() == true)
       return NewProvides(Ver, Package, Cache.NativeArch(), Version, Flags);
    else
@@ -1085,8 +1134,11 @@ bool pkgCacheListParser::NewProvidesAllArch(pkgCache::VerIterator &Ver, string c
 
       bool const isImplicit = (Flags & pkgCache::Flag::MultiArchImplicit) == pkgCache::Flag::MultiArchImplicit;
       bool const isArchSpecific = (Flags & pkgCache::Flag::ArchSpecific) == pkgCache::Flag::ArchSpecific;
-      pkgCache::PkgIterator const OwnerPkg = Ver.ParentPkg();
-      for (pkgCache::PkgIterator Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
+      pkgCache::PkgIterator OwnerPkg = Ver.ParentPkg();
+      Dynamic<pkgCache::PkgIterator> DynOwnerPkg(OwnerPkg);
+      pkgCache::PkgIterator Pkg;
+      Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
+      for (Pkg = Grp.PackageList(); Pkg.end() == false; Pkg = Grp.NextPkg(Pkg))
       {
 	 if (isImplicit && OwnerPkg == Pkg)
 	    continue;
@@ -1224,12 +1276,18 @@ map_stringitem_t pkgCacheGenerator::StoreString(enum StringType const type, cons
 /* This just verifies that each file in the list of index files exists,
    has matching attributes with the cache and the cache does not have
    any extra files. */
+class APT_HIDDEN ScopedErrorRevert {
+public:
+   ScopedErrorRevert() { _error->PushToStack(); }
+   ~ScopedErrorRevert() { _error->RevertToStack(); }
+};
 static bool CheckValidity(const string &CacheFile,
                           pkgSourceList &List,
                           FileIterator const Start,
                           FileIterator const End,
                           MMap **OutMap = 0)
 {
+   ScopedErrorRevert ser;
    bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
    // No file, certainly invalid
    if (CacheFile.empty() == true || FileExists(CacheFile) == false)
@@ -1250,11 +1308,10 @@ static bool CheckValidity(const string &CacheFile,
    FileFd CacheF(CacheFile,FileFd::ReadOnly);
    std::unique_ptr<MMap> Map(new MMap(CacheF,0));
    pkgCache Cache(Map.get());
-   if (_error->PendingError() == true || Map->Size() == 0)
+   if (_error->PendingError() || Map->Size() == 0)
    {
       if (Debug == true)
 	 std::clog << "Errors are pending or Map is empty() for " << CacheFile << std::endl;
-      _error->Discard();
       return false;
    }
 
@@ -1333,12 +1390,11 @@ static bool CheckValidity(const string &CacheFile,
       if (Debug == true)
       {
 	 std::clog << "Validity failed because of pending errors:" << std::endl;
-	 _error->DumpErrors();
+	 _error->DumpErrors(std::clog, GlobalError::DEBUG, false);
       }
-      _error->Discard();
       return false;
    }
-   
+
    if (OutMap != 0)
       *OutMap = Map.release();
    return true;
@@ -1461,7 +1517,7 @@ static bool writeBackMMapToFile(pkgCacheGenerator * const Gen, DynamicMMap * con
       std::string const &FileName)
 {
    FileFd SCacheF(FileName, FileFd::WriteAtomic);
-   if (_error->PendingError() == true)
+   if (SCacheF.IsOpen() == false || SCacheF.Failed())
       return false;
 
    fchmod(SCacheF.Fd(),0644);
@@ -1485,10 +1541,15 @@ static bool loadBackMMapFromFile(std::unique_ptr<pkgCacheGenerator> &Gen,
 {
    Map.reset(CreateDynamicMMap(NULL, 0));
    FileFd CacheF(FileName, FileFd::ReadOnly);
+   if (CacheF.IsOpen() == false || CacheF.Failed())
+      return false;
+   _error->PushToStack();
    map_pointer_t const alloc = Map->RawAllocate(CacheF.Size());
-   if ((alloc == 0 && _error->PendingError())
-	 || CacheF.Read((unsigned char *)Map->Data() + alloc,
-	    CacheF.Size()) == false)
+   bool const newError = _error->PendingError();
+   _error->MergeWithStack();
+   if (alloc == 0 && newError)
+      return false;
+   if (CacheF.Read((unsigned char *)Map->Data() + alloc, CacheF.Size()) == false)
       return false;
    Gen.reset(new pkgCacheGenerator(Map.get(),Progress));
    return true;
@@ -1497,8 +1558,9 @@ APT_DEPRECATED bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
 			MMap **OutMap, bool AllowMem)
    { return pkgCacheGenerator::MakeStatusCache(List, &Progress, OutMap, AllowMem); }
 bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress,
-			MMap **OutMap,bool AllowMem)
+			MMap **OutMap,bool)
 {
+   // FIXME: deprecate the ignored AllowMem parameter
    bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
 
    std::vector<pkgIndexFile *> Files;
@@ -1563,16 +1625,6 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
 
       if (Debug == true)
 	 std::clog << "Do we have write-access to the cache files? " << (Writeable ? "YES" : "NO") << std::endl;
-
-      if (Writeable == false && AllowMem == false)
-      {
-	 if (CacheFile.empty() == false)
-	    return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
-	 else if (SrcCacheFile.empty() == false)
-	    return _error->Error(_("Unable to write to %s"),flNotFile(SrcCacheFile).c_str());
-	 else
-	    return _error->Error("Unable to create caches as file usage is disabled, but memory not allowed either!");
-      }
    }
 
    // At this point we know we need to construct something, so get storage ready
@@ -1650,8 +1702,11 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
 }
 									/*}}}*/
 // CacheGenerator::MakeOnlyStatusCache - Build only a status files cache/*{{{*/
-// ---------------------------------------------------------------------
-/* */
+class APT_HIDDEN ScopedErrorMerge {
+public:
+   ScopedErrorMerge() { _error->PushToStack(); }
+   ~ScopedErrorMerge() { _error->MergeWithStack(); }
+};
 APT_DEPRECATED bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
    { return pkgCacheGenerator::MakeOnlyStatusCache(&Progress, OutMap); }
 bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap)
@@ -1660,12 +1715,12 @@ bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **O
    if (_system->AddStatusFiles(Files) == false)
       return false;
 
+   ScopedErrorMerge sem;
    std::unique_ptr<DynamicMMap> Map(CreateDynamicMMap(NULL, 0));
    map_filesize_t CurrentSize = 0;
    map_filesize_t TotalSize = 0;
-   
    TotalSize = ComputeSize(NULL, Files.begin(), Files.end());
-   
+
    // Build the status cache
    if (Progress != NULL)
       Progress->OverallProgress(0,1,1,_("Reading package lists"));