]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcachegen.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcachegen.cc,v 1.53.2.1 2003/12/24 23:09:17 mdz Exp $
4 /* ######################################################################
6 Package Cache Generator - Generator for the cache structure.
8 This builds the cache structure from the abstract package list parser.
10 ##################################################################### */
12 // Include Files /*{{{*/
13 #define APT_COMPATIBILITY 986
15 #include <apt-pkg/pkgcachegen.h>
16 #include <apt-pkg/error.h>
17 #include <apt-pkg/version.h>
18 #include <apt-pkg/progress.h>
19 #include <apt-pkg/sourcelist.h>
20 #include <apt-pkg/configuration.h>
21 #include <apt-pkg/aptconfiguration.h>
22 #include <apt-pkg/strutl.h>
23 #include <apt-pkg/sptr.h>
24 #include <apt-pkg/pkgsystem.h>
26 #include <apt-pkg/tagfile.h>
38 typedef vector
<pkgIndexFile
*>::iterator FileIterator
;
40 // CacheGenerator::pkgCacheGenerator - Constructor /*{{{*/
41 // ---------------------------------------------------------------------
42 /* We set the dirty flag and make sure that is written to the disk */
43 pkgCacheGenerator::pkgCacheGenerator(DynamicMMap
*pMap
,OpProgress
*Prog
) :
44 Map(*pMap
), Cache(pMap
,false), Progress(Prog
),
48 memset(UniqHash
,0,sizeof(UniqHash
));
50 if (_error
->PendingError() == true)
55 // Setup the map interface..
56 Cache
.HeaderP
= (pkgCache::Header
*)Map
.Data();
57 if (Map
.RawAllocate(sizeof(pkgCache::Header
)) == 0 && _error
->PendingError() == true)
60 Map
.UsePools(*Cache
.HeaderP
->Pools
,sizeof(Cache
.HeaderP
->Pools
)/sizeof(Cache
.HeaderP
->Pools
[0]));
63 *Cache
.HeaderP
= pkgCache::Header();
64 Cache
.HeaderP
->VerSysName
= Map
.WriteString(_system
->VS
->Label
);
65 Cache
.HeaderP
->Architecture
= Map
.WriteString(_config
->Find("APT::Architecture"));
70 // Map directly from the existing file
72 Map
.UsePools(*Cache
.HeaderP
->Pools
,sizeof(Cache
.HeaderP
->Pools
)/sizeof(Cache
.HeaderP
->Pools
[0]));
73 if (Cache
.VS
!= _system
->VS
)
75 _error
->Error(_("Cache has an incompatible versioning system"));
80 Cache
.HeaderP
->Dirty
= true;
81 Map
.Sync(0,sizeof(pkgCache::Header
));
84 // CacheGenerator::~pkgCacheGenerator - Destructor /*{{{*/
85 // ---------------------------------------------------------------------
86 /* We sync the data then unset the dirty flag in two steps so as to
87 advoid a problem during a crash */
88 pkgCacheGenerator::~pkgCacheGenerator()
90 if (_error
->PendingError() == true)
92 if (Map
.Sync() == false)
95 Cache
.HeaderP
->Dirty
= false;
96 Map
.Sync(0,sizeof(pkgCache::Header
));
99 // CacheGenerator::MergeList - Merge the package list /*{{{*/
100 // ---------------------------------------------------------------------
101 /* This provides the generation of the entries in the cache. Each loop
102 goes through a single package record from the underlying parse engine. */
103 bool pkgCacheGenerator::MergeList(ListParser
&List
,
104 pkgCache::VerIterator
*OutVer
)
108 unsigned int Counter
= 0;
109 while (List
.Step() == true)
111 string
const PackageName
= List
.Package();
112 if (PackageName
.empty() == true)
115 /* As we handle Arch all packages as architecture bounded
116 we add all information to every (simulated) arch package */
117 std::vector
<string
> genArch
;
118 if (List
.ArchitectureAll() == true)
119 genArch
= APT::Configuration::getArchitectures();
121 genArch
.push_back(List
.Architecture());
123 for (std::vector
<string
>::const_iterator arch
= genArch
.begin();
124 arch
!= genArch
.end(); ++arch
)
126 // Get a pointer to the package structure
127 pkgCache::PkgIterator Pkg
;
128 if (NewPackage(Pkg
, PackageName
, *arch
) == false)
129 return _error
->Error(_("Error occurred while processing %s (NewPackage)"),PackageName
.c_str());
131 if (Counter
% 100 == 0 && Progress
!= 0)
132 Progress
->Progress(List
.Offset());
134 /* Get a pointer to the version structure. We know the list is sorted
135 so we use that fact in the search. Insertion of new versions is
136 done with correct sorting */
137 string Version
= List
.Version();
138 if (Version
.empty() == true)
140 // we first process the package, then the descriptions
141 // (this has the bonus that we get MMap error when we run out
143 if (List
.UsePackage(Pkg
,pkgCache::VerIterator(Cache
)) == false)
144 return _error
->Error(_("Error occurred while processing %s (UsePackage1)"),
145 PackageName
.c_str());
147 // Find the right version to write the description
148 MD5SumValue CurMd5
= List
.Description_md5();
149 pkgCache::VerIterator Ver
= Pkg
.VersionList();
150 map_ptrloc
*LastVer
= &Pkg
->VersionList
;
152 for (; Ver
.end() == false; LastVer
= &Ver
->NextVer
, Ver
++)
154 pkgCache::DescIterator Desc
= Ver
.DescriptionList();
155 map_ptrloc
*LastDesc
= &Ver
->DescriptionList
;
156 bool duplicate
=false;
158 // don't add a new description if we have one for the given
160 for ( ; Desc
.end() == false; Desc
++)
161 if (MD5SumValue(Desc
.md5()) == CurMd5
&&
162 Desc
.LanguageCode() == List
.DescriptionLanguage())
167 for (Desc
= Ver
.DescriptionList();
169 LastDesc
= &Desc
->NextDesc
, Desc
++)
171 if (MD5SumValue(Desc
.md5()) == CurMd5
)
173 // Add new description
174 *LastDesc
= NewDescription(Desc
, List
.DescriptionLanguage(), CurMd5
, *LastDesc
);
175 Desc
->ParentPkg
= Pkg
.Index();
177 if ((*LastDesc
== 0 && _error
->PendingError()) || NewFileDesc(Desc
,List
) == false)
178 return _error
->Error(_("Error occurred while processing %s (NewFileDesc1)"),PackageName
.c_str());
187 pkgCache::VerIterator Ver
= Pkg
.VersionList();
188 map_ptrloc
*LastVer
= &Pkg
->VersionList
;
190 for (; Ver
.end() == false; LastVer
= &Ver
->NextVer
, Ver
++)
192 Res
= Cache
.VS
->CmpVersion(Version
,Ver
.VerStr());
197 /* We already have a version for this item, record that we
199 unsigned long Hash
= List
.VersionHash();
200 if (Res
== 0 && Ver
->Hash
== Hash
)
202 if (List
.UsePackage(Pkg
,Ver
) == false)
203 return _error
->Error(_("Error occurred while processing %s (UsePackage2)"),
204 PackageName
.c_str());
206 if (NewFileVer(Ver
,List
) == false)
207 return _error
->Error(_("Error occurred while processing %s (NewFileVer1)"),
208 PackageName
.c_str());
210 // Read only a single record and return
214 FoundFileDeps
|= List
.HasFileDeps();
221 // Skip to the end of the same version set.
224 for (; Ver
.end() == false; LastVer
= &Ver
->NextVer
, Ver
++)
226 Res
= Cache
.VS
->CmpVersion(Version
,Ver
.VerStr());
233 *LastVer
= NewVersion(Ver
,Version
,*LastVer
);
234 Ver
->ParentPkg
= Pkg
.Index();
237 if ((*LastVer
== 0 && _error
->PendingError()) || List
.NewVersion(Ver
) == false)
238 return _error
->Error(_("Error occurred while processing %s (NewVersion1)"),
239 PackageName
.c_str());
241 if (List
.UsePackage(Pkg
,Ver
) == false)
242 return _error
->Error(_("Error occurred while processing %s (UsePackage3)"),
243 PackageName
.c_str());
245 if (NewFileVer(Ver
,List
) == false)
246 return _error
->Error(_("Error occurred while processing %s (NewVersion2)"),
247 PackageName
.c_str());
249 // Read only a single record and return
253 FoundFileDeps
|= List
.HasFileDeps();
257 /* Record the Description data. Description data always exist in
258 Packages and Translation-* files. */
259 pkgCache::DescIterator Desc
= Ver
.DescriptionList();
260 map_ptrloc
*LastDesc
= &Ver
->DescriptionList
;
262 // Skip to the end of description set
263 for (; Desc
.end() == false; LastDesc
= &Desc
->NextDesc
, Desc
++);
265 // Add new description
266 *LastDesc
= NewDescription(Desc
, List
.DescriptionLanguage(), List
.Description_md5(), *LastDesc
);
267 Desc
->ParentPkg
= Pkg
.Index();
269 if ((*LastDesc
== 0 && _error
->PendingError()) || NewFileDesc(Desc
,List
) == false)
270 return _error
->Error(_("Error occurred while processing %s (NewFileDesc2)"),PackageName
.c_str());
274 FoundFileDeps
|= List
.HasFileDeps();
276 if (Cache
.HeaderP
->PackageCount
>= (1ULL<<sizeof(Cache
.PkgP
->ID
)*8)-1)
277 return _error
->Error(_("Wow, you exceeded the number of package "
278 "names this APT is capable of."));
279 if (Cache
.HeaderP
->VersionCount
>= (1ULL<<(sizeof(Cache
.VerP
->ID
)*8))-1)
280 return _error
->Error(_("Wow, you exceeded the number of versions "
281 "this APT is capable of."));
282 if (Cache
.HeaderP
->DescriptionCount
>= (1ULL<<(sizeof(Cache
.DescP
->ID
)*8))-1)
283 return _error
->Error(_("Wow, you exceeded the number of descriptions "
284 "this APT is capable of."));
285 if (Cache
.HeaderP
->DependsCount
>= (1ULL<<(sizeof(Cache
.DepP
->ID
)*8))-1ULL)
286 return _error
->Error(_("Wow, you exceeded the number of dependencies "
287 "this APT is capable of."));
291 // CacheGenerator::MergeFileProvides - Merge file provides /*{{{*/
292 // ---------------------------------------------------------------------
293 /* If we found any file depends while parsing the main list we need to
294 resolve them. Since it is undesired to load the entire list of files
295 into the cache as virtual packages we do a two stage effort. MergeList
296 identifies the file depends and this creates Provdies for them by
297 re-parsing all the indexs. */
298 bool pkgCacheGenerator::MergeFileProvides(ListParser
&List
)
302 unsigned int Counter
= 0;
303 while (List
.Step() == true)
305 string PackageName
= List
.Package();
306 if (PackageName
.empty() == true)
308 string Version
= List
.Version();
309 if (Version
.empty() == true)
312 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(PackageName
);
313 if (Pkg
.end() == true)
314 return _error
->Error(_("Error occurred while processing %s (FindPkg)"),
315 PackageName
.c_str());
317 if (Counter
% 100 == 0 && Progress
!= 0)
318 Progress
->Progress(List
.Offset());
320 unsigned long Hash
= List
.VersionHash();
321 pkgCache::VerIterator Ver
= Pkg
.VersionList();
322 for (; Ver
.end() == false; Ver
++)
324 if (Ver
->Hash
== Hash
&& Version
.c_str() == Ver
.VerStr())
326 if (List
.CollectFileProvides(Cache
,Ver
) == false)
327 return _error
->Error(_("Error occurred while processing %s (CollectFileProvides)"),PackageName
.c_str());
332 if (Ver
.end() == true)
333 _error
->Warning(_("Package %s %s was not found while processing file dependencies"),PackageName
.c_str(),Version
.c_str());
339 // CacheGenerator::NewGroup - Add a new group /*{{{*/
340 // ---------------------------------------------------------------------
341 /* This creates a new group structure and adds it to the hash table */
342 bool pkgCacheGenerator::NewGroup(pkgCache::GrpIterator
&Grp
, const string
&Name
) {
343 Grp
= Cache
.FindGrp(Name
);
344 if (Grp
.end() == false)
348 unsigned long const Group
= Map
.Allocate(sizeof(pkgCache::Group
));
349 if (unlikely(Group
== 0))
352 Grp
= pkgCache::GrpIterator(Cache
, Cache
.GrpP
+ Group
);
353 Grp
->Name
= Map
.WriteString(Name
);
354 if (unlikely(Grp
->Name
== 0))
357 // Insert it into the hash table
358 unsigned long const Hash
= Cache
.Hash(Name
);
359 Grp
->Next
= Cache
.HeaderP
->GrpHashTable
[Hash
];
360 Cache
.HeaderP
->GrpHashTable
[Hash
] = Group
;
362 Cache
.HeaderP
->GroupCount
++;
367 // CacheGenerator::NewPackage - Add a new package /*{{{*/
368 // ---------------------------------------------------------------------
369 /* This creates a new package structure and adds it to the hash table */
370 bool pkgCacheGenerator::NewPackage(pkgCache::PkgIterator
&Pkg
,const string
&Name
,
371 const string
&Arch
) {
372 pkgCache::GrpIterator Grp
;
373 if (unlikely(NewGroup(Grp
, Name
) == false))
376 Pkg
= Grp
.FindPkg(Arch
);
377 if (Pkg
.end() == false)
381 unsigned long const Package
= Map
.Allocate(sizeof(pkgCache::Package
));
382 if (unlikely(Package
== 0))
384 Pkg
= pkgCache::PkgIterator(Cache
,Cache
.PkgP
+ Package
);
386 // Insert it into the hash table
387 unsigned long const Hash
= Cache
.Hash(Name
);
388 Pkg
->NextPackage
= Cache
.HeaderP
->PkgHashTable
[Hash
];
389 Cache
.HeaderP
->PkgHashTable
[Hash
] = Package
;
391 // remember the packages in the group
392 Grp
->FirstPackage
= Package
;
393 if (Grp
->LastPackage
== 0)
394 Grp
->LastPackage
= Package
;
396 // Set the name, arch and the ID
397 Pkg
->Name
= Grp
->Name
;
398 Pkg
->Group
= Grp
.Index();
399 Pkg
->Arch
= WriteUniqString(Arch
.c_str());
400 if (unlikely(Pkg
->Arch
== 0))
402 Pkg
->ID
= Cache
.HeaderP
->PackageCount
++;
407 // CacheGenerator::NewFileVer - Create a new File<->Version association /*{{{*/
408 // ---------------------------------------------------------------------
410 bool pkgCacheGenerator::NewFileVer(pkgCache::VerIterator
&Ver
,
413 if (CurrentFile
== 0)
417 unsigned long VerFile
= Map
.Allocate(sizeof(pkgCache::VerFile
));
421 pkgCache::VerFileIterator
VF(Cache
,Cache
.VerFileP
+ VerFile
);
422 VF
->File
= CurrentFile
- Cache
.PkgFileP
;
424 // Link it to the end of the list
425 map_ptrloc
*Last
= &Ver
->FileList
;
426 for (pkgCache::VerFileIterator V
= Ver
.FileList(); V
.end() == false; V
++)
428 VF
->NextFile
= *Last
;
431 VF
->Offset
= List
.Offset();
432 VF
->Size
= List
.Size();
433 if (Cache
.HeaderP
->MaxVerFileSize
< VF
->Size
)
434 Cache
.HeaderP
->MaxVerFileSize
= VF
->Size
;
435 Cache
.HeaderP
->VerFileCount
++;
440 // CacheGenerator::NewVersion - Create a new Version /*{{{*/
441 // ---------------------------------------------------------------------
442 /* This puts a version structure in the linked list */
443 unsigned long pkgCacheGenerator::NewVersion(pkgCache::VerIterator
&Ver
,
444 const string
&VerStr
,
448 unsigned long Version
= Map
.Allocate(sizeof(pkgCache::Version
));
453 Ver
= pkgCache::VerIterator(Cache
,Cache
.VerP
+ Version
);
455 Ver
->ID
= Cache
.HeaderP
->VersionCount
++;
456 Ver
->VerStr
= Map
.WriteString(VerStr
);
457 if (Ver
->VerStr
== 0)
463 // CacheGenerator::NewFileDesc - Create a new File<->Desc association /*{{{*/
464 // ---------------------------------------------------------------------
466 bool pkgCacheGenerator::NewFileDesc(pkgCache::DescIterator
&Desc
,
469 if (CurrentFile
== 0)
473 unsigned long DescFile
= Map
.Allocate(sizeof(pkgCache::DescFile
));
477 pkgCache::DescFileIterator
DF(Cache
,Cache
.DescFileP
+ DescFile
);
478 DF
->File
= CurrentFile
- Cache
.PkgFileP
;
480 // Link it to the end of the list
481 map_ptrloc
*Last
= &Desc
->FileList
;
482 for (pkgCache::DescFileIterator D
= Desc
.FileList(); D
.end() == false; D
++)
485 DF
->NextFile
= *Last
;
488 DF
->Offset
= List
.Offset();
489 DF
->Size
= List
.Size();
490 if (Cache
.HeaderP
->MaxDescFileSize
< DF
->Size
)
491 Cache
.HeaderP
->MaxDescFileSize
= DF
->Size
;
492 Cache
.HeaderP
->DescFileCount
++;
497 // CacheGenerator::NewDescription - Create a new Description /*{{{*/
498 // ---------------------------------------------------------------------
499 /* This puts a description structure in the linked list */
500 map_ptrloc
pkgCacheGenerator::NewDescription(pkgCache::DescIterator
&Desc
,
502 const MD5SumValue
&md5sum
,
506 map_ptrloc Description
= Map
.Allocate(sizeof(pkgCache::Description
));
507 if (Description
== 0)
511 Desc
= pkgCache::DescIterator(Cache
,Cache
.DescP
+ Description
);
512 Desc
->NextDesc
= Next
;
513 Desc
->ID
= Cache
.HeaderP
->DescriptionCount
++;
514 Desc
->language_code
= Map
.WriteString(Lang
);
515 Desc
->md5sum
= Map
.WriteString(md5sum
.Value());
516 if (Desc
->language_code
== 0 || Desc
->md5sum
== 0)
522 // CacheGenerator::FinishCache - do various finish operations /*{{{*/
523 // ---------------------------------------------------------------------
524 /* This prepares the Cache for delivery */
525 bool pkgCacheGenerator::FinishCache(OpProgress
&Progress
) {
526 // FIXME: add progress reporting for this operation
527 // Do we have different architectures in your groups ?
528 vector
<string
> archs
= APT::Configuration::getArchitectures();
529 if (archs
.size() > 1) {
530 // Create Conflicts in between the group
531 for (pkgCache::GrpIterator G
= GetCache().GrpBegin(); G
.end() != true; G
++) {
532 string
const PkgName
= G
.Name();
533 for (pkgCache::PkgIterator P
= G
.PackageList(); P
.end() != true; P
= G
.NextPkg(P
)) {
534 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() != true; V
++) {
535 string
const Arch
= V
.Arch();
536 map_ptrloc
*OldDepLast
= NULL
;
537 /* MultiArch handling introduces a lot of implicit Dependencies:
538 - MultiArch: same → Co-Installable if they have the same version
539 - Architecture: all → Need to be Co-Installable for internal reasons
540 - All others conflict with all other group members */
541 bool const coInstall
= (V
->MultiArch
== pkgCache::Version::All
||
542 V
->MultiArch
== pkgCache::Version::Same
);
543 for (vector
<string
>::const_iterator A
= archs
.begin(); A
!= archs
.end(); ++A
) {
546 /* We allow only one installed arch at the time
547 per group, therefore each group member conflicts
548 with all other group members */
549 pkgCache::PkgIterator D
= G
.FindPkg(*A
);
552 if (coInstall
== true) {
553 // Replaces: ${self}:other ( << ${binary:Version})
554 NewDepends(D
, V
, V
.VerStr(),
555 pkgCache::Dep::Less
, pkgCache::Dep::Replaces
,
557 // Breaks: ${self}:other (!= ${binary:Version})
558 NewDepends(D
, V
, V
.VerStr(),
559 pkgCache::Dep::Less
, pkgCache::Dep::DpkgBreaks
,
561 NewDepends(D
, V
, V
.VerStr(),
562 pkgCache::Dep::Greater
, pkgCache::Dep::DpkgBreaks
,
565 // Conflicts: ${self}:other
567 pkgCache::Dep::NoOp
, pkgCache::Dep::Conflicts
,
578 // CacheGenerator::NewDepends - Create a dependency element /*{{{*/
579 // ---------------------------------------------------------------------
580 /* This creates a dependency element in the tree. It is linked to the
581 version and to the package that it is pointing to. */
582 bool pkgCacheGenerator::NewDepends(pkgCache::PkgIterator
&Pkg
,
583 pkgCache::VerIterator
&Ver
,
584 string
const &Version
,
585 unsigned int const &Op
,
586 unsigned int const &Type
,
587 map_ptrloc
*OldDepLast
)
590 unsigned long const Dependency
= Map
.Allocate(sizeof(pkgCache::Dependency
));
591 if (unlikely(Dependency
== 0))
595 pkgCache::DepIterator
Dep(Cache
,Cache
.DepP
+ Dependency
);
596 Dep
->ParentVer
= Ver
.Index();
599 Dep
->ID
= Cache
.HeaderP
->DependsCount
++;
601 // Probe the reverse dependency list for a version string that matches
602 if (Version
.empty() == false)
604 /* for (pkgCache::DepIterator I = Pkg.RevDependsList(); I.end() == false; I++)
605 if (I->Version != 0 && I.TargetVer() == Version)
606 Dep->Version = I->Version;*/
607 if (Dep
->Version
== 0)
608 if (unlikely((Dep
->Version
= Map
.WriteString(Version
)) == 0))
612 // Link it to the package
613 Dep
->Package
= Pkg
.Index();
614 Dep
->NextRevDepends
= Pkg
->RevDepends
;
615 Pkg
->RevDepends
= Dep
.Index();
617 // Do we know where to link the Dependency to?
618 if (OldDepLast
== NULL
)
620 OldDepLast
= &Ver
->DependsList
;
621 for (pkgCache::DepIterator D
= Ver
.DependsList(); D
.end() == false; D
++)
622 OldDepLast
= &D
->NextDepends
;
625 Dep
->NextDepends
= *OldDepLast
;
626 *OldDepLast
= Dep
.Index();
627 OldDepLast
= &Dep
->NextDepends
;
632 // ListParser::NewDepends - Create the environment for a new dependency /*{{{*/
633 // ---------------------------------------------------------------------
634 /* This creates a Group and the Package to link this dependency to if
635 needed and handles also the caching of the old endpoint */
636 bool pkgCacheGenerator::ListParser::NewDepends(pkgCache::VerIterator Ver
,
637 const string
&PackageName
,
639 const string
&Version
,
643 pkgCache::GrpIterator Grp
;
644 if (unlikely(Owner
->NewGroup(Grp
, PackageName
) == false))
647 // Locate the target package
648 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(Arch
);
649 if (Pkg
.end() == true) {
650 if (unlikely(Owner
->NewPackage(Pkg
, PackageName
, Arch
) == false))
654 // Is it a file dependency?
655 if (unlikely(PackageName
[0] == '/'))
656 FoundFileDeps
= true;
658 /* Caching the old end point speeds up generation substantially */
659 if (OldDepVer
!= Ver
) {
664 return Owner
->NewDepends(Pkg
, Ver
, Version
, Op
, Type
, OldDepLast
);
667 // ListParser::NewProvides - Create a Provides element /*{{{*/
668 // ---------------------------------------------------------------------
670 bool pkgCacheGenerator::ListParser::NewProvides(pkgCache::VerIterator Ver
,
671 const string
&PkgName
,
672 const string
&PkgArch
,
673 const string
&Version
)
675 pkgCache
&Cache
= Owner
->Cache
;
677 // We do not add self referencing provides
678 if (Ver
.ParentPkg().Name() == PkgName
&& PkgArch
== Ver
.Arch())
682 unsigned long const Provides
= Owner
->Map
.Allocate(sizeof(pkgCache::Provides
));
683 if (unlikely(Provides
== 0))
685 Cache
.HeaderP
->ProvidesCount
++;
688 pkgCache::PrvIterator
Prv(Cache
,Cache
.ProvideP
+ Provides
,Cache
.PkgP
);
689 Prv
->Version
= Ver
.Index();
690 Prv
->NextPkgProv
= Ver
->ProvidesList
;
691 Ver
->ProvidesList
= Prv
.Index();
692 if (Version
.empty() == false && unlikely((Prv
->ProvideVersion
= WriteString(Version
)) == 0))
695 // Locate the target package
696 pkgCache::PkgIterator Pkg
;
697 if (unlikely(Owner
->NewPackage(Pkg
,PkgName
, PkgArch
) == false))
700 // Link it to the package
701 Prv
->ParentPkg
= Pkg
.Index();
702 Prv
->NextProvides
= Pkg
->ProvidesList
;
703 Pkg
->ProvidesList
= Prv
.Index();
708 // CacheGenerator::SelectFile - Select the current file being parsed /*{{{*/
709 // ---------------------------------------------------------------------
710 /* This is used to select which file is to be associated with all newly
711 added versions. The caller is responsible for setting the IMS fields. */
712 bool pkgCacheGenerator::SelectFile(const string
&File
,const string
&Site
,
713 const pkgIndexFile
&Index
,
716 // Get some space for the structure
717 CurrentFile
= Cache
.PkgFileP
+ Map
.Allocate(sizeof(*CurrentFile
));
718 if (CurrentFile
== Cache
.PkgFileP
)
722 CurrentFile
->FileName
= Map
.WriteString(File
);
723 CurrentFile
->Site
= WriteUniqString(Site
);
724 CurrentFile
->NextFile
= Cache
.HeaderP
->FileList
;
725 CurrentFile
->Flags
= Flags
;
726 CurrentFile
->ID
= Cache
.HeaderP
->PackageFileCount
;
727 CurrentFile
->IndexType
= WriteUniqString(Index
.GetType()->Label
);
729 Cache
.HeaderP
->FileList
= CurrentFile
- Cache
.PkgFileP
;
730 Cache
.HeaderP
->PackageFileCount
++;
732 if (CurrentFile
->FileName
== 0)
736 Progress
->SubProgress(Index
.Size());
740 // CacheGenerator::WriteUniqueString - Insert a unique string /*{{{*/
741 // ---------------------------------------------------------------------
742 /* This is used to create handles to strings. Given the same text it
743 always returns the same number */
744 unsigned long pkgCacheGenerator::WriteUniqString(const char *S
,
747 /* We use a very small transient hash table here, this speeds up generation
748 by a fair amount on slower machines */
749 pkgCache::StringItem
*&Bucket
= UniqHash
[(S
[0]*5 + S
[1]) % _count(UniqHash
)];
751 stringcmp(S
,S
+Size
,Cache
.StrP
+ Bucket
->String
) == 0)
752 return Bucket
->String
;
754 // Search for an insertion point
755 pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.HeaderP
->StringList
;
757 map_ptrloc
*Last
= &Cache
.HeaderP
->StringList
;
758 for (; I
!= Cache
.StringItemP
; Last
= &I
->NextItem
,
759 I
= Cache
.StringItemP
+ I
->NextItem
)
761 Res
= stringcmp(S
,S
+Size
,Cache
.StrP
+ I
->String
);
774 unsigned long Item
= Map
.Allocate(sizeof(pkgCache::StringItem
));
778 // Fill in the structure
779 pkgCache::StringItem
*ItemP
= Cache
.StringItemP
+ Item
;
780 ItemP
->NextItem
= I
- Cache
.StringItemP
;
782 ItemP
->String
= Map
.WriteString(S
,Size
);
783 if (ItemP
->String
== 0)
787 return ItemP
->String
;
790 // CheckValidity - Check that a cache is up-to-date /*{{{*/
791 // ---------------------------------------------------------------------
792 /* This just verifies that each file in the list of index files exists,
793 has matching attributes with the cache and the cache does not have
795 static bool CheckValidity(const string
&CacheFile
, FileIterator Start
,
796 FileIterator End
,MMap
**OutMap
= 0)
798 // No file, certainly invalid
799 if (CacheFile
.empty() == true || FileExists(CacheFile
) == false)
803 FileFd
CacheF(CacheFile
,FileFd::ReadOnly
);
804 SPtr
<MMap
> Map
= new MMap(CacheF
,0);
806 if (_error
->PendingError() == true || Map
->Size() == 0)
812 /* Now we check every index file, see if it is in the cache,
813 verify the IMS data and check that it is on the disk too.. */
814 SPtrArray
<bool> Visited
= new bool[Cache
.HeaderP
->PackageFileCount
];
815 memset(Visited
,0,sizeof(*Visited
)*Cache
.HeaderP
->PackageFileCount
);
816 for (; Start
!= End
; Start
++)
818 if ((*Start
)->HasPackages() == false)
821 if ((*Start
)->Exists() == false)
823 #if 0 // mvo: we no longer give a message here (Default Sources spec)
824 _error
->WarningE("stat",_("Couldn't stat source package list %s"),
825 (*Start
)->Describe().c_str());
830 // FindInCache is also expected to do an IMS check.
831 pkgCache::PkgFileIterator File
= (*Start
)->FindInCache(Cache
);
832 if (File
.end() == true)
835 Visited
[File
->ID
] = true;
838 for (unsigned I
= 0; I
!= Cache
.HeaderP
->PackageFileCount
; I
++)
839 if (Visited
[I
] == false)
842 if (_error
->PendingError() == true)
849 *OutMap
= Map
.UnGuard();
853 // ComputeSize - Compute the total size of a bunch of files /*{{{*/
854 // ---------------------------------------------------------------------
855 /* Size is kind of an abstract notion that is only used for the progress
857 static unsigned long ComputeSize(FileIterator Start
,FileIterator End
)
859 unsigned long TotalSize
= 0;
860 for (; Start
!= End
; Start
++)
862 if ((*Start
)->HasPackages() == false)
864 TotalSize
+= (*Start
)->Size();
869 // BuildCache - Merge the list of index files into the cache /*{{{*/
870 // ---------------------------------------------------------------------
872 static bool BuildCache(pkgCacheGenerator
&Gen
,
873 OpProgress
&Progress
,
874 unsigned long &CurrentSize
,unsigned long TotalSize
,
875 FileIterator Start
, FileIterator End
)
878 for (I
= Start
; I
!= End
; I
++)
880 if ((*I
)->HasPackages() == false)
883 if ((*I
)->Exists() == false)
886 if ((*I
)->FindInCache(Gen
.GetCache()).end() == false)
888 _error
->Warning("Duplicate sources.list entry %s",
889 (*I
)->Describe().c_str());
893 unsigned long Size
= (*I
)->Size();
894 Progress
.OverallProgress(CurrentSize
,TotalSize
,Size
,_("Reading package lists"));
897 if ((*I
)->Merge(Gen
,Progress
) == false)
901 if (Gen
.HasFileDeps() == true)
904 TotalSize
= ComputeSize(Start
, End
);
906 for (I
= Start
; I
!= End
; I
++)
908 unsigned long Size
= (*I
)->Size();
909 Progress
.OverallProgress(CurrentSize
,TotalSize
,Size
,_("Collecting File Provides"));
911 if ((*I
)->MergeFileProvides(Gen
,Progress
) == false)
919 // MakeStatusCache - Construct the status cache /*{{{*/
920 // ---------------------------------------------------------------------
921 /* This makes sure that the status cache (the cache that has all
922 index files from the sources list and all local ones) is ready
923 to be mmaped. If OutMap is not zero then a MMap object representing
924 the cache will be stored there. This is pretty much mandetory if you
925 are using AllowMem. AllowMem lets the function be run as non-root
926 where it builds the cache 'fast' into a memory buffer. */
927 bool pkgMakeStatusCache(pkgSourceList
&List
,OpProgress
&Progress
,
928 MMap
**OutMap
,bool AllowMem
)
930 unsigned long MapSize
= _config
->FindI("APT::Cache-Limit",24*1024*1024);
932 vector
<pkgIndexFile
*> Files
;
933 for (vector
<metaIndex
*>::const_iterator i
= List
.begin();
937 vector
<pkgIndexFile
*> *Indexes
= (*i
)->GetIndexFiles();
938 for (vector
<pkgIndexFile
*>::const_iterator j
= Indexes
->begin();
941 Files
.push_back (*j
);
944 unsigned long EndOfSource
= Files
.size();
945 if (_system
->AddStatusFiles(Files
) == false)
948 // Decide if we can write to the files..
949 string CacheFile
= _config
->FindFile("Dir::Cache::pkgcache");
950 string SrcCacheFile
= _config
->FindFile("Dir::Cache::srcpkgcache");
952 // Decide if we can write to the cache
953 bool Writeable
= false;
954 if (CacheFile
.empty() == false)
955 Writeable
= access(flNotFile(CacheFile
).c_str(),W_OK
) == 0;
957 if (SrcCacheFile
.empty() == false)
958 Writeable
= access(flNotFile(SrcCacheFile
).c_str(),W_OK
) == 0;
960 if (Writeable
== false && AllowMem
== false && CacheFile
.empty() == false)
961 return _error
->Error(_("Unable to write to %s"),flNotFile(CacheFile
).c_str());
963 Progress
.OverallProgress(0,1,1,_("Reading package lists"));
966 if (CheckValidity(CacheFile
,Files
.begin(),Files
.end(),OutMap
) == true)
968 Progress
.OverallProgress(1,1,1,_("Reading package lists"));
972 /* At this point we know we need to reconstruct the package cache,
975 SPtr
<DynamicMMap
> Map
;
976 if (Writeable
== true && CacheFile
.empty() == false)
978 unlink(CacheFile
.c_str());
979 CacheF
= new FileFd(CacheFile
,FileFd::WriteEmpty
);
980 fchmod(CacheF
->Fd(),0644);
981 Map
= new DynamicMMap(*CacheF
,MMap::Public
,MapSize
);
982 if (_error
->PendingError() == true)
987 // Just build it in memory..
988 Map
= new DynamicMMap(0,MapSize
);
991 // Lets try the source cache.
992 unsigned long CurrentSize
= 0;
993 unsigned long TotalSize
= 0;
994 if (CheckValidity(SrcCacheFile
,Files
.begin(),
995 Files
.begin()+EndOfSource
) == true)
997 // Preload the map with the source cache
998 FileFd
SCacheF(SrcCacheFile
,FileFd::ReadOnly
);
999 unsigned long alloc
= Map
->RawAllocate(SCacheF
.Size());
1000 if ((alloc
== 0 && _error
->PendingError())
1001 || SCacheF
.Read((unsigned char *)Map
->Data() + alloc
,
1002 SCacheF
.Size()) == false)
1005 TotalSize
= ComputeSize(Files
.begin()+EndOfSource
,Files
.end());
1007 // Build the status cache
1008 pkgCacheGenerator
Gen(Map
.Get(),&Progress
);
1009 if (_error
->PendingError() == true)
1011 if (BuildCache(Gen
,Progress
,CurrentSize
,TotalSize
,
1012 Files
.begin()+EndOfSource
,Files
.end()) == false)
1015 // FIXME: move me to a better place
1016 Gen
.FinishCache(Progress
);
1020 TotalSize
= ComputeSize(Files
.begin(),Files
.end());
1022 // Build the source cache
1023 pkgCacheGenerator
Gen(Map
.Get(),&Progress
);
1024 if (_error
->PendingError() == true)
1026 if (BuildCache(Gen
,Progress
,CurrentSize
,TotalSize
,
1027 Files
.begin(),Files
.begin()+EndOfSource
) == false)
1031 if (Writeable
== true && SrcCacheFile
.empty() == false)
1033 FileFd
SCacheF(SrcCacheFile
,FileFd::WriteEmpty
);
1034 if (_error
->PendingError() == true)
1037 fchmod(SCacheF
.Fd(),0644);
1039 // Write out the main data
1040 if (SCacheF
.Write(Map
->Data(),Map
->Size()) == false)
1041 return _error
->Error(_("IO Error saving source cache"));
1044 // Write out the proper header
1045 Gen
.GetCache().HeaderP
->Dirty
= false;
1046 if (SCacheF
.Seek(0) == false ||
1047 SCacheF
.Write(Map
->Data(),sizeof(*Gen
.GetCache().HeaderP
)) == false)
1048 return _error
->Error(_("IO Error saving source cache"));
1049 Gen
.GetCache().HeaderP
->Dirty
= true;
1053 // Build the status cache
1054 if (BuildCache(Gen
,Progress
,CurrentSize
,TotalSize
,
1055 Files
.begin()+EndOfSource
,Files
.end()) == false)
1058 // FIXME: move me to a better place
1059 Gen
.FinishCache(Progress
);
1062 if (_error
->PendingError() == true)
1068 delete Map
.UnGuard();
1069 *OutMap
= new MMap(*CacheF
,0);
1073 *OutMap
= Map
.UnGuard();
1080 // MakeOnlyStatusCache - Build a cache with just the status files /*{{{*/
1081 // ---------------------------------------------------------------------
1083 bool pkgMakeOnlyStatusCache(OpProgress
&Progress
,DynamicMMap
**OutMap
)
1085 unsigned long MapSize
= _config
->FindI("APT::Cache-Limit",20*1024*1024);
1086 vector
<pkgIndexFile
*> Files
;
1087 unsigned long EndOfSource
= Files
.size();
1088 if (_system
->AddStatusFiles(Files
) == false)
1091 SPtr
<DynamicMMap
> Map
= new DynamicMMap(0,MapSize
);
1092 unsigned long CurrentSize
= 0;
1093 unsigned long TotalSize
= 0;
1095 TotalSize
= ComputeSize(Files
.begin()+EndOfSource
,Files
.end());
1097 // Build the status cache
1098 Progress
.OverallProgress(0,1,1,_("Reading package lists"));
1099 pkgCacheGenerator
Gen(Map
.Get(),&Progress
);
1100 if (_error
->PendingError() == true)
1102 if (BuildCache(Gen
,Progress
,CurrentSize
,TotalSize
,
1103 Files
.begin()+EndOfSource
,Files
.end()) == false)
1106 // FIXME: move me to a better place
1107 Gen
.FinishCache(Progress
);
1109 if (_error
->PendingError() == true)
1111 *OutMap
= Map
.UnGuard();