display warnings instead of errors if the parts dirs doesn't exist
[ntk/apt.git] / apt-pkg / pkgcachegen.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
7db98ffc 3// $Id: pkgcachegen.cc,v 1.53.2.1 2003/12/24 23:09:17 mdz 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 /*{{{*/
b2e465d6
AL
13#define APT_COMPATIBILITY 986
14
094a497d
AL
15#include <apt-pkg/pkgcachegen.h>
16#include <apt-pkg/error.h>
17#include <apt-pkg/version.h>
b35d2f5f
AL
18#include <apt-pkg/progress.h>
19#include <apt-pkg/sourcelist.h>
20#include <apt-pkg/configuration.h>
cdcc6d34 21#include <apt-pkg/strutl.h>
b2e465d6
AL
22#include <apt-pkg/sptr.h>
23#include <apt-pkg/pkgsystem.h>
578bfd0a 24
afb1e2e3
MV
25#include <apt-pkg/tagfile.h>
26
b2e465d6 27#include <apti18n.h>
e7b470ee
AL
28
29#include <vector>
30
578bfd0a
AL
31#include <sys/stat.h>
32#include <unistd.h>
803fafcb 33#include <errno.h>
7ef72446 34#include <stdio.h>
1ae93c94 35#include <system.h>
578bfd0a 36 /*}}}*/
e7b470ee 37typedef vector<pkgIndexFile *>::iterator FileIterator;
578bfd0a
AL
38
39// CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
40// ---------------------------------------------------------------------
41/* We set the diry flag and make sure that is written to the disk */
b2e465d6 42pkgCacheGenerator::pkgCacheGenerator(DynamicMMap *pMap,OpProgress *Prog) :
45415543
AL
43 Map(*pMap), Cache(pMap,false), Progress(Prog),
44 FoundFileDeps(0)
578bfd0a 45{
ddc1d8d0 46 CurrentFile = 0;
b2e465d6 47 memset(UniqHash,0,sizeof(UniqHash));
ddc1d8d0 48
578bfd0a
AL
49 if (_error->PendingError() == true)
50 return;
b2e465d6 51
578bfd0a
AL
52 if (Map.Size() == 0)
53 {
b2e465d6
AL
54 // Setup the map interface..
55 Cache.HeaderP = (pkgCache::Header *)Map.Data();
578bfd0a 56 Map.RawAllocate(sizeof(pkgCache::Header));
b2e465d6
AL
57 Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
58
59 // Starting header
578bfd0a 60 *Cache.HeaderP = pkgCache::Header();
b2e465d6
AL
61 Cache.HeaderP->VerSysName = Map.WriteString(_system->VS->Label);
62 Cache.HeaderP->Architecture = Map.WriteString(_config->Find("APT::Architecture"));
63 Cache.ReMap();
578bfd0a 64 }
b2e465d6
AL
65 else
66 {
67 // Map directly from the existing file
68 Cache.ReMap();
69 Map.UsePools(*Cache.HeaderP->Pools,sizeof(Cache.HeaderP->Pools)/sizeof(Cache.HeaderP->Pools[0]));
70 if (Cache.VS != _system->VS)
71 {
72 _error->Error(_("Cache has an incompatible versioning system"));
73 return;
74 }
75 }
76
578bfd0a
AL
77 Cache.HeaderP->Dirty = true;
78 Map.Sync(0,sizeof(pkgCache::Header));
578bfd0a
AL
79}
80 /*}}}*/
81// CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
82// ---------------------------------------------------------------------
83/* We sync the data then unset the dirty flag in two steps so as to
84 advoid a problem during a crash */
85pkgCacheGenerator::~pkgCacheGenerator()
86{
87 if (_error->PendingError() == true)
88 return;
89 if (Map.Sync() == false)
90 return;
91
92 Cache.HeaderP->Dirty = false;
93 Map.Sync(0,sizeof(pkgCache::Header));
94}
95 /*}}}*/
96// CacheGenerator::MergeList - Merge the package list /*{{{*/
97// ---------------------------------------------------------------------
98/* This provides the generation of the entries in the cache. Each loop
99 goes through a single package record from the underlying parse engine. */
ddc1d8d0
AL
100bool pkgCacheGenerator::MergeList(ListParser &List,
101 pkgCache::VerIterator *OutVer)
578bfd0a
AL
102{
103 List.Owner = this;
0149949b 104
f9eec0e7 105 unsigned int Counter = 0;
0149949b 106 while (List.Step() == true)
578bfd0a
AL
107 {
108 // Get a pointer to the package structure
9ddf7030 109 string PackageName = List.Package();
65a1e968
AL
110 if (PackageName.empty() == true)
111 return false;
112
9ddf7030 113 pkgCache::PkgIterator Pkg;
8efa2a3b 114 if (NewPackage(Pkg,PackageName) == false)
6804503b 115 return _error->Error(_("Error occurred while processing %s (NewPackage)"),PackageName.c_str());
a246f2dc 116 Counter++;
ddc1d8d0
AL
117 if (Counter % 100 == 0 && Progress != 0)
118 Progress->Progress(List.Offset());
8ce4327b 119
578bfd0a
AL
120 /* Get a pointer to the version structure. We know the list is sorted
121 so we use that fact in the search. Insertion of new versions is
122 done with correct sorting */
123 string Version = List.Version();
f55a958f
AL
124 if (Version.empty() == true)
125 {
770c32ec
MV
126 // we first process the package, then the descriptions
127 // (this has the bonus that we get MMap error when we run out
128 // of MMap space)
f55a958f 129 if (List.UsePackage(Pkg,pkgCache::VerIterator(Cache)) == false)
6804503b 130 return _error->Error(_("Error occurred while processing %s (UsePackage1)"),
7a3c2ab0 131 PackageName.c_str());
770c32ec 132
a52f938b
OS
133 // Find the right version to write the description
134 MD5SumValue CurMd5 = List.Description_md5();
135 pkgCache::VerIterator Ver = Pkg.VersionList();
136 map_ptrloc *LastVer = &Pkg->VersionList;
770c32ec 137
a52f938b
OS
138 for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
139 {
140 pkgCache::DescIterator Desc = Ver.DescriptionList();
141 map_ptrloc *LastDesc = &Ver->DescriptionList;
c76c44b1 142 bool duplicate=false;
143
144 // don't add a new description if we have one for the given
145 // md5 && language
915bdcf7 146 for ( ; Desc.end() == false; Desc++)
c76c44b1 147 if (MD5SumValue(Desc.md5()) == CurMd5 &&
148 Desc.LanguageCode() == List.DescriptionLanguage())
149 duplicate=true;
150 if(duplicate)
151 continue;
152
153 for (Desc = Ver.DescriptionList();
154 Desc.end() == false;
155 LastDesc = &Desc->NextDesc, Desc++)
770c32ec 156 {
770c32ec
MV
157 if (MD5SumValue(Desc.md5()) == CurMd5)
158 {
a52f938b
OS
159 // Add new description
160 *LastDesc = NewDescription(Desc, List.DescriptionLanguage(), CurMd5, *LastDesc);
161 Desc->ParentPkg = Pkg.Index();
770c32ec 162
a52f938b 163 if (NewFileDesc(Desc,List) == false)
f0f66a3d 164 return _error->Error(_("Error occurred while processing %s (NewFileDesc1)"),PackageName.c_str());
a52f938b
OS
165 break;
166 }
770c32ec 167 }
a52f938b 168 }
770c32ec 169
f55a958f
AL
170 continue;
171 }
172
578bfd0a 173 pkgCache::VerIterator Ver = Pkg.VersionList();
a52f938b 174 map_ptrloc *LastVer = &Pkg->VersionList;
2246928b 175 int Res = 1;
a52f938b 176 for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
578bfd0a 177 {
c24972cb 178 Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
578bfd0a
AL
179 if (Res >= 0)
180 break;
181 }
182
183 /* We already have a version for this item, record that we
184 saw it */
204fbdcc
AL
185 unsigned long Hash = List.VersionHash();
186 if (Res == 0 && Ver->Hash == Hash)
578bfd0a 187 {
f55a958f 188 if (List.UsePackage(Pkg,Ver) == false)
6804503b 189 return _error->Error(_("Error occurred while processing %s (UsePackage2)"),
7a3c2ab0 190 PackageName.c_str());
f78439bf 191
578bfd0a 192 if (NewFileVer(Ver,List) == false)
6804503b 193 return _error->Error(_("Error occurred while processing %s (NewFileVer1)"),
7a3c2ab0 194 PackageName.c_str());
578bfd0a 195
ddc1d8d0
AL
196 // Read only a single record and return
197 if (OutVer != 0)
198 {
199 *OutVer = Ver;
45415543 200 FoundFileDeps |= List.HasFileDeps();
ddc1d8d0
AL
201 return true;
202 }
203
578bfd0a
AL
204 continue;
205 }
206
204fbdcc
AL
207 // Skip to the end of the same version set.
208 if (Res == 0)
209 {
a52f938b 210 for (; Ver.end() == false; LastVer = &Ver->NextVer, Ver++)
204fbdcc 211 {
c24972cb 212 Res = Cache.VS->CmpVersion(Version,Ver.VerStr());
204fbdcc
AL
213 if (Res != 0)
214 break;
215 }
216 }
217
578bfd0a 218 // Add a new version
a52f938b 219 *LastVer = NewVersion(Ver,Version,*LastVer);
f55a958f 220 Ver->ParentPkg = Pkg.Index();
204fbdcc 221 Ver->Hash = Hash;
a52f938b 222
578bfd0a 223 if (List.NewVersion(Ver) == false)
6804503b 224 return _error->Error(_("Error occurred while processing %s (NewVersion1)"),
7a3c2ab0 225 PackageName.c_str());
0149949b 226
f55a958f 227 if (List.UsePackage(Pkg,Ver) == false)
6804503b 228 return _error->Error(_("Error occurred while processing %s (UsePackage3)"),
7a3c2ab0 229 PackageName.c_str());
f55a958f 230
578bfd0a 231 if (NewFileVer(Ver,List) == false)
6804503b 232 return _error->Error(_("Error occurred while processing %s (NewVersion2)"),
7a3c2ab0 233 PackageName.c_str());
ddc1d8d0
AL
234
235 // Read only a single record and return
236 if (OutVer != 0)
237 {
238 *OutVer = Ver;
45415543 239 FoundFileDeps |= List.HasFileDeps();
ddc1d8d0
AL
240 return true;
241 }
a52f938b
OS
242
243 /* Record the Description data. Description data always exist in
244 Packages and Translation-* files. */
245 pkgCache::DescIterator Desc = Ver.DescriptionList();
246 map_ptrloc *LastDesc = &Ver->DescriptionList;
247
248 // Skip to the end of description set
249 for (; Desc.end() == false; LastDesc = &Desc->NextDesc, Desc++);
250
251 // Add new description
252 *LastDesc = NewDescription(Desc, List.DescriptionLanguage(), List.Description_md5(), *LastDesc);
253 Desc->ParentPkg = Pkg.Index();
254
255 if (NewFileDesc(Desc,List) == false)
f0f66a3d 256 return _error->Error(_("Error occurred while processing %s (NewFileDesc2)"),PackageName.c_str());
578bfd0a 257 }
0149949b 258
45415543
AL
259 FoundFileDeps |= List.HasFileDeps();
260
6a3da7a6
AL
261 if (Cache.HeaderP->PackageCount >= (1ULL<<sizeof(Cache.PkgP->ID)*8)-1)
262 return _error->Error(_("Wow, you exceeded the number of package "
263 "names this APT is capable of."));
264 if (Cache.HeaderP->VersionCount >= (1ULL<<(sizeof(Cache.VerP->ID)*8))-1)
265 return _error->Error(_("Wow, you exceeded the number of versions "
266 "this APT is capable of."));
a52f938b
OS
267 if (Cache.HeaderP->DescriptionCount >= (1ULL<<(sizeof(Cache.DescP->ID)*8))-1)
268 return _error->Error(_("Wow, you exceeded the number of descriptions "
269 "this APT is capable of."));
6a3da7a6
AL
270 if (Cache.HeaderP->DependsCount >= (1ULL<<(sizeof(Cache.DepP->ID)*8))-1ULL)
271 return _error->Error(_("Wow, you exceeded the number of dependencies "
272 "this APT is capable of."));
578bfd0a
AL
273 return true;
274}
275 /*}}}*/
45415543
AL
276// CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
277// ---------------------------------------------------------------------
278/* If we found any file depends while parsing the main list we need to
279 resolve them. Since it is undesired to load the entire list of files
280 into the cache as virtual packages we do a two stage effort. MergeList
281 identifies the file depends and this creates Provdies for them by
282 re-parsing all the indexs. */
283bool pkgCacheGenerator::MergeFileProvides(ListParser &List)
284{
285 List.Owner = this;
286
287 unsigned int Counter = 0;
288 while (List.Step() == true)
289 {
290 string PackageName = List.Package();
291 if (PackageName.empty() == true)
292 return false;
293 string Version = List.Version();
294 if (Version.empty() == true)
295 continue;
296
297 pkgCache::PkgIterator Pkg = Cache.FindPkg(PackageName);
298 if (Pkg.end() == true)
6804503b 299 return _error->Error(_("Error occurred while processing %s (FindPkg)"),
45415543
AL
300 PackageName.c_str());
301 Counter++;
302 if (Counter % 100 == 0 && Progress != 0)
303 Progress->Progress(List.Offset());
304
305 unsigned long Hash = List.VersionHash();
306 pkgCache::VerIterator Ver = Pkg.VersionList();
307 for (; Ver.end() == false; Ver++)
308 {
309 if (Ver->Hash == Hash && Version.c_str() == Ver.VerStr())
310 {
311 if (List.CollectFileProvides(Cache,Ver) == false)
6804503b 312 return _error->Error(_("Error occurred while processing %s (CollectFileProvides)"),PackageName.c_str());
45415543
AL
313 break;
314 }
315 }
316
317 if (Ver.end() == true)
318 _error->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName.c_str(),Version.c_str());
319 }
320
321 return true;
322}
323 /*}}}*/
578bfd0a
AL
324// CacheGenerator::NewPackage - Add a new package /*{{{*/
325// ---------------------------------------------------------------------
326/* This creates a new package structure and adds it to the hash table */
171c75f1 327bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator &Pkg,const string &Name)
578bfd0a 328{
8efa2a3b
AL
329 Pkg = Cache.FindPkg(Name);
330 if (Pkg.end() == false)
331 return true;
a52f938b 332
578bfd0a
AL
333 // Get a structure
334 unsigned long Package = Map.Allocate(sizeof(pkgCache::Package));
335 if (Package == 0)
336 return false;
337
f55a958f 338 Pkg = pkgCache::PkgIterator(Cache,Cache.PkgP + Package);
578bfd0a
AL
339
340 // Insert it into the hash table
f55a958f 341 unsigned long Hash = Cache.Hash(Name);
578bfd0a
AL
342 Pkg->NextPackage = Cache.HeaderP->HashTable[Hash];
343 Cache.HeaderP->HashTable[Hash] = Package;
344
345 // Set the name and the ID
346 Pkg->Name = Map.WriteString(Name);
347 if (Pkg->Name == 0)
348 return false;
349 Pkg->ID = Cache.HeaderP->PackageCount++;
350
351 return true;
352}
353 /*}}}*/
354// CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
355// ---------------------------------------------------------------------
356/* */
f55a958f 357bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator &Ver,
578bfd0a
AL
358 ListParser &List)
359{
ddc1d8d0
AL
360 if (CurrentFile == 0)
361 return true;
362
dcb79bae
AL
363 // Get a structure
364 unsigned long VerFile = Map.Allocate(sizeof(pkgCache::VerFile));
365 if (VerFile == 0)
366 return 0;
367
368 pkgCache::VerFileIterator VF(Cache,Cache.VerFileP + VerFile);
369 VF->File = CurrentFile - Cache.PkgFileP;
03e39e59
AL
370
371 // Link it to the end of the list
349cd3b8 372 map_ptrloc *Last = &Ver->FileList;
03e39e59
AL
373 for (pkgCache::VerFileIterator V = Ver.FileList(); V.end() == false; V++)
374 Last = &V->NextFile;
375 VF->NextFile = *Last;
376 *Last = VF.Index();
377
dcb79bae
AL
378 VF->Offset = List.Offset();
379 VF->Size = List.Size();
ad00ae81
AL
380 if (Cache.HeaderP->MaxVerFileSize < VF->Size)
381 Cache.HeaderP->MaxVerFileSize = VF->Size;
a7e66b17
AL
382 Cache.HeaderP->VerFileCount++;
383
f55a958f 384 return true;
578bfd0a
AL
385}
386 /*}}}*/
387// CacheGenerator::NewVersion - Create a new Version /*{{{*/
388// ---------------------------------------------------------------------
f55a958f 389/* This puts a version structure in the linked list */
578bfd0a 390unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator &Ver,
171c75f1 391 const string &VerStr,
578bfd0a
AL
392 unsigned long Next)
393{
f55a958f
AL
394 // Get a structure
395 unsigned long Version = Map.Allocate(sizeof(pkgCache::Version));
396 if (Version == 0)
0149949b 397 return 0;
f55a958f
AL
398
399 // Fill it in
400 Ver = pkgCache::VerIterator(Cache,Cache.VerP + Version);
f55a958f
AL
401 Ver->NextVer = Next;
402 Ver->ID = Cache.HeaderP->VersionCount++;
403 Ver->VerStr = Map.WriteString(VerStr);
404 if (Ver->VerStr == 0)
0149949b 405 return 0;
f55a958f 406
0149949b 407 return Version;
578bfd0a
AL
408}
409 /*}}}*/
a52f938b
OS
410// CacheGenerator::NewFileDesc - Create a new File<->Desc association /*{{{*/
411// ---------------------------------------------------------------------
412/* */
413bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator &Desc,
414 ListParser &List)
415{
416 if (CurrentFile == 0)
417 return true;
418
419 // Get a structure
420 unsigned long DescFile = Map.Allocate(sizeof(pkgCache::DescFile));
421 if (DescFile == 0)
422 return 0;
770c32ec 423
a52f938b
OS
424 pkgCache::DescFileIterator DF(Cache,Cache.DescFileP + DescFile);
425 DF->File = CurrentFile - Cache.PkgFileP;
770c32ec 426
a52f938b
OS
427 // Link it to the end of the list
428 map_ptrloc *Last = &Desc->FileList;
429 for (pkgCache::DescFileIterator D = Desc.FileList(); D.end() == false; D++)
430 Last = &D->NextFile;
770c32ec 431
a52f938b
OS
432 DF->NextFile = *Last;
433 *Last = DF.Index();
434
435 DF->Offset = List.Offset();
436 DF->Size = List.Size();
437 if (Cache.HeaderP->MaxDescFileSize < DF->Size)
438 Cache.HeaderP->MaxDescFileSize = DF->Size;
439 Cache.HeaderP->DescFileCount++;
440
441 return true;
442}
443 /*}}}*/
444// CacheGenerator::NewDescription - Create a new Description /*{{{*/
445// ---------------------------------------------------------------------
446/* This puts a description structure in the linked list */
447map_ptrloc pkgCacheGenerator::NewDescription(pkgCache::DescIterator &Desc,
c76c44b1 448 const string &Lang,
449 const MD5SumValue &md5sum,
a52f938b
OS
450 map_ptrloc Next)
451{
452 // Get a structure
453 map_ptrloc Description = Map.Allocate(sizeof(pkgCache::Description));
454 if (Description == 0)
455 return 0;
456
457 // Fill it in
458 Desc = pkgCache::DescIterator(Cache,Cache.DescP + Description);
459 Desc->NextDesc = Next;
460 Desc->ID = Cache.HeaderP->DescriptionCount++;
461 Desc->language_code = Map.WriteString(Lang);
462 Desc->md5sum = Map.WriteString(md5sum.Value());
463
464 return Description;
465}
466 /*}}}*/
dcb79bae
AL
467// ListParser::NewDepends - Create a dependency element /*{{{*/
468// ---------------------------------------------------------------------
469/* This creates a dependency element in the tree. It is linked to the
470 version and to the package that it is pointing to. */
471bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver,
171c75f1
MV
472 const string &PackageName,
473 const string &Version,
dcb79bae
AL
474 unsigned int Op,
475 unsigned int Type)
476{
477 pkgCache &Cache = Owner->Cache;
478
479 // Get a structure
480 unsigned long Dependency = Owner->Map.Allocate(sizeof(pkgCache::Dependency));
481 if (Dependency == 0)
482 return false;
483
484 // Fill it in
485 pkgCache::DepIterator Dep(Cache,Cache.DepP + Dependency);
486 Dep->ParentVer = Ver.Index();
487 Dep->Type = Type;
488 Dep->CompareOp = Op;
489 Dep->ID = Cache.HeaderP->DependsCount++;
490
491 // Locate the target package
8efa2a3b
AL
492 pkgCache::PkgIterator Pkg;
493 if (Owner->NewPackage(Pkg,PackageName) == false)
494 return false;
dcb79bae
AL
495
496 // Probe the reverse dependency list for a version string that matches
497 if (Version.empty() == false)
498 {
b2e465d6 499/* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
dcb79bae 500 if (I->Version != 0 && I.TargetVer() == Version)
f9eec0e7 501 Dep->Version = I->Version;*/
dcb79bae
AL
502 if (Dep->Version == 0)
503 if ((Dep->Version = WriteString(Version)) == 0)
504 return false;
505 }
c1a22377 506
dcb79bae
AL
507 // Link it to the package
508 Dep->Package = Pkg.Index();
509 Dep->NextRevDepends = Pkg->RevDepends;
510 Pkg->RevDepends = Dep.Index();
511
c1a22377
AL
512 /* Link it to the version (at the end of the list)
513 Caching the old end point speeds up generation substantially */
f9eec0e7 514 if (OldDepVer != Ver)
c1a22377 515 {
f9eec0e7 516 OldDepLast = &Ver->DependsList;
c1a22377 517 for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
f9eec0e7
AL
518 OldDepLast = &D->NextDepends;
519 OldDepVer = Ver;
c1a22377 520 }
45415543
AL
521
522 // Is it a file dependency?
523 if (PackageName[0] == '/')
524 FoundFileDeps = true;
dcb79bae 525
f9eec0e7
AL
526 Dep->NextDepends = *OldDepLast;
527 *OldDepLast = Dep.Index();
528 OldDepLast = &Dep->NextDepends;
c1a22377 529
dcb79bae
AL
530 return true;
531}
532 /*}}}*/
533// ListParser::NewProvides - Create a Provides element /*{{{*/
534// ---------------------------------------------------------------------
535/* */
536bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver,
171c75f1
MV
537 const string &PackageName,
538 const string &Version)
dcb79bae
AL
539{
540 pkgCache &Cache = Owner->Cache;
8efa2a3b
AL
541
542 // We do not add self referencing provides
543 if (Ver.ParentPkg().Name() == PackageName)
544 return true;
dcb79bae
AL
545
546 // Get a structure
547 unsigned long Provides = Owner->Map.Allocate(sizeof(pkgCache::Provides));
548 if (Provides == 0)
549 return false;
a7e66b17 550 Cache.HeaderP->ProvidesCount++;
dcb79bae
AL
551
552 // Fill it in
553 pkgCache::PrvIterator Prv(Cache,Cache.ProvideP + Provides,Cache.PkgP);
554 Prv->Version = Ver.Index();
555 Prv->NextPkgProv = Ver->ProvidesList;
556 Ver->ProvidesList = Prv.Index();
b2e465d6 557 if (Version.empty() == false && (Prv->ProvideVersion = WriteString(Version)) == 0)
dcb79bae
AL
558 return false;
559
560 // Locate the target package
8efa2a3b
AL
561 pkgCache::PkgIterator Pkg;
562 if (Owner->NewPackage(Pkg,PackageName) == false)
563 return false;
dcb79bae
AL
564
565 // Link it to the package
566 Prv->ParentPkg = Pkg.Index();
567 Prv->NextProvides = Pkg->ProvidesList;
568 Pkg->ProvidesList = Prv.Index();
569
570 return true;
571}
572 /*}}}*/
578bfd0a
AL
573// CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
574// ---------------------------------------------------------------------
575/* This is used to select which file is to be associated with all newly
b2e465d6 576 added versions. The caller is responsible for setting the IMS fields. */
171c75f1 577bool pkgCacheGenerator::SelectFile(const string &File,const string &Site,
b2e465d6
AL
578 const pkgIndexFile &Index,
579 unsigned long Flags)
578bfd0a 580{
578bfd0a
AL
581 // Get some space for the structure
582 CurrentFile = Cache.PkgFileP + Map.Allocate(sizeof(*CurrentFile));
583 if (CurrentFile == Cache.PkgFileP)
584 return false;
585
586 // Fill it in
587 CurrentFile->FileName = Map.WriteString(File);
b2e465d6 588 CurrentFile->Site = WriteUniqString(Site);
578bfd0a
AL
589 CurrentFile->NextFile = Cache.HeaderP->FileList;
590 CurrentFile->Flags = Flags;
e1b74f61 591 CurrentFile->ID = Cache.HeaderP->PackageFileCount;
b2e465d6 592 CurrentFile->IndexType = WriteUniqString(Index.GetType()->Label);
578bfd0a 593 PkgFileName = File;
ad00ae81 594 Cache.HeaderP->FileList = CurrentFile - Cache.PkgFileP;
b35d2f5f 595 Cache.HeaderP->PackageFileCount++;
b2e465d6 596
578bfd0a
AL
597 if (CurrentFile->FileName == 0)
598 return false;
404ec98e 599
ddc1d8d0 600 if (Progress != 0)
b2e465d6 601 Progress->SubProgress(Index.Size());
8efa2a3b 602 return true;
578bfd0a
AL
603}
604 /*}}}*/
f55a958f
AL
605// CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
606// ---------------------------------------------------------------------
607/* This is used to create handles to strings. Given the same text it
608 always returns the same number */
609unsigned long pkgCacheGenerator::WriteUniqString(const char *S,
610 unsigned int Size)
611{
f9eec0e7
AL
612 /* We use a very small transient hash table here, this speeds up generation
613 by a fair amount on slower machines */
614 pkgCache::StringItem *&Bucket = UniqHash[(S[0]*5 + S[1]) % _count(UniqHash)];
615 if (Bucket != 0 &&
616 stringcmp(S,S+Size,Cache.StrP + Bucket->String) == 0)
617 return Bucket->String;
618
f55a958f
AL
619 // Search for an insertion point
620 pkgCache::StringItem *I = Cache.StringItemP + Cache.HeaderP->StringList;
621 int Res = 1;
349cd3b8 622 map_ptrloc *Last = &Cache.HeaderP->StringList;
f55a958f
AL
623 for (; I != Cache.StringItemP; Last = &I->NextItem,
624 I = Cache.StringItemP + I->NextItem)
625 {
9c14e3d6 626 Res = stringcmp(S,S+Size,Cache.StrP + I->String);
f55a958f
AL
627 if (Res >= 0)
628 break;
629 }
630
631 // Match
632 if (Res == 0)
f9eec0e7
AL
633 {
634 Bucket = I;
0149949b 635 return I->String;
f9eec0e7 636 }
f55a958f
AL
637
638 // Get a structure
639 unsigned long Item = Map.Allocate(sizeof(pkgCache::StringItem));
640 if (Item == 0)
0149949b
AL
641 return 0;
642
f55a958f
AL
643 // Fill in the structure
644 pkgCache::StringItem *ItemP = Cache.StringItemP + Item;
645 ItemP->NextItem = I - Cache.StringItemP;
646 *Last = Item;
647 ItemP->String = Map.WriteString(S,Size);
648 if (ItemP->String == 0)
0149949b 649 return 0;
f55a958f 650
f9eec0e7 651 Bucket = ItemP;
0149949b 652 return ItemP->String;
f55a958f
AL
653}
654 /*}}}*/
b2e465d6 655// CheckValidity - Check that a cache is up-to-date /*{{{*/
b35d2f5f 656// ---------------------------------------------------------------------
b2e465d6
AL
657/* This just verifies that each file in the list of index files exists,
658 has matching attributes with the cache and the cache does not have
659 any extra files. */
171c75f1 660static bool CheckValidity(const string &CacheFile, FileIterator Start,
e7b470ee 661 FileIterator End,MMap **OutMap = 0)
b35d2f5f 662{
b2e465d6
AL
663 // No file, certainly invalid
664 if (CacheFile.empty() == true || FileExists(CacheFile) == false)
b35d2f5f
AL
665 return false;
666
b2e465d6 667 // Map it
b35d2f5f 668 FileFd CacheF(CacheFile,FileFd::ReadOnly);
b2e465d6 669 SPtr<MMap> Map = new MMap(CacheF,MMap::Public | MMap::ReadOnly);
b35d2f5f 670 pkgCache Cache(Map);
b2e465d6 671 if (_error->PendingError() == true || Map->Size() == 0)
b35d2f5f
AL
672 {
673 _error->Discard();
674 return false;
675 }
b35d2f5f 676
b2e465d6
AL
677 /* Now we check every index file, see if it is in the cache,
678 verify the IMS data and check that it is on the disk too.. */
679 SPtrArray<bool> Visited = new bool[Cache.HeaderP->PackageFileCount];
680 memset(Visited,0,sizeof(*Visited)*Cache.HeaderP->PackageFileCount);
681 for (; Start != End; Start++)
a77ad7c3 682 {
b2e465d6
AL
683 if ((*Start)->HasPackages() == false)
684 continue;
a77ad7c3 685
b2e465d6 686 if ((*Start)->Exists() == false)
b35d2f5f 687 {
a791a450 688#if 0 // mvo: we no longer give a message here (Default Sources spec)
b2e465d6
AL
689 _error->WarningE("stat",_("Couldn't stat source package list %s"),
690 (*Start)->Describe().c_str());
a791a450 691#endif
b2e465d6 692 continue;
b35d2f5f 693 }
b2e465d6
AL
694
695 // FindInCache is also expected to do an IMS check.
696 pkgCache::PkgFileIterator File = (*Start)->FindInCache(Cache);
697 if (File.end() == true)
b35d2f5f 698 return false;
a52f938b 699
b2e465d6 700 Visited[File->ID] = true;
b35d2f5f
AL
701 }
702
b2e465d6
AL
703 for (unsigned I = 0; I != Cache.HeaderP->PackageFileCount; I++)
704 if (Visited[I] == false)
705 return false;
b35d2f5f 706
b35d2f5f
AL
707 if (_error->PendingError() == true)
708 {
709 _error->Discard();
710 return false;
711 }
b35d2f5f 712
b2e465d6
AL
713 if (OutMap != 0)
714 *OutMap = Map.UnGuard();
b35d2f5f
AL
715 return true;
716}
717 /*}}}*/
b2e465d6 718// ComputeSize - Compute the total size of a bunch of files /*{{{*/
b35d2f5f 719// ---------------------------------------------------------------------
b2e465d6
AL
720/* Size is kind of an abstract notion that is only used for the progress
721 meter */
e7b470ee 722static unsigned long ComputeSize(FileIterator Start,FileIterator End)
b35d2f5f 723{
b2e465d6
AL
724 unsigned long TotalSize = 0;
725 for (; Start != End; Start++)
b35d2f5f 726 {
b2e465d6
AL
727 if ((*Start)->HasPackages() == false)
728 continue;
729 TotalSize += (*Start)->Size();
b35d2f5f 730 }
b2e465d6 731 return TotalSize;
2d11135a
AL
732}
733 /*}}}*/
b2e465d6 734// BuildCache - Merge the list of index files into the cache /*{{{*/
2d11135a 735// ---------------------------------------------------------------------
b2e465d6
AL
736/* */
737static bool BuildCache(pkgCacheGenerator &Gen,
738 OpProgress &Progress,
739 unsigned long &CurrentSize,unsigned long TotalSize,
e7b470ee 740 FileIterator Start, FileIterator End)
2d11135a 741{
45415543
AL
742 FileIterator I;
743 for (I = Start; I != End; I++)
2d11135a 744 {
45415543 745 if ((*I)->HasPackages() == false)
2d11135a
AL
746 continue;
747
45415543 748 if ((*I)->Exists() == false)
2d11135a 749 continue;
b2e465d6 750
45415543 751 if ((*I)->FindInCache(Gen.GetCache()).end() == false)
a77ad7c3
AL
752 {
753 _error->Warning("Duplicate sources.list entry %s",
45415543 754 (*I)->Describe().c_str());
a77ad7c3
AL
755 continue;
756 }
757
45415543 758 unsigned long Size = (*I)->Size();
db0db9fe 759 Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Reading package lists"));
b2e465d6 760 CurrentSize += Size;
2d11135a 761
45415543 762 if ((*I)->Merge(Gen,Progress) == false)
b2e465d6
AL
763 return false;
764 }
45415543
AL
765
766 if (Gen.HasFileDeps() == true)
767 {
768 Progress.Done();
769 TotalSize = ComputeSize(Start, End);
770 CurrentSize = 0;
771 for (I = Start; I != End; I++)
772 {
773 unsigned long Size = (*I)->Size();
774 Progress.OverallProgress(CurrentSize,TotalSize,Size,_("Collecting File Provides"));
775 CurrentSize += Size;
776 if ((*I)->MergeFileProvides(Gen,Progress) == false)
777 return false;
778 }
779 }
2d11135a 780
b35d2f5f
AL
781 return true;
782}
783 /*}}}*/
b2e465d6 784// MakeStatusCache - Construct the status cache /*{{{*/
b35d2f5f 785// ---------------------------------------------------------------------
b2e465d6
AL
786/* This makes sure that the status cache (the cache that has all
787 index files from the sources list and all local ones) is ready
788 to be mmaped. If OutMap is not zero then a MMap object representing
789 the cache will be stored there. This is pretty much mandetory if you
790 are using AllowMem. AllowMem lets the function be run as non-root
791 where it builds the cache 'fast' into a memory buffer. */
792bool pkgMakeStatusCache(pkgSourceList &List,OpProgress &Progress,
793 MMap **OutMap,bool AllowMem)
b35d2f5f 794{
5177f802 795 unsigned long MapSize = _config->FindI("APT::Cache-Limit",24*1024*1024);
67db871e 796
7db98ffc
MZ
797 vector<pkgIndexFile *> Files;
798 for (vector<metaIndex *>::const_iterator i = List.begin();
799 i != List.end();
800 i++)
801 {
802 vector <pkgIndexFile *> *Indexes = (*i)->GetIndexFiles();
803 for (vector<pkgIndexFile *>::const_iterator j = Indexes->begin();
804 j != Indexes->end();
805 j++)
806 Files.push_back (*j);
807 }
808
b2e465d6
AL
809 unsigned long EndOfSource = Files.size();
810 if (_system->AddStatusFiles(Files) == false)
811 return false;
8ce4327b 812
b2e465d6 813 // Decide if we can write to the files..
3b5421b4 814 string CacheFile = _config->FindFile("Dir::Cache::pkgcache");
b2e465d6
AL
815 string SrcCacheFile = _config->FindFile("Dir::Cache::srcpkgcache");
816
817 // Decide if we can write to the cache
818 bool Writeable = false;
819 if (CacheFile.empty() == false)
820 Writeable = access(flNotFile(CacheFile).c_str(),W_OK) == 0;
821 else
822 if (SrcCacheFile.empty() == false)
823 Writeable = access(flNotFile(SrcCacheFile).c_str(),W_OK) == 0;
824
825 if (Writeable == false && AllowMem == false && CacheFile.empty() == false)
826 return _error->Error(_("Unable to write to %s"),flNotFile(CacheFile).c_str());
827
db0db9fe 828 Progress.OverallProgress(0,1,1,_("Reading package lists"));
b2e465d6
AL
829
830 // Cache is OK, Fin.
831 if (CheckValidity(CacheFile,Files.begin(),Files.end(),OutMap) == true)
832 {
db0db9fe 833 Progress.OverallProgress(1,1,1,_("Reading package lists"));
b2e465d6
AL
834 return true;
835 }
836
837 /* At this point we know we need to reconstruct the package cache,
838 begin. */
839 SPtr<FileFd> CacheF;
840 SPtr<DynamicMMap> Map;
841 if (Writeable == true && CacheFile.empty() == false)
842 {
843 unlink(CacheFile.c_str());
844 CacheF = new FileFd(CacheFile,FileFd::WriteEmpty);
7a3c2ab0 845 fchmod(CacheF->Fd(),0644);
b2e465d6 846 Map = new DynamicMMap(*CacheF,MMap::Public,MapSize);
b35d2f5f
AL
847 if (_error->PendingError() == true)
848 return false;
b35d2f5f 849 }
b2e465d6 850 else
8ce4327b 851 {
b2e465d6
AL
852 // Just build it in memory..
853 Map = new DynamicMMap(MMap::Public,MapSize);
8ce4327b 854 }
b35d2f5f 855
b2e465d6 856 // Lets try the source cache.
b35d2f5f 857 unsigned long CurrentSize = 0;
b2e465d6
AL
858 unsigned long TotalSize = 0;
859 if (CheckValidity(SrcCacheFile,Files.begin(),
860 Files.begin()+EndOfSource) == true)
2d11135a 861 {
b2e465d6
AL
862 // Preload the map with the source cache
863 FileFd SCacheF(SrcCacheFile,FileFd::ReadOnly);
864 if (SCacheF.Read((unsigned char *)Map->Data() + Map->RawAllocate(SCacheF.Size()),
865 SCacheF.Size()) == false)
866 return false;
867
868 TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
2d11135a 869
b2e465d6
AL
870 // Build the status cache
871 pkgCacheGenerator Gen(Map.Get(),&Progress);
2d11135a 872 if (_error->PendingError() == true)
b2e465d6
AL
873 return false;
874 if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
875 Files.begin()+EndOfSource,Files.end()) == false)
876 return false;
877 }
878 else
2d11135a 879 {
b2e465d6 880 TotalSize = ComputeSize(Files.begin(),Files.end());
2d11135a 881
b2e465d6
AL
882 // Build the source cache
883 pkgCacheGenerator Gen(Map.Get(),&Progress);
884 if (_error->PendingError() == true)
885 return false;
886 if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
887 Files.begin(),Files.begin()+EndOfSource) == false)
888 return false;
2d11135a 889
b2e465d6
AL
890 // Write it back
891 if (Writeable == true && SrcCacheFile.empty() == false)
2d11135a 892 {
b2e465d6
AL
893 FileFd SCacheF(SrcCacheFile,FileFd::WriteEmpty);
894 if (_error->PendingError() == true)
895 return false;
7a3c2ab0
AL
896
897 fchmod(SCacheF.Fd(),0644);
898
b2e465d6
AL
899 // Write out the main data
900 if (SCacheF.Write(Map->Data(),Map->Size()) == false)
901 return _error->Error(_("IO Error saving source cache"));
902 SCacheF.Sync();
903
904 // Write out the proper header
905 Gen.GetCache().HeaderP->Dirty = false;
906 if (SCacheF.Seek(0) == false ||
907 SCacheF.Write(Map->Data(),sizeof(*Gen.GetCache().HeaderP)) == false)
908 return _error->Error(_("IO Error saving source cache"));
b2e465d6 909 Gen.GetCache().HeaderP->Dirty = true;
7a3c2ab0 910 SCacheF.Sync();
2d11135a
AL
911 }
912
b2e465d6
AL
913 // Build the status cache
914 if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
915 Files.begin()+EndOfSource,Files.end()) == false)
916 return false;
2d11135a
AL
917 }
918
b2e465d6
AL
919 if (_error->PendingError() == true)
920 return false;
921 if (OutMap != 0)
2d11135a 922 {
b2e465d6 923 if (CacheF != 0)
2d11135a 924 {
b2e465d6
AL
925 delete Map.UnGuard();
926 *OutMap = new MMap(*CacheF,MMap::Public | MMap::ReadOnly);
2d11135a 927 }
b2e465d6
AL
928 else
929 {
930 *OutMap = Map.UnGuard();
931 }
2d11135a
AL
932 }
933
b2e465d6
AL
934 return true;
935}
936 /*}}}*/
937// MakeOnlyStatusCache - Build a cache with just the status files /*{{{*/
938// ---------------------------------------------------------------------
939/* */
940bool pkgMakeOnlyStatusCache(OpProgress &Progress,DynamicMMap **OutMap)
941{
5177f802 942 unsigned long MapSize = _config->FindI("APT::Cache-Limit",20*1024*1024);
b2e465d6
AL
943 vector<pkgIndexFile *> Files;
944 unsigned long EndOfSource = Files.size();
945 if (_system->AddStatusFiles(Files) == false)
946 return false;
947
948 SPtr<DynamicMMap> Map;
949 Map = new DynamicMMap(MMap::Public,MapSize);
950 unsigned long CurrentSize = 0;
951 unsigned long TotalSize = 0;
952
953 TotalSize = ComputeSize(Files.begin()+EndOfSource,Files.end());
954
955 // Build the status cache
db0db9fe 956 Progress.OverallProgress(0,1,1,_("Reading package lists"));
b2e465d6 957 pkgCacheGenerator Gen(Map.Get(),&Progress);
2d11135a 958 if (_error->PendingError() == true)
b2e465d6
AL
959 return false;
960 if (BuildCache(Gen,Progress,CurrentSize,TotalSize,
961 Files.begin()+EndOfSource,Files.end()) == false)
962 return false;
2d11135a 963
b2e465d6
AL
964 if (_error->PendingError() == true)
965 return false;
966 *OutMap = Map.UnGuard();
2d11135a 967
b2e465d6 968 return true;
2d11135a
AL
969}
970 /*}}}*/