]> git.saurik.com Git - apt.git/blame - apt-pkg/pkgcachegen.cc
Optimizations
[apt.git] / apt-pkg / pkgcachegen.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
c1a22377 3// $Id: pkgcachegen.cc,v 1.24 1998/12/07 07:26:21 jgg Exp $
578bfd0a
AL
4/* ######################################################################
5
6 Package Cache Generator - Generator for the cache structure.
7
8 This builds the cache structure from the abstract package list parser.
9
10 ##################################################################### */
11 /*}}}*/
12// Include Files /*{{{*/
6c139d6e 13#ifdef __GNUG__
094a497d 14#pragma implementation "apt-pkg/pkgcachegen.h"
6c139d6e
AL
15#endif
16
094a497d
AL
17#include <apt-pkg/pkgcachegen.h>
18#include <apt-pkg/error.h>
19#include <apt-pkg/version.h>
b35d2f5f
AL
20#include <apt-pkg/progress.h>
21#include <apt-pkg/sourcelist.h>
22#include <apt-pkg/configuration.h>
23#include <apt-pkg/deblistparser.h>
24
9c14e3d6 25#include <strutl.h>
578bfd0a
AL
26
27#include <sys/stat.h>
28#include <unistd.h>
29 /*}}}*/
30
31// CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
32// ---------------------------------------------------------------------
33/* We set the diry flag and make sure that is written to the disk */
404ec98e
AL
34pkgCacheGenerator::pkgCacheGenerator(DynamicMMap &Map,OpProgress &Prog) :
35 Map(Map), Cache(Map), Progress(Prog)
578bfd0a
AL
36{
37 if (_error->PendingError() == true)
38 return;
39
40 if (Map.Size() == 0)
41 {
42 Map.RawAllocate(sizeof(pkgCache::Header));
43 *Cache.HeaderP = pkgCache::Header();
44 }
45 Cache.HeaderP->Dirty = true;
46 Map.Sync(0,sizeof(pkgCache::Header));
47 Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
48}
49 /*}}}*/
50// CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
51// ---------------------------------------------------------------------
52/* We sync the data then unset the dirty flag in two steps so as to
53 advoid a problem during a crash */
54pkgCacheGenerator::~pkgCacheGenerator()
55{
56 if (_error->PendingError() == true)
57 return;
58 if (Map.Sync() == false)
59 return;
60
61 Cache.HeaderP->Dirty = false;
62 Map.Sync(0,sizeof(pkgCache::Header));
63}
64 /*}}}*/
65// CacheGenerator::MergeList - Merge the package list /*{{{*/
66// ---------------------------------------------------------------------
67/* This provides the generation of the entries in the cache. Each loop
68 goes through a single package record from the underlying parse engine. */
69bool pkgCacheGenerator::MergeList(ListParser &List)
70{
71 List.Owner = this;
0149949b 72
a246f2dc 73 int Counter = 0;
0149949b 74 while (List.Step() == true)
578bfd0a
AL
75 {
76 // Get a pointer to the package structure
9ddf7030
AL
77 string PackageName = List.Package();
78 pkgCache::PkgIterator Pkg;
8efa2a3b 79 if (NewPackage(Pkg,PackageName) == false)
b35d2f5f 80 return _error->Error("Error occured while processing %s (NewPackage)",PackageName.c_str());
a246f2dc
AL
81 Counter++;
82 if (Counter % 100 == 0)
83 Progress.Progress(List.Offset());
8ce4327b 84
578bfd0a
AL
85 /* Get a pointer to the version structure. We know the list is sorted
86 so we use that fact in the search. Insertion of new versions is
87 done with correct sorting */
88 string Version = List.Version();
f55a958f
AL
89 if (Version.empty() == true)
90 {
91 if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
b35d2f5f 92 return _error->Error("Error occured while processing %s (UsePackage1)",PackageName.c_str());
f55a958f
AL
93 continue;
94 }
95
578bfd0a 96 pkgCache::VerIterator Ver = Pkg.VersionList();
031aa375 97 __apt_ptrloc *Last = &Pkg->VersionList;
2246928b 98 int Res = 1;
f55a958f 99 for (; Ver.end() == false; Last = &Ver->NextVer, Ver++)
578bfd0a
AL
100 {
101 Res = pkgVersionCompare(Version.begin(),Version.end(),Ver.VerStr(),
102 Ver.VerStr() + strlen(Ver.VerStr()));
103 if (Res >= 0)
104 break;
105 }
106
107 /* We already have a version for this item, record that we
108 saw it */
109 if (Res == 0)
110 {
f55a958f 111 if (List.UsePackage(Pkg,Ver) == false)
b35d2f5f 112 return _error->Error("Error occured while processing %s (UsePackage2)",PackageName.c_str());
f55a958f 113
578bfd0a 114 if (NewFileVer(Ver,List) == false)
b35d2f5f 115 return _error->Error("Error occured while processing %s (NewFileVer1)",PackageName.c_str());
578bfd0a
AL
116
117 continue;
118 }
119
120 // Add a new version
f55a958f
AL
121 *Last = NewVersion(Ver,Version,*Last);
122 Ver->ParentPkg = Pkg.Index();
578bfd0a 123 if (List.NewVersion(Ver) == false)
b35d2f5f 124 return _error->Error("Error occured while processing %s (NewVersion1)",PackageName.c_str());
0149949b 125
f55a958f 126 if (List.UsePackage(Pkg,Ver) == false)
b35d2f5f 127 return _error->Error("Error occured while processing %s (UsePackage3)",PackageName.c_str());
f55a958f 128
578bfd0a 129 if (NewFileVer(Ver,List) == false)
b35d2f5f 130 return _error->Error("Error occured while processing %s (NewVersion2)",PackageName.c_str());
578bfd0a 131 }
0149949b 132
578bfd0a
AL
133 return true;
134}
135 /*}}}*/
136// CacheGenerator::NewPackage - Add a new package /*{{{*/
137// ---------------------------------------------------------------------
138/* This creates a new package structure and adds it to the hash table */
f55a958f 139bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,string Name)
578bfd0a 140{
8efa2a3b
AL
141 Pkg = Cache.FindPkg(Name);
142 if (Pkg.end() == false)
143 return true;
144
578bfd0a
AL
145 // Get a structure
146 unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
147 if (Package == 0)
148 return false;
149
f55a958f 150 Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
578bfd0a
AL
151
152 // Insert it into the hash table
f55a958f 153 unsigned long Hash = Cache.Hash(Name);
578bfd0a
AL
154 Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
155 Cache.HeaderP->HashTable[Hash] = Package;
156
157 // Set the name and the ID
158 Pkg->Name = Map.WriteString(Name);
159 if (Pkg->Name == 0)
160 return false;
161 Pkg->ID = Cache.HeaderP->PackageCount++;
162
163 return true;
164}
165 /*}}}*/
166// CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
167// ---------------------------------------------------------------------
168/* */
f55a958f 169bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
578bfd0a
AL
170 ListParser &List)
171{
dcb79bae
AL
172 // Get a structure
173 unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
174 if (VerFile == 0)
175 return 0;
176
177 pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
178 VF->File = CurrentFile - Cache.PkgFileP;
03e39e59
AL
179
180 // Link it to the end of the list
181 __apt_ptrloc *Last = &Ver->FileList;
182 for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
183 Last = &V->NextFile;
184 VF->NextFile = *Last;
185 *Last = VF.Index();
186
dcb79bae
AL
187 VF->Offset = List.Offset();
188 VF->Size = List.Size();
ad00ae81
AL
189 if (Cache.HeaderP->MaxVerFileSize < VF->Size)
190 Cache.HeaderP->MaxVerFileSize = VF->Size;
f55a958f 191 return true;
578bfd0a
AL
192}
193 /*}}}*/
194// CacheGenerator::NewVersion - Create a new Version /*{{{*/
195// ---------------------------------------------------------------------
f55a958f 196/* This puts a version structure in the linked list */
578bfd0a 197unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
f55a958f 198 string VerStr,
578bfd0a
AL
199 unsigned long Next)
200{
f55a958f
AL
201 // Get a structure
202 unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
203 if (Version == 0)
0149949b 204 return 0;
f55a958f
AL
205
206 // Fill it in
207 Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
f55a958f
AL
208 Ver->NextVer = Next;
209 Ver->ID = Cache.HeaderP->VersionCount++;
210 Ver->VerStr = Map.WriteString(VerStr);
211 if (Ver->VerStr == 0)
0149949b 212 return 0;
f55a958f 213
0149949b 214 return Version;
578bfd0a
AL
215}
216 /*}}}*/
dcb79bae
AL
217// ListParser::NewDepends - Create a dependency element /*{{{*/
218// ---------------------------------------------------------------------
219/* This creates a dependency element in the tree. It is linked to the
220 version and to the package that it is pointing to. */
221bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
9ddf7030
AL
222 string PackageName,
223 string Version,
dcb79bae
AL
224 unsigned int Op,
225 unsigned int Type)
226{
227 pkgCache &Cache = Owner->Cache;
228
229 // Get a structure
230 unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
231 if (Dependency == 0)
232 return false;
233
234 // Fill it in
235 pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
236 Dep->ParentVer = Ver.Index();
237 Dep->Type = Type;
238 Dep->CompareOp = Op;
239 Dep->ID = Cache.HeaderP->DependsCount++;
240
241 // Locate the target package
8efa2a3b
AL
242 pkgCache::PkgIterator Pkg;
243 if (Owner->NewPackage(Pkg,PackageName) == false)
244 return false;
dcb79bae
AL
245
246 // Probe the reverse dependency list for a version string that matches
247 if (Version.empty() == false)
248 {
249 for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
250 if (I->Version != 0 && I.TargetVer() == Version)
251 Dep->Version = I->Version;
252 if (Dep->Version == 0)
253 if ((Dep->Version = WriteString(Version)) == 0)
254 return false;
255 }
c1a22377 256
dcb79bae
AL
257 // Link it to the package
258 Dep->Package = Pkg.Index();
259 Dep->NextRevDepends = Pkg->RevDepends;
260 Pkg->RevDepends = Dep.Index();
261
c1a22377
AL
262 /* Link it to the version (at the end of the list)
263 Caching the old end point speeds up generation substantially */
264 static pkgCache::VerIterator OldVer(Cache);
265 static __apt_ptrloc *OldLast;
266 if (OldVer != Ver)
267 {
268 OldLast = &Ver->DependsList;
269 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
270 OldLast = &D->NextDepends;
271 OldVer = Ver;
272 }
dcb79bae 273
c1a22377
AL
274 Dep->NextDepends = *OldLast;
275 *OldLast = Dep.Index();
276 OldLast = &Dep->NextDepends;
277
dcb79bae
AL
278 return true;
279}
280 /*}}}*/
281// ListParser::NewProvides - Create a Provides element /*{{{*/
282// ---------------------------------------------------------------------
283/* */
284bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
8efa2a3b 285 string PackageName,
9ddf7030 286 string Version)
dcb79bae
AL
287{
288 pkgCache &Cache = Owner->Cache;
8efa2a3b
AL
289
290 // We do not add self referencing provides
291 if (Ver.ParentPkg().Name() == PackageName)
292 return true;
dcb79bae
AL
293
294 // Get a structure
295 unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
296 if (Provides == 0)
297 return false;
298
299 // Fill it in
300 pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
301 Prv->Version = Ver.Index();
302 Prv->NextPkgProv = Ver->ProvidesList;
303 Ver->ProvidesList = Prv.Index();
304 if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0)
305 return false;
306
307 // Locate the target package
8efa2a3b
AL
308 pkgCache::PkgIterator Pkg;
309 if (Owner->NewPackage(Pkg,PackageName) == false)
310 return false;
dcb79bae
AL
311
312 // Link it to the package
313 Prv->ParentPkg = Pkg.Index();
314 Prv->NextProvides = Pkg->ProvidesList;
315 Pkg->ProvidesList = Prv.Index();
316
317 return true;
318}
319 /*}}}*/
578bfd0a
AL
320// CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
321// ---------------------------------------------------------------------
322/* This is used to select which file is to be associated with all newly
323 added versions. */
324bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags)
325{
326 struct stat Buf;
327 if (stat(File.c_str(),&Buf) == -1)
328 return _error->Errno("stat","Couldn't stat ",File.c_str());
329
330 // Get some space for the structure
331 CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
332 if (CurrentFile == Cache.PkgFileP)
333 return false;
334
335 // Fill it in
336 CurrentFile->FileName = Map.WriteString(File);
337 CurrentFile->Size = Buf.st_size;
338 CurrentFile->mtime = Buf.st_mtime;
339 CurrentFile->NextFile = Cache.HeaderP->FileList;
340 CurrentFile->Flags = Flags;
e1b74f61 341 CurrentFile->ID = Cache.HeaderP->PackageFileCount;
578bfd0a 342 PkgFileName = File;
ad00ae81 343 Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
b35d2f5f
AL
344 Cache.HeaderP->PackageFileCount++;
345
578bfd0a
AL
346 if (CurrentFile->FileName == 0)
347 return false;
404ec98e 348
8ce4327b 349 Progress.SubProgress(Buf.st_size);
8efa2a3b 350 return true;
578bfd0a
AL
351}
352 /*}}}*/
f55a958f
AL
353// CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
354// ---------------------------------------------------------------------
355/* This is used to create handles to strings. Given the same text it
356 always returns the same number */
357unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
358 unsigned int Size)
359{
360 // Search for an insertion point
361 pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
362 int Res = 1;
031aa375 363 __apt_ptrloc *Last = &Cache.HeaderP->StringList;
f55a958f
AL
364 for (; I != Cache.StringItemP; Last = &I->NextItem,
365 I = Cache.StringItemP + I->NextItem)
366 {
9c14e3d6 367 Res = stringcmp(S,S+Size,Cache.StrP + I->String);
f55a958f
AL
368 if (Res >= 0)
369 break;
370 }
371
372 // Match
373 if (Res == 0)
0149949b 374 return I->String;
f55a958f
AL
375
376 // Get a structure
377 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
378 if (Item == 0)
0149949b
AL
379 return 0;
380
f55a958f
AL
381 // Fill in the structure
382 pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
383 ItemP->NextItem = I - Cache.StringItemP;
384 *Last = Item;
385 ItemP->String = Map.WriteString(S,Size);
386 if (ItemP->String == 0)
0149949b 387 return 0;
f55a958f 388
0149949b 389 return ItemP->String;
f55a958f
AL
390}
391 /*}}}*/
b35d2f5f
AL
392
393// SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/
394// ---------------------------------------------------------------------
395/* The source cache is checked against the source list and the files
396 on disk, any difference results in a false. */
397bool pkgSrcCacheCheck(pkgSourceList &List)
398{
399 if (_error->PendingError() == true)
400 return false;
e1b74f61 401
3b5421b4 402 string CacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
0919e3f9 403 string ListDir = _config->FindDir("Dir::State::lists");
e1b74f61
AL
404
405 // Count the number of missing files
406 int Missing = 0;
407 for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
408 {
409 string File = ListDir + URItoFileName(I->PackagesURI());
410 struct stat Buf;
411 if (stat(File.c_str(),&Buf) != 0)
412 {
413 _error->WarningE("stat","Couldn't stat source package list '%s' (%s)",
414 I->PackagesInfo().c_str(),File.c_str());
415 Missing++;
416 }
417 }
418
419 // Open the source package cache
b35d2f5f
AL
420 if (FileExists(CacheFile) == false)
421 return false;
422
423 FileFd CacheF(CacheFile,FileFd::ReadOnly);
424 if (_error->PendingError() == true)
425 {
426 _error->Discard();
427 return false;
428 }
429
430 MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
0a8e3465 431 if (_error->PendingError() == true || Map.Size() == 0)
b35d2f5f
AL
432 {
433 _error->Discard();
434 return false;
435 }
436
437 pkgCache Cache(Map);
438 if (_error->PendingError() == true)
439 {
440 _error->Discard();
441 return false;
442 }
c5162d56 443
b35d2f5f 444 // They are certianly out of sync
c5162d56 445 if (Cache.Head().PackageFileCount != List.size() - Missing)
b35d2f5f
AL
446 return false;
447
448 for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
449 {
450 // Search for a match in the source list
451 bool Bad = true;
452 for (pkgSourceList::const_iterator I = List.begin();
453 I != List.end(); I++)
454 {
455 string File = ListDir + URItoFileName(I->PackagesURI());
456 if (F.FileName() == File)
457 {
458 Bad = false;
459 break;
460 }
461 }
462
463 // Check if the file matches what was cached
464 Bad |= !F.IsOk();
465 if (Bad == true)
466 return false;
467 }
468
469 return true;
470}
471 /*}}}*/
472// PkgCacheCheck - Check if the package cache is uptodate /*{{{*/
473// ---------------------------------------------------------------------
474/* This does a simple check of all files used to compose the cache */
475bool pkgPkgCacheCheck(string CacheFile)
476{
477 if (_error->PendingError() == true)
478 return false;
479
480 // Open the source package cache
481 if (FileExists(CacheFile) == false)
482 return false;
483
484 FileFd CacheF(CacheFile,FileFd::ReadOnly);
485 if (_error->PendingError() == true)
486 {
487 _error->Discard();
488 return false;
489 }
490
491 MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
0a8e3465 492 if (_error->PendingError() == true || Map.Size() == 0)
b35d2f5f
AL
493 {
494 _error->Discard();
495 return false;
496 }
497
498 pkgCache Cache(Map);
499 if (_error->PendingError() == true)
500 {
501 _error->Discard();
502 return false;
503 }
504
0118833a
AL
505 // Status files that must be in the cache
506 string Status[3];
3b5421b4
AL
507 Status[0] = _config->FindFile("Dir::State::xstatus");
508 Status[1]= _config->FindFile("Dir::State::userstatus");
509 Status[2] = _config->FindFile("Dir::State::status");
0118833a 510
b35d2f5f
AL
511 // Cheack each file
512 for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
0118833a 513 {
b35d2f5f
AL
514 if (F.IsOk() == false)
515 return false;
0118833a
AL
516
517 // See if this is one of the status files
518 for (int I = 0; I != 3; I++)
519 if (F.FileName() == Status[I])
520 Status[I] = string();
521 }
522
523 // Make sure all the status files are loaded.
524 for (int I = 0; I != 3; I++)
525 {
526 if (Status[I].empty() == false && FileExists(Status[I]) == true)
527 return false;
528 }
529
b35d2f5f
AL
530 return true;
531}
532 /*}}}*/
533// AddSourcesSize - Add the size of the status files /*{{{*/
534// ---------------------------------------------------------------------
535/* This adds the size of all the status files to the size counter */
536static bool pkgAddSourcesSize(unsigned long &TotalSize)
537{
538 // Grab the file names
3b5421b4
AL
539 string xstatus = _config->FindFile("Dir::State::xstatus");
540 string userstatus = _config->FindFile("Dir::State::userstatus");
541 string status = _config->FindFile("Dir::State::status");
b35d2f5f
AL
542
543 // Grab the sizes
544 struct stat Buf;
545 if (stat(xstatus.c_str(),&Buf) == 0)
546 TotalSize += Buf.st_size;
547 if (stat(userstatus.c_str(),&Buf) == 0)
548 TotalSize += Buf.st_size;
549 if (stat(status.c_str(),&Buf) != 0)
550 return _error->Errno("stat","Couldn't stat the status file %s",status.c_str());
551 TotalSize += Buf.st_size;
552
553 return true;
554}
555 /*}}}*/
556// MergeStatus - Add the status files to the cache /*{{{*/
557// ---------------------------------------------------------------------
558/* This adds the status files to the map */
559static bool pkgMergeStatus(OpProgress &Progress,pkgCacheGenerator &Gen,
560 unsigned long &CurrentSize,unsigned long TotalSize)
561{
562 // Grab the file names
563 string Status[3];
3b5421b4
AL
564 Status[0] = _config->FindFile("Dir::State::xstatus");
565 Status[1]= _config->FindFile("Dir::State::userstatus");
566 Status[2] = _config->FindFile("Dir::State::status");
b35d2f5f
AL
567
568 for (int I = 0; I != 3; I++)
569 {
570 // Check if the file exists and it is not the primary status file.
571 string File = Status[I];
572 if (I != 2 && FileExists(File) == false)
573 continue;
574
575 FileFd Pkg(File,FileFd::ReadOnly);
576 debListParser Parser(Pkg);
8ce4327b 577 Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
b35d2f5f
AL
578 if (_error->PendingError() == true)
579 return _error->Error("Problem opening %s",File.c_str());
580 CurrentSize += Pkg.Size();
8ce4327b
AL
581
582 Progress.SubProgress(0,"Local Package State - " + flNotDir(File));
b35d2f5f
AL
583 if (Gen.SelectFile(File,pkgCache::Flag::NotSource) == false)
584 return _error->Error("Problem with SelectFile %s",File.c_str());
585
586 if (Gen.MergeList(Parser) == false)
587 return _error->Error("Problem with MergeList %s",File.c_str());
8ce4327b 588 Progress.Progress(Pkg.Size());
b35d2f5f
AL
589 }
590
591 return true;
592}
593 /*}}}*/
594// MakeStatusCache - Generates a cache that includes the status files /*{{{*/
595// ---------------------------------------------------------------------
596/* This copies the package source cache and then merges the status and
597 xstatus files into it. */
598bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress)
599{
8ce4327b
AL
600 Progress.OverallProgress(0,1,1,"Reading Package Lists");
601
3b5421b4 602 string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
b35d2f5f 603 bool SrcOk = pkgSrcCacheCheck(List);
0a8e3465 604 bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile);
b35d2f5f
AL
605
606 // Rebuild the source and package caches
607 if (SrcOk == false)
608 {
3b5421b4 609 string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
0919e3f9 610 string ListDir = _config->FindDir("Dir::State::lists");
b35d2f5f
AL
611 FileFd SCacheF(SCacheFile,FileFd::WriteEmpty);
612 FileFd CacheF(CacheFile,FileFd::WriteEmpty);
613 DynamicMMap Map(CacheF,MMap::Public);
614 if (_error->PendingError() == true)
615 return false;
616
617 pkgCacheGenerator Gen(Map,Progress);
618
619 // Prepare the progress indicator
620 unsigned long TotalSize = 0;
621 struct stat Buf;
622 for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
623 {
624 string File = ListDir + URItoFileName(I->PackagesURI());
625 if (stat(File.c_str(),&Buf) != 0)
c5162d56 626 continue;
b35d2f5f
AL
627 TotalSize += Buf.st_size;
628 }
629
630 if (pkgAddSourcesSize(TotalSize) == false)
631 return false;
632
633 // Generate the pkg source cache
634 unsigned long CurrentSize = 0;
635 for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++)
636 {
637 string File = ListDir + URItoFileName(I->PackagesURI());
c5162d56
AL
638
639 if (stat(File.c_str(),&Buf) != 0)
640 continue;
641
b35d2f5f
AL
642 FileFd Pkg(File,FileFd::ReadOnly);
643 debListParser Parser(Pkg);
8ce4327b 644 Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists");
b35d2f5f
AL
645 if (_error->PendingError() == true)
646 return _error->Error("Problem opening %s",File.c_str());
647 CurrentSize += Pkg.Size();
648
8ce4327b 649 Progress.SubProgress(0,I->PackagesInfo());
b35d2f5f
AL
650 if (Gen.SelectFile(File) == false)
651 return _error->Error("Problem with SelectFile %s",File.c_str());
652
653 if (Gen.MergeList(Parser) == false)
654 return _error->Error("Problem with MergeList %s",File.c_str());
655 }
656
657 // Write the src cache
658 Gen.GetCache().HeaderP->Dirty = false;
659 if (SCacheF.Write(Map.Data(),Map.Size()) == false)
660 return _error->Error("IO Error saving source cache");
661 Gen.GetCache().HeaderP->Dirty = true;
662
663 // Merge in the source caches
664 return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
665 }
666
667 if (PkgOk == true)
8ce4327b
AL
668 {
669 Progress.OverallProgress(1,1,1,"Reading Package Lists");
b35d2f5f 670 return true;
8ce4327b 671 }
b35d2f5f
AL
672
673 // We use the source cache to generate the package cache
3b5421b4 674 string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
b35d2f5f
AL
675
676 FileFd SCacheF(SCacheFile,FileFd::ReadOnly);
677 FileFd CacheF(CacheFile,FileFd::WriteEmpty);
678 DynamicMMap Map(CacheF,MMap::Public);
679 if (_error->PendingError() == true)
680 return false;
681
682 // Preload the map with the source cache
683 if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()),
684 SCacheF.Size()) == false)
685 return false;
686
687 pkgCacheGenerator Gen(Map,Progress);
688
689 // Compute the progress
690 unsigned long TotalSize = 0;
691 if (pkgAddSourcesSize(TotalSize) == false)
692 return false;
693
694 unsigned long CurrentSize = 0;
695 return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize);
696}
697 /*}}}*/