+ ItemP->String = idxString;
+
+ 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)
+{
+ bool const Debug = _config->FindB("Debug::pkgCacheGen", false);
+ // No file, certainly invalid
+ if (CacheFile.empty() == true || FileExists(CacheFile) == false)
+ {
+ if (Debug == true)
+ std::clog << "CacheFile doesn't exist" << std::endl;
+ return false;
+ }
+
+ // Map it
+ FileFd CacheF(CacheFile,FileFd::ReadOnly);
+ SPtr<MMap> Map = new MMap(CacheF,0);
+ pkgCache Cache(Map);
+ if (_error->PendingError() == true || Map->Size() == 0)
+ {
+ if (Debug == true)
+ std::clog << "Errors are pending or Map is empty()" << std::endl;
+ _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 (Debug == true)
+ std::clog << "Checking PkgFile " << (*Start)->Describe() << ": ";
+ if ((*Start)->HasPackages() == false)
+ {
+ if (Debug == true)
+ std::clog << "Has NO packages" << std::endl;
+ continue;
+ }
+
+ if ((*Start)->Exists() == false)
+ {
+#if 0 // mvo: we no longer give a message here (Default Sources spec)
+ _error->WarningE("stat",_("Couldn't stat source package list %s"),
+ (*Start)->Describe().c_str());
+#endif
+ if (Debug == true)
+ std::clog << "file doesn't exist" << std::endl;
+ continue;
+ }
+
+ // FindInCache is also expected to do an IMS check.
+ pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
+ if (File.end() == true)
+ {
+ if (Debug == true)
+ std::clog << "FindInCache returned end-Pointer" << std::endl;
+ return false;
+ }
+
+ Visited[File->ID] = true;
+ if (Debug == true)
+ std::clog << "with ID " << File->ID << " is valid" << std::endl;
+ }
+
+ for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
+ if (Visited[I] == false)
+ {
+ if (Debug == true)
+ std::clog << "File with ID" << I << " wasn't visited" << std::endl;
+ return false;
+ }
+
+ if (_error->PendingError() == true)
+ {
+ if (Debug == true)
+ {
+ std::clog << "Validity failed because of pending errors:" << std::endl;
+ _error->DumpErrors();
+ }
+ _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();
+ if (Progress != NULL)
+ Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
+ CurrentSize += Size;
+
+ if ((*I)->Merge(Gen,Progress) == false)
+ return false;
+ }
+
+ if (Gen.HasFileDeps() == true)
+ {
+ if (Progress != NULL)
+ Progress->Done();
+ TotalSize = ComputeSize(Start, End);
+ CurrentSize = 0;
+ for (I = Start; I != End; I++)
+ {
+ unsigned long Size = (*I)->Size();
+ if (Progress != NULL)
+ Progress->OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
+ CurrentSize += Size;
+ if ((*I)->MergeFileProvides(Gen,Progress) == false)
+ return false;
+ }
+ }
+
+ 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
+ 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. */
+__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);
+
+ 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 const EndOfSource = Files.size();
+ if (_system->AddStatusFiles(Files) == false)
+ return false;
+
+ // 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)
+ Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
+ else
+ if (SrcCacheFile.empty() == false)
+ Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
+ if (Debug == true)
+ std::clog << "Do we have write-access to the cache files? " << (Writeable ? "YES" : "NO") << std::endl;
+
+ if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
+ return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
+
+ if (Progress != NULL)
+ Progress->OverallProgress(0,1,1,_("Reading package lists"));
+
+ // Cache is OK, Fin.
+ if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
+ {
+ 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;
+ }
+ else if (Debug == true)
+ std::clog << "pkgcache.bin is NOT valid" << std::endl;
+
+ /* 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::WriteAtomic);
+ fchmod(CacheF->Fd(),0644);
+ Map = CreateDynamicMMap(CacheF, MMap::Public);
+ if (_error->PendingError() == true)
+ return false;
+ if (Debug == true)
+ std::clog << "Open filebased MMap" << std::endl;
+ }
+ else
+ {
+ // Just build it in memory..
+ Map = CreateDynamicMMap(NULL);
+ if (Debug == true)
+ std::clog << "Open memory Map (not filebased)" << std::endl;
+ }
+
+ // Lets try the source cache.
+ unsigned long CurrentSize = 0;
+ unsigned long TotalSize = 0;
+ if (CheckValidity(SrcCacheFile,Files.begin(),
+ Files.begin()+EndOfSource) == true)
+ {
+ if (Debug == true)
+ std::clog << "srcpkgcache.bin is valid - populate MMap with it." << std::endl;
+ // Preload the map with the source cache
+ FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
+ unsigned long const alloc = Map->RawAllocate(SCacheF.Size());
+ if ((alloc == 0 && _error->PendingError())
+ || SCacheF.Read((unsigned char *)Map->Data() + alloc,
+ 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;
+
+ // FIXME: move me to a better place
+ Gen.FinishCache(Progress);
+ }
+ else
+ {
+ if (Debug == true)
+ std::clog << "srcpkgcache.bin is NOT valid - rebuild" << std::endl;
+ 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::WriteAtomic);
+ 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;
+
+ // FIXME: move me to a better place
+ Gen.FinishCache(Progress);
+ }
+ if (Debug == true)
+ std::clog << "Caches are ready for shipping" << std::endl;
+
+ if (_error->PendingError() == true)
+ return false;
+ if (OutMap != 0)
+ {
+ if (CacheF != 0)
+ {
+ delete Map.UnGuard();
+ *OutMap = new MMap(*CacheF,0);
+ }
+ else
+ {
+ *OutMap = Map.UnGuard();
+ }
+ }
+
+ return true;
+}
+ /*}}}*/
+// CacheGenerator::MakeOnlyStatusCache - Build only a status files cache/*{{{*/
+// ---------------------------------------------------------------------
+/* */
+__deprecated bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
+ { return pkgCacheGenerator::MakeOnlyStatusCache(&Progress, OutMap); }
+bool pkgCacheGenerator::MakeOnlyStatusCache(OpProgress *Progress,DynamicMMap **OutMap)
+{
+ vector<pkgIndexFile *> Files;
+ unsigned long EndOfSource = Files.size();
+ if (_system->AddStatusFiles(Files) == false)
+ return false;
+
+ SPtr<DynamicMMap> Map = CreateDynamicMMap(NULL);
+ unsigned long CurrentSize = 0;
+ unsigned long TotalSize = 0;
+
+ TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
+
+ // Build the status cache
+ 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,
+ Files.begin()+EndOfSource,Files.end()) == false)
+ return false;
+
+ // FIXME: move me to a better place
+ Gen.FinishCache(Progress);
+
+ if (_error->PendingError() == true)