]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/pkgcachegen.cc
follow the googletest merge in build-depends
[apt.git] / apt-pkg / pkgcachegen.cc
index 120b0c414df73ccf750bb41f9399a7c5f48a1ca2..107f1f1413b16763fe530a703e67ca70b3cc248a 100644 (file)
 #include <unistd.h>
 
 #include <apti18n.h>
-
-template<class T> using Dynamic = pkgCacheGenerator::Dynamic<T>;                                                       /*}}}*/
+                                                                       /*}}}*/
+template<class T> using Dynamic = pkgCacheGenerator::Dynamic<T>;
 typedef std::vector<pkgIndexFile *>::iterator FileIterator;
 template <typename Iter> std::vector<Iter*> pkgCacheGenerator::Dynamic<Iter>::toReMap;
 
-static bool IsDuplicateDescription(pkgCache::DescIterator Desc,
-                           MD5SumValue const &CurMd5, std::string const &CurLang);
+static bool IsDuplicateDescription(pkgCache &Cache, pkgCache::DescIterator Desc,
+                           APT::StringView CurMd5, std::string const &CurLang);
 
 using std::string;
 using APT::StringView;
@@ -57,6 +57,9 @@ using APT::StringView;
 pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
                    Map(*pMap), Cache(pMap,false), Progress(Prog),
                     CurrentRlsFile(NULL), CurrentFile(NULL), d(NULL)
+{
+}
+bool pkgCacheGenerator::Start()
 {
    if (Map.Size() == 0)
    {
@@ -67,7 +70,9 @@ pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
       bool const newError = _error->PendingError();
       _error->MergeWithStack();
       if (newError)
-        return;
+        return false;
+      if (Map.Size() <= 0)
+        return false;
 
       Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
 
@@ -76,16 +81,15 @@ pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
 
       // make room for the hashtables for packages and groups
       if (Map.RawAllocate(2 * (Cache.HeaderP->GetHashTableSize() * sizeof(map_pointer_t))) == 0)
-        return;
+        return false;
 
       map_stringitem_t const idxVerSysName = WriteStringInMap(_system->VS->Label);
       if (unlikely(idxVerSysName == 0))
-        return;
-      Cache.HeaderP->VerSysName = idxVerSysName;
+        return false;
       map_stringitem_t const idxArchitecture = StoreString(MIXED, _config->Find("APT::Architecture"));
       if (unlikely(idxArchitecture == 0))
-        return;
-      Cache.HeaderP->Architecture = idxArchitecture;
+        return false;
+      map_stringitem_t idxArchitectures;
 
       std::vector<std::string> archs = APT::Configuration::getArchitectures();
       if (archs.size() > 1)
@@ -94,13 +98,17 @@ pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
         std::string list = *a;
         for (++a; a != archs.end(); ++a)
            list.append(",").append(*a);
-        map_stringitem_t const idxArchitectures = WriteStringInMap(list);
+        idxArchitectures = WriteStringInMap(list);
         if (unlikely(idxArchitectures == 0))
-           return;
-        Cache.HeaderP->SetArchitectures(idxArchitectures);
+           return false;
       }
       else
-        Cache.HeaderP->SetArchitectures(idxArchitecture);
+        idxArchitectures = idxArchitecture;
+
+      Cache.HeaderP = (pkgCache::Header *)Map.Data();
+      Cache.HeaderP->VerSysName = idxVerSysName;
+      Cache.HeaderP->Architecture = idxArchitecture;
+      Cache.HeaderP->SetArchitectures(idxArchitectures);
 
       // Calculate the hash for the empty map, so ReMap does not fail
       Cache.HeaderP->CacheFileSize = Cache.CacheHash();
@@ -112,14 +120,12 @@ pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
       Cache.ReMap(); 
       Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
       if (Cache.VS != _system->VS)
-      {
-        _error->Error(_("Cache has an incompatible versioning system"));
-        return;
-      }
+        return _error->Error(_("Cache has an incompatible versioning system"));
    }
 
    Cache.HeaderP->Dirty = true;
    Map.Sync(0,sizeof(pkgCache::Header));
+   return true;
 }
                                                                        /*}}}*/
 // CacheGenerator::~pkgCacheGenerator - Destructor                     /*{{{*/
