1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcache.cc,v 1.37 2003/02/10 01:40:58 doogie Exp $
4 /* ######################################################################
6 Package Cache - Accessor code for the cache
8 Please see doc/apt-pkg/cache.sgml for a more detailed description of
9 this format. Also be sure to keep that file up-to-date!!
11 This is the general utility functions for cache management. They provide
12 a complete set of accessor functions for the cache. The cacheiterators
13 header contains the STL-like iterators that can be used to easially
14 navigate the cache as well as seemlessly dereference the mmap'd
15 indexes. Use these always.
17 The main class provides for ways to get package indexes and some
18 general lookup functions to start the iterators.
20 ##################################################################### */
22 // Include Files /*{{{*/
25 #include <apt-pkg/pkgcache.h>
26 #include <apt-pkg/policy.h>
27 #include <apt-pkg/version.h>
28 #include <apt-pkg/error.h>
29 #include <apt-pkg/strutl.h>
30 #include <apt-pkg/configuration.h>
31 #include <apt-pkg/aptconfiguration.h>
32 #include <apt-pkg/mmap.h>
33 #include <apt-pkg/macros.h>
48 using APT::StringView
;
51 // Cache::Header::Header - Constructor /*{{{*/
52 // ---------------------------------------------------------------------
53 /* Simply initialize the header */
54 pkgCache::Header::Header()
56 #define APT_HEADER_SET(X,Y) X = Y; static_assert(std::numeric_limits<decltype(X)>::max() > Y, "Size violation detected in pkgCache::Header")
57 APT_HEADER_SET(Signature
, 0x98FE76DC);
59 /* Whenever the structures change the major version should be bumped,
60 whenever the generator changes the minor version should be bumped. */
61 APT_HEADER_SET(MajorVersion
, 11);
62 APT_HEADER_SET(MinorVersion
, 0);
63 APT_HEADER_SET(Dirty
, false);
65 APT_HEADER_SET(HeaderSz
, sizeof(pkgCache::Header
));
66 APT_HEADER_SET(GroupSz
, sizeof(pkgCache::Group
));
67 APT_HEADER_SET(PackageSz
, sizeof(pkgCache::Package
));
68 APT_HEADER_SET(ReleaseFileSz
, sizeof(pkgCache::ReleaseFile
));
69 APT_HEADER_SET(PackageFileSz
, sizeof(pkgCache::PackageFile
));
70 APT_HEADER_SET(VersionSz
, sizeof(pkgCache::Version
));
71 APT_HEADER_SET(DescriptionSz
, sizeof(pkgCache::Description
));
72 APT_HEADER_SET(DependencySz
, sizeof(pkgCache::Dependency
));
73 APT_HEADER_SET(DependencyDataSz
, sizeof(pkgCache::DependencyData
));
74 APT_HEADER_SET(ProvidesSz
, sizeof(pkgCache::Provides
));
75 APT_HEADER_SET(VerFileSz
, sizeof(pkgCache::VerFile
));
76 APT_HEADER_SET(DescFileSz
, sizeof(pkgCache::DescFile
));
98 SetHashTableSize(_config
->FindI("APT::Cache-HashTableSize", 50503));
99 memset(Pools
,0,sizeof(Pools
));
104 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
105 // ---------------------------------------------------------------------
107 bool pkgCache::Header::CheckSizes(Header
&Against
) const
109 if (HeaderSz
== Against
.HeaderSz
&&
110 GroupSz
== Against
.GroupSz
&&
111 PackageSz
== Against
.PackageSz
&&
112 ReleaseFileSz
== Against
.ReleaseFileSz
&&
113 PackageFileSz
== Against
.PackageFileSz
&&
114 VersionSz
== Against
.VersionSz
&&
115 DescriptionSz
== Against
.DescriptionSz
&&
116 DependencySz
== Against
.DependencySz
&&
117 DependencyDataSz
== Against
.DependencyDataSz
&&
118 VerFileSz
== Against
.VerFileSz
&&
119 DescFileSz
== Against
.DescFileSz
&&
120 ProvidesSz
== Against
.ProvidesSz
)
126 // Cache::pkgCache - Constructor /*{{{*/
127 // ---------------------------------------------------------------------
129 APT_IGNORE_DEPRECATED_PUSH
130 pkgCache::pkgCache(MMap
*Map
, bool DoMap
) : Map(*Map
), VS(nullptr), d(NULL
)
132 // call getArchitectures() with cached=false to ensure that the
133 // architectures cache is re-evaulated. this is needed in cases
134 // when the APT::Architecture field changes between two cache creations
135 MultiArchEnabled
= APT::Configuration::getArchitectures(false).size() > 1;
139 APT_IGNORE_DEPRECATED_POP
141 // Cache::ReMap - Reopen the cache file /*{{{*/
142 // ---------------------------------------------------------------------
143 /* If the file is already closed then this will open it open it. */
144 bool pkgCache::ReMap(bool const &Errorchecks
)
146 // Apply the typecasts.
147 HeaderP
= (Header
*)Map
.Data();
148 GrpP
= (Group
*)Map
.Data();
149 PkgP
= (Package
*)Map
.Data();
150 VerFileP
= (VerFile
*)Map
.Data();
151 DescFileP
= (DescFile
*)Map
.Data();
152 RlsFileP
= (ReleaseFile
*)Map
.Data();
153 PkgFileP
= (PackageFile
*)Map
.Data();
154 VerP
= (Version
*)Map
.Data();
155 DescP
= (Description
*)Map
.Data();
156 ProvideP
= (Provides
*)Map
.Data();
157 TagP
= (Tag
*)Map
.Data();
158 DepP
= (Dependency
*)Map
.Data();
159 DepDataP
= (DependencyData
*)Map
.Data();
160 StrP
= (char *)Map
.Data();
162 if (Errorchecks
== false)
165 if (Map
.Size() == 0 || HeaderP
== 0)
166 return _error
->Error(_("Empty package cache"));
170 if (HeaderP
->Signature
!= DefHeader
.Signature
||
171 HeaderP
->Dirty
== true)
172 return _error
->Error(_("The package cache file is corrupted"));
174 if (HeaderP
->MajorVersion
!= DefHeader
.MajorVersion
||
175 HeaderP
->MinorVersion
!= DefHeader
.MinorVersion
||
176 HeaderP
->CheckSizes(DefHeader
) == false)
177 return _error
->Error(_("The package cache file is an incompatible version"));
179 if (HeaderP
->VerSysName
== 0 || HeaderP
->Architecture
== 0 || HeaderP
->GetArchitectures() == 0)
180 return _error
->Error(_("The package cache file is corrupted"));
183 if ((VS
= pkgVersioningSystem::GetVS(StrP
+ HeaderP
->VerSysName
)) == 0)
184 return _error
->Error(_("This APT does not support the versioning system '%s'"),StrP
+ HeaderP
->VerSysName
);
186 // Check the architecture
187 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
188 std::string list
= "";
189 for (auto const & arch
: archs
) {
194 if (_config
->Find("APT::Architecture") != StrP
+ HeaderP
->Architecture
||
195 list
!= StrP
+ HeaderP
->GetArchitectures())
196 return _error
->Error(_("The package cache was built for different architectures: %s vs %s"), StrP
+ HeaderP
->GetArchitectures(), list
.c_str());
199 auto hash
= CacheHash();
200 if (_config
->FindB("Debug::pkgCacheGen", false))
201 std::clog
<< "Opened cache with hash " << hash
<< ", expecting " << HeaderP
->CacheFileSize
<< "\n";
202 if (hash
!= HeaderP
->CacheFileSize
)
203 return _error
->Error(_("The package cache file is corrupted, it has the wrong hash"));
208 // Cache::Hash - Hash a string /*{{{*/
209 // ---------------------------------------------------------------------
210 /* This is used to generate the hash entries for the HashTable. With my
211 package list from bo this function gets 94% table usage on a 512 item
212 table (480 used items) */
213 map_id_t
pkgCache::sHash(StringView Str
) const
215 uint32_t Hash
= 5381;
216 for (auto I
= Str
.begin(); I
!= Str
.end(); ++I
)
217 Hash
= 33 * Hash
+ tolower_ascii_unsafe(*I
);
218 return Hash
% HeaderP
->GetHashTableSize();
220 map_id_t
pkgCache::sHash(const string
&Str
) const
222 uint32_t Hash
= 5381;
223 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); ++I
)
224 Hash
= 33 * Hash
+ tolower_ascii_unsafe((signed char)*I
);
225 return Hash
% HeaderP
->GetHashTableSize();
228 map_id_t
pkgCache::sHash(const char *Str
) const
230 uint32_t Hash
= 5381;
231 for (const char *I
= Str
; *I
!= 0; ++I
)
232 Hash
= 33 * Hash
+ tolower_ascii_unsafe((signed char)*I
);
233 return Hash
% HeaderP
->GetHashTableSize();
236 uint32_t pkgCache::CacheHash()
238 pkgCache::Header header
= {};
239 uLong adler
= adler32(0L, Z_NULL
, 0);
241 if (Map
.Size() < sizeof(header
))
243 memcpy(&header
, GetMap().Data(), sizeof(header
));
245 header
.Dirty
= false;
246 header
.CacheFileSize
= 0;
248 adler
= adler32(adler
,
249 reinterpret_cast<const unsigned char *>(&header
),
252 if (Map
.Size() > sizeof(header
)) {
253 adler
= adler32(adler
,
254 static_cast<const unsigned char *>(GetMap().Data()) + sizeof(header
),
255 GetMap().Size() - sizeof(header
));
261 // Cache::FindPkg - Locate a package by name /*{{{*/
262 // ---------------------------------------------------------------------
263 /* Returns 0 on error, pointer to the package otherwise */
264 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
) {
265 return FindPkg(StringView(Name
));
268 pkgCache::PkgIterator
pkgCache::FindPkg(StringView Name
) {
269 auto const found
= Name
.rfind(':');
270 if (found
== string::npos
)
271 return FindPkg(Name
, "native");
272 auto const Arch
= Name
.substr(found
+1);
273 /* Beware: This is specialcased to handle pkg:any in dependencies
274 as these are linked to virtual pkg:any named packages.
275 If you want any arch from a pkg, use FindPkg(pkg,"any") */
277 return FindPkg(Name
, "any");
278 return FindPkg(Name
.substr(0, found
), Arch
);
281 // Cache::FindPkg - Locate a package by name /*{{{*/
282 // ---------------------------------------------------------------------
283 /* Returns 0 on error, pointer to the package otherwise */
284 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
, string
const &Arch
) {
285 return FindPkg(StringView(Name
), StringView(Arch
));
288 pkgCache::PkgIterator
pkgCache::FindPkg(StringView Name
, StringView Arch
) {
289 /* We make a detour via the GrpIterator here as
290 on a multi-arch environment a group is easier to
291 find than a package (less entries in the buckets) */
292 pkgCache::GrpIterator Grp
= FindGrp(Name
);
293 if (Grp
.end() == true)
294 return PkgIterator(*this,0);
296 return Grp
.FindPkg(Arch
);
299 // Cache::FindGrp - Locate a group by name /*{{{*/
300 // ---------------------------------------------------------------------
301 /* Returns End-Pointer on error, pointer to the group otherwise */
302 pkgCache::GrpIterator
pkgCache::FindGrp(const string
&Name
) {
303 return FindGrp(StringView(Name
));
306 pkgCache::GrpIterator
pkgCache::FindGrp(StringView Name
) {
307 if (unlikely(Name
.empty() == true))
308 return GrpIterator(*this,0);
310 // Look at the hash bucket for the group
311 Group
*Grp
= GrpP
+ HeaderP
->GrpHashTableP()[sHash(Name
)];
312 for (; Grp
!= GrpP
; Grp
= GrpP
+ Grp
->Next
) {
313 int const cmp
= StringViewCompareFast(Name
, ViewString(Grp
->Name
));
315 return GrpIterator(*this, Grp
);
320 return GrpIterator(*this,0);
323 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
324 // ---------------------------------------------------------------------
325 /* This returns a string representation of the dependency compare
326 type in the weird debian style.. */
327 const char *pkgCache::CompTypeDeb(unsigned char Comp
)
329 const char * const Ops
[] = {"","<=",">=","<<",">>","=","!="};
330 if (unlikely((unsigned)(Comp
& 0xF) >= sizeof(Ops
)/sizeof(Ops
[0])))
332 return Ops
[Comp
& 0xF];
335 // Cache::CompType - Return a string describing the compare type /*{{{*/
336 // ---------------------------------------------------------------------
337 /* This returns a string representation of the dependency compare
339 const char *pkgCache::CompType(unsigned char Comp
)
341 const char * const Ops
[] = {"","<=",">=","<",">","=","!="};
342 if (unlikely((unsigned)(Comp
& 0xF) >= sizeof(Ops
)/sizeof(Ops
[0])))
344 return Ops
[Comp
& 0xF];
347 // Cache::DepType - Return a string describing the dep type /*{{{*/
348 // ---------------------------------------------------------------------
350 const char *pkgCache::DepType(unsigned char Type
)
352 const char *Types
[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
353 _("Recommends"),_("Conflicts"),_("Replaces"),
354 _("Obsoletes"),_("Breaks"), _("Enhances")};
355 if (Type
< sizeof(Types
)/sizeof(*Types
))
360 // Cache::Priority - Convert a priority value to a string /*{{{*/
361 // ---------------------------------------------------------------------
363 const char *pkgCache::Priority(unsigned char Prio
)
365 const char *Mapping
[] = {0,_("required"),_("important"),_("standard"),
366 _("optional"),_("extra")};
367 if (Prio
< _count(Mapping
))
368 return Mapping
[Prio
];
372 // GrpIterator::FindPkg - Locate a package by arch /*{{{*/
373 // ---------------------------------------------------------------------
374 /* Returns an End-Pointer on error, pointer to the package otherwise */
375 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPkg(string Arch
) const {
376 return FindPkg(StringView(Arch
));
378 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPkg(const char *Arch
) const {
379 return FindPkg(StringView(Arch
));
381 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPkg(StringView Arch
) const {
382 if (unlikely(IsGood() == false || S
->FirstPackage
== 0))
383 return PkgIterator(*Owner
, 0);
385 /* If we accept any package we simply return the "first"
386 package in this group */
388 return PkgIterator(*Owner
, Owner
->PkgP
+ S
->FirstPackage
);
389 if (Arch
== "native" || Arch
== "all")
390 Arch
= Owner
->NativeArch();
392 // Iterate over the list to find the matching arch
393 for (pkgCache::Package
*Pkg
= PackageList(); Pkg
!= Owner
->PkgP
;
394 Pkg
= Owner
->PkgP
+ Pkg
->NextPackage
) {
395 if (Arch
== Owner
->ViewString(Pkg
->Arch
))
396 return PkgIterator(*Owner
, Pkg
);
397 if ((Owner
->PkgP
+ S
->LastPackage
) == Pkg
)
401 return PkgIterator(*Owner
, 0);
404 // GrpIterator::FindPreferredPkg - Locate the "best" package /*{{{*/
405 // ---------------------------------------------------------------------
406 /* Returns an End-Pointer on error, pointer to the package otherwise */
407 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPreferredPkg(bool const &PreferNonVirtual
) const {
408 pkgCache::PkgIterator Pkg
= FindPkg(StringView("native", 6));
409 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
412 std::vector
<std::string
> const archs
= APT::Configuration::getArchitectures();
413 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
414 a
!= archs
.end(); ++a
) {
416 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
419 // packages without an architecture
420 Pkg
= FindPkg(StringView("none", 4));
421 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
424 if (PreferNonVirtual
== true)
425 return FindPreferredPkg(false);
426 return PkgIterator(*Owner
, 0);
429 // GrpIterator::NextPkg - Locate the next package in the group /*{{{*/
430 // ---------------------------------------------------------------------
431 /* Returns an End-Pointer on error, pointer to the package otherwise.
432 We can't simply ++ to the next as the next package of the last will
433 be from a different group (with the same hash value) */
434 pkgCache::PkgIterator
pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator
const &LastPkg
) const {
435 if (unlikely(IsGood() == false || S
->FirstPackage
== 0 ||
436 LastPkg
.end() == true))
437 return PkgIterator(*Owner
, 0);
439 if (S
->LastPackage
== LastPkg
.Index())
440 return PkgIterator(*Owner
, 0);
442 return PkgIterator(*Owner
, Owner
->PkgP
+ LastPkg
->NextPackage
);
445 // GrpIterator::operator++ - Prefix incr /*{{{*/
446 // ---------------------------------------------------------------------
447 /* This will advance to the next logical group in the hash table. */
448 pkgCache::GrpIterator
& pkgCache::GrpIterator::operator++()
450 // Follow the current links
451 if (S
!= Owner
->GrpP
)
452 S
= Owner
->GrpP
+ S
->Next
;
454 // Follow the hash table
455 while (S
== Owner
->GrpP
&& (HashIndex
+1) < (signed)Owner
->HeaderP
->GetHashTableSize())
458 S
= Owner
->GrpP
+ Owner
->HeaderP
->GrpHashTableP()[HashIndex
];
463 // PkgIterator::operator++ - Prefix incr /*{{{*/
464 // ---------------------------------------------------------------------
465 /* This will advance to the next logical package in the hash table. */
466 pkgCache::PkgIterator
& pkgCache::PkgIterator::operator++()
468 // Follow the current links
469 if (S
!= Owner
->PkgP
)
470 S
= Owner
->PkgP
+ S
->NextPackage
;
472 // Follow the hash table
473 while (S
== Owner
->PkgP
&& (HashIndex
+1) < (signed)Owner
->HeaderP
->GetHashTableSize())
476 S
= Owner
->PkgP
+ Owner
->HeaderP
->PkgHashTableP()[HashIndex
];
481 pkgCache::DepIterator
& pkgCache::DepIterator::operator++() /*{{{*/
483 if (S
== Owner
->DepP
)
485 S
= Owner
->DepP
+ (Type
== DepVer
? S
->NextDepends
: S
->NextRevDepends
);
486 if (S
== Owner
->DepP
)
487 S2
= Owner
->DepDataP
;
489 S2
= Owner
->DepDataP
+ S
->DependencyData
;
493 // PkgIterator::State - Check the State of the package /*{{{*/
494 // ---------------------------------------------------------------------
495 /* By this we mean if it is either cleanly installed or cleanly removed. */
496 pkgCache::PkgIterator::OkState
pkgCache::PkgIterator::State() const
498 if (S
->InstState
== pkgCache::State::ReInstReq
||
499 S
->InstState
== pkgCache::State::HoldReInstReq
)
502 if (S
->CurrentState
== pkgCache::State::UnPacked
||
503 S
->CurrentState
== pkgCache::State::HalfConfigured
)
504 // we leave triggers alone completely. dpkg deals with
505 // them in a hard-to-predict manner and if they get
506 // resolved by dpkg before apt run dpkg --configure on
507 // the TriggersPending package dpkg returns a error
508 //Pkg->CurrentState == pkgCache::State::TriggersAwaited
509 //Pkg->CurrentState == pkgCache::State::TriggersPending)
510 return NeedsConfigure
;
512 if (S
->CurrentState
== pkgCache::State::HalfInstalled
||
513 S
->InstState
!= pkgCache::State::Ok
)
519 // PkgIterator::CandVersion - Returns the candidate version string /*{{{*/
520 // ---------------------------------------------------------------------
521 /* Return string representing of the candidate version. */
523 pkgCache::PkgIterator::CandVersion() const
525 //TargetVer is empty, so don't use it.
526 VerIterator version
= pkgPolicy(Owner
).GetCandidateVer(*this);
527 if (version
.IsGood())
528 return version
.VerStr();
532 // PkgIterator::CurVersion - Returns the current version string /*{{{*/
533 // ---------------------------------------------------------------------
534 /* Return string representing of the current version. */
536 pkgCache::PkgIterator::CurVersion() const
538 VerIterator version
= CurrentVer();
539 if (version
.IsGood())
540 return CurrentVer().VerStr();
544 // ostream operator to handle string representation of a package /*{{{*/
545 // ---------------------------------------------------------------------
546 /* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section)
547 Note that the characters <|>() are all literal above. Versions will be omitted
548 if they provide no new information (e.g. there is no newer version than candidate)
549 If no version and/or section can be found "none" is used. */
551 operator<<(std::ostream
& out
, pkgCache::PkgIterator Pkg
)
553 if (Pkg
.end() == true)
554 return out
<< "invalid package";
556 string current
= string(Pkg
.CurVersion() == 0 ? "none" : Pkg
.CurVersion());
557 APT_IGNORE_DEPRECATED_PUSH
558 string candidate
= string(Pkg
.CandVersion() == 0 ? "none" : Pkg
.CandVersion());
559 APT_IGNORE_DEPRECATED_POP
560 string newest
= string(Pkg
.VersionList().end() ? "none" : Pkg
.VersionList().VerStr());
562 out
<< Pkg
.Name() << " [ " << Pkg
.Arch() << " ] < " << current
;
563 if (current
!= candidate
)
564 out
<< " -> " << candidate
;
565 if ( newest
!= "none" && candidate
!= newest
)
566 out
<< " | " << newest
;
567 if (Pkg
->VersionList
== 0)
568 out
<< " > ( none )";
570 out
<< " > ( " << string(Pkg
.VersionList().Section()==0?"unknown":Pkg
.VersionList().Section()) << " )";
574 // PkgIterator::FullName - Returns Name and (maybe) Architecture /*{{{*/
575 // ---------------------------------------------------------------------
576 /* Returns a name:arch string */
577 std::string
pkgCache::PkgIterator::FullName(bool const &Pretty
) const
579 string fullname
= Name();
580 if (Pretty
== false ||
581 (strcmp(Arch(), "all") != 0 && strcmp(Arch(), "any") != 0 &&
582 strcmp(Owner
->NativeArch(), Arch()) != 0))
583 return fullname
.append(":").append(Arch());
587 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
588 // ---------------------------------------------------------------------
589 /* Currently critical deps are defined as depends, predepends and
590 conflicts (including dpkg's Breaks fields). */
591 bool pkgCache::DepIterator::IsCritical() const
593 if (IsNegative() == true ||
594 S2
->Type
== pkgCache::Dep::Depends
||
595 S2
->Type
== pkgCache::Dep::PreDepends
)
600 // DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
601 // ---------------------------------------------------------------------
602 /* Some dependencies are positive like Depends and Recommends, others
603 are negative like Conflicts which can and should be handled differently */
604 bool pkgCache::DepIterator::IsNegative() const
606 return S2
->Type
== Dep::DpkgBreaks
||
607 S2
->Type
== Dep::Conflicts
||
608 S2
->Type
== Dep::Obsoletes
;
611 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
612 // ---------------------------------------------------------------------
613 /* This intellegently looks at dep target packages and tries to figure
614 out which package should be used. This is needed to nicely handle
615 provide mapping. If the target package has no other providing packages
616 then it returned. Otherwise the providing list is looked at to
617 see if there is one one unique providing package if so it is returned.
618 Otherwise true is returned and the target package is set. The return
619 result indicates whether the node should be expandable
621 In Conjunction with the DepCache the value of Result may not be
622 super-good since the policy may have made it uninstallable. Using
623 AllTargets is better in this case. */
624 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator
&Result
) const
626 Result
= TargetPkg();
628 // No provides at all
629 if (Result
->ProvidesList
== 0)
632 // There is the Base package and the providing ones which is at least 2
633 if (Result
->VersionList
!= 0)
636 /* We have to skip over indirect provisions of the package that
637 owns the dependency. For instance, if libc5-dev depends on the
638 virtual package libc-dev which is provided by libc5-dev and libc6-dev
639 we must ignore libc5-dev when considering the provides list. */
640 PrvIterator PStart
= Result
.ProvidesList();
641 for (; PStart
.end() != true && PStart
.OwnerPkg() == ParentPkg(); ++PStart
);
643 // Nothing but indirect self provides
644 if (PStart
.end() == true)
647 // Check for single packages in the provides list
648 PrvIterator P
= PStart
;
649 for (; P
.end() != true; ++P
)
651 // Skip over self provides
652 if (P
.OwnerPkg() == ParentPkg())
654 if (PStart
.OwnerPkg() != P
.OwnerPkg())
658 Result
= PStart
.OwnerPkg();
660 // Check for non dups
667 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
668 // ---------------------------------------------------------------------
669 /* This is a more useful version of TargetPkg() that follows versioned
670 provides. It includes every possible package-version that could satisfy
671 the dependency. The last item in the list has a 0. The resulting pointer
672 must be delete [] 'd */
673 pkgCache::Version
**pkgCache::DepIterator::AllTargets() const
676 unsigned long Size
=0;
680 PkgIterator DPkg
= TargetPkg();
682 // Walk along the actual package providing versions
683 for (VerIterator I
= DPkg
.VersionList(); I
.end() == false; ++I
)
685 if (IsIgnorable(I
.ParentPkg()) == true)
687 if (IsSatisfied(I
) == false)
695 // Follow all provides
696 for (PrvIterator I
= DPkg
.ProvidesList(); I
.end() == false; ++I
)
698 if (IsIgnorable(I
) == true)
700 if (IsSatisfied(I
) == false)
705 *End
++ = I
.OwnerVer();
708 // Do it again and write it into the array
711 Res
= new Version
*[Size
+1];
724 // DepIterator::GlobOr - Compute an OR group /*{{{*/
725 // ---------------------------------------------------------------------
726 /* This Takes an iterator, iterates past the current dependency grouping
727 and returns Start and End so that so End is the final element
728 in the group, if End == Start then D is End++ and End is the
729 dependency D was pointing to. Use in loops to iterate sensibly. */
730 void pkgCache::DepIterator::GlobOr(DepIterator
&Start
,DepIterator
&End
)
732 // Compute a single dependency element (glob or)
735 for (bool LastOR
= true; end() == false && LastOR
== true;)
737 LastOR
= (S2
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
744 // DepIterator::IsIgnorable - should this packag/providr be ignored? /*{{{*/
745 // ---------------------------------------------------------------------
746 /* Deps like self-conflicts should be ignored as well as implicit conflicts
747 on virtual packages. */
748 bool pkgCache::DepIterator::IsIgnorable(PkgIterator
const &PT
) const
750 if (IsNegative() == false)
753 pkgCache::PkgIterator
const PP
= ParentPkg();
754 if (PP
->Group
!= PT
->Group
)
759 pkgCache::VerIterator
const PV
= ParentVer();
760 // ignore group-conflict on a M-A:same package - but not our implicit dependencies
761 // so that we can have M-A:same packages conflicting with their own real name
762 if ((PV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
763 return IsMultiArchImplicit() == false;
767 bool pkgCache::DepIterator::IsIgnorable(PrvIterator
const &Prv
) const
769 if (IsNegative() == false)
772 PkgIterator
const Pkg
= ParentPkg();
773 /* Provides may never be applied against the same package (or group)
774 if it is a conflicts. See the comment above. */
775 if (Prv
.OwnerPkg()->Group
== Pkg
->Group
)
777 // Implicit group-conflicts should not be applied on providers of other groups
778 if (IsMultiArchImplicit() && Prv
.OwnerPkg()->Group
!= Pkg
->Group
)
784 // DepIterator::IsSatisfied - check if a version satisfied the dependency /*{{{*/
785 bool pkgCache::DepIterator::IsSatisfied(VerIterator
const &Ver
) const
787 return Owner
->VS
->CheckDep(Ver
.VerStr(),S2
->CompareOp
,TargetVer());
789 bool pkgCache::DepIterator::IsSatisfied(PrvIterator
const &Prv
) const
791 return Owner
->VS
->CheckDep(Prv
.ProvideVersion(),S2
->CompareOp
,TargetVer());
794 // DepIterator::IsImplicit - added by the cache generation /*{{{*/
795 bool pkgCache::DepIterator::IsImplicit() const
797 if (IsMultiArchImplicit() == true)
799 if (IsNegative() || S2
->Type
== pkgCache::Dep::Replaces
)
801 if ((S2
->CompareOp
& pkgCache::Dep::ArchSpecific
) != pkgCache::Dep::ArchSpecific
&&
802 strcmp(ParentPkg().Arch(), TargetPkg().Arch()) != 0)
808 // ostream operator to handle string representation of a dependency /*{{{*/
809 // ---------------------------------------------------------------------
811 std::ostream
& operator<<(std::ostream
& out
, pkgCache::DepIterator D
)
814 return out
<< "invalid dependency";
816 pkgCache::PkgIterator P
= D
.ParentPkg();
817 pkgCache::PkgIterator T
= D
.TargetPkg();
819 out
<< (P
.end() ? "invalid pkg" : P
.FullName(false)) << " " << D
.DepType()
821 APT_IGNORE_DEPRECATED_PUSH
823 out
<< "invalid pkg";
826 APT_IGNORE_DEPRECATED_POP
829 out
<< " (" << D
.CompType() << " " << D
.TargetVer() << ")";
834 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
835 // ---------------------------------------------------------------------
836 /* This just looks over the version list to see if B is listed before A. In
837 most cases this will return in under 4 checks, ver lists are short. */
838 int pkgCache::VerIterator::CompareVer(const VerIterator
&B
) const
840 // Check if they are equal
848 /* Start at A and look for B. If B is found then A > B otherwise
849 B was before A so A < B */
850 VerIterator I
= *this;
851 for (;I
.end() == false; ++I
)
857 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
858 // ---------------------------------------------------------------------
860 APT_PURE
bool pkgCache::VerIterator::Downloadable() const
862 VerFileIterator Files
= FileList();
863 for (; Files
.end() == false; ++Files
)
864 if (Files
.File().Flagged(pkgCache::Flag::NotSource
) == false)
869 // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
870 // ---------------------------------------------------------------------
871 /* This checks to see if any of the versions files are not NotAutomatic.
872 True if this version is selectable for automatic installation. */
873 APT_PURE
bool pkgCache::VerIterator::Automatic() const
875 VerFileIterator Files
= FileList();
876 for (; Files
.end() == false; ++Files
)
877 // Do not check ButAutomaticUpgrades here as it is kind of automatic…
878 if (Files
.File().Flagged(pkgCache::Flag::NotAutomatic
) == false)
883 // VerIterator::NewestFile - Return the newest file version relation /*{{{*/
884 // ---------------------------------------------------------------------
885 /* This looks at the version numbers associated with all of the sources
886 this version is in and returns the highest.*/
887 pkgCache::VerFileIterator
pkgCache::VerIterator::NewestFile() const
889 VerFileIterator Files
= FileList();
890 VerFileIterator Highest
= Files
;
891 for (; Files
.end() == false; ++Files
)
893 if (Owner
->VS
->CmpReleaseVer(Files
.File().Version(),Highest
.File().Version()) > 0)
900 // VerIterator::RelStr - Release description string /*{{{*/
901 // ---------------------------------------------------------------------
902 /* This describes the version from a release-centric manner. The output is a
903 list of Label:Version/Archive */
904 static std::string
PkgFileIteratorToRelString(pkgCache::PkgFileIterator
const &File
)
907 if (File
.Label() != 0)
908 Res
= Res
+ File
.Label() + ':';
910 if (File
.Archive() != 0)
912 if (File
.Version() == 0)
913 Res
+= File
.Archive();
915 Res
= Res
+ File
.Version() + '/' + File
.Archive();
919 // No release file, print the host name that this came from
920 if (File
.Site() == 0 || File
.Site()[0] == 0)
927 string
pkgCache::VerIterator::RelStr() const
929 std::vector
<std::string
> RelStrs
;
930 for (pkgCache::VerFileIterator I
= this->FileList(); I
.end() == false; ++I
)
932 // Do not print 'not source' entries'
933 pkgCache::PkgFileIterator
const File
= I
.File();
934 if (File
.Flagged(pkgCache::Flag::NotSource
))
937 std::string
const RS
= PkgFileIteratorToRelString(File
);
938 if (std::find(RelStrs
.begin(), RelStrs
.end(), RS
) != RelStrs
.end())
941 RelStrs
.push_back(RS
);
943 std::ostringstream os
;
944 if (likely(RelStrs
.empty() == false))
946 std::copy(RelStrs
.begin(), RelStrs
.end()-1, std::ostream_iterator
<std::string
>(os
, ", "));
947 os
<< *RelStrs
.rbegin();
949 if (S
->ParentPkg
!= 0)
950 os
<< " [" << Arch() << "]";
954 // VerIterator::MultiArchType - string representing MultiArch flag /*{{{*/
955 const char * pkgCache::VerIterator::MultiArchType() const
957 if ((S
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
959 else if ((S
->MultiArch
& pkgCache::Version::Foreign
) == pkgCache::Version::Foreign
)
961 else if ((S
->MultiArch
& pkgCache::Version::Allowed
) == pkgCache::Version::Allowed
)
966 // RlsFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
967 // ---------------------------------------------------------------------
968 /* This stats the file and compares its stats with the ones that were
969 stored during generation. Date checks should probably also be
971 bool pkgCache::RlsFileIterator::IsOk()
974 if (stat(FileName(),&Buf
) != 0)
977 if (Buf
.st_size
!= (signed)S
->Size
|| Buf
.st_mtime
!= S
->mtime
)
983 // RlsFileIterator::RelStr - Return the release string /*{{{*/
984 string
pkgCache::RlsFileIterator::RelStr()
988 Res
= Res
+ (Res
.empty() == true?"v=":",v=") + Version();
990 Res
= Res
+ (Res
.empty() == true?"o=":",o=") + Origin();
992 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Archive();
994 Res
= Res
+ (Res
.empty() == true?"n=":",n=") + Codename();
996 Res
= Res
+ (Res
.empty() == true?"l=":",l=") + Label();
1000 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
1001 // ---------------------------------------------------------------------
1002 /* This stats the file and compares its stats with the ones that were
1003 stored during generation. Date checks should probably also be
1005 bool pkgCache::PkgFileIterator::IsOk()
1008 if (stat(FileName(),&Buf
) != 0)
1011 if (Buf
.st_size
!= (signed)S
->Size
|| Buf
.st_mtime
!= S
->mtime
)
1017 string
pkgCache::PkgFileIterator::RelStr() /*{{{*/
1020 if (ReleaseFile() == 0)
1022 if (Component() != 0)
1023 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Component();
1027 Res
= ReleaseFile().RelStr();
1028 if (Component() != 0)
1029 Res
= Res
+ (Res
.empty() == true?"c=":",c=") + Component();
1031 if (Architecture() != 0)
1032 Res
= Res
+ (Res
.empty() == true?"b=":",b=") + Architecture();
1036 // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
1037 // ---------------------------------------------------------------------
1038 /* return a DescIter for the current locale or the default if none is
1041 pkgCache::DescIterator
pkgCache::VerIterator::TranslatedDescription() const
1043 std::vector
<string
> const lang
= APT::Configuration::getLanguages();
1044 for (std::vector
<string
>::const_iterator l
= lang
.begin();
1045 l
!= lang
.end(); ++l
)
1047 pkgCache::DescIterator Desc
= DescriptionList();
1048 for (; Desc
.end() == false; ++Desc
)
1049 if (*l
== Desc
.LanguageCode())
1051 if (Desc
.end() == true)
1055 Desc
= DescriptionList();
1056 for (; Desc
.end() == false; ++Desc
)
1057 if (strcmp(Desc
.LanguageCode(), "") == 0)
1059 if (Desc
.end() == true)
1067 for (pkgCache::DescIterator Desc
= DescriptionList();
1068 Desc
.end() == false; ++Desc
)
1069 if (strcmp(Desc
.LanguageCode(), "") == 0)
1071 return DescriptionList();
1076 pkgCache::~pkgCache() {}