]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/pkgcachegen.cc
fix crash when P.Arch() was used but the cache got remapped
[apt.git] / apt-pkg / pkgcachegen.cc
index 18bad6727100bb59e7e3c7686c2f0d7b3a52385b..0e6ae698d39cf3e4b61bbffb8d3adc2ab0c0361e 100644 (file)
@@ -36,7 +36,7 @@
 #include <stdio.h>
                                                                        /*}}}*/
 typedef vector<pkgIndexFile *>::iterator FileIterator;
-template <typename Iter> std::set<Iter*> pkgCacheGenerator::Dynamic<Iter>::toReMap;
+template <typename Iter> std::vector<Iter*> pkgCacheGenerator::Dynamic<Iter>::toReMap;
 
 // CacheGenerator::pkgCacheGenerator - Constructor                     /*{{{*/
 // ---------------------------------------------------------------------
@@ -98,6 +98,7 @@ pkgCacheGenerator::~pkgCacheGenerator()
       return;
    
    Cache.HeaderP->Dirty = false;
+   Cache.HeaderP->CacheFileSize = Map.Size();
    Map.Sync(0,sizeof(pkgCache::Header));
 }
                                                                        /*}}}*/
@@ -105,6 +106,9 @@ void pkgCacheGenerator::ReMap(void const * const oldMap, void const * const newM
    if (oldMap == newMap)
       return;
 
+   if (_config->FindB("Debug::pkgCacheGen", false))
+      std::clog << "Remaping from " << oldMap << " to " << newMap << std::endl;
+
    Cache.ReMap(false);
 
    CurrentFile += (pkgCache::PackageFile*) newMap - (pkgCache::PackageFile*) oldMap;
@@ -113,27 +117,27 @@ void pkgCacheGenerator::ReMap(void const * const oldMap, void const * const newM
       if (UniqHash[i] != 0)
         UniqHash[i] += (pkgCache::StringItem*) newMap - (pkgCache::StringItem*) oldMap;
 
-   for (std::set<pkgCache::GrpIterator*>::const_iterator i = Dynamic<pkgCache::GrpIterator>::toReMap.begin();
+   for (std::vector<pkgCache::GrpIterator*>::const_iterator i = Dynamic<pkgCache::GrpIterator>::toReMap.begin();
        i != Dynamic<pkgCache::GrpIterator>::toReMap.end(); ++i)
-      (*i)->ReOwn(Cache, oldMap, newMap);
-   for (std::set<pkgCache::PkgIterator*>::const_iterator i = Dynamic<pkgCache::PkgIterator>::toReMap.begin();
+      (*i)->ReMap(oldMap, newMap);
+   for (std::vector<pkgCache::PkgIterator*>::const_iterator i = Dynamic<pkgCache::PkgIterator>::toReMap.begin();
        i != Dynamic<pkgCache::PkgIterator>::toReMap.end(); ++i)
-      (*i)->ReOwn(Cache, oldMap, newMap);
-   for (std::set<pkgCache::VerIterator*>::const_iterator i = Dynamic<pkgCache::VerIterator>::toReMap.begin();
+      (*i)->ReMap(oldMap, newMap);
+   for (std::vector<pkgCache::VerIterator*>::const_iterator i = Dynamic<pkgCache::VerIterator>::toReMap.begin();
        i != Dynamic<pkgCache::VerIterator>::toReMap.end(); ++i)
-      (*i)->ReOwn(Cache, oldMap, newMap);
-   for (std::set<pkgCache::DepIterator*>::const_iterator i = Dynamic<pkgCache::DepIterator>::toReMap.begin();
+      (*i)->ReMap(oldMap, newMap);
+   for (std::vector<pkgCache::DepIterator*>::const_iterator i = Dynamic<pkgCache::DepIterator>::toReMap.begin();
        i != Dynamic<pkgCache::DepIterator>::toReMap.end(); ++i)
-      (*i)->ReOwn(Cache, oldMap, newMap);
-   for (std::set<pkgCache::DescIterator*>::const_iterator i = Dynamic<pkgCache::DescIterator>::toReMap.begin();
+      (*i)->ReMap(oldMap, newMap);
+   for (std::vector<pkgCache::DescIterator*>::const_iterator i = Dynamic<pkgCache::DescIterator>::toReMap.begin();
        i != Dynamic<pkgCache::DescIterator>::toReMap.end(); ++i)
-      (*i)->ReOwn(Cache, oldMap, newMap);
-   for (std::set<pkgCache::PrvIterator*>::const_iterator i = Dynamic<pkgCache::PrvIterator>::toReMap.begin();
+      (*i)->ReMap(oldMap, newMap);
+   for (std::vector<pkgCache::PrvIterator*>::const_iterator i = Dynamic<pkgCache::PrvIterator>::toReMap.begin();
        i != Dynamic<pkgCache::PrvIterator>::toReMap.end(); ++i)
-      (*i)->ReOwn(Cache, oldMap, newMap);
-   for (std::set<pkgCache::PkgFileIterator*>::const_iterator i = Dynamic<pkgCache::PkgFileIterator>::toReMap.begin();
+      (*i)->ReMap(oldMap, newMap);
+   for (std::vector<pkgCache::PkgFileIterator*>::const_iterator i = Dynamic<pkgCache::PkgFileIterator>::toReMap.begin();
        i != Dynamic<pkgCache::PkgFileIterator>::toReMap.end(); ++i)
-      (*i)->ReOwn(Cache, oldMap, newMap);
+      (*i)->ReMap(oldMap, newMap);
 }                                                                      /*}}}*/
 // CacheGenerator::WriteStringInMap                                    /*{{{*/
 map_ptrloc pkgCacheGenerator::WriteStringInMap(const char *String,
@@ -178,23 +182,12 @@ bool pkgCacheGenerator::MergeList(ListParser &List,
       if (PackageName.empty() == true)
         return false;
 
-      /* As we handle Arch all packages as architecture bounded
-         we add all information to every (simulated) arch package */
-      std::vector<string> genArch;
-      if (List.ArchitectureAll() == true) {
-        genArch = APT::Configuration::getArchitectures();
-        if (genArch.size() != 1)
-           genArch.push_back("all");
-      } else
-        genArch.push_back(List.Architecture());
-
-      for (std::vector<string>::const_iterator arch = genArch.begin();
-          arch != genArch.end(); ++arch)
-      {
+      string const Arch = List.Architecture();
       // Get a pointer to the package structure
       pkgCache::PkgIterator Pkg;
       Dynamic<pkgCache::PkgIterator> DynPkg(Pkg);
-      if (NewPackage(Pkg, PackageName, *arch) == false)
+      if (NewPackage(Pkg, PackageName, Arch) == false)
         return _error->Error(_("Error occurred while processing %s (NewPackage)"),PackageName.c_str());
       Counter++;
       if (Counter % 100 == 0 && Progress != 0)
@@ -351,7 +344,6 @@ bool pkgCacheGenerator::MergeList(ListParser &List,
 
       if ((*LastDesc == 0 && _error->PendingError()) || NewFileDesc(Desc,List) == false)
         return _error->Error(_("Error occurred while processing %s (NewFileDesc2)"),PackageName.c_str());
-      }
    }
 
    FoundFileDeps |= List.HasFileDeps();
@@ -491,7 +483,8 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name
    // Set the name, arch and the ID
    Pkg->Name = Grp->Name;
    Pkg->Group = Grp.Index();
-   map_ptrloc const idxArch = WriteUniqString(Arch.c_str());
+   // all is mapped to the native architecture
+   map_ptrloc const idxArch = (Arch == "all") ? Cache.HeaderP->Architecture : WriteUniqString(Arch.c_str());
    if (unlikely(idxArch == 0))
       return false;
    Pkg->Arch = idxArch;
@@ -638,27 +631,24 @@ bool pkgCacheGenerator::FinishCache(OpProgress *Progress)
         Dynamic<pkgCache::PkgIterator> DynP(P);
         for (; P.end() != true; P = G.NextPkg(P))
         {
-           if (strcmp(P.Arch(),"all") == 0)
-              continue;
            pkgCache::PkgIterator allPkg;
            Dynamic<pkgCache::PkgIterator> DynallPkg(allPkg);
            pkgCache::VerIterator V = P.VersionList();
            Dynamic<pkgCache::VerIterator> DynV(V);
            for (; V.end() != true; V++)
            {
-              string const Arch = V.Arch(true);
+               // copy P.Arch() into a string here as a cache remap
+               // in NewDepends() later may alter the pointer location
+              string Arch = P.Arch() == NULL ? "" : P.Arch();
               map_ptrloc *OldDepLast = NULL;
               /* MultiArch handling introduces a lot of implicit Dependencies:
                - MultiArch: same → Co-Installable if they have the same version
                - Architecture: all → Need to be Co-Installable for internal reasons
                - All others conflict with all other group members */
-              bool const coInstall = (V->MultiArch == pkgCache::Version::All ||
-                                       V->MultiArch == pkgCache::Version::Same);
-              if (V->MultiArch == pkgCache::Version::All && allPkg.end() == true)
-                 allPkg = G.FindPkg("all");
+              bool const coInstall = ((V->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same);
               for (vector<string>::const_iterator A = archs.begin(); A != archs.end(); ++A)
               {
-                 if (*A == Arch)
+                 if (Arch == 0 || *A == Arch)
                     continue;
                  /* We allow only one installed arch at the time
                     per group, therefore each group member conflicts
@@ -677,13 +667,6 @@ bool pkgCacheGenerator::FinishCache(OpProgress *Progress)
                     NewDepends(D, V, V.VerStr(),
                                pkgCache::Dep::NotEquals, pkgCache::Dep::DpkgBreaks,
                                OldDepLast);
-                    if (V->MultiArch == pkgCache::Version::All)
-                    {
-                       // Depend on ${self}:all which does depend on nothing
-                       NewDepends(allPkg, V, V.VerStr(),
-                                  pkgCache::Dep::Equals, pkgCache::Dep::Depends,
-                                  OldDepLast);
-                    }
                  } else {
                        // Conflicts: ${self}:other
                        NewDepends(D, V, "",
@@ -707,7 +690,7 @@ bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator &Pkg,
                                   string const &Version,
                                   unsigned int const &Op,
                                   unsigned int const &Type,
-                                  map_ptrloc *OldDepLast)
+                                  map_ptrloc* &OldDepLast)
 {
    void const * const oldMap = Map.Data();
    // Get a structure
@@ -806,7 +789,8 @@ bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator &Ver,
    pkgCache &Cache = Owner->Cache;
 
    // We do not add self referencing provides
-   if (Ver.ParentPkg().Name() == PkgName && PkgArch == Ver.Arch(true))
+   if (Ver.ParentPkg().Name() == PkgName && (PkgArch == Ver.ParentPkg().Arch() ||
+       (PkgArch == "all" && strcmp((Cache.StrP + Cache.HeaderP->Architecture), Ver.ParentPkg().Arch()) == 0)))
       return true;
    
    // Get a structure
@@ -937,8 +921,11 @@ unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
 /* 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. */
-static bool CheckValidity(const string &CacheFile, FileIterator Start, 
-                          FileIterator End,MMap **OutMap = 0)
+static bool CheckValidity(const string &CacheFile, 
+                          pkgSourceList &List,
+                          FileIterator Start, 
+                          FileIterator End,
+                          MMap **OutMap = 0)
 {
    bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
    // No file, certainly invalid
@@ -949,6 +936,13 @@ static bool CheckValidity(const string &CacheFile, FileIterator Start,
       return false;
    }
 
+   if (List.GetLastModifiedTime() < GetModificationTime(CacheFile))
+   {
+      if (Debug == true)
+        std::clog << "sources.list is newer than the cache" << std::endl;
+      return false;
+   }
+
    // Map it
    FileFd CacheF(CacheFile,FileFd::ReadOnly);
    SPtr<MMap> Map = new MMap(CacheF,0);
@@ -1094,6 +1088,20 @@ static bool BuildCache(pkgCacheGenerator &Gen,
    return true;
 }
                                                                        /*}}}*/
+// CacheGenerator::CreateDynamicMMap - load an mmap with configuration options /*{{{*/
+DynamicMMap* pkgCacheGenerator::CreateDynamicMMap(FileFd *CacheF, unsigned long Flags) {
+   unsigned long const MapStart = _config->FindI("APT::Cache-Start", 24*1024*1024);
+   unsigned long const MapGrow = _config->FindI("APT::Cache-Grow", 1*1024*1024);
+   unsigned long const MapLimit = _config->FindI("APT::Cache-Limit", 0);
+   Flags |= MMap::Moveable;
+   if (_config->FindB("APT::Cache-Fallback", false) == true)
+      Flags |= MMap::Fallback;
+   if (CacheF != NULL)
+      return new DynamicMMap(*CacheF, Flags, MapStart, MapGrow, MapLimit);
+   else
+      return new DynamicMMap(Flags, MapStart, MapGrow, MapLimit);
+}
+                                                                       /*}}}*/
 // CacheGenerator::MakeStatusCache - Construct the status cache                /*{{{*/
 // ---------------------------------------------------------------------
 /* This makes sure that the status cache (the cache that has all 
@@ -1109,7 +1117,6 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
                        MMap **OutMap,bool AllowMem)
 {
    bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
-   unsigned long const MapSize = _config->FindI("APT::Cache-Limit",24*1024*1024);
    
    vector<pkgIndexFile *> Files;
    for (vector<metaIndex *>::const_iterator i = List.begin();
@@ -1161,7 +1168,7 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
       Progress->OverallProgress(0,1,1,_("Reading package lists"));
 
    // Cache is OK, Fin.
-   if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
+   if (CheckValidity(CacheFile, List, Files.begin(),Files.end(),OutMap) == true)
    {
       if (Progress != NULL)
         Progress->OverallProgress(1,1,1,_("Reading package lists"));
@@ -1178,19 +1185,35 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
    SPtr<DynamicMMap> Map;
    if (Writeable == true && CacheFile.empty() == false)
    {
+      _error->PushToStack();
       unlink(CacheFile.c_str());
-      CacheF = new FileFd(CacheFile,FileFd::WriteEmpty);
+      CacheF = new FileFd(CacheFile,FileFd::WriteAtomic);
       fchmod(CacheF->Fd(),0644);
-      Map = new DynamicMMap(*CacheF,MMap::Public | MMap::Moveable, MapSize);
+      Map = CreateDynamicMMap(CacheF, MMap::Public);
       if (_error->PendingError() == true)
-        return false;
-      if (Debug == true)
+      {
+        delete CacheF.UnGuard();
+        delete Map.UnGuard();
+        if (Debug == true)
+           std::clog << "Open filebased MMap FAILED" << std::endl;
+        Writeable = false;
+        if (AllowMem == false)
+        {
+           _error->MergeWithStack();
+           return false;
+        }
+        _error->RevertToStack();
+      }
+      else if (Debug == true)
+      {
+        _error->MergeWithStack();
         std::clog << "Open filebased MMap" << std::endl;
+      }
    }
-   else
+   if (Writeable == false || CacheFile.empty() == true)
    {
       // Just build it in memory..
-      Map = new DynamicMMap(MMap::Moveable, MapSize);
+      Map = CreateDynamicMMap(NULL);
       if (Debug == true)
         std::clog << "Open memory Map (not filebased)" << std::endl;
    }
@@ -1198,7 +1221,7 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
    // Lets try the source cache.
    unsigned long CurrentSize = 0;
    unsigned long TotalSize = 0;
-   if (CheckValidity(SrcCacheFile,Files.begin(),
+   if (CheckValidity(SrcCacheFile, List, Files.begin(),
                     Files.begin()+EndOfSource) == true)
    {
       if (Debug == true)
@@ -1241,7 +1264,7 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
       // Write it back
       if (Writeable == true && SrcCacheFile.empty() == false)
       {
-        FileFd SCacheF(SrcCacheFile,FileFd::WriteEmpty);
+        FileFd SCacheF(SrcCacheFile,FileFd::WriteAtomic);
         if (_error->PendingError() == true)
            return false;
         
@@ -1297,13 +1320,12 @@ __deprecated bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutM
    { return pkgCacheGenerator::MakeOnlyStatusCache(&Progress, OutMap); }
 bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap)
 {
-   unsigned long MapSize = _config->FindI("APT::Cache-Limit",20*1024*1024);
    vector<pkgIndexFile *> Files;
    unsigned long EndOfSource = Files.size();
    if (_system->AddStatusFiles(Files) == false)
       return false;
-   
-   SPtr<DynamicMMap> Map = new DynamicMMap(MMap::Moveable, MapSize);
+
+   SPtr<DynamicMMap> Map = CreateDynamicMMap(NULL);
    unsigned long CurrentSize = 0;
    unsigned long TotalSize = 0;