]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
3b5421b4 | 3 | // $Id: pkgcachegen.cc,v 1.20 1998/10/20 02:39: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 |
34 | pkgCacheGenerator::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 */ | |
54 | pkgCacheGenerator::~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. */ | |
69 | bool 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 AL |
96 | pkgCache::VerIterator Ver = Pkg.VersionList(); |
97 | unsigned long *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 | 139 | bool 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 | 169 | bool 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; | |
179 | VF->NextFile = Ver->FileList; | |
180 | Ver->FileList = VF.Index(); | |
181 | VF->Offset = List.Offset(); | |
182 | VF->Size = List.Size(); | |
ad00ae81 AL |
183 | if (Cache.HeaderP->MaxVerFileSize < VF->Size) |
184 | Cache.HeaderP->MaxVerFileSize = VF->Size; | |
f55a958f | 185 | return true; |
578bfd0a AL |
186 | } |
187 | /*}}}*/ | |
188 | // CacheGenerator::NewVersion - Create a new Version /*{{{*/ | |
189 | // --------------------------------------------------------------------- | |
f55a958f | 190 | /* This puts a version structure in the linked list */ |
578bfd0a | 191 | unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver, |
f55a958f | 192 | string VerStr, |
578bfd0a AL |
193 | unsigned long Next) |
194 | { | |
f55a958f AL |
195 | // Get a structure |
196 | unsigned long Version = Map.Allocate(sizeof(pkgCache::Version)); | |
197 | if (Version == 0) | |
0149949b | 198 | return 0; |
f55a958f AL |
199 | |
200 | // Fill it in | |
201 | Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version); | |
f55a958f AL |
202 | Ver->NextVer = Next; |
203 | Ver->ID = Cache.HeaderP->VersionCount++; | |
204 | Ver->VerStr = Map.WriteString(VerStr); | |
205 | if (Ver->VerStr == 0) | |
0149949b | 206 | return 0; |
f55a958f | 207 | |
0149949b | 208 | return Version; |
578bfd0a AL |
209 | } |
210 | /*}}}*/ | |
dcb79bae AL |
211 | // ListParser::NewDepends - Create a dependency element /*{{{*/ |
212 | // --------------------------------------------------------------------- | |
213 | /* This creates a dependency element in the tree. It is linked to the | |
214 | version and to the package that it is pointing to. */ | |
215 | bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver, | |
9ddf7030 AL |
216 | string PackageName, |
217 | string Version, | |
dcb79bae AL |
218 | unsigned int Op, |
219 | unsigned int Type) | |
220 | { | |
221 | pkgCache &Cache = Owner->Cache; | |
222 | ||
223 | // Get a structure | |
224 | unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency)); | |
225 | if (Dependency == 0) | |
226 | return false; | |
227 | ||
228 | // Fill it in | |
229 | pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency); | |
230 | Dep->ParentVer = Ver.Index(); | |
231 | Dep->Type = Type; | |
232 | Dep->CompareOp = Op; | |
233 | Dep->ID = Cache.HeaderP->DependsCount++; | |
234 | ||
235 | // Locate the target package | |
8efa2a3b AL |
236 | pkgCache::PkgIterator Pkg; |
237 | if (Owner->NewPackage(Pkg,PackageName) == false) | |
238 | return false; | |
dcb79bae AL |
239 | |
240 | // Probe the reverse dependency list for a version string that matches | |
241 | if (Version.empty() == false) | |
242 | { | |
243 | for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++) | |
244 | if (I->Version != 0 && I.TargetVer() == Version) | |
245 | Dep->Version = I->Version; | |
246 | if (Dep->Version == 0) | |
247 | if ((Dep->Version = WriteString(Version)) == 0) | |
248 | return false; | |
249 | } | |
250 | ||
251 | // Link it to the package | |
252 | Dep->Package = Pkg.Index(); | |
253 | Dep->NextRevDepends = Pkg->RevDepends; | |
254 | Pkg->RevDepends = Dep.Index(); | |
255 | ||
256 | // Link it to the version (at the end of the list) | |
257 | unsigned long *Last = &Ver->DependsList; | |
258 | for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++) | |
259 | Last = &D->NextDepends; | |
260 | Dep->NextDepends = *Last; | |
261 | *Last = Dep.Index(); | |
262 | ||
263 | return true; | |
264 | } | |
265 | /*}}}*/ | |
266 | // ListParser::NewProvides - Create a Provides element /*{{{*/ | |
267 | // --------------------------------------------------------------------- | |
268 | /* */ | |
269 | bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver, | |
8efa2a3b | 270 | string PackageName, |
9ddf7030 | 271 | string Version) |
dcb79bae AL |
272 | { |
273 | pkgCache &Cache = Owner->Cache; | |
8efa2a3b AL |
274 | |
275 | // We do not add self referencing provides | |
276 | if (Ver.ParentPkg().Name() == PackageName) | |
277 | return true; | |
dcb79bae AL |
278 | |
279 | // Get a structure | |
280 | unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides)); | |
281 | if (Provides == 0) | |
282 | return false; | |
283 | ||
284 | // Fill it in | |
285 | pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP); | |
286 | Prv->Version = Ver.Index(); | |
287 | Prv->NextPkgProv = Ver->ProvidesList; | |
288 | Ver->ProvidesList = Prv.Index(); | |
289 | if (Version.empty() == false && (Prv->Version = WriteString(Version)) == 0) | |
290 | return false; | |
291 | ||
292 | // Locate the target package | |
8efa2a3b AL |
293 | pkgCache::PkgIterator Pkg; |
294 | if (Owner->NewPackage(Pkg,PackageName) == false) | |
295 | return false; | |
dcb79bae AL |
296 | |
297 | // Link it to the package | |
298 | Prv->ParentPkg = Pkg.Index(); | |
299 | Prv->NextProvides = Pkg->ProvidesList; | |
300 | Pkg->ProvidesList = Prv.Index(); | |
301 | ||
302 | return true; | |
303 | } | |
304 | /*}}}*/ | |
578bfd0a AL |
305 | // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/ |
306 | // --------------------------------------------------------------------- | |
307 | /* This is used to select which file is to be associated with all newly | |
308 | added versions. */ | |
309 | bool pkgCacheGenerator::SelectFile(string File,unsigned long Flags) | |
310 | { | |
311 | struct stat Buf; | |
312 | if (stat(File.c_str(),&Buf) == -1) | |
313 | return _error->Errno("stat","Couldn't stat ",File.c_str()); | |
314 | ||
315 | // Get some space for the structure | |
316 | CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile)); | |
317 | if (CurrentFile == Cache.PkgFileP) | |
318 | return false; | |
319 | ||
320 | // Fill it in | |
321 | CurrentFile->FileName = Map.WriteString(File); | |
322 | CurrentFile->Size = Buf.st_size; | |
323 | CurrentFile->mtime = Buf.st_mtime; | |
324 | CurrentFile->NextFile = Cache.HeaderP->FileList; | |
325 | CurrentFile->Flags = Flags; | |
e1b74f61 | 326 | CurrentFile->ID = Cache.HeaderP->PackageFileCount; |
578bfd0a | 327 | PkgFileName = File; |
ad00ae81 | 328 | Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP; |
b35d2f5f AL |
329 | Cache.HeaderP->PackageFileCount++; |
330 | ||
578bfd0a AL |
331 | if (CurrentFile->FileName == 0) |
332 | return false; | |
404ec98e | 333 | |
8ce4327b | 334 | Progress.SubProgress(Buf.st_size); |
8efa2a3b | 335 | return true; |
578bfd0a AL |
336 | } |
337 | /*}}}*/ | |
f55a958f AL |
338 | // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/ |
339 | // --------------------------------------------------------------------- | |
340 | /* This is used to create handles to strings. Given the same text it | |
341 | always returns the same number */ | |
342 | unsigned long pkgCacheGenerator::WriteUniqString(const char *S, | |
343 | unsigned int Size) | |
344 | { | |
345 | // Search for an insertion point | |
346 | pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList; | |
347 | int Res = 1; | |
348 | unsigned long *Last = &Cache.HeaderP->StringList; | |
349 | for (; I != Cache.StringItemP; Last = &I->NextItem, | |
350 | I = Cache.StringItemP + I->NextItem) | |
351 | { | |
9c14e3d6 | 352 | Res = stringcmp(S,S+Size,Cache.StrP + I->String); |
f55a958f AL |
353 | if (Res >= 0) |
354 | break; | |
355 | } | |
356 | ||
357 | // Match | |
358 | if (Res == 0) | |
0149949b | 359 | return I->String; |
f55a958f AL |
360 | |
361 | // Get a structure | |
362 | unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem)); | |
363 | if (Item == 0) | |
0149949b AL |
364 | return 0; |
365 | ||
f55a958f AL |
366 | // Fill in the structure |
367 | pkgCache::StringItem *ItemP = Cache.StringItemP + Item; | |
368 | ItemP->NextItem = I - Cache.StringItemP; | |
369 | *Last = Item; | |
370 | ItemP->String = Map.WriteString(S,Size); | |
371 | if (ItemP->String == 0) | |
0149949b | 372 | return 0; |
f55a958f | 373 | |
0149949b | 374 | return ItemP->String; |
f55a958f AL |
375 | } |
376 | /*}}}*/ | |
b35d2f5f AL |
377 | |
378 | // SrcCacheCheck - Check if the source package cache is uptodate /*{{{*/ | |
379 | // --------------------------------------------------------------------- | |
380 | /* The source cache is checked against the source list and the files | |
381 | on disk, any difference results in a false. */ | |
382 | bool pkgSrcCacheCheck(pkgSourceList &List) | |
383 | { | |
384 | if (_error->PendingError() == true) | |
385 | return false; | |
e1b74f61 | 386 | |
3b5421b4 AL |
387 | string CacheFile = _config->FindFile("Dir::Cache::srcpkgcache"); |
388 | string ListDir = _config->FindFile("Dir::State::lists"); | |
e1b74f61 AL |
389 | |
390 | // Count the number of missing files | |
391 | int Missing = 0; | |
392 | for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++) | |
393 | { | |
394 | string File = ListDir + URItoFileName(I->PackagesURI()); | |
395 | struct stat Buf; | |
396 | if (stat(File.c_str(),&Buf) != 0) | |
397 | { | |
398 | _error->WarningE("stat","Couldn't stat source package list '%s' (%s)", | |
399 | I->PackagesInfo().c_str(),File.c_str()); | |
400 | Missing++; | |
401 | } | |
402 | } | |
403 | ||
404 | // Open the source package cache | |
b35d2f5f AL |
405 | if (FileExists(CacheFile) == false) |
406 | return false; | |
407 | ||
408 | FileFd CacheF(CacheFile,FileFd::ReadOnly); | |
409 | if (_error->PendingError() == true) | |
410 | { | |
411 | _error->Discard(); | |
412 | return false; | |
413 | } | |
414 | ||
415 | MMap Map(CacheF,MMap::Public | MMap::ReadOnly); | |
0a8e3465 | 416 | if (_error->PendingError() == true || Map.Size() == 0) |
b35d2f5f AL |
417 | { |
418 | _error->Discard(); | |
419 | return false; | |
420 | } | |
421 | ||
422 | pkgCache Cache(Map); | |
423 | if (_error->PendingError() == true) | |
424 | { | |
425 | _error->Discard(); | |
426 | return false; | |
427 | } | |
c5162d56 | 428 | |
b35d2f5f | 429 | // They are certianly out of sync |
c5162d56 | 430 | if (Cache.Head().PackageFileCount != List.size() - Missing) |
b35d2f5f AL |
431 | return false; |
432 | ||
433 | for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++) | |
434 | { | |
435 | // Search for a match in the source list | |
436 | bool Bad = true; | |
437 | for (pkgSourceList::const_iterator I = List.begin(); | |
438 | I != List.end(); I++) | |
439 | { | |
440 | string File = ListDir + URItoFileName(I->PackagesURI()); | |
441 | if (F.FileName() == File) | |
442 | { | |
443 | Bad = false; | |
444 | break; | |
445 | } | |
446 | } | |
447 | ||
448 | // Check if the file matches what was cached | |
449 | Bad |= !F.IsOk(); | |
450 | if (Bad == true) | |
451 | return false; | |
452 | } | |
453 | ||
454 | return true; | |
455 | } | |
456 | /*}}}*/ | |
457 | // PkgCacheCheck - Check if the package cache is uptodate /*{{{*/ | |
458 | // --------------------------------------------------------------------- | |
459 | /* This does a simple check of all files used to compose the cache */ | |
460 | bool pkgPkgCacheCheck(string CacheFile) | |
461 | { | |
462 | if (_error->PendingError() == true) | |
463 | return false; | |
464 | ||
465 | // Open the source package cache | |
466 | if (FileExists(CacheFile) == false) | |
467 | return false; | |
468 | ||
469 | FileFd CacheF(CacheFile,FileFd::ReadOnly); | |
470 | if (_error->PendingError() == true) | |
471 | { | |
472 | _error->Discard(); | |
473 | return false; | |
474 | } | |
475 | ||
476 | MMap Map(CacheF,MMap::Public | MMap::ReadOnly); | |
0a8e3465 | 477 | if (_error->PendingError() == true || Map.Size() == 0) |
b35d2f5f AL |
478 | { |
479 | _error->Discard(); | |
480 | return false; | |
481 | } | |
482 | ||
483 | pkgCache Cache(Map); | |
484 | if (_error->PendingError() == true) | |
485 | { | |
486 | _error->Discard(); | |
487 | return false; | |
488 | } | |
489 | ||
0118833a AL |
490 | // Status files that must be in the cache |
491 | string Status[3]; | |
3b5421b4 AL |
492 | Status[0] = _config->FindFile("Dir::State::xstatus"); |
493 | Status[1]= _config->FindFile("Dir::State::userstatus"); | |
494 | Status[2] = _config->FindFile("Dir::State::status"); | |
0118833a | 495 | |
b35d2f5f AL |
496 | // Cheack each file |
497 | for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++) | |
0118833a | 498 | { |
b35d2f5f AL |
499 | if (F.IsOk() == false) |
500 | return false; | |
0118833a AL |
501 | |
502 | // See if this is one of the status files | |
503 | for (int I = 0; I != 3; I++) | |
504 | if (F.FileName() == Status[I]) | |
505 | Status[I] = string(); | |
506 | } | |
507 | ||
508 | // Make sure all the status files are loaded. | |
509 | for (int I = 0; I != 3; I++) | |
510 | { | |
511 | if (Status[I].empty() == false && FileExists(Status[I]) == true) | |
512 | return false; | |
513 | } | |
514 | ||
b35d2f5f AL |
515 | return true; |
516 | } | |
517 | /*}}}*/ | |
518 | // AddSourcesSize - Add the size of the status files /*{{{*/ | |
519 | // --------------------------------------------------------------------- | |
520 | /* This adds the size of all the status files to the size counter */ | |
521 | static bool pkgAddSourcesSize(unsigned long &TotalSize) | |
522 | { | |
523 | // Grab the file names | |
3b5421b4 AL |
524 | string xstatus = _config->FindFile("Dir::State::xstatus"); |
525 | string userstatus = _config->FindFile("Dir::State::userstatus"); | |
526 | string status = _config->FindFile("Dir::State::status"); | |
b35d2f5f AL |
527 | |
528 | // Grab the sizes | |
529 | struct stat Buf; | |
530 | if (stat(xstatus.c_str(),&Buf) == 0) | |
531 | TotalSize += Buf.st_size; | |
532 | if (stat(userstatus.c_str(),&Buf) == 0) | |
533 | TotalSize += Buf.st_size; | |
534 | if (stat(status.c_str(),&Buf) != 0) | |
535 | return _error->Errno("stat","Couldn't stat the status file %s",status.c_str()); | |
536 | TotalSize += Buf.st_size; | |
537 | ||
538 | return true; | |
539 | } | |
540 | /*}}}*/ | |
541 | // MergeStatus - Add the status files to the cache /*{{{*/ | |
542 | // --------------------------------------------------------------------- | |
543 | /* This adds the status files to the map */ | |
544 | static bool pkgMergeStatus(OpProgress &Progress,pkgCacheGenerator &Gen, | |
545 | unsigned long &CurrentSize,unsigned long TotalSize) | |
546 | { | |
547 | // Grab the file names | |
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"); | |
b35d2f5f AL |
552 | |
553 | for (int I = 0; I != 3; I++) | |
554 | { | |
555 | // Check if the file exists and it is not the primary status file. | |
556 | string File = Status[I]; | |
557 | if (I != 2 && FileExists(File) == false) | |
558 | continue; | |
559 | ||
560 | FileFd Pkg(File,FileFd::ReadOnly); | |
561 | debListParser Parser(Pkg); | |
8ce4327b | 562 | Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists"); |
b35d2f5f AL |
563 | if (_error->PendingError() == true) |
564 | return _error->Error("Problem opening %s",File.c_str()); | |
565 | CurrentSize += Pkg.Size(); | |
8ce4327b AL |
566 | |
567 | Progress.SubProgress(0,"Local Package State - " + flNotDir(File)); | |
b35d2f5f AL |
568 | if (Gen.SelectFile(File,pkgCache::Flag::NotSource) == false) |
569 | return _error->Error("Problem with SelectFile %s",File.c_str()); | |
570 | ||
571 | if (Gen.MergeList(Parser) == false) | |
572 | return _error->Error("Problem with MergeList %s",File.c_str()); | |
8ce4327b | 573 | Progress.Progress(Pkg.Size()); |
b35d2f5f AL |
574 | } |
575 | ||
576 | return true; | |
577 | } | |
578 | /*}}}*/ | |
579 | // MakeStatusCache - Generates a cache that includes the status files /*{{{*/ | |
580 | // --------------------------------------------------------------------- | |
581 | /* This copies the package source cache and then merges the status and | |
582 | xstatus files into it. */ | |
583 | bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress) | |
584 | { | |
8ce4327b AL |
585 | Progress.OverallProgress(0,1,1,"Reading Package Lists"); |
586 | ||
3b5421b4 | 587 | string CacheFile = _config->FindFile("Dir::Cache::pkgcache"); |
b35d2f5f | 588 | bool SrcOk = pkgSrcCacheCheck(List); |
0a8e3465 | 589 | bool PkgOk = SrcOk && pkgPkgCacheCheck(CacheFile); |
b35d2f5f AL |
590 | |
591 | // Rebuild the source and package caches | |
592 | if (SrcOk == false) | |
593 | { | |
3b5421b4 AL |
594 | string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache"); |
595 | string ListDir = _config->FindFile("Dir::State::lists"); | |
b35d2f5f AL |
596 | |
597 | FileFd SCacheF(SCacheFile,FileFd::WriteEmpty); | |
598 | FileFd CacheF(CacheFile,FileFd::WriteEmpty); | |
599 | DynamicMMap Map(CacheF,MMap::Public); | |
600 | if (_error->PendingError() == true) | |
601 | return false; | |
602 | ||
603 | pkgCacheGenerator Gen(Map,Progress); | |
604 | ||
605 | // Prepare the progress indicator | |
606 | unsigned long TotalSize = 0; | |
607 | struct stat Buf; | |
608 | for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++) | |
609 | { | |
610 | string File = ListDir + URItoFileName(I->PackagesURI()); | |
611 | if (stat(File.c_str(),&Buf) != 0) | |
c5162d56 | 612 | continue; |
b35d2f5f AL |
613 | TotalSize += Buf.st_size; |
614 | } | |
615 | ||
616 | if (pkgAddSourcesSize(TotalSize) == false) | |
617 | return false; | |
618 | ||
619 | // Generate the pkg source cache | |
620 | unsigned long CurrentSize = 0; | |
621 | for (pkgSourceList::const_iterator I = List.begin(); I != List.end(); I++) | |
622 | { | |
623 | string File = ListDir + URItoFileName(I->PackagesURI()); | |
c5162d56 AL |
624 | |
625 | if (stat(File.c_str(),&Buf) != 0) | |
626 | continue; | |
627 | ||
b35d2f5f AL |
628 | FileFd Pkg(File,FileFd::ReadOnly); |
629 | debListParser Parser(Pkg); | |
8ce4327b | 630 | Progress.OverallProgress(CurrentSize,TotalSize,Pkg.Size(),"Reading Package Lists"); |
b35d2f5f AL |
631 | if (_error->PendingError() == true) |
632 | return _error->Error("Problem opening %s",File.c_str()); | |
633 | CurrentSize += Pkg.Size(); | |
634 | ||
8ce4327b | 635 | Progress.SubProgress(0,I->PackagesInfo()); |
b35d2f5f AL |
636 | if (Gen.SelectFile(File) == false) |
637 | return _error->Error("Problem with SelectFile %s",File.c_str()); | |
638 | ||
639 | if (Gen.MergeList(Parser) == false) | |
640 | return _error->Error("Problem with MergeList %s",File.c_str()); | |
641 | } | |
642 | ||
643 | // Write the src cache | |
644 | Gen.GetCache().HeaderP->Dirty = false; | |
645 | if (SCacheF.Write(Map.Data(),Map.Size()) == false) | |
646 | return _error->Error("IO Error saving source cache"); | |
647 | Gen.GetCache().HeaderP->Dirty = true; | |
648 | ||
649 | // Merge in the source caches | |
650 | return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize); | |
651 | } | |
652 | ||
653 | if (PkgOk == true) | |
8ce4327b AL |
654 | { |
655 | Progress.OverallProgress(1,1,1,"Reading Package Lists"); | |
b35d2f5f | 656 | return true; |
8ce4327b | 657 | } |
b35d2f5f AL |
658 | |
659 | // We use the source cache to generate the package cache | |
3b5421b4 | 660 | string SCacheFile = _config->FindFile("Dir::Cache::srcpkgcache"); |
b35d2f5f AL |
661 | |
662 | FileFd SCacheF(SCacheFile,FileFd::ReadOnly); | |
663 | FileFd CacheF(CacheFile,FileFd::WriteEmpty); | |
664 | DynamicMMap Map(CacheF,MMap::Public); | |
665 | if (_error->PendingError() == true) | |
666 | return false; | |
667 | ||
668 | // Preload the map with the source cache | |
669 | if (SCacheF.Read((unsigned char *)Map.Data() + Map.RawAllocate(SCacheF.Size()), | |
670 | SCacheF.Size()) == false) | |
671 | return false; | |
672 | ||
673 | pkgCacheGenerator Gen(Map,Progress); | |
674 | ||
675 | // Compute the progress | |
676 | unsigned long TotalSize = 0; | |
677 | if (pkgAddSourcesSize(TotalSize) == false) | |
678 | return false; | |
679 | ||
680 | unsigned long CurrentSize = 0; | |
681 | return pkgMergeStatus(Progress,Gen,CurrentSize,TotalSize); | |
682 | } | |
683 | /*}}}*/ |