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
, 10);
62 APT_HEADER_SET(MinorVersion
, 5);
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 DepP
= (Dependency
*)Map
.Data();
158 DepDataP
= (DependencyData
*)Map
.Data();
159 StrP
= (char *)Map
.Data();
161 if (Errorchecks
== false)
164 if (Map
.Size() == 0 || HeaderP
== 0)
165 return _error
->Error(_("Empty package cache"));
169 if (HeaderP
->Signature
!= DefHeader
.Signature
||
170 HeaderP
->Dirty
== true)
171 return _error
->Error(_("The package cache file is corrupted"));
173 if (HeaderP
->MajorVersion
!= DefHeader
.MajorVersion
||
174 HeaderP
->MinorVersion
!= DefHeader
.MinorVersion
||
175 HeaderP
->CheckSizes(DefHeader
) == false)
176 return _error
->Error(_("The package cache file is an incompatible version"));
178 if (HeaderP
->VerSysName
== 0 || HeaderP
->Architecture
== 0 || HeaderP
->GetArchitectures() == 0)
179 return _error
->Error(_("The package cache file is corrupted"));
182 if ((VS
= pkgVersioningSystem::GetVS(StrP
+ HeaderP
->VerSysName
)) == 0)
183 return _error
->Error(_("This APT does not support the versioning system '%s'"),StrP
+ HeaderP
->VerSysName
);
185 // Check the architecture
186 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
187 std::string list
= "";
188 for (auto const & arch
: archs
) {
193 if (_config
->Find("APT::Architecture") != StrP
+ HeaderP
->Architecture
||
194 list
!= StrP
+ HeaderP
->GetArchitectures())
195 return _error
->Error(_("The package cache was built for different architectures: %s vs %s"), StrP
+ HeaderP
->GetArchitectures(), list
.c_str());
198 auto hash
= CacheHash();
199 if (_config
->FindB("Debug::pkgCacheGen", false))
200 std::clog
<< "Opened cache with hash " << hash
<< ", expecting " << HeaderP
->CacheFileSize
<< "\n";
201 if (hash
!= HeaderP
->CacheFileSize
)
202 return _error
->Error(_("The package cache file is corrupted, it has the wrong hash"));
207 // Cache::Hash - Hash a string /*{{{*/
208 // ---------------------------------------------------------------------
209 /* This is used to generate the hash entries for the HashTable. With my
210 package list from bo this function gets 94% table usage on a 512 item
211 table (480 used items) */
212 map_id_t
pkgCache::sHash(StringView Str
) const
214 uint32_t Hash
= 5381;
215 for (auto I
= Str
.begin(); I
!= Str
.end(); ++I
)
216 Hash
= 33 * Hash
+ tolower_ascii(*I
);
217 return Hash
% HeaderP
->GetHashTableSize();
219 map_id_t
pkgCache::sHash(const string
&Str
) const
221 uint32_t Hash
= 5381;
222 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); ++I
)
223 Hash
= 33 * Hash
+ tolower_ascii((signed char)*I
);
224 return Hash
% HeaderP
->GetHashTableSize();
227 map_id_t
pkgCache::sHash(const char *Str
) const
229 uint32_t Hash
= 5381;
230 for (const char *I
= Str
; *I
!= 0; ++I
)
231 Hash
= 33 * Hash
+ tolower_ascii((signed char)*I
);
232 return Hash
% HeaderP
->GetHashTableSize();
235 uint32_t pkgCache::CacheHash()
237 pkgCache::Header header
= {};
238 uLong adler
= adler32(0L, Z_NULL
, 0);
240 if (Map
.Size() < sizeof(header
))
242 memcpy(&header
, GetMap().Data(), sizeof(header
));
244 header
.Dirty
= false;
245 header
.CacheFileSize
= 0;
247 adler
= adler32(adler
,
248 reinterpret_cast<const unsigned char *>(&header
),
251 if (Map
.Size() > sizeof(header
)) {
252 adler
= adler32(adler
,
253 static_cast<const unsigned char *>(GetMap().Data()) + sizeof(header
),
254 GetMap().Size() - sizeof(header
));
260 // Cache::FindPkg - Locate a package by name /*{{{*/
261 // ---------------------------------------------------------------------
262 /* Returns 0 on error, pointer to the package otherwise */
263 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
) {
264 return FindPkg(StringView(Name
));
267 pkgCache::PkgIterator
pkgCache::FindPkg(StringView Name
) {
268 auto const found
= Name
.rfind(':');
269 if (found
== string::npos
)
270 return FindPkg(Name
, "native");
271 auto const Arch
= Name
.substr(found
+1);
272 /* Beware: This is specialcased to handle pkg:any in dependencies
273 as these are linked to virtual pkg:any named packages.
274 If you want any arch from a pkg, use FindPkg(pkg,"any") */
276 return FindPkg(Name
, "any");
277 return FindPkg(Name
.substr(0, found
), Arch
);
280 // Cache::FindPkg - Locate a package by name /*{{{*/
281 // ---------------------------------------------------------------------
282 /* Returns 0 on error, pointer to the package otherwise */
283 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
, string
const &Arch
) {
284 return FindPkg(StringView(Name
), StringView(Arch
));
287 pkgCache::PkgIterator
pkgCache::FindPkg(StringView Name
, StringView Arch
) {
288 /* We make a detour via the GrpIterator here as
289 on a multi-arch environment a group is easier to
290 find than a package (less entries in the buckets) */
291 pkgCache::GrpIterator Grp
= FindGrp(Name
);
292 if (Grp
.end() == true)
293 return PkgIterator(*this,0);
295 return Grp
.FindPkg(Arch
);
298 // Cache::FindGrp - Locate a group by name /*{{{*/
299 // ---------------------------------------------------------------------
300 /* Returns End-Pointer on error, pointer to the group otherwise */
301 pkgCache::GrpIterator
pkgCache::FindGrp(const string
&Name
) {
302 return FindGrp(StringView(Name
));
305 pkgCache::GrpIterator
pkgCache::FindGrp(StringView Name
) {
306 if (unlikely(Name
.empty() == true))
307 return GrpIterator(*this,0);
309 // Look at the hash bucket for the group
310 Group
*Grp
= GrpP
+ HeaderP
->GrpHashTableP()[sHash(Name
)];
311 for (; Grp
!= GrpP
; Grp
= GrpP
+ Grp
->Next
) {
312 int const cmp
= Name
.compare(ViewString(Grp
->Name
));
314 return GrpIterator(*this, Grp
);
319 return GrpIterator(*this,0);
322 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
323 // ---------------------------------------------------------------------
324 /* This returns a string representation of the dependency compare
325 type in the weird debian style.. */
326 const char *pkgCache::CompTypeDeb(unsigned char Comp
)
328 const char * const Ops
[] = {"","<=",">=","<<",">>","=","!="};
329 if (unlikely((unsigned)(Comp
& 0xF) >= sizeof(Ops
)/sizeof(Ops
[0])))
331 return Ops
[Comp
& 0xF];
334 // Cache::CompType - Return a string describing the compare type /*{{{*/
335 // ---------------------------------------------------------------------
336 /* This returns a string representation of the dependency compare
338 const char *pkgCache::CompType(unsigned char Comp
)
340 const char * const Ops
[] = {"","<=",">=","<",">","=","!="};
341 if (unlikely((unsigned)(Comp
& 0xF) >= sizeof(Ops
)/sizeof(Ops
[0])))
343 return Ops
[Comp
& 0xF];
346 // Cache::DepType - Return a string describing the dep type /*{{{*/
347 // ---------------------------------------------------------------------
349 const char *pkgCache::DepType(unsigned char Type
)
351 const char *Types
[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
352 _("Recommends"),_("Conflicts"),_("Replaces"),
353 _("Obsoletes"),_("Breaks"), _("Enhances")};
354 if (Type
< sizeof(Types
)/sizeof(*Types
))
359 // Cache::Priority - Convert a priority value to a string /*{{{*/
360 // ---------------------------------------------------------------------
362 const char *pkgCache::Priority(unsigned char Prio
)
364 const char *Mapping
[] = {0,_("required"),_("important"),_("standard"),
365 _("optional"),_("extra")};
366 if (Prio
< _count(Mapping
))
367 return Mapping
[Prio
];
371 // GrpIterator::FindPkg - Locate a package by arch /*{{{*/
372 // ---------------------------------------------------------------------
373 /* Returns an End-Pointer on error, pointer to the package otherwise */
374 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPkg(string Arch
) const {
375 return FindPkg(StringView(Arch
));
377 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPkg(const char *Arch
) const {
378 return FindPkg(StringView(Arch
));
380 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPkg(StringView Arch
) const {
381 if (unlikely(IsGood() == false || S
->FirstPackage
== 0))
382 return PkgIterator(*Owner
, 0);
384 /* If we accept any package we simply return the "first"
385 package in this group */
387 return PkgIterator(*Owner
, Owner
->PkgP
+ S
->FirstPackage
);
388 if (Arch
== "native" || Arch
== "all")
389 Arch
= Owner
->NativeArch();
391 // Iterate over the list to find the matching arch
392 for (pkgCache::Package
*Pkg
= PackageList(); Pkg
!= Owner
->PkgP
;
393 Pkg
= Owner
->PkgP
+ Pkg
->NextPackage
) {
394 if (Arch
== Owner
->ViewString(Pkg
->Arch
))
395 return PkgIterator(*Owner
, Pkg
);
396 if ((Owner
->PkgP
+ S
->LastPackage
) == Pkg
)
400 return PkgIterator(*Owner
, 0);
403 // GrpIterator::FindPreferredPkg - Locate the "best" package /*{{{*/
404 // ---------------------------------------------------------------------
405 /* Returns an End-Pointer on error, pointer to the package otherwise */
406 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPreferredPkg(bool const &PreferNonVirtual
) const {
407 pkgCache::PkgIterator Pkg
= FindPkg(StringView("native", 6));
408 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
411 std::vector
<std::string
> const archs
= APT::Configuration::getArchitectures();
412 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
413 a
!= archs
.end(); ++a
) {
415 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
418 // packages without an architecture
419 Pkg
= FindPkg(StringView("none", 4));
420 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
423 if (PreferNonVirtual
== true)
424 return FindPreferredPkg(false);
425 return PkgIterator(*Owner
, 0);
428 // GrpIterator::NextPkg - Locate the next package in the group /*{{{*/
429 // ---------------------------------------------------------------------
430 /* Returns an End-Pointer on error, pointer to the package otherwise.
431 We can't simply ++ to the next as the next package of the last will
432 be from a different group (with the same hash value) */
433 pkgCache::PkgIterator
pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator
const &LastPkg
) const {
434 if (unlikely(IsGood() == false || S
->FirstPackage
== 0 ||
435 LastPkg
.end() == true))
436 return PkgIterator(*Owner
, 0);
438 if (S
->LastPackage
== LastPkg
.Index())
439 return PkgIterator(*Owner
, 0);
441 return PkgIterator(*Owner
, Owner
->PkgP
+ LastPkg
->NextPackage
);
444 // GrpIterator::operator++ - Prefix incr /*{{{*/
445 // ---------------------------------------------------------------------
446 /* This will advance to the next logical group in the hash table. */
447 pkgCache::GrpIterator
& pkgCache::GrpIterator::operator++()
449 // Follow the current links
450 if (S
!= Owner
->GrpP
)
451 S
= Owner
->GrpP
+ S
->Next
;
453 // Follow the hash table
454 while (S
== Owner
->GrpP
&& (HashIndex
+1) < (signed)Owner
->HeaderP
->GetHashTableSize())
457 S
= Owner
->GrpP
+ Owner
->HeaderP
->GrpHashTableP()[HashIndex
];
462 // PkgIterator::operator++ - Prefix incr /*{{{*/
463 // ---------------------------------------------------------------------
464 /* This will advance to the next logical package in the hash table. */
465 pkgCache::PkgIterator
& pkgCache::PkgIterator::operator++()
467 // Follow the current links
468 if (S
!= Owner
->PkgP
)
469 S
= Owner
->PkgP
+ S
->NextPackage
;
471 // Follow the hash table
472 while (S
== Owner
->PkgP
&& (HashIndex
+1) < (signed)Owner
->HeaderP
->GetHashTableSize())
475 S
= Owner
->PkgP
+ Owner
->HeaderP
->PkgHashTableP()[HashIndex
];
480 pkgCache::DepIterator
& pkgCache::DepIterator::operator++() /*{{{*/
482 if (S
== Owner
->DepP
)
484 S
= Owner
->DepP
+ (Type
== DepVer
? S
->NextDepends
: S
->NextRevDepends
);
485 if (S
== Owner
->DepP
)
486 S2
= Owner
->DepDataP
;
488 S2
= Owner
->DepDataP
+ S
->DependencyData
;
492 // PkgIterator::State - Check the State of the package /*{{{*/
493 // ---------------------------------------------------------------------
494 /* By this we mean if it is either cleanly installed or cleanly removed. */
495 pkgCache::PkgIterator::OkState
pkgCache::PkgIterator::State() const
497 if (S
->InstState
== pkgCache::State::ReInstReq
||
498 S
->InstState
== pkgCache::State::HoldReInstReq
)
501 if (S
->CurrentState
== pkgCache::State::UnPacked
||
502 S
->CurrentState
== pkgCache::State::HalfConfigured
)
503 // we leave triggers alone completely. dpkg deals with
504 // them in a hard-to-predict manner and if they get
505 // resolved by dpkg before apt run dpkg --configure on
506 // the TriggersPending package dpkg returns a error
507 //Pkg->CurrentState == pkgCache::State::TriggersAwaited
508 //Pkg->CurrentState == pkgCache::State::TriggersPending)
509 return NeedsConfigure
;
511 if (S
->CurrentState
== pkgCache::State::HalfInstalled
||
512 S
->InstState
!= pkgCache::State::Ok
)
518 // PkgIterator::CandVersion - Returns the candidate version string /*{{{*/
519 // ---------------------------------------------------------------------
520 /* Return string representing of the candidate version. */
522 pkgCache::PkgIterator::CandVersion() const
524 //TargetVer is empty, so don't use it.
525 VerIterator version
= pkgPolicy(Owner
).GetCandidateVer(*this);
526 if (version
.IsGood())
527 return version
.VerStr();
531 // PkgIterator::CurVersion - Returns the current version string /*{{{*/
532 // ---------------------------------------------------------------------
533 /* Return string representing of the current version. */
535 pkgCache::PkgIterator::CurVersion() const
537 VerIterator version
= CurrentVer();
538 if (version
.IsGood())
539 return CurrentVer().VerStr();
543 // ostream operator to handle string representation of a package /*{{{*/
544 // ---------------------------------------------------------------------
545 /* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section)
546 Note that the characters <|>() are all literal above. Versions will be omitted
547 if they provide no new information (e.g. there is no newer version than candidate)
548 If no version and/or section can be found "none" is used. */
550 operator<<(std::ostream
& out
, pkgCache::PkgIterator Pkg
)
552 if (Pkg
.end() == true)
553 return out
<< "invalid package";
555 string current
= string(Pkg
.CurVersion() == 0 ? "none" : Pkg
.CurVersion());
556 string candidate
= string(Pkg
.CandVersion() == 0 ? "none" : Pkg
.CandVersion());
557 string newest
= string(Pkg
.VersionList().end() ? "none" : Pkg
.VersionList().VerStr());
559 out
<< Pkg
.Name() << " [ " << Pkg
.Arch() << " ] < " << current
;
560 if (current
!= candidate
)
561 out
<< " -> " << candidate
;
562 if ( newest
!= "none" && candidate
!= newest
)
563 out
<< " | " << newest
;
564 if (Pkg
->VersionList
== 0)
565 out
<< " > ( none )";
567 out
<< " > ( " << string(Pkg
.VersionList().Section()==0?"unknown":Pkg
.VersionList().Section()) << " )";
571 // PkgIterator::FullName - Returns Name and (maybe) Architecture /*{{{*/
572 // ---------------------------------------------------------------------
573 /* Returns a name:arch string */
574 std::string
pkgCache::PkgIterator::FullName(bool const &Pretty
) const
576 string fullname
= Name();
577 if (Pretty
== false ||
578 (strcmp(Arch(), "all") != 0 && strcmp(Arch(), "any") != 0 &&
579 strcmp(Owner
->NativeArch(), Arch()) != 0))
580 return fullname
.append(":").append(Arch());
584 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
585 // ---------------------------------------------------------------------
586 /* Currently critical deps are defined as depends, predepends and
587 conflicts (including dpkg's Breaks fields). */
588 bool pkgCache::DepIterator::IsCritical() const
590 if (IsNegative() == true ||
591 S2
->Type
== pkgCache::Dep::Depends
||
592 S2
->Type
== pkgCache::Dep::PreDepends
)
597 // DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
598 // ---------------------------------------------------------------------
599 /* Some dependencies are positive like Depends and Recommends, others
600 are negative like Conflicts which can and should be handled differently */
601 bool pkgCache::DepIterator::IsNegative() const
603 return S2
->Type
== Dep::DpkgBreaks
||
604 S2
->Type
== Dep::Conflicts
||
605 S2
->Type
== Dep::Obsoletes
;
608 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
609 // ---------------------------------------------------------------------
610 /* This intellegently looks at dep target packages and tries to figure
611 out which package should be used. This is needed to nicely handle
612 provide mapping. If the target package has no other providing packages
613 then it returned. Otherwise the providing list is looked at to
614 see if there is one one unique providing package if so it is returned.
615 Otherwise true is returned and the target package is set. The return
616 result indicates whether the node should be expandable
618 In Conjunction with the DepCache the value of Result may not be
619 super-good since the policy may have made it uninstallable. Using
620 AllTargets is better in this case. */
621 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator
&Result
) const
623 Result
= TargetPkg();
625 // No provides at all
626 if (Result
->ProvidesList
== 0)
629 // There is the Base package and the providing ones which is at least 2
630 if (Result
->VersionList
!= 0)
633 /* We have to skip over indirect provisions of the package that
634 owns the dependency. For instance, if libc5-dev depends on the
635 virtual package libc-dev which is provided by libc5-dev and libc6-dev
636 we must ignore libc5-dev when considering the provides list. */
637 PrvIterator PStart
= Result
.ProvidesList();
638 for (; PStart
.end() != true && PStart
.OwnerPkg() == ParentPkg(); ++PStart
);
640 // Nothing but indirect self provides
641 if (PStart
.end() == true)
644 // Check for single packages in the provides list
645 PrvIterator P
= PStart
;
646 for (; P
.end() != true; ++P
)
648 // Skip over self provides
649 if (P
.OwnerPkg() == ParentPkg())
651 if (PStart
.OwnerPkg() != P
.OwnerPkg())
655 Result
= PStart
.OwnerPkg();
657 // Check for non dups
664 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
665 // ---------------------------------------------------------------------
666 /* This is a more useful version of TargetPkg() that follows versioned
667 provides. It includes every possible package-version that could satisfy
668 the dependency. The last item in the list has a 0. The resulting pointer
669 must be delete [] 'd */
670 pkgCache::Version
**pkgCache::DepIterator::AllTargets() const
673 unsigned long Size
=0;
677 PkgIterator DPkg
= TargetPkg();
679 // Walk along the actual package providing versions
680 for (VerIterator I
= DPkg
.VersionList(); I
.end() == false; ++I
)
682 if (IsIgnorable(I
.ParentPkg()) == true)
684 if (IsSatisfied(I
) == false)
692 // Follow all provides
693 for (PrvIterator I
= DPkg
.ProvidesList(); I
.end() == false; ++I
)
695 if (IsIgnorable(I
) == true)
697 if (IsSatisfied(I
) == false)
702 *End
++ = I
.OwnerVer();
705 // Do it again and write it into the array
708 Res
= new Version
*[Size
+1];
721 // DepIterator::GlobOr - Compute an OR group /*{{{*/
722 // ---------------------------------------------------------------------
723 /* This Takes an iterator, iterates past the current dependency grouping
724 and returns Start and End so that so End is the final element
725 in the group, if End == Start then D is End++ and End is the
726 dependency D was pointing to. Use in loops to iterate sensibly. */
727 void pkgCache::DepIterator::GlobOr(DepIterator
&Start
,DepIterator
&End
)
729 // Compute a single dependency element (glob or)
732 for (bool LastOR
= true; end() == false && LastOR
== true;)
734 LastOR
= (S2
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
741 // DepIterator::IsIgnorable - should this packag/providr be ignored? /*{{{*/
742 // ---------------------------------------------------------------------
743 /* Deps like self-conflicts should be ignored as well as implicit conflicts
744 on virtual packages. */
745 bool pkgCache::DepIterator::IsIgnorable(PkgIterator
const &PT
) const
747 if (IsNegative() == false)
750 pkgCache::PkgIterator
const PP
= ParentPkg();
751 if (PP
->Group
!= PT
->Group
)
756 pkgCache::VerIterator
const PV
= ParentVer();
757 // ignore group-conflict on a M-A:same package - but not our implicit dependencies
758 // so that we can have M-A:same packages conflicting with their own real name
759 if ((PV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
760 return IsMultiArchImplicit() == false;
764 bool pkgCache::DepIterator::IsIgnorable(PrvIterator
const &Prv
) const
766 if (IsNegative() == false)
769 PkgIterator
const Pkg
= ParentPkg();
770 /* Provides may never be applied against the same package (or group)
771 if it is a conflicts. See the comment above. */
772 if (Prv
.OwnerPkg()->Group
== Pkg
->Group
)
774 // Implicit group-conflicts should not be applied on providers of other groups
775 if (IsMultiArchImplicit() && Prv
.OwnerPkg()->Group
!= Pkg
->Group
)
781 // DepIterator::IsSatisfied - check if a version satisfied the dependency /*{{{*/
782 bool pkgCache::DepIterator::IsSatisfied(VerIterator
const &Ver
) const
784 return Owner
->VS
->CheckDep(Ver
.VerStr(),S2
->CompareOp
,TargetVer());
786 bool pkgCache::DepIterator::IsSatisfied(PrvIterator
const &Prv
) const
788 return Owner
->VS
->CheckDep(Prv
.ProvideVersion(),S2
->CompareOp
,TargetVer());
791 // DepIterator::IsImplicit - added by the cache generation /*{{{*/
792 bool pkgCache::DepIterator::IsImplicit() const
794 if (IsMultiArchImplicit() == true)
796 if (IsNegative() || S2
->Type
== pkgCache::Dep::Replaces
)
798 if ((S2
->CompareOp
& pkgCache::Dep::ArchSpecific
) != pkgCache::Dep::ArchSpecific
&&
799 strcmp(ParentPkg().Arch(), TargetPkg().Arch()) != 0)
805 // ostream operator to handle string representation of a dependency /*{{{*/
806 // ---------------------------------------------------------------------
808 std::ostream
& operator<<(std::ostream
& out
, pkgCache::DepIterator D
)
811 return out
<< "invalid dependency";
813 pkgCache::PkgIterator P
= D
.ParentPkg();
814 pkgCache::PkgIterator T
= D
.TargetPkg();
816 out
<< (P
.end() ? "invalid pkg" : P
.FullName(false)) << " " << D
.DepType()
819 out
<< "invalid pkg";
824 out
<< " (" << D
.CompType() << " " << D
.TargetVer() << ")";
829 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
830 // ---------------------------------------------------------------------
831 /* This just looks over the version list to see if B is listed before A. In
832 most cases this will return in under 4 checks, ver lists are short. */
833 int pkgCache::VerIterator::CompareVer(const VerIterator
&B
) const
835 // Check if they are equal
843 /* Start at A and look for B. If B is found then A > B otherwise
844 B was before A so A < B */
845 VerIterator I
= *this;
846 for (;I
.end() == false; ++I
)
852 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
853 // ---------------------------------------------------------------------
855 APT_PURE
bool pkgCache::VerIterator::Downloadable() const
857 VerFileIterator Files
= FileList();
858 for (; Files
.end() == false; ++Files
)
859 if (Files
.File().Flagged(pkgCache::Flag::NotSource
) == false)
864 // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
865 // ---------------------------------------------------------------------
866 /* This checks to see if any of the versions files are not NotAutomatic.
867 True if this version is selectable for automatic installation. */
868 APT_PURE
bool pkgCache::VerIterator::Automatic() const
870 VerFileIterator Files
= FileList();
871 for (; Files
.end() == false; ++Files
)
872 // Do not check ButAutomaticUpgrades here as it is kind of automatic…
873 if (Files
.File().Flagged(pkgCache::Flag::NotAutomatic
) == false)
878 // VerIterator::NewestFile - Return the newest file version relation /*{{{*/
879 // ---------------------------------------------------------------------
880 /* This looks at the version numbers associated with all of the sources
881 this version is in and returns the highest.*/
882 pkgCache::VerFileIterator
pkgCache::VerIterator::NewestFile() const
884 VerFileIterator Files
= FileList();
885 VerFileIterator Highest
= Files
;
886 for (; Files
.end() == false; ++Files
)
888 if (Owner
->VS
->CmpReleaseVer(Files
.File().Version(),Highest
.File().Version()) > 0)
895 // VerIterator::RelStr - Release description string /*{{{*/
896 // ---------------------------------------------------------------------
897 /* This describes the version from a release-centric manner. The output is a
898 list of Label:Version/Archive */
899 static std::string
PkgFileIteratorToRelString(pkgCache::PkgFileIterator
const &File
)
902 if (File
.Label() != 0)
903 Res
= Res
+ File
.Label() + ':';
905 if (File
.Archive() != 0)
907 if (File
.Version() == 0)
908 Res
+= File
.Archive();
910 Res
= Res
+ File
.Version() + '/' + File
.Archive();
914 // No release file, print the host name that this came from
915 if (File
.Site() == 0 || File
.Site()[0] == 0)
922 string
pkgCache::VerIterator::RelStr() const
924 std::vector
<std::string
> RelStrs
;
925 for (pkgCache::VerFileIterator I
= this->FileList(); I
.end() == false; ++I
)
927 // Do not print 'not source' entries'
928 pkgCache::PkgFileIterator
const File
= I
.File();
929 if (File
.Flagged(pkgCache::Flag::NotSource
))
932 std::string
const RS
= PkgFileIteratorToRelString(File
);
933 if (std::find(RelStrs
.begin(), RelStrs
.end(), RS
) != RelStrs
.end())
936 RelStrs
.push_back(RS
);
938 std::ostringstream os
;
939 if (likely(RelStrs
.empty() == false))
941 std::copy(RelStrs
.begin(), RelStrs
.end()-1, std::ostream_iterator
<std::string
>(os
, ", "));
942 os
<< *RelStrs
.rbegin();
944 if (S
->ParentPkg
!= 0)
945 os
<< " [" << Arch() << "]";
949 // VerIterator::MultiArchType - string representing MultiArch flag /*{{{*/
950 const char * pkgCache::VerIterator::MultiArchType() const
952 if ((S
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
954 else if ((S
->MultiArch
& pkgCache::Version::Foreign
) == pkgCache::Version::Foreign
)
956 else if ((S
->MultiArch
& pkgCache::Version::Allowed
) == pkgCache::Version::Allowed
)
961 // RlsFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
962 // ---------------------------------------------------------------------
963 /* This stats the file and compares its stats with the ones that were
964 stored during generation. Date checks should probably also be
966 bool pkgCache::RlsFileIterator::IsOk()
969 if (stat(FileName(),&Buf
) != 0)
972 if (Buf
.st_size
!= (signed)S
->Size
|| Buf
.st_mtime
!= S
->mtime
)
978 // RlsFileIterator::RelStr - Return the release string /*{{{*/
979 string
pkgCache::RlsFileIterator::RelStr()
983 Res
= Res
+ (Res
.empty() == true?"v=":",v=") + Version();
985 Res
= Res
+ (Res
.empty() == true?"o=":",o=") + Origin();
987 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Archive();
989 Res
= Res
+ (Res
.empty() == true?"n=":",n=") + Codename();
991 Res
= Res
+ (Res
.empty() == true?"l=":",l=") + Label();
995 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
996 // ---------------------------------------------------------------------
997 /* This stats the file and compares its stats with the ones that were
998 stored during generation. Date checks should probably also be
1000 bool pkgCache::PkgFileIterator::IsOk()
1003 if (stat(FileName(),&Buf
) != 0)
1006 if (Buf
.st_size
!= (signed)S
->Size
|| Buf
.st_mtime
!= S
->mtime
)
1012 string
pkgCache::PkgFileIterator::RelStr() /*{{{*/
1015 if (ReleaseFile() == 0)
1017 if (Component() != 0)
1018 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Component();
1022 Res
= ReleaseFile().RelStr();
1023 if (Component() != 0)
1024 Res
= Res
+ (Res
.empty() == true?"c=":",c=") + Component();
1026 if (Architecture() != 0)
1027 Res
= Res
+ (Res
.empty() == true?"b=":",b=") + Architecture();
1031 // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
1032 // ---------------------------------------------------------------------
1033 /* return a DescIter for the current locale or the default if none is
1036 pkgCache::DescIterator
pkgCache::VerIterator::TranslatedDescription() const
1038 std::vector
<string
> const lang
= APT::Configuration::getLanguages();
1039 for (std::vector
<string
>::const_iterator l
= lang
.begin();
1040 l
!= lang
.end(); ++l
)
1042 pkgCache::DescIterator Desc
= DescriptionList();
1043 for (; Desc
.end() == false; ++Desc
)
1044 if (*l
== Desc
.LanguageCode())
1046 if (Desc
.end() == true)
1050 Desc
= DescriptionList();
1051 for (; Desc
.end() == false; ++Desc
)
1052 if (strcmp(Desc
.LanguageCode(), "") == 0)
1054 if (Desc
.end() == true)
1062 for (pkgCache::DescIterator Desc
= DescriptionList();
1063 Desc
.end() == false; ++Desc
)
1064 if (strcmp(Desc
.LanguageCode(), "") == 0)
1066 return DescriptionList();
1071 pkgCache::~pkgCache() {}