]> git.saurik.com Git - apt.git/blobdiff - apt-pkg/pkgcachegen.cc
* apt-pkg/cachefile.{cc,h}:
[apt.git] / apt-pkg / pkgcachegen.cc
index 3d1a654d1af65ca3071919bcead5ad1517588a42..5649cd6f8c41c67b6654d65d67a6dbbddcf99dbb 100644 (file)
@@ -189,17 +189,22 @@ bool pkgCacheGenerator::MergeList(ListParser &List,
       pkgCache::VerIterator Ver = Pkg.VersionList();
       map_ptrloc *LastVer = &Pkg->VersionList;
       int Res = 1;
+      unsigned long const Hash = List.VersionHash();
       for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
       {
         Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
-        if (Res >= 0)
+        // Version is higher as current version - insert here
+        if (Res > 0)
            break;
+        // Versionstrings are equal - is hash also equal?
+        if (Res == 0 && Ver->Hash == Hash)
+           break;
+        // proceed with the next till we have either the right
+        // or we found another version (which will be lower)
       }
-      
-      /* We already have a version for this item, record that we
-         saw it */
-      unsigned long Hash = List.VersionHash();
-      if (Res == 0 && Ver->Hash == Hash)
+
+      /* We already have a version for this item, record that we saw it */
+      if (Res == 0 && Ver.end() == false && Ver->Hash == Hash)
       {
         if (List.UsePackage(Pkg,Ver) == false)
            return _error->Error(_("Error occurred while processing %s (UsePackage2)"),
@@ -218,17 +223,6 @@ bool pkgCacheGenerator::MergeList(ListParser &List,
         }
         
         continue;
-      }      
-
-      // Skip to the end of the same version set.
-      if (Res == 0)
-      {
-        for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
-        {
-           Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
-           if (Res != 0)
-              break;
-        }
       }
 
       // Add a new version
@@ -341,29 +335,29 @@ bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
 // CacheGenerator::NewGroup - Add a new group                          /*{{{*/
 // ---------------------------------------------------------------------
 /* This creates a new group structure and adds it to the hash table */
-bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, const string &Name) {
-       Grp = Cache.FindGrp(Name);
-       if (Grp.end() == false)
-               return true;
-
-       // Get a structure
-       unsigned long const Group = Map.Allocate(sizeof(pkgCache::Group));
-       if (unlikely(Group == 0))
-               return false;
+bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator &Grp, const string &Name)
+{
+   Grp = Cache.FindGrp(Name);
+   if (Grp.end() == false)
+      return true;
 
-       Grp = pkgCache::GrpIterator(Cache, Cache.GrpP + Group);
-       Grp->Name = Map.WriteString(Name);
-       if (unlikely(Grp->Name == 0))
-               return false;
+   // Get a structure
+   unsigned long const Group = Map.Allocate(sizeof(pkgCache::Group));
+   if (unlikely(Group == 0))
+      return false;
 
-       // Insert it into the hash table
-       unsigned long const Hash = Cache.Hash(Name);
-       Grp->Next = Cache.HeaderP->GrpHashTable[Hash];
-       Cache.HeaderP->GrpHashTable[Hash] = Group;
+   Grp = pkgCache::GrpIterator(Cache, Cache.GrpP + Group);
+   Grp->Name = Map.WriteString(Name);
+   if (unlikely(Grp->Name == 0))
+      return false;
 
-       Cache.HeaderP->GroupCount++;
+   // Insert it into the hash table
+   unsigned long const Hash = Cache.Hash(Name);
+   Grp->Next = Cache.HeaderP->GrpHashTable[Hash];
+   Cache.HeaderP->GrpHashTable[Hash] = Group;
 
-       return true;
+   Grp->ID = Cache.HeaderP->GroupCount++;
+   return true;
 }
                                                                        /*}}}*/
 // CacheGenerator::NewPackage - Add a new package                      /*{{{*/
@@ -385,15 +379,23 @@ bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name
       return false;
    Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
 
-   // Insert it into the hash table
-   unsigned long const Hash = Cache.Hash(Name);
-   Pkg->NextPackage = Cache.HeaderP->PkgHashTable[Hash];
-   Cache.HeaderP->PkgHashTable[Hash] = Package;
-
-   // remember the packages in the group
-   Grp->FirstPackage = Package;
-   if (Grp->LastPackage == 0)
-      Grp->LastPackage = Package;
+   // Insert the package into our package list
+   if (Grp->FirstPackage == 0) // the group is new
+   {
+      // Insert it into the hash table
+      unsigned long const Hash = Cache.Hash(Name);
+      Pkg->NextPackage = Cache.HeaderP->PkgHashTable[Hash];
+      Cache.HeaderP->PkgHashTable[Hash] = Package;
+      Grp->FirstPackage = Package;
+   }
+   else // Group the Packages together
+   {
+      // this package is the new last package
+      pkgCache::PkgIterator LastPkg(Cache, Cache.PkgP + Grp->LastPackage);
+      Pkg->NextPackage = LastPkg->NextPackage;
+      LastPkg->NextPackage = Package;
+   }
+   Grp->LastPackage = Package;
 
    // Set the name, arch and the ID
    Pkg->Name = Grp->Name;
@@ -524,68 +526,76 @@ map_ptrloc pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
 // CacheGenerator::FinishCache - do various finish operations          /*{{{*/
 // ---------------------------------------------------------------------
 /* This prepares the Cache for delivery */
-bool pkgCacheGenerator::FinishCache(OpProgress &Progress) {
-       // FIXME: add progress reporting for this operation
-       // Do we have different architectures in your groups ?
-       vector<string> archs = APT::Configuration::getArchitectures();
-       if (archs.size() > 1) {
-               // Create Conflicts in between the group
-               for (pkgCache::GrpIterator G = GetCache().GrpBegin(); G.end() != true; G++) {
-                       string const PkgName = G.Name();
-                       for (pkgCache::PkgIterator P = G.PackageList(); P.end() != true; P = G.NextPkg(P)) {
-                               if (strcmp(P.Arch(),"all") == 0)
-                                       continue;
-                               pkgCache::PkgIterator allPkg;
-                               for (pkgCache::VerIterator V = P.VersionList(); V.end() != true; V++) {
-                                       string const Arch = V.Arch(true);
-                                       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");
-                                       for (vector<string>::const_iterator A = archs.begin(); A != archs.end(); ++A) {
-                                               if (*A == Arch)
-                                                       continue;
-                                               /* We allow only one installed arch at the time
-                                                  per group, therefore each group member conflicts
-                                                  with all other group members */
-                                               pkgCache::PkgIterator D = G.FindPkg(*A);
-                                               if (D.end() == true)
-                                                       continue;
-                                               if (coInstall == true) {
-                                                       // Replaces: ${self}:other ( << ${binary:Version})
-                                                       NewDepends(D, V, V.VerStr(),
-                                                                  pkgCache::Dep::Less, pkgCache::Dep::Replaces,
-                                                                  OldDepLast);
-                                                       // Breaks: ${self}:other (!= ${binary:Version})
-                                                       NewDepends(D, V, V.VerStr(),
-                                                                  pkgCache::Dep::Less, pkgCache::Dep::DpkgBreaks,
-                                                                  OldDepLast);
-                                                       NewDepends(D, V, V.VerStr(),
-                                                                  pkgCache::Dep::Greater, 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, "",
-                                                                  pkgCache::Dep::NoOp, pkgCache::Dep::Conflicts,
-                                                                  OldDepLast);
-                                               }
-                                       }
-                               }
-                       }
-               }
-       }
-       return true;
+bool pkgCacheGenerator::FinishCache(OpProgress *Progress)
+{
+   // FIXME: add progress reporting for this operation
+   // Do we have different architectures in your groups ?
+   vector<string> archs = APT::Configuration::getArchitectures();
+   if (archs.size() > 1)
+   {
+      // Create Conflicts in between the group
+      for (pkgCache::GrpIterator G = GetCache().GrpBegin(); G.end() != true; G++)
+      {
+        string const PkgName = G.Name();
+        for (pkgCache::PkgIterator P = G.PackageList(); P.end() != true; P = G.NextPkg(P))
+        {
+           if (strcmp(P.Arch(),"all") == 0)
+              continue;
+           pkgCache::PkgIterator allPkg;
+           for (pkgCache::VerIterator V = P.VersionList(); V.end() != true; V++)
+           {
+              string const Arch = V.Arch(true);
+              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");
+              for (vector<string>::const_iterator A = archs.begin(); A != archs.end(); ++A)
+              {
+                 if (*A == Arch)
+                    continue;
+                 /* We allow only one installed arch at the time
+                    per group, therefore each group member conflicts
+                    with all other group members */
+                 pkgCache::PkgIterator D = G.FindPkg(*A);
+                 if (D.end() == true)
+                    continue;
+                 if (coInstall == true)
+                 {
+                    // Replaces: ${self}:other ( << ${binary:Version})
+                    NewDepends(D, V, V.VerStr(),
+                               pkgCache::Dep::Less, pkgCache::Dep::Replaces,
+                               OldDepLast);
+                    // Breaks: ${self}:other (!= ${binary:Version})
+                    NewDepends(D, V, V.VerStr(),
+                               pkgCache::Dep::Less, pkgCache::Dep::DpkgBreaks,
+                               OldDepLast);
+                    NewDepends(D, V, V.VerStr(),
+                               pkgCache::Dep::Greater, 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, "",
+                                  pkgCache::Dep::NoOp, pkgCache::Dep::Conflicts,
+                                  OldDepLast);
+                 }
+              }
+           }
+        }
+      }
+   }
+   return true;
 }
                                                                        /*}}}*/
 // CacheGenerator::NewDepends - Create a dependency element            /*{{{*/
@@ -913,7 +923,7 @@ static unsigned long ComputeSize(FileIterator Start,FileIterator End)
 // ---------------------------------------------------------------------
 /* */
 static bool BuildCache(pkgCacheGenerator &Gen,
-                      OpProgress &Progress,
+                      OpProgress *Progress,
                       unsigned long &CurrentSize,unsigned long TotalSize,
                       FileIterator Start, FileIterator End)
 {
@@ -934,7 +944,8 @@ static bool BuildCache(pkgCacheGenerator &Gen,
       }
       
       unsigned long Size = (*I)->Size();
-      Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
+      if (Progress != NULL)
+        Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
       CurrentSize += Size;
       
       if ((*I)->Merge(Gen,Progress) == false)
@@ -943,13 +954,15 @@ static bool BuildCache(pkgCacheGenerator &Gen,
 
    if (Gen.HasFileDeps() == true)
    {
-      Progress.Done();
+      if (Progress != NULL)
+        Progress->Done();
       TotalSize = ComputeSize(Start, End);
       CurrentSize = 0;
       for (I = Start; I != End; I++)
       {
         unsigned long Size = (*I)->Size();
-        Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
+        if (Progress != NULL)
+           Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
         CurrentSize += Size;
         if ((*I)->MergeFileProvides(Gen,Progress) == false)
            return false;
@@ -959,7 +972,7 @@ static bool BuildCache(pkgCacheGenerator &Gen,
    return true;
 }
                                                                        /*}}}*/
-// MakeStatusCache - Construct the status cache                                /*{{{*/
+// CacheGenerator::MakeStatusCache - Construct the status cache                /*{{{*/
 // ---------------------------------------------------------------------
 /* This makes sure that the status cache (the cache that has all 
    index files from the sources list and all local ones) is ready
@@ -967,7 +980,10 @@ static bool BuildCache(pkgCacheGenerator &Gen,
    the cache will be stored there. This is pretty much mandetory if you
    are using AllowMem. AllowMem lets the function be run as non-root
    where it builds the cache 'fast' into a memory buffer. */
-bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
+__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)
 {
    bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
@@ -992,7 +1008,20 @@ bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
    // Decide if we can write to the files..
    string const CacheFile = _config->FindFile("Dir::Cache::pkgcache");
    string const SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
-   
+
+   // ensure the cache directory exists
+   if (CacheFile.empty() == false || SrcCacheFile.empty() == false)
+   {
+      string dir = _config->FindDir("Dir::Cache");
+      size_t const len = dir.size();
+      if (len > 5 && dir.find("/apt/", len - 6, 5) == len - 5)
+        dir = dir.substr(0, len - 5);
+      if (CacheFile.empty() == false)
+        CreateDirectory(dir, flNotFile(CacheFile));
+      if (SrcCacheFile.empty() == false)
+        CreateDirectory(dir, flNotFile(SrcCacheFile));
+   }
+
    // Decide if we can write to the cache
    bool Writeable = false;
    if (CacheFile.empty() == false)
@@ -1005,13 +1034,15 @@ bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
 
    if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
       return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
-   
-   Progress.OverallProgress(0,1,1,_("Reading package lists"));
-   
+
+   if (Progress != NULL)
+      Progress->OverallProgress(0,1,1,_("Reading package lists"));
+
    // Cache is OK, Fin.
    if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
    {
-      Progress.OverallProgress(1,1,1,_("Reading package lists"));
+      if (Progress != NULL)
+        Progress->OverallProgress(1,1,1,_("Reading package lists"));
       if (Debug == true)
         std::clog << "pkgcache.bin is valid - no need to build anything" << std::endl;
       return true;
@@ -1061,7 +1092,7 @@ bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
       TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
 
       // Build the status cache
-      pkgCacheGenerator Gen(Map.Get(),&Progress);
+      pkgCacheGenerator Gen(Map.Get(),Progress);
       if (_error->PendingError() == true)
         return false;
       if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
@@ -1078,7 +1109,7 @@ bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
       TotalSize = ComputeSize(Files.begin(),Files.end());
       
       // Build the source cache
-      pkgCacheGenerator Gen(Map.Get(),&Progress);
+      pkgCacheGenerator Gen(Map.Get(),Progress);
       if (_error->PendingError() == true)
         return false;
       if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
@@ -1137,10 +1168,12 @@ bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
    return true;
 }
                                                                        /*}}}*/
-// MakeOnlyStatusCache - Build a cache with just the status files      /*{{{*/
+// CacheGenerator::MakeOnlyStatusCache - Build only a status files cache/*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
+__deprecated bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
+   { 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;
@@ -1155,8 +1188,9 @@ bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
    TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
    
    // Build the status cache
-   Progress.OverallProgress(0,1,1,_("Reading package lists"));
-   pkgCacheGenerator Gen(Map.Get(),&Progress);
+   if (Progress != NULL)
+      Progress->OverallProgress(0,1,1,_("Reading package lists"));
+   pkgCacheGenerator Gen(Map.Get(),Progress);
    if (_error->PendingError() == true)
       return false;
    if (BuildCache(Gen,Progress,CurrentSize,TotalSize,