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