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 // Cache::Header::Header - Constructor /*{{{*/
49 // ---------------------------------------------------------------------
50 /* Simply initialize the header */
51 pkgCache::Header::Header()
53 #define APT_HEADER_SET(X,Y) X = Y; static_assert(std::numeric_limits<decltype(X)>::max() > Y, "Size violation detected in pkgCache::Header")
54 APT_HEADER_SET(Signature
, 0x98FE76DC);
56 /* Whenever the structures change the major version should be bumped,
57 whenever the generator changes the minor version should be bumped. */
58 APT_HEADER_SET(MajorVersion
, 10);
59 APT_HEADER_SET(MinorVersion
, 0);
60 APT_HEADER_SET(Dirty
, false);
62 APT_HEADER_SET(HeaderSz
, sizeof(pkgCache::Header
));
63 APT_HEADER_SET(GroupSz
, sizeof(pkgCache::Group
));
64 APT_HEADER_SET(PackageSz
, sizeof(pkgCache::Package
));
65 APT_HEADER_SET(ReleaseFileSz
, sizeof(pkgCache::ReleaseFile
));
66 APT_HEADER_SET(PackageFileSz
, sizeof(pkgCache::PackageFile
));
67 APT_HEADER_SET(VersionSz
, sizeof(pkgCache::Version
));
68 APT_HEADER_SET(DescriptionSz
, sizeof(pkgCache::Description
));
69 APT_HEADER_SET(DependencySz
, sizeof(pkgCache::Dependency
));
70 APT_HEADER_SET(DependencyDataSz
, sizeof(pkgCache::DependencyData
));
71 APT_HEADER_SET(ProvidesSz
, sizeof(pkgCache::Provides
));
72 APT_HEADER_SET(VerFileSz
, sizeof(pkgCache::VerFile
));
73 APT_HEADER_SET(DescFileSz
, sizeof(pkgCache::DescFile
));
95 SetHashTableSize(_config
->FindI("APT::Cache-HashTableSize", 10 * 1048));
96 memset(Pools
,0,sizeof(Pools
));
101 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
102 // ---------------------------------------------------------------------
104 bool pkgCache::Header::CheckSizes(Header
&Against
) const
106 if (HeaderSz
== Against
.HeaderSz
&&
107 GroupSz
== Against
.GroupSz
&&
108 PackageSz
== Against
.PackageSz
&&
109 ReleaseFileSz
== Against
.ReleaseFileSz
&&
110 PackageFileSz
== Against
.PackageFileSz
&&
111 VersionSz
== Against
.VersionSz
&&
112 DescriptionSz
== Against
.DescriptionSz
&&
113 DependencySz
== Against
.DependencySz
&&
114 DependencyDataSz
== Against
.DependencyDataSz
&&
115 VerFileSz
== Against
.VerFileSz
&&
116 DescFileSz
== Against
.DescFileSz
&&
117 ProvidesSz
== Against
.ProvidesSz
)
123 // Cache::pkgCache - Constructor /*{{{*/
124 // ---------------------------------------------------------------------
126 APT_IGNORE_DEPRECATED_PUSH
127 pkgCache::pkgCache(MMap
*Map
, bool DoMap
) : Map(*Map
), d(NULL
)
129 // call getArchitectures() with cached=false to ensure that the
130 // architectures cache is re-evaulated. this is needed in cases
131 // when the APT::Architecture field changes between two cache creations
132 MultiArchEnabled
= APT::Configuration::getArchitectures(false).size() > 1;
136 APT_IGNORE_DEPRECATED_POP
138 // Cache::ReMap - Reopen the cache file /*{{{*/
139 // ---------------------------------------------------------------------
140 /* If the file is already closed then this will open it open it. */
141 bool pkgCache::ReMap(bool const &Errorchecks
)
143 // Apply the typecasts.
144 HeaderP
= (Header
*)Map
.Data();
145 GrpP
= (Group
*)Map
.Data();
146 PkgP
= (Package
*)Map
.Data();
147 VerFileP
= (VerFile
*)Map
.Data();
148 DescFileP
= (DescFile
*)Map
.Data();
149 RlsFileP
= (ReleaseFile
*)Map
.Data();
150 PkgFileP
= (PackageFile
*)Map
.Data();
151 VerP
= (Version
*)Map
.Data();
152 DescP
= (Description
*)Map
.Data();
153 ProvideP
= (Provides
*)Map
.Data();
154 DepP
= (Dependency
*)Map
.Data();
155 DepDataP
= (DependencyData
*)Map
.Data();
156 StrP
= (char *)Map
.Data();
158 if (Errorchecks
== false)
161 if (Map
.Size() == 0 || HeaderP
== 0)
162 return _error
->Error(_("Empty package cache"));
166 if (HeaderP
->Signature
!= DefHeader
.Signature
||
167 HeaderP
->Dirty
== true)
168 return _error
->Error(_("The package cache file is corrupted"));
170 if (HeaderP
->MajorVersion
!= DefHeader
.MajorVersion
||
171 HeaderP
->MinorVersion
!= DefHeader
.MinorVersion
||
172 HeaderP
->CheckSizes(DefHeader
) == false)
173 return _error
->Error(_("The package cache file is an incompatible version"));
175 if (Map
.Size() < HeaderP
->CacheFileSize
)
176 return _error
->Error(_("The package cache file is corrupted, it is too small"));
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::vector
<std::string
>::const_iterator a
= archs
.begin();
188 std::string list
= *a
;
189 for (++a
; a
!= archs
.end(); ++a
)
190 list
.append(",").append(*a
);
191 if (_config
->Find("APT::Architecture") != StrP
+ HeaderP
->Architecture
||
192 list
!= StrP
+ HeaderP
->GetArchitectures())
193 return _error
->Error(_("The package cache was built for different architectures: %s vs %s"), StrP
+ HeaderP
->GetArchitectures(), list
.c_str());
198 // Cache::Hash - Hash a string /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This is used to generate the hash entries for the HashTable. With my
201 package list from bo this function gets 94% table usage on a 512 item
202 table (480 used items) */
203 map_id_t
pkgCache::sHash(const string
&Str
) const
205 unsigned long Hash
= 0;
206 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); ++I
)
207 Hash
= 41 * Hash
+ tolower_ascii(*I
);
208 return Hash
% HeaderP
->GetHashTableSize();
211 map_id_t
pkgCache::sHash(const char *Str
) const
213 unsigned long Hash
= tolower_ascii(*Str
);
214 for (const char *I
= Str
+ 1; *I
!= 0; ++I
)
215 Hash
= 41 * Hash
+ tolower_ascii(*I
);
216 return Hash
% HeaderP
->GetHashTableSize();
219 // Cache::SingleArchFindPkg - Locate a package by name /*{{{*/
220 // ---------------------------------------------------------------------
221 /* Returns 0 on error, pointer to the package otherwise
222 The multiArch enabled methods will fallback to this one as it is (a bit)
223 faster for single arch environments and realworld is mostly singlearch… */
224 pkgCache::PkgIterator
pkgCache::SingleArchFindPkg(const string
&Name
)
226 // Look at the hash bucket
227 Package
*Pkg
= PkgP
+ HeaderP
->PkgHashTableP()[Hash(Name
)];
228 for (; Pkg
!= PkgP
; Pkg
= PkgP
+ Pkg
->NextPackage
)
230 int const cmp
= strcmp(Name
.c_str(), StrP
+ (GrpP
+ Pkg
->Group
)->Name
);
232 return PkgIterator(*this, Pkg
);
236 return PkgIterator(*this,0);
239 // Cache::FindPkg - Locate a package by name /*{{{*/
240 // ---------------------------------------------------------------------
241 /* Returns 0 on error, pointer to the package otherwise */
242 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
) {
243 size_t const found
= Name
.find(':');
244 if (found
== string::npos
)
245 return FindPkg(Name
, "native");
246 string
const Arch
= Name
.substr(found
+1);
247 /* Beware: This is specialcased to handle pkg:any in dependencies as
248 these are linked to virtual pkg:any named packages with all archs.
249 If you want any arch from a given pkg, use FindPkg(pkg,arch) */
251 return FindPkg(Name
, "any");
252 return FindPkg(Name
.substr(0, found
), Arch
);
255 // Cache::FindPkg - Locate a package by name /*{{{*/
256 // ---------------------------------------------------------------------
257 /* Returns 0 on error, pointer to the package otherwise */
258 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
, string
const &Arch
) {
259 /* We make a detour via the GrpIterator here as
260 on a multi-arch environment a group is easier to
261 find than a package (less entries in the buckets) */
262 pkgCache::GrpIterator Grp
= FindGrp(Name
);
263 if (Grp
.end() == true)
264 return PkgIterator(*this,0);
266 return Grp
.FindPkg(Arch
);
269 // Cache::FindGrp - Locate a group by name /*{{{*/
270 // ---------------------------------------------------------------------
271 /* Returns End-Pointer on error, pointer to the group otherwise */
272 pkgCache::GrpIterator
pkgCache::FindGrp(const string
&Name
) {
273 if (unlikely(Name
.empty() == true))
274 return GrpIterator(*this,0);
276 // Look at the hash bucket for the group
277 Group
*Grp
= GrpP
+ HeaderP
->GrpHashTableP()[sHash(Name
)];
278 for (; Grp
!= GrpP
; Grp
= GrpP
+ Grp
->Next
) {
279 int const cmp
= strcmp(Name
.c_str(), StrP
+ Grp
->Name
);
281 return GrpIterator(*this, Grp
);
286 return GrpIterator(*this,0);
289 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
290 // ---------------------------------------------------------------------
291 /* This returns a string representation of the dependency compare
292 type in the weird debian style.. */
293 const char *pkgCache::CompTypeDeb(unsigned char Comp
)
295 const char * const Ops
[] = {"","<=",">=","<<",">>","=","!="};
296 if (unlikely((unsigned)(Comp
& 0xF) >= sizeof(Ops
)/sizeof(Ops
[0])))
298 return Ops
[Comp
& 0xF];
301 // Cache::CompType - Return a string describing the compare type /*{{{*/
302 // ---------------------------------------------------------------------
303 /* This returns a string representation of the dependency compare
305 const char *pkgCache::CompType(unsigned char Comp
)
307 const char * const Ops
[] = {"","<=",">=","<",">","=","!="};
308 if (unlikely((unsigned)(Comp
& 0xF) >= sizeof(Ops
)/sizeof(Ops
[0])))
310 return Ops
[Comp
& 0xF];
313 // Cache::DepType - Return a string describing the dep type /*{{{*/
314 // ---------------------------------------------------------------------
316 const char *pkgCache::DepType(unsigned char Type
)
318 const char *Types
[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
319 _("Recommends"),_("Conflicts"),_("Replaces"),
320 _("Obsoletes"),_("Breaks"), _("Enhances")};
321 if (Type
< sizeof(Types
)/sizeof(*Types
))
326 // Cache::Priority - Convert a priority value to a string /*{{{*/
327 // ---------------------------------------------------------------------
329 const char *pkgCache::Priority(unsigned char Prio
)
331 const char *Mapping
[] = {0,_("important"),_("required"),_("standard"),
332 _("optional"),_("extra")};
333 if (Prio
< _count(Mapping
))
334 return Mapping
[Prio
];
338 // GrpIterator::FindPkg - Locate a package by arch /*{{{*/
339 // ---------------------------------------------------------------------
340 /* Returns an End-Pointer on error, pointer to the package otherwise */
341 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPkg(string Arch
) const {
342 if (unlikely(IsGood() == false || S
->FirstPackage
== 0))
343 return PkgIterator(*Owner
, 0);
345 /* If we accept any package we simply return the "first"
346 package in this group (the last one added). */
348 return PkgIterator(*Owner
, Owner
->PkgP
+ S
->FirstPackage
);
350 char const* const myArch
= Owner
->NativeArch();
351 /* Most of the time the package for our native architecture is
352 the one we add at first to the cache, but this would be the
353 last one we check, so we do it now. */
354 if (Arch
== "native" || Arch
== myArch
|| Arch
== "all") {
355 pkgCache::Package
*Pkg
= Owner
->PkgP
+ S
->LastPackage
;
356 if (strcmp(myArch
, Owner
->StrP
+ Pkg
->Arch
) == 0)
357 return PkgIterator(*Owner
, Pkg
);
361 // Iterate over the list to find the matching arch
362 for (pkgCache::Package
*Pkg
= PackageList(); Pkg
!= Owner
->PkgP
;
363 Pkg
= Owner
->PkgP
+ Pkg
->NextPackage
) {
364 if (stringcmp(Arch
, Owner
->StrP
+ Pkg
->Arch
) == 0)
365 return PkgIterator(*Owner
, Pkg
);
366 if ((Owner
->PkgP
+ S
->LastPackage
) == Pkg
)
370 return PkgIterator(*Owner
, 0);
373 // GrpIterator::FindPreferredPkg - Locate the "best" package /*{{{*/
374 // ---------------------------------------------------------------------
375 /* Returns an End-Pointer on error, pointer to the package otherwise */
376 pkgCache::PkgIterator
pkgCache::GrpIterator::FindPreferredPkg(bool const &PreferNonVirtual
) const {
377 pkgCache::PkgIterator Pkg
= FindPkg("native");
378 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
381 std::vector
<std::string
> const archs
= APT::Configuration::getArchitectures();
382 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
383 a
!= archs
.end(); ++a
) {
385 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
388 // packages without an architecture
389 Pkg
= FindPkg("none");
390 if (Pkg
.end() == false && (PreferNonVirtual
== false || Pkg
->VersionList
!= 0))
393 if (PreferNonVirtual
== true)
394 return FindPreferredPkg(false);
395 return PkgIterator(*Owner
, 0);
398 // GrpIterator::NextPkg - Locate the next package in the group /*{{{*/
399 // ---------------------------------------------------------------------
400 /* Returns an End-Pointer on error, pointer to the package otherwise.
401 We can't simply ++ to the next as the next package of the last will
402 be from a different group (with the same hash value) */
403 pkgCache::PkgIterator
pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator
const &LastPkg
) const {
404 if (unlikely(IsGood() == false || S
->FirstPackage
== 0 ||
405 LastPkg
.end() == true))
406 return PkgIterator(*Owner
, 0);
408 if (S
->LastPackage
== LastPkg
.Index())
409 return PkgIterator(*Owner
, 0);
411 return PkgIterator(*Owner
, Owner
->PkgP
+ LastPkg
->NextPackage
);
414 // GrpIterator::operator++ - Prefix incr /*{{{*/
415 // ---------------------------------------------------------------------
416 /* This will advance to the next logical group in the hash table. */
417 pkgCache::GrpIterator
& pkgCache::GrpIterator::operator++()
419 // Follow the current links
420 if (S
!= Owner
->GrpP
)
421 S
= Owner
->GrpP
+ S
->Next
;
423 // Follow the hash table
424 while (S
== Owner
->GrpP
&& (HashIndex
+1) < (signed)Owner
->HeaderP
->GetHashTableSize())
427 S
= Owner
->GrpP
+ Owner
->HeaderP
->GrpHashTableP()[HashIndex
];
432 // PkgIterator::operator++ - Prefix incr /*{{{*/
433 // ---------------------------------------------------------------------
434 /* This will advance to the next logical package in the hash table. */
435 pkgCache::PkgIterator
& pkgCache::PkgIterator::operator++()
437 // Follow the current links
438 if (S
!= Owner
->PkgP
)
439 S
= Owner
->PkgP
+ S
->NextPackage
;
441 // Follow the hash table
442 while (S
== Owner
->PkgP
&& (HashIndex
+1) < (signed)Owner
->HeaderP
->GetHashTableSize())
445 S
= Owner
->PkgP
+ Owner
->HeaderP
->PkgHashTableP()[HashIndex
];
450 pkgCache::DepIterator
& pkgCache::DepIterator::operator++() /*{{{*/
452 if (S
== Owner
->DepP
)
454 S
= Owner
->DepP
+ (Type
== DepVer
? S
->NextDepends
: S
->NextRevDepends
);
455 if (S
== Owner
->DepP
)
456 S2
= Owner
->DepDataP
;
458 S2
= Owner
->DepDataP
+ S
->DependencyData
;
462 // PkgIterator::State - Check the State of the package /*{{{*/
463 // ---------------------------------------------------------------------
464 /* By this we mean if it is either cleanly installed or cleanly removed. */
465 pkgCache::PkgIterator::OkState
pkgCache::PkgIterator::State() const
467 if (S
->InstState
== pkgCache::State::ReInstReq
||
468 S
->InstState
== pkgCache::State::HoldReInstReq
)
471 if (S
->CurrentState
== pkgCache::State::UnPacked
||
472 S
->CurrentState
== pkgCache::State::HalfConfigured
)
473 // we leave triggers alone complettely. dpkg deals with
474 // them in a hard-to-predict manner and if they get
475 // resolved by dpkg before apt run dpkg --configure on
476 // the TriggersPending package dpkg returns a error
477 //Pkg->CurrentState == pkgCache::State::TriggersAwaited
478 //Pkg->CurrentState == pkgCache::State::TriggersPending)
479 return NeedsConfigure
;
481 if (S
->CurrentState
== pkgCache::State::HalfInstalled
||
482 S
->InstState
!= pkgCache::State::Ok
)
488 // PkgIterator::CandVersion - Returns the candidate version string /*{{{*/
489 // ---------------------------------------------------------------------
490 /* Return string representing of the candidate version. */
492 pkgCache::PkgIterator::CandVersion() const
494 //TargetVer is empty, so don't use it.
495 VerIterator version
= pkgPolicy(Owner
).GetCandidateVer(*this);
496 if (version
.IsGood())
497 return version
.VerStr();
501 // PkgIterator::CurVersion - Returns the current version string /*{{{*/
502 // ---------------------------------------------------------------------
503 /* Return string representing of the current version. */
505 pkgCache::PkgIterator::CurVersion() const
507 VerIterator version
= CurrentVer();
508 if (version
.IsGood())
509 return CurrentVer().VerStr();
513 // ostream operator to handle string representation of a package /*{{{*/
514 // ---------------------------------------------------------------------
515 /* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section)
516 Note that the characters <|>() are all literal above. Versions will be omitted
517 if they provide no new information (e.g. there is no newer version than candidate)
518 If no version and/or section can be found "none" is used. */
520 operator<<(std::ostream
& out
, pkgCache::PkgIterator Pkg
)
522 if (Pkg
.end() == true)
523 return out
<< "invalid package";
525 string current
= string(Pkg
.CurVersion() == 0 ? "none" : Pkg
.CurVersion());
526 string candidate
= string(Pkg
.CandVersion() == 0 ? "none" : Pkg
.CandVersion());
527 string newest
= string(Pkg
.VersionList().end() ? "none" : Pkg
.VersionList().VerStr());
529 out
<< Pkg
.Name() << " [ " << Pkg
.Arch() << " ] < " << current
;
530 if (current
!= candidate
)
531 out
<< " -> " << candidate
;
532 if ( newest
!= "none" && candidate
!= newest
)
533 out
<< " | " << newest
;
534 if (Pkg
->VersionList
== 0)
535 out
<< " > ( none )";
537 out
<< " > ( " << string(Pkg
.VersionList().Section()==0?"unknown":Pkg
.VersionList().Section()) << " )";
541 // PkgIterator::FullName - Returns Name and (maybe) Architecture /*{{{*/
542 // ---------------------------------------------------------------------
543 /* Returns a name:arch string */
544 std::string
pkgCache::PkgIterator::FullName(bool const &Pretty
) const
546 string fullname
= Name();
547 if (Pretty
== false ||
548 (strcmp(Arch(), "all") != 0 &&
549 strcmp(Owner
->NativeArch(), Arch()) != 0))
550 return fullname
.append(":").append(Arch());
554 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
555 // ---------------------------------------------------------------------
556 /* Currently critical deps are defined as depends, predepends and
557 conflicts (including dpkg's Breaks fields). */
558 bool pkgCache::DepIterator::IsCritical() const
560 if (IsNegative() == true ||
561 S2
->Type
== pkgCache::Dep::Depends
||
562 S2
->Type
== pkgCache::Dep::PreDepends
)
567 // DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
568 // ---------------------------------------------------------------------
569 /* Some dependencies are positive like Depends and Recommends, others
570 are negative like Conflicts which can and should be handled differently */
571 bool pkgCache::DepIterator::IsNegative() const
573 return S2
->Type
== Dep::DpkgBreaks
||
574 S2
->Type
== Dep::Conflicts
||
575 S2
->Type
== Dep::Obsoletes
;
578 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
579 // ---------------------------------------------------------------------
580 /* This intellegently looks at dep target packages and tries to figure
581 out which package should be used. This is needed to nicely handle
582 provide mapping. If the target package has no other providing packages
583 then it returned. Otherwise the providing list is looked at to
584 see if there is one one unique providing package if so it is returned.
585 Otherwise true is returned and the target package is set. The return
586 result indicates whether the node should be expandable
588 In Conjunction with the DepCache the value of Result may not be
589 super-good since the policy may have made it uninstallable. Using
590 AllTargets is better in this case. */
591 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator
&Result
) const
593 Result
= TargetPkg();
595 // No provides at all
596 if (Result
->ProvidesList
== 0)
599 // There is the Base package and the providing ones which is at least 2
600 if (Result
->VersionList
!= 0)
603 /* We have to skip over indirect provisions of the package that
604 owns the dependency. For instance, if libc5-dev depends on the
605 virtual package libc-dev which is provided by libc5-dev and libc6-dev
606 we must ignore libc5-dev when considering the provides list. */
607 PrvIterator PStart
= Result
.ProvidesList();
608 for (; PStart
.end() != true && PStart
.OwnerPkg() == ParentPkg(); ++PStart
);
610 // Nothing but indirect self provides
611 if (PStart
.end() == true)
614 // Check for single packages in the provides list
615 PrvIterator P
= PStart
;
616 for (; P
.end() != true; ++P
)
618 // Skip over self provides
619 if (P
.OwnerPkg() == ParentPkg())
621 if (PStart
.OwnerPkg() != P
.OwnerPkg())
625 Result
= PStart
.OwnerPkg();
627 // Check for non dups
634 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
635 // ---------------------------------------------------------------------
636 /* This is a more useful version of TargetPkg() that follows versioned
637 provides. It includes every possible package-version that could satisfy
638 the dependency. The last item in the list has a 0. The resulting pointer
639 must be delete [] 'd */
640 pkgCache::Version
**pkgCache::DepIterator::AllTargets() const
643 unsigned long Size
=0;
647 PkgIterator DPkg
= TargetPkg();
649 // Walk along the actual package providing versions
650 for (VerIterator I
= DPkg
.VersionList(); I
.end() == false; ++I
)
652 if (IsIgnorable(I
.ParentPkg()) == true)
654 if (IsSatisfied(I
) == false)
662 // Follow all provides
663 for (PrvIterator I
= DPkg
.ProvidesList(); I
.end() == false; ++I
)
665 if (IsIgnorable(I
) == true)
667 if (IsSatisfied(I
) == false)
672 *End
++ = I
.OwnerVer();
675 // Do it again and write it into the array
678 Res
= new Version
*[Size
+1];
691 // DepIterator::GlobOr - Compute an OR group /*{{{*/
692 // ---------------------------------------------------------------------
693 /* This Takes an iterator, iterates past the current dependency grouping
694 and returns Start and End so that so End is the final element
695 in the group, if End == Start then D is End++ and End is the
696 dependency D was pointing to. Use in loops to iterate sensibly. */
697 void pkgCache::DepIterator::GlobOr(DepIterator
&Start
,DepIterator
&End
)
699 // Compute a single dependency element (glob or)
702 for (bool LastOR
= true; end() == false && LastOR
== true;)
704 LastOR
= (S2
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
711 // DepIterator::IsIgnorable - should this packag/providr be ignored? /*{{{*/
712 // ---------------------------------------------------------------------
713 /* Deps like self-conflicts should be ignored as well as implicit conflicts
714 on virtual packages. */
715 bool pkgCache::DepIterator::IsIgnorable(PkgIterator
const &PT
) const
717 if (IsNegative() == false)
720 pkgCache::PkgIterator
const PP
= ParentPkg();
721 if (PP
->Group
!= PT
->Group
)
726 pkgCache::VerIterator
const PV
= ParentVer();
727 // ignore group-conflict on a M-A:same package - but not our implicit dependencies
728 // so that we can have M-A:same packages conflicting with their own real name
729 if ((PV
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
730 return IsMultiArchImplicit() == false;
734 bool pkgCache::DepIterator::IsIgnorable(PrvIterator
const &Prv
) const
736 if (IsNegative() == false)
739 PkgIterator
const Pkg
= ParentPkg();
740 /* Provides may never be applied against the same package (or group)
741 if it is a conflicts. See the comment above. */
742 if (Prv
.OwnerPkg()->Group
== Pkg
->Group
)
744 // Implicit group-conflicts should not be applied on providers of other groups
745 if (IsMultiArchImplicit() && Prv
.OwnerPkg()->Group
!= Pkg
->Group
)
751 // DepIterator::IsSatisfied - check if a version satisfied the dependency /*{{{*/
752 bool pkgCache::DepIterator::IsSatisfied(VerIterator
const &Ver
) const
754 return Owner
->VS
->CheckDep(Ver
.VerStr(),S2
->CompareOp
,TargetVer());
756 bool pkgCache::DepIterator::IsSatisfied(PrvIterator
const &Prv
) const
758 return Owner
->VS
->CheckDep(Prv
.ProvideVersion(),S2
->CompareOp
,TargetVer());
761 // DepIterator::IsImplicit - added by the cache generation /*{{{*/
762 bool pkgCache::DepIterator::IsImplicit() const
764 if (IsMultiArchImplicit() == true)
766 if (IsNegative() || S2
->Type
== pkgCache::Dep::Replaces
)
768 if ((S2
->CompareOp
& pkgCache::Dep::ArchSpecific
) != pkgCache::Dep::ArchSpecific
&&
769 strcmp(ParentPkg().Arch(), TargetPkg().Arch()) != 0)
775 // ostream operator to handle string representation of a dependecy /*{{{*/
776 // ---------------------------------------------------------------------
778 std::ostream
& operator<<(std::ostream
& out
, pkgCache::DepIterator D
)
781 return out
<< "invalid dependency";
783 pkgCache::PkgIterator P
= D
.ParentPkg();
784 pkgCache::PkgIterator T
= D
.TargetPkg();
786 out
<< (P
.end() ? "invalid pkg" : P
.FullName(false)) << " " << D
.DepType()
789 out
<< "invalid pkg";
794 out
<< " (" << D
.CompType() << " " << D
.TargetVer() << ")";
799 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
800 // ---------------------------------------------------------------------
801 /* This just looks over the version list to see if B is listed before A. In
802 most cases this will return in under 4 checks, ver lists are short. */
803 int pkgCache::VerIterator::CompareVer(const VerIterator
&B
) const
805 // Check if they are equal
813 /* Start at A and look for B. If B is found then A > B otherwise
814 B was before A so A < B */
815 VerIterator I
= *this;
816 for (;I
.end() == false; ++I
)
822 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
823 // ---------------------------------------------------------------------
825 APT_PURE
bool pkgCache::VerIterator::Downloadable() const
827 VerFileIterator Files
= FileList();
828 for (; Files
.end() == false; ++Files
)
829 if (Files
.File().Flagged(pkgCache::Flag::NotSource
) == false)
834 // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
835 // ---------------------------------------------------------------------
836 /* This checks to see if any of the versions files are not NotAutomatic.
837 True if this version is selectable for automatic installation. */
838 APT_PURE
bool pkgCache::VerIterator::Automatic() const
840 VerFileIterator Files
= FileList();
841 for (; Files
.end() == false; ++Files
)
842 // Do not check ButAutomaticUpgrades here as it is kind of automatic…
843 if (Files
.File().Flagged(pkgCache::Flag::NotAutomatic
) == false)
848 // VerIterator::NewestFile - Return the newest file version relation /*{{{*/
849 // ---------------------------------------------------------------------
850 /* This looks at the version numbers associated with all of the sources
851 this version is in and returns the highest.*/
852 pkgCache::VerFileIterator
pkgCache::VerIterator::NewestFile() const
854 VerFileIterator Files
= FileList();
855 VerFileIterator Highest
= Files
;
856 for (; Files
.end() == false; ++Files
)
858 if (Owner
->VS
->CmpReleaseVer(Files
.File().Version(),Highest
.File().Version()) > 0)
865 // VerIterator::RelStr - Release description string /*{{{*/
866 // ---------------------------------------------------------------------
867 /* This describes the version from a release-centric manner. The output is a
868 list of Label:Version/Archive */
869 string
pkgCache::VerIterator::RelStr() const
873 for (pkgCache::VerFileIterator I
= this->FileList(); I
.end() == false; ++I
)
875 // Do not print 'not source' entries'
876 pkgCache::PkgFileIterator
const File
= I
.File();
877 if (File
.Flagged(pkgCache::Flag::NotSource
))
880 // See if we have already printed this out..
882 for (pkgCache::VerFileIterator J
= this->FileList(); I
!= J
; ++J
)
884 pkgCache::PkgFileIterator
const File2
= J
.File();
885 if (File2
.Label() == 0 || File
.Label() == 0)
888 if (strcmp(File
.Label(),File2
.Label()) != 0)
891 if (File2
.Version() == File
.Version())
896 if (File2
.Version() == 0 || File
.Version() == 0)
898 if (strcmp(File
.Version(),File2
.Version()) == 0)
910 if (File
.Label() != 0)
911 Res
= Res
+ File
.Label() + ':';
913 if (File
.Archive() != 0)
915 if (File
.Version() == 0)
916 Res
+= File
.Archive();
918 Res
= Res
+ File
.Version() + '/' + File
.Archive();
922 // No release file, print the host name that this came from
923 if (File
.Site() == 0 || File
.Site()[0] == 0)
929 if (S
->ParentPkg
!= 0)
930 Res
.append(" [").append(Arch()).append("]");
934 // VerIterator::MultiArchType - string representing MultiArch flag /*{{{*/
935 const char * pkgCache::VerIterator::MultiArchType() const
937 if ((S
->MultiArch
& pkgCache::Version::Same
) == pkgCache::Version::Same
)
939 else if ((S
->MultiArch
& pkgCache::Version::Foreign
) == pkgCache::Version::Foreign
)
941 else if ((S
->MultiArch
& pkgCache::Version::Allowed
) == pkgCache::Version::Allowed
)
946 // RlsFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
947 // ---------------------------------------------------------------------
948 /* This stats the file and compares its stats with the ones that were
949 stored during generation. Date checks should probably also be
951 bool pkgCache::RlsFileIterator::IsOk()
954 if (stat(FileName(),&Buf
) != 0)
957 if (Buf
.st_size
!= (signed)S
->Size
|| Buf
.st_mtime
!= S
->mtime
)
963 // RlsFileIterator::RelStr - Return the release string /*{{{*/
964 string
pkgCache::RlsFileIterator::RelStr()
968 Res
= Res
+ (Res
.empty() == true?"v=":",v=") + Version();
970 Res
= Res
+ (Res
.empty() == true?"o=":",o=") + Origin();
972 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Archive();
974 Res
= Res
+ (Res
.empty() == true?"n=":",n=") + Codename();
976 Res
= Res
+ (Res
.empty() == true?"l=":",l=") + Label();
980 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
981 // ---------------------------------------------------------------------
982 /* This stats the file and compares its stats with the ones that were
983 stored during generation. Date checks should probably also be
985 bool pkgCache::PkgFileIterator::IsOk()
988 if (stat(FileName(),&Buf
) != 0)
991 if (Buf
.st_size
!= (signed)S
->Size
|| Buf
.st_mtime
!= S
->mtime
)
997 string
pkgCache::PkgFileIterator::RelStr() /*{{{*/
1000 if (ReleaseFile() == 0)
1002 if (Component() != 0)
1003 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Component();
1007 Res
= ReleaseFile().RelStr();
1008 if (Component() != 0)
1009 Res
= Res
+ (Res
.empty() == true?"c=":",c=") + Component();
1011 if (Architecture() != 0)
1012 Res
= Res
+ (Res
.empty() == true?"b=":",b=") + Architecture();
1016 // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
1017 // ---------------------------------------------------------------------
1018 /* return a DescIter for the current locale or the default if none is
1021 pkgCache::DescIterator
pkgCache::VerIterator::TranslatedDescription() const
1023 std::vector
<string
> const lang
= APT::Configuration::getLanguages();
1024 for (std::vector
<string
>::const_iterator l
= lang
.begin();
1025 l
!= lang
.end(); ++l
)
1027 pkgCache::DescIterator Desc
= DescriptionList();
1028 for (; Desc
.end() == false; ++Desc
)
1029 if (*l
== Desc
.LanguageCode())
1031 if (Desc
.end() == true)
1035 Desc
= DescriptionList();
1036 for (; Desc
.end() == false; ++Desc
)
1037 if (strcmp(Desc
.LanguageCode(), "") == 0)
1039 if (Desc
.end() == true)
1047 for (pkgCache::DescIterator Desc
= DescriptionList();
1048 Desc
.end() == false; ++Desc
)
1049 if (strcmp(Desc
.LanguageCode(), "") == 0)
1051 return DescriptionList();
1056 pkgCache::~pkgCache() {}