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