@@ -142,6 +148,10 @@ pkgCacheGenerator::~pkgCacheGenerator()
 }
                                                                        /*}}}*/
 void pkgCacheGenerator::ReMap(void const * const oldMap, void const * const newMap, size_t oldSize) {/*{{{*/
+   // Prevent multiple remaps of the same iterator. If seen.insert(iterator)
+   // returns (something, true) the iterator was not yet seen and we can
+   // remap it.
+   std::unordered_set<void *> seen;
    if (oldMap == newMap)
       return;
 
@@ -155,29 +165,39 @@ void pkgCacheGenerator::ReMap(void const * const oldMap, void const * const newM
 
    for (std::vector<pkgCache::GrpIterator*>::const_iterator i = Dynamic<pkgCache::GrpIterator>::toReMap.begin();
        i != Dynamic<pkgCache::GrpIterator>::toReMap.end(); ++i)
-      (*i)->ReMap(oldMap, newMap);
+      if (std::get<1>(seen.insert(*i)) == true)
+        (*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)->ReMap(oldMap, newMap);
+      if (std::get<1>(seen.insert(*i)) == true)
+        (*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)->ReMap(oldMap, newMap);
+      if (std::get<1>(seen.insert(*i)) == true)
+        (*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)->ReMap(oldMap, newMap);
+       if (std::get<1>(seen.insert(*i)) == true)
+        (*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)->ReMap(oldMap, newMap);
+      if (std::get<1>(seen.insert(*i)) == true)
+        (*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)->ReMap(oldMap, newMap);
+      if (std::get<1>(seen.insert(*i)) == true)
+        (*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)->ReMap(oldMap, newMap);
+      if (std::get<1>(seen.insert(*i)) == true)
+        (*i)->ReMap(oldMap, newMap);
    for (std::vector<pkgCache::RlsFileIterator*>::const_iterator i = Dynamic<pkgCache::RlsFileIterator>::toReMap.begin();
        i != Dynamic<pkgCache::RlsFileIterator>::toReMap.end(); ++i)
-      (*i)->ReMap(oldMap, newMap);
+      if (std::get<1>(seen.insert(*i)) == true)
+        (*i)->ReMap(oldMap, newMap);
    for (APT::StringView* ViewP : Dynamic<APT::StringView>::toReMap) {
+      if (std::get<1>(seen.insert(ViewP)) == false)
+        continue;
       // Ignore views outside of the cache.
       if (ViewP->data() < static_cast<const char*>(oldMap)
         || ViewP->data() > static_cast<const char*>(oldMap) + oldSize)
@@ -236,8 +256,10 @@ bool pkgCacheGenerator::MergeList(ListParser &List,
       if (Counter % 100 == 0 && Progress != 0)
         Progress->Progress(List.Offset());
 
-      string Arch = List.Architecture();
-      string const Version = List.Version();
+      APT::StringView Arch = List.Architecture();
+      Dynamic<APT::StringView> DynArch(Arch);
+      APT::StringView Version = List.Version();
+      Dynamic<APT::StringView> DynVersion(Version);
       if (Version.empty() == true && Arch.empty() == true)
       {
         // package descriptions
@@ -318,14 +340,14 @@ bool pkgCacheGenerator::MergeListPackage(ListParser &List, pkgCache::PkgIterator
                           Pkg.Name(), "UsePackage", 1);
 
    // Find the right version to write the description
-   MD5SumValue CurMd5 = List.Description_md5();
+   StringView CurMd5 = List.Description_md5();
    std::vector<std::string> availDesc = List.AvailableDescriptionLanguages();
    for (Ver = Pkg.VersionList(); Ver.end() == false; ++Ver)
    {
       pkgCache::DescIterator VerDesc = Ver.DescriptionList();
 
       // a version can only have one md5 describing it
-      if (VerDesc.end() == true || MD5SumValue(VerDesc.md5()) != CurMd5)
+      if (VerDesc.end() == true || Cache.ViewString(VerDesc->md5sum) != CurMd5)
         continue;
 
       map_stringitem_t md5idx = VerDesc->md5sum;
@@ -333,7 +355,7 @@ bool pkgCacheGenerator::MergeListPackage(ListParser &List, pkgCache::PkgIterator
       {
         // don't add a new description if we have one for the given
         // md5 && language
-        if (IsDuplicateDescription(VerDesc, CurMd5, *CurLang) == true)
+        if (IsDuplicateDescription(Cache, VerDesc, CurMd5, *CurLang) == true)
            continue;
 
         AddNewDescription(List, Ver, *CurLang, CurMd5, md5idx);
@@ -348,7 +370,7 @@ bool pkgCacheGenerator::MergeListPackage(ListParser &List, pkgCache::PkgIterator
                                                                        /*}}}*/
 // CacheGenerator::MergeListVersion                                    /*{{{*/
 bool pkgCacheGenerator::MergeListVersion(ListParser &List, pkgCache::PkgIterator &Pkg,
-                                        std::string const &Version, pkgCache::VerIterator* &OutVer)
+                                        APT::StringView const &Version, pkgCache::VerIterator* &OutVer)
 {
    pkgCache::VerIterator Ver = Pkg.VersionList();
    Dynamic<pkgCache::VerIterator> DynVer(Ver);
@@ -363,13 +385,28 @@ bool pkgCacheGenerator::MergeListVersion(ListParser &List, pkgCache::PkgIterator
       int Res = 1;
       for (; Ver.end() == false; LastVer = &Ver->NextVer, ++Ver)
       {
-        Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
+        char const * const VerStr = Ver.VerStr();
+        Res = Cache.VS->DoCmpVersion(Version.data(), Version.data() + Version.length(),
+              VerStr, VerStr + strlen(VerStr));
         // Version is higher as current version - insert here
         if (Res > 0)
            break;
         // Versionstrings are equal - is hash also equal?
-        if (Res == 0 && List.SameVersion(Hash, Ver) == true)
-           break;
+        if (Res == 0)
+        {
+           if (List.SameVersion(Hash, Ver) == true)
+              break;
+           // sort (volatile) sources above not-sources like the status file
+           if ((CurrentFile->Flags & pkgCache::Flag::NotSource) == 0)
+           {
+              auto VF = Ver.FileList();
+              for (; VF.end() == false; ++VF)
+                 if (VF.File().Flagged(pkgCache::Flag::NotSource) == false)
+                    break;
+              if (VF.end() == true)
+                 break;
+           }
+        }
         // proceed with the next till we have either the right
         // or we found another version (which will be lower)
       }
@@ -452,7 +489,7 @@ bool pkgCacheGenerator::MergeListVersion(ListParser &List, pkgCache::PkgIterator
    }
 
    /* Record the Description(s) based on their master md5sum */
-   MD5SumValue CurMd5 = List.Description_md5();
+   StringView CurMd5 = List.Description_md5();
 
    /* Before we add a new description we first search in the group for
       a version with a description of the same MD5 - if so we reuse this
@@ -463,7 +500,7 @@ bool pkgCacheGenerator::MergeListVersion(ListParser &List, pkgCache::PkgIterator
       for (pkgCache::VerIterator V = P.VersionList();
           V.end() == false; ++V)
       {
-        if (V->DescriptionList == 0 || MD5SumValue(V.DescriptionList().md5()) != CurMd5)
+        if (V->DescriptionList == 0 || Cache.ViewString(V.DescriptionList()->md5sum) != CurMd5)
            continue;
         Ver->DescriptionList = V->DescriptionList;
       }
@@ -478,7 +515,7 @@ bool pkgCacheGenerator::MergeListVersion(ListParser &List, pkgCache::PkgIterator
    return true;
 }
                                                                        /*}}}*/
-bool pkgCacheGenerator::AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver, std::string const &lang, MD5SumValue const &CurMd5, map_stringitem_t &md5idx) /*{{{*/
+bool pkgCacheGenerator::AddNewDescription(ListParser &List, pkgCache::VerIterator &Ver, std::string const &lang, APT::StringView CurMd5, map_stringitem_t &md5idx) /*{{{*/
 {
    pkgCache::DescIterator Desc;
    Dynamic<pkgCache::DescIterator> DynDesc(Desc);
@@ -531,7 +568,7 @@ bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, StringView Name)
    unsigned long const Hash = Cache.Hash(Name);
    map_pointer_t *insertAt = &Cache.HeaderP->GrpHashTableP()[Hash];
 
-   while (*insertAt != 0 && Name.compare(Cache.ViewString((Cache.GrpP + *insertAt)->Name)) > 0)
+   while (*insertAt != 0 && StringViewCompareFast(Name, Cache.ViewString((Cache.GrpP + *insertAt)->Name)) > 0)
       insertAt = &(Cache.GrpP + *insertAt)->Next;
    Grp->Next = *insertAt;
    *insertAt = Group;
@@ -579,7 +616,7 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg, StringView Name,
       // Insert it into the hash table
       map_id_t const Hash = Cache.Hash(Name);
       map_pointer_t *insertAt = &Cache.HeaderP->PkgHashTableP()[Hash];
-      while (*insertAt != 0 && Name.compare(Cache.StrP + (Cache.GrpP + (Cache.PkgP + *insertAt)->Group)->Name) > 0)
+      while (*insertAt != 0 && StringViewCompareFast(Name, Cache.ViewString((Cache.GrpP + (Cache.PkgP + *insertAt)->Group)->Name)) > 0)
         insertAt = &(Cache.PkgP + *insertAt)->NextPackage;
       Pkg->NextPackage = *insertAt;
       *insertAt = Package;
@@ -709,9 +746,8 @@ bool pkgCacheGenerator::AddImplicitDepends(pkgCache::GrpIterator &G,
                                           pkgCache::PkgIterator &P,
                                           pkgCache::VerIterator &V)
 {
-   // 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();
+   APT::StringView Arch = P.Arch() == NULL ? "" : P.Arch();
+   Dynamic<APT::StringView> DynArch(Arch);
    map_pointer_t *OldDepLast = NULL;
    /* MultiArch handling introduces a lot of implicit Dependencies:
       - MultiArch: same → Co-Installable if they have the same version
@@ -812,7 +848,7 @@ bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
 // ---------------------------------------------------------------------
 /* This puts a version structure in the linked list */
 map_pointer_t pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
-                                           const string &VerStr,
+                                           APT::StringView const &VerStr,
                                            map_pointer_t const ParentPkg,
                                            unsigned short const Hash,
                                            map_pointer_t const Next)
@@ -841,8 +877,8 @@ map_pointer_t pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
            continue;
         for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
         {
-           int const cmp = strcmp(V.VerStr(), VerStr.c_str());
-           if (cmp == 0)
+           int const cmp = strncmp(V.VerStr(), VerStr.data(), VerStr.length());
+           if (cmp == 0 && V.VerStr()[VerStr.length()] == '\0')
            {
               Ver->VerStr = V->VerStr;
               return Version;
@@ -899,7 +935,7 @@ bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc,
 /* This puts a description structure in the linked list */
 map_pointer_t pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
                                            const string &Lang,
-                                           const MD5SumValue &md5sum,
+                                           APT::StringView md5sum,
                                            map_stringitem_t const idxmd5str)
 {
    // Get a structure
@@ -919,7 +955,7 @@ map_pointer_t pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
       Desc->md5sum = idxmd5str;
    else
    {
-      map_stringitem_t const idxmd5sum = WriteStringInMap(md5sum.Value());
+      map_stringitem_t const idxmd5sum = WriteStringInMap(md5sum);
       if (unlikely(idxmd5sum == 0))
         return 0;
       Desc->md5sum = idxmd5sum;
@@ -1604,7 +1640,7 @@ static bool loadBackMMapFromFile(std::unique_ptr<pkgCacheGenerator> &Gen,
    if (CacheF.Read((unsigned char *)Map->Data() + alloc, CacheF.Size()) == false)
       return false;
    Gen.reset(new pkgCacheGenerator(Map.get(),Progress));
-   return true;
+   return Gen->Start();
 }
 bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
                        MMap **OutMap, bool AllowMem)
@@ -1710,6 +1746,8 @@ bool pkgCacheGenerator::MakeStatusCache(pkgSourceList &List,OpProgress *Progress
       if (Debug == true)
         std::clog << "srcpkgcache.bin is NOT valid - rebuild" << std::endl;
       Gen.reset(new pkgCacheGenerator(Map.get(),Progress));
+      if (Gen->Start() == false)
+        return false;
 
       TotalSize += ComputeSize(&List, Files.begin(),Files.end());
       if (BuildCache(*Gen, Progress, CurrentSize, TotalSize, &List,
@@ -1787,7 +1825,7 @@ bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **O
    if (Progress != NULL)
       Progress->OverallProgress(0,1,1,_("Reading package lists"));
    pkgCacheGenerator Gen(Map.get(),Progress);
-   if (_error->PendingError() == true)
+   if (Gen.Start() == false || _error->PendingError() == true)
       return false;
    if (BuildCache(Gen,Progress,CurrentSize,TotalSize, NULL,
                  Files.begin(), Files.end()) == false)
@@ -1801,11 +1839,11 @@ bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **O
 }
                                                                        /*}}}*/
 // IsDuplicateDescription                                              /*{{{*/
-static bool IsDuplicateDescription(pkgCache::DescIterator Desc,
-                           MD5SumValue const &CurMd5, std::string const &CurLang)
+static bool IsDuplicateDescription(pkgCache &Cache, pkgCache::DescIterator Desc,
+                           APT::StringView CurMd5, std::string const &CurLang)
 {
    // Descriptions in the same link-list have all the same md5
-   if (Desc.end() == true || MD5SumValue(Desc.md5()) != CurMd5)
+   if (Desc.end() == true || Cache.ViewString(Desc->md5sum) != CurMd5)
       return false;
    for (; Desc.end() == false; ++Desc)
       if (Desc.LanguageCode() == CurLang)