+ return 0;
+
+ Bucket = ItemP;
+ return ItemP->String;
+}
+ /*}}}*/
+
+// CheckValidity - Check that a cache is up-to-date /*{{{*/
+// ---------------------------------------------------------------------
+/* 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)
+{
+ // No file, certainly invalid
+ if (CacheFile.empty() == true || FileExists(CacheFile) == false)
+ return false;
+
+ // Map it
+ FileFd CacheF(CacheFile,FileFd::ReadOnly);
+ SPtr<MMap> Map = new MMap(CacheF,MMap::Public | MMap::ReadOnly);
+ pkgCache Cache(Map);
+ if (_error->PendingError() == true || Map->Size() == 0)
+ {
+ _error->Discard();
+ return false;
+ }
+
+ /* Now we check every index file, see if it is in the cache,
+ verify the IMS data and check that it is on the disk too.. */
+ SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
+ memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
+ for (; Start != End; Start++)
+ {
+ if ((*Start)->HasPackages() == false)
+ continue;
+
+ if ((*Start)->Exists() == false)
+ {
+ _error->WarningE("stat",_("Couldn't stat source package list %s"),
+ (*Start)->Describe().c_str());
+ continue;
+ }
+
+ // FindInCache is also expected to do an IMS check.
+ pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
+ if (File.end() == true)
+ return false;
+
+ Visited[File->ID] = true;
+ }
+
+ for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
+ if (Visited[I] == false)
+ return false;
+
+ if (_error->PendingError() == true)
+ {
+ _error->Discard();
+ return false;
+ }
+
+ if (OutMap != 0)
+ *OutMap = Map.UnGuard();
+ return true;
+}
+ /*}}}*/
+// ComputeSize - Compute the total size of a bunch of files /*{{{*/
+// ---------------------------------------------------------------------
+/* Size is kind of an abstract notion that is only used for the progress
+ meter */
+static unsigned long ComputeSize(FileIterator Start,FileIterator End)
+{
+ unsigned long TotalSize = 0;
+ for (; Start != End; Start++)
+ {
+ if ((*Start)->HasPackages() == false)
+ continue;
+ TotalSize += (*Start)->Size();
+ }
+ return TotalSize;
+}
+ /*}}}*/
+// BuildCache - Merge the list of index files into the cache /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+static bool BuildCache(pkgCacheGenerator &Gen,
+ OpProgress &Progress,
+ unsigned long &CurrentSize,unsigned long TotalSize,
+ FileIterator Start, FileIterator End)
+{
+ FileIterator I;
+ for (I = Start; I != End; I++)
+ {
+ if ((*I)->HasPackages() == false)
+ continue;
+
+ if ((*I)->Exists() == false)
+ continue;
+
+ if ((*I)->FindInCache(Gen.GetCache()).end() == false)
+ {
+ _error->Warning("Duplicate sources.list entry %s",
+ (*I)->Describe().c_str());
+ continue;
+ }
+
+ unsigned long Size = (*I)->Size();
+ Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
+ CurrentSize += Size;
+
+ if ((*I)->Merge(Gen,Progress) == false)
+ return false;
+ }
+
+ if (Gen.HasFileDeps() == true)
+ {
+ 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"));
+ CurrentSize += Size;
+ if ((*I)->MergeFileProvides(Gen,Progress) == false)
+ return false;
+ }
+ }
+
+ return true;
+}
+ /*}}}*/
+// 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
+ to be mmaped. If OutMap is not zero then a MMap object representing
+ 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,
+ MMap **OutMap,bool AllowMem)
+{
+ unsigned long MapSize = _config->FindI("APT::Cache-Limit",12*1024*1024);
+
+ vector<pkgIndexFile *> Files;
+ for (vector<metaIndex *>::const_iterator i = List.begin();
+ i != List.end();
+ i++)
+ {
+ vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
+ for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
+ j != Indexes->end();
+ j++)
+ Files.push_back (*j);
+ }
+
+ unsigned long EndOfSource = Files.size();
+ if (_system->AddStatusFiles(Files) == false)
+ return false;
+
+ // Decide if we can write to the files..
+ string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
+ string SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
+
+ // Decide if we can write to the cache
+ bool Writeable = false;
+ if (CacheFile.empty() == false)
+ Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
+ else
+ if (SrcCacheFile.empty() == false)
+ Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
+
+ 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"));
+
+ // Cache is OK, Fin.
+ if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
+ {
+ Progress.OverallProgress(1,1,1,_("Reading package lists"));
+ return true;
+ }
+
+ /* At this point we know we need to reconstruct the package cache,
+ begin. */
+ SPtr<FileFd> CacheF;
+ SPtr<DynamicMMap> Map;
+ if (Writeable == true && CacheFile.empty() == false)
+ {
+ unlink(CacheFile.c_str());
+ CacheF = new FileFd(CacheFile,FileFd::WriteEmpty);
+ fchmod(CacheF->Fd(),0644);
+ Map = new DynamicMMap(*CacheF,MMap::Public,MapSize);
+ if (_error->PendingError() == true)
+ return false;
+ }
+ else
+ {
+ // Just build it in memory..
+ Map = new DynamicMMap(MMap::Public,MapSize);
+ }
+
+ // Lets try the source cache.
+ unsigned long CurrentSize = 0;
+ unsigned long TotalSize = 0;
+ if (CheckValidity(SrcCacheFile,Files.begin(),
+ Files.begin()+EndOfSource) == true)
+ {
+ // Preload the map with the source cache
+ FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
+ if (SCacheF.Read((unsigned char *)Map->Data() + Map->RawAllocate(SCacheF.Size()),
+ SCacheF.Size()) == false)
+ return false;
+
+ TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
+
+ // Build the status cache
+ pkgCacheGenerator Gen(Map.Get(),&Progress);
+ if (_error->PendingError() == true)
+ return false;
+ if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
+ Files.begin()+EndOfSource,Files.end()) == false)
+ return false;
+ }
+ else
+ {
+ TotalSize = ComputeSize(Files.begin(),Files.end());
+
+ // Build the source cache
+ pkgCacheGenerator Gen(Map.Get(),&Progress);
+ if (_error->PendingError() == true)
+ return false;
+ if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
+ Files.begin(),Files.begin()+EndOfSource) == false)
+ return false;
+
+ // Write it back
+ if (Writeable == true && SrcCacheFile.empty() == false)
+ {
+ FileFd SCacheF(SrcCacheFile,FileFd::WriteEmpty);
+ if (_error->PendingError() == true)
+ return false;
+
+ fchmod(SCacheF.Fd(),0644);
+
+ // Write out the main data
+ if (SCacheF.Write(Map->Data(),Map->Size()) == false)
+ return _error->Error(_("IO Error saving source cache"));
+ SCacheF.Sync();
+
+ // Write out the proper header
+ Gen.GetCache().HeaderP->Dirty = false;
+ if (SCacheF.Seek(0) == false ||
+ SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
+ return _error->Error(_("IO Error saving source cache"));
+ Gen.GetCache().HeaderP->Dirty = true;
+ SCacheF.Sync();
+ }
+
+ // Build the status cache
+ if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
+ Files.begin()+EndOfSource,Files.end()) == false)
+ return false;
+ }
+
+ if (_error->PendingError() == true)
+ return false;
+ if (OutMap != 0)
+ {
+ if (CacheF != 0)
+ {
+ delete Map.UnGuard();
+ *OutMap = new MMap(*CacheF,MMap::Public | MMap::ReadOnly);
+ }
+ else
+ {
+ *OutMap = Map.UnGuard();
+ }
+ }
+
+ return true;
+}
+ /*}}}*/
+// MakeOnlyStatusCache - Build a cache with just the status files /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
+{
+ unsigned long MapSize = _config->FindI("APT::Cache-Limit",8*1024*1024);
+ vector<pkgIndexFile *> Files;
+ unsigned long EndOfSource = Files.size();
+ if (_system->AddStatusFiles(Files) == false)
+ return false;
+
+ SPtr<DynamicMMap> Map;
+ Map = new DynamicMMap(MMap::Public,MapSize);
+ unsigned long CurrentSize = 0;
+ unsigned long TotalSize = 0;
+
+ 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 (_error->PendingError() == true)
+ return false;
+ if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
+ Files.begin()+EndOfSource,Files.end()) == false)
+ return false;
+
+ if (_error->PendingError() == true)