]> git.saurik.com Git - apt.git/blame - apt-pkg/pkgcache.cc
Do not swap required and important in pkgCache::Priority()
[apt.git] / apt-pkg / pkgcache.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
bac2e715 3// $Id: pkgcache.cc,v 1.37 2003/02/10 01:40:58 doogie Exp $
578bfd0a
AL
4/* ######################################################################
5
6 Package Cache - Accessor code for the cache
7
094a497d 8 Please see doc/apt-pkg/cache.sgml for a more detailed description of
578bfd0a
AL
9 this format. Also be sure to keep that file up-to-date!!
10
1e3f4083 11 This is the general utility functions for cache management. They provide
578bfd0a
AL
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.
16
17 The main class provides for ways to get package indexes and some
18 general lookup functions to start the iterators.
19
20 ##################################################################### */
21 /*}}}*/
22// Include Files /*{{{*/
ea542140
DK
23#include<config.h>
24
094a497d 25#include <apt-pkg/pkgcache.h>
af29ffb4 26#include <apt-pkg/policy.h>
094a497d
AL
27#include <apt-pkg/version.h>
28#include <apt-pkg/error.h>
231fea14 29#include <apt-pkg/strutl.h>
b2e465d6 30#include <apt-pkg/configuration.h>
45df0ad2 31#include <apt-pkg/aptconfiguration.h>
453b82a3 32#include <apt-pkg/mmap.h>
5c0d3668 33#include <apt-pkg/macros.h>
578bfd0a 34
453b82a3
DK
35#include <stddef.h>
36#include <string.h>
7f8c0eed
DK
37#include <sstream>
38#include <algorithm>
453b82a3 39#include <vector>
578bfd0a
AL
40#include <string>
41#include <sys/stat.h>
ea542140
DK
42
43#include <apti18n.h>
578bfd0a
AL
44 /*}}}*/
45
851a45a8
AL
46using std::string;
47
012b102a 48
578bfd0a
AL
49// Cache::Header::Header - Constructor /*{{{*/
50// ---------------------------------------------------------------------
51/* Simply initialize the header */
52pkgCache::Header::Header()
53{
dfe66c72
DK
54#define APT_HEADER_SET(X,Y) X = Y; static_assert(std::numeric_limits<decltype(X)>::max() > Y, "Size violation detected in pkgCache::Header")
55 APT_HEADER_SET(Signature, 0x98FE76DC);
56
578bfd0a
AL
57 /* Whenever the structures change the major version should be bumped,
58 whenever the generator changes the minor version should be bumped. */
dfe66c72 59 APT_HEADER_SET(MajorVersion, 10);
3addaba1 60 APT_HEADER_SET(MinorVersion, 1);
dfe66c72
DK
61 APT_HEADER_SET(Dirty, false);
62
63 APT_HEADER_SET(HeaderSz, sizeof(pkgCache::Header));
64 APT_HEADER_SET(GroupSz, sizeof(pkgCache::Group));
65 APT_HEADER_SET(PackageSz, sizeof(pkgCache::Package));
66 APT_HEADER_SET(ReleaseFileSz, sizeof(pkgCache::ReleaseFile));
67 APT_HEADER_SET(PackageFileSz, sizeof(pkgCache::PackageFile));
68 APT_HEADER_SET(VersionSz, sizeof(pkgCache::Version));
69 APT_HEADER_SET(DescriptionSz, sizeof(pkgCache::Description));
70 APT_HEADER_SET(DependencySz, sizeof(pkgCache::Dependency));
71 APT_HEADER_SET(DependencyDataSz, sizeof(pkgCache::DependencyData));
72 APT_HEADER_SET(ProvidesSz, sizeof(pkgCache::Provides));
73 APT_HEADER_SET(VerFileSz, sizeof(pkgCache::VerFile));
74 APT_HEADER_SET(DescFileSz, sizeof(pkgCache::DescFile));
75#undef APT_HEADER_SET
76
52c41485 77 GroupCount = 0;
578bfd0a
AL
78 PackageCount = 0;
79 VersionCount = 0;
a52f938b 80 DescriptionCount = 0;
578bfd0a 81 DependsCount = 0;
71c9e95b 82 DependsDataCount = 0;
b07aeb1a 83 ReleaseFileCount = 0;
578bfd0a 84 PackageFileCount = 0;
a7e66b17 85 VerFileCount = 0;
a52f938b 86 DescFileCount = 0;
a7e66b17 87 ProvidesCount = 0;
ad00ae81 88 MaxVerFileSize = 0;
a52f938b 89 MaxDescFileSize = 0;
dfe66c72 90
578bfd0a 91 FileList = 0;
b07aeb1a 92 RlsFileList = 0;
b2e465d6
AL
93 VerSysName = 0;
94 Architecture = 0;
32ab4bd0
DK
95 SetArchitectures(0);
96 SetHashTableSize(_config->FindI("APT::Cache-HashTableSize", 10 * 1048));
578bfd0a 97 memset(Pools,0,sizeof(Pools));
0688ccd8
JAK
98
99 CacheFileSize = 0;
578bfd0a
AL
100}
101 /*}}}*/
102// Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
103// ---------------------------------------------------------------------
104/* */
105bool pkgCache::Header::CheckSizes(Header &Against) const
106{
107 if (HeaderSz == Against.HeaderSz &&
52c41485 108 GroupSz == Against.GroupSz &&
578bfd0a 109 PackageSz == Against.PackageSz &&
b07aeb1a 110 ReleaseFileSz == Against.ReleaseFileSz &&
578bfd0a
AL
111 PackageFileSz == Against.PackageFileSz &&
112 VersionSz == Against.VersionSz &&
a52f938b 113 DescriptionSz == Against.DescriptionSz &&
dcb79bae 114 DependencySz == Against.DependencySz &&
71c9e95b 115 DependencyDataSz == Against.DependencyDataSz &&
dcb79bae 116 VerFileSz == Against.VerFileSz &&
a52f938b 117 DescFileSz == Against.DescFileSz &&
578bfd0a
AL
118 ProvidesSz == Against.ProvidesSz)
119 return true;
120 return false;
121}
122 /*}}}*/
123
124// Cache::pkgCache - Constructor /*{{{*/
125// ---------------------------------------------------------------------
126/* */
32ab4bd0 127APT_IGNORE_DEPRECATED_PUSH
6c55f07a 128pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map), d(NULL)
578bfd0a 129{
4cbf323f
MV
130 // call getArchitectures() with cached=false to ensure that the
131 // architectures cache is re-evaulated. this is needed in cases
132 // when the APT::Architecture field changes between two cache creations
133 MultiArchEnabled = APT::Configuration::getArchitectures(false).size() > 1;
b2e465d6
AL
134 if (DoMap == true)
135 ReMap();
578bfd0a 136}
32ab4bd0 137APT_IGNORE_DEPRECATED_POP
578bfd0a
AL
138 /*}}}*/
139// Cache::ReMap - Reopen the cache file /*{{{*/
140// ---------------------------------------------------------------------
141/* If the file is already closed then this will open it open it. */
a9fe5928 142bool pkgCache::ReMap(bool const &Errorchecks)
578bfd0a
AL
143{
144 // Apply the typecasts.
145 HeaderP = (Header *)Map.Data();
5bf15716 146 GrpP = (Group *)Map.Data();
578bfd0a 147 PkgP = (Package *)Map.Data();
dcb79bae 148 VerFileP = (VerFile *)Map.Data();
a52f938b 149 DescFileP = (DescFile *)Map.Data();
b07aeb1a 150 RlsFileP = (ReleaseFile *)Map.Data();
578bfd0a
AL
151 PkgFileP = (PackageFile *)Map.Data();
152 VerP = (Version *)Map.Data();
a52f938b 153 DescP = (Description *)Map.Data();
578bfd0a
AL
154 ProvideP = (Provides *)Map.Data();
155 DepP = (Dependency *)Map.Data();
71c9e95b 156 DepDataP = (DependencyData *)Map.Data();
578bfd0a
AL
157 StrP = (char *)Map.Data();
158
a9fe5928
DK
159 if (Errorchecks == false)
160 return true;
161
b2e465d6
AL
162 if (Map.Size() == 0 || HeaderP == 0)
163 return _error->Error(_("Empty package cache"));
578bfd0a
AL
164
165 // Check the header
166 Header DefHeader;
167 if (HeaderP->Signature != DefHeader.Signature ||
168 HeaderP->Dirty == true)
b2e465d6 169 return _error->Error(_("The package cache file is corrupted"));
578bfd0a
AL
170
171 if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
172 HeaderP->MinorVersion != DefHeader.MinorVersion ||
173 HeaderP->CheckSizes(DefHeader) == false)
b2e465d6
AL
174 return _error->Error(_("The package cache file is an incompatible version"));
175
7d79339f
JAK
176 if (Map.Size() < HeaderP->CacheFileSize)
177 return _error->Error(_("The package cache file is corrupted, it is too small"));
178
32ab4bd0 179 if (HeaderP->VerSysName == 0 || HeaderP->Architecture == 0 || HeaderP->GetArchitectures() == 0)
7a223b93
DK
180 return _error->Error(_("The package cache file is corrupted"));
181
b2e465d6 182 // Locate our VS..
7a223b93 183 if ((VS = pkgVersioningSystem::GetVS(StrP + HeaderP->VerSysName)) == 0)
db0db9fe 184 return _error->Error(_("This APT does not support the versioning system '%s'"),StrP + HeaderP->VerSysName);
b2e465d6 185
7a223b93
DK
186 // Check the architecture
187 std::vector<std::string> archs = APT::Configuration::getArchitectures();
188 std::vector<std::string>::const_iterator a = archs.begin();
189 std::string list = *a;
190 for (++a; a != archs.end(); ++a)
191 list.append(",").append(*a);
192 if (_config->Find("APT::Architecture") != StrP + HeaderP->Architecture ||
32ab4bd0
DK
193 list != StrP + HeaderP->GetArchitectures())
194 return _error->Error(_("The package cache was built for different architectures: %s vs %s"), StrP + HeaderP->GetArchitectures(), list.c_str());
7a223b93 195
578bfd0a
AL
196 return true;
197}
198 /*}}}*/
199// Cache::Hash - Hash a string /*{{{*/
200// ---------------------------------------------------------------------
201/* This is used to generate the hash entries for the HashTable. With my
202 package list from bo this function gets 94% table usage on a 512 item
203 table (480 used items) */
4ad8619b 204map_id_t pkgCache::sHash(const string &Str) const
578bfd0a
AL
205{
206 unsigned long Hash = 0;
f7f0d6c7 207 for (string::const_iterator I = Str.begin(); I != Str.end(); ++I)
aa0fe657 208 Hash = 41 * Hash + tolower_ascii(*I);
32ab4bd0 209 return Hash % HeaderP->GetHashTableSize();
578bfd0a
AL
210}
211
4ad8619b 212map_id_t pkgCache::sHash(const char *Str) const
578bfd0a 213{
aa0fe657
DK
214 unsigned long Hash = tolower_ascii(*Str);
215 for (const char *I = Str + 1; *I != 0; ++I)
216 Hash = 41 * Hash + tolower_ascii(*I);
32ab4bd0 217 return Hash % HeaderP->GetHashTableSize();
578bfd0a 218}
8d4c859d 219 /*}}}*/
578bfd0a
AL
220// Cache::FindPkg - Locate a package by name /*{{{*/
221// ---------------------------------------------------------------------
222/* Returns 0 on error, pointer to the package otherwise */
25396fb0
DK
223pkgCache::PkgIterator pkgCache::FindPkg(const string &Name) {
224 size_t const found = Name.find(':');
225 if (found == string::npos)
6c9937da 226 return FindPkg(Name, "native");
4d174dc8 227 string const Arch = Name.substr(found+1);
8fec289a
DK
228 /* Beware: This is specialcased to handle pkg:any in dependencies
229 as these are linked to virtual pkg:any named packages.
230 If you want any arch from a pkg, use FindPkg(pkg,"any") */
4d174dc8
DK
231 if (Arch == "any")
232 return FindPkg(Name, "any");
233 return FindPkg(Name.substr(0, found), Arch);
25396fb0
DK
234}
235 /*}}}*/
236// Cache::FindPkg - Locate a package by name /*{{{*/
237// ---------------------------------------------------------------------
238/* Returns 0 on error, pointer to the package otherwise */
8d4c859d 239pkgCache::PkgIterator pkgCache::FindPkg(const string &Name, string const &Arch) {
5bf15716
DK
240 /* We make a detour via the GrpIterator here as
241 on a multi-arch environment a group is easier to
242 find than a package (less entries in the buckets) */
243 pkgCache::GrpIterator Grp = FindGrp(Name);
244 if (Grp.end() == true)
245 return PkgIterator(*this,0);
246
247 return Grp.FindPkg(Arch);
248}
249 /*}}}*/
250// Cache::FindGrp - Locate a group by name /*{{{*/
251// ---------------------------------------------------------------------
252/* Returns End-Pointer on error, pointer to the group otherwise */
253pkgCache::GrpIterator pkgCache::FindGrp(const string &Name) {
254 if (unlikely(Name.empty() == true))
255 return GrpIterator(*this,0);
256
257 // Look at the hash bucket for the group
32ab4bd0 258 Group *Grp = GrpP + HeaderP->GrpHashTableP()[sHash(Name)];
5bf15716 259 for (; Grp != GrpP; Grp = GrpP + Grp->Next) {
b59325e8 260 int const cmp = strcmp(Name.c_str(), StrP + Grp->Name);
aa0fe657 261 if (cmp == 0)
5bf15716 262 return GrpIterator(*this, Grp);
aa0fe657
DK
263 else if (cmp < 0)
264 break;
5bf15716
DK
265 }
266
267 return GrpIterator(*this,0);
578bfd0a
AL
268}
269 /*}}}*/
b2e465d6
AL
270// Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
271// ---------------------------------------------------------------------
272/* This returns a string representation of the dependency compare
273 type in the weird debian style.. */
274const char *pkgCache::CompTypeDeb(unsigned char Comp)
275{
69c2ecbd
DK
276 const char * const Ops[] = {"","<=",">=","<<",">>","=","!="};
277 if (unlikely((unsigned)(Comp & 0xF) >= sizeof(Ops)/sizeof(Ops[0])))
278 return "";
279 return Ops[Comp & 0xF];
b2e465d6
AL
280}
281 /*}}}*/
282// Cache::CompType - Return a string describing the compare type /*{{{*/
283// ---------------------------------------------------------------------
69c2ecbd 284/* This returns a string representation of the dependency compare
b2e465d6
AL
285 type */
286const char *pkgCache::CompType(unsigned char Comp)
287{
69c2ecbd
DK
288 const char * const Ops[] = {"","<=",">=","<",">","=","!="};
289 if (unlikely((unsigned)(Comp & 0xF) >= sizeof(Ops)/sizeof(Ops[0])))
290 return "";
291 return Ops[Comp & 0xF];
b2e465d6
AL
292}
293 /*}}}*/
294// Cache::DepType - Return a string describing the dep type /*{{{*/
295// ---------------------------------------------------------------------
296/* */
297const char *pkgCache::DepType(unsigned char Type)
298{
299 const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
300 _("Recommends"),_("Conflicts"),_("Replaces"),
f8ae7e8b 301 _("Obsoletes"),_("Breaks"), _("Enhances")};
308c7d30 302 if (Type < sizeof(Types)/sizeof(*Types))
b2e465d6
AL
303 return Types[Type];
304 return "";
305}
306 /*}}}*/
0149949b
AL
307// Cache::Priority - Convert a priority value to a string /*{{{*/
308// ---------------------------------------------------------------------
309/* */
310const char *pkgCache::Priority(unsigned char Prio)
311{
2fcd25a9 312 const char *Mapping[] = {0,_("required"),_("important"),_("standard"),
b2e465d6 313 _("optional"),_("extra")};
0149949b
AL
314 if (Prio < _count(Mapping))
315 return Mapping[Prio];
316 return 0;
317}
318 /*}}}*/
5bf15716
DK
319// GrpIterator::FindPkg - Locate a package by arch /*{{{*/
320// ---------------------------------------------------------------------
321/* Returns an End-Pointer on error, pointer to the package otherwise */
e841200b 322pkgCache::PkgIterator pkgCache::GrpIterator::FindPkg(string Arch) const {
5bf15716
DK
323 if (unlikely(IsGood() == false || S->FirstPackage == 0))
324 return PkgIterator(*Owner, 0);
325
60dcec6d 326 /* If we accept any package we simply return the "first"
8fec289a 327 package in this group */
60dcec6d
DK
328 if (Arch == "any")
329 return PkgIterator(*Owner, Owner->PkgP + S->FirstPackage);
8fec289a
DK
330 if (Arch == "native" || Arch == "all")
331 Arch = Owner->NativeArch();
5bf15716 332
5d1e330d 333 // Iterate over the list to find the matching arch
5bf15716 334 for (pkgCache::Package *Pkg = PackageList(); Pkg != Owner->PkgP;
32ab4bd0 335 Pkg = Owner->PkgP + Pkg->NextPackage) {
5d1e330d 336 if (stringcmp(Arch, Owner->StrP + Pkg->Arch) == 0)
5bf15716
DK
337 return PkgIterator(*Owner, Pkg);
338 if ((Owner->PkgP + S->LastPackage) == Pkg)
339 break;
340 }
341
bd2fb30a
DK
342 return PkgIterator(*Owner, 0);
343}
344 /*}}}*/
345// GrpIterator::FindPreferredPkg - Locate the "best" package /*{{{*/
346// ---------------------------------------------------------------------
347/* Returns an End-Pointer on error, pointer to the package otherwise */
3db58cf4 348pkgCache::PkgIterator pkgCache::GrpIterator::FindPreferredPkg(bool const &PreferNonVirtual) const {
bd2fb30a 349 pkgCache::PkgIterator Pkg = FindPkg("native");
3db58cf4 350 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
bd2fb30a
DK
351 return Pkg;
352
353 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
354 for (std::vector<std::string>::const_iterator a = archs.begin();
355 a != archs.end(); ++a) {
356 Pkg = FindPkg(*a);
3db58cf4 357 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
bd2fb30a
DK
358 return Pkg;
359 }
c919ad6e
DK
360 // packages without an architecture
361 Pkg = FindPkg("none");
362 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
363 return Pkg;
bd2fb30a 364
3db58cf4
DK
365 if (PreferNonVirtual == true)
366 return FindPreferredPkg(false);
5bf15716
DK
367 return PkgIterator(*Owner, 0);
368}
369 /*}}}*/
370// GrpIterator::NextPkg - Locate the next package in the group /*{{{*/
371// ---------------------------------------------------------------------
372/* Returns an End-Pointer on error, pointer to the package otherwise.
c408e01e
DK
373 We can't simply ++ to the next as the next package of the last will
374 be from a different group (with the same hash value) */
e841200b 375pkgCache::PkgIterator pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator const &LastPkg) const {
5bf15716
DK
376 if (unlikely(IsGood() == false || S->FirstPackage == 0 ||
377 LastPkg.end() == true))
378 return PkgIterator(*Owner, 0);
379
c408e01e
DK
380 if (S->LastPackage == LastPkg.Index())
381 return PkgIterator(*Owner, 0);
5bf15716 382
32ab4bd0 383 return PkgIterator(*Owner, Owner->PkgP + LastPkg->NextPackage);
5bf15716
DK
384}
385 /*}}}*/
71c9e95b 386// GrpIterator::operator++ - Prefix incr /*{{{*/
25396fb0
DK
387// ---------------------------------------------------------------------
388/* This will advance to the next logical group in the hash table. */
3707fd4f 389pkgCache::GrpIterator& pkgCache::GrpIterator::operator++()
25396fb0
DK
390{
391 // Follow the current links
392 if (S != Owner->GrpP)
393 S = Owner->GrpP + S->Next;
394
395 // Follow the hash table
32ab4bd0 396 while (S == Owner->GrpP && (HashIndex+1) < (signed)Owner->HeaderP->GetHashTableSize())
25396fb0 397 {
71c9e95b 398 ++HashIndex;
32ab4bd0 399 S = Owner->GrpP + Owner->HeaderP->GrpHashTableP()[HashIndex];
25396fb0 400 }
3707fd4f 401 return *this;
d3e8fbb3 402}
f55a958f 403 /*}}}*/
71c9e95b 404// PkgIterator::operator++ - Prefix incr /*{{{*/
578bfd0a
AL
405// ---------------------------------------------------------------------
406/* This will advance to the next logical package in the hash table. */
71c9e95b 407pkgCache::PkgIterator& pkgCache::PkgIterator::operator++()
578bfd0a
AL
408{
409 // Follow the current links
773e2c1f 410 if (S != Owner->PkgP)
32ab4bd0 411 S = Owner->PkgP + S->NextPackage;
b2e465d6 412
578bfd0a 413 // Follow the hash table
32ab4bd0 414 while (S == Owner->PkgP && (HashIndex+1) < (signed)Owner->HeaderP->GetHashTableSize())
578bfd0a 415 {
71c9e95b 416 ++HashIndex;
32ab4bd0 417 S = Owner->PkgP + Owner->HeaderP->PkgHashTableP()[HashIndex];
578bfd0a 418 }
3707fd4f 419 return *this;
d3e8fbb3 420}
578bfd0a 421 /*}}}*/
71c9e95b
DK
422pkgCache::DepIterator& pkgCache::DepIterator::operator++() /*{{{*/
423{
424 if (S == Owner->DepP)
425 return *this;
426 S = Owner->DepP + (Type == DepVer ? S->NextDepends : S->NextRevDepends);
427 if (S == Owner->DepP)
428 S2 = Owner->DepDataP;
429 else
430 S2 = Owner->DepDataP + S->DependencyData;
431 return *this;
432}
433 /*}}}*/
578bfd0a
AL
434// PkgIterator::State - Check the State of the package /*{{{*/
435// ---------------------------------------------------------------------
436/* By this we mean if it is either cleanly installed or cleanly removed. */
437pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
d38b7b3d 438{
773e2c1f
DK
439 if (S->InstState == pkgCache::State::ReInstReq ||
440 S->InstState == pkgCache::State::HoldReInstReq)
c7c1b0f6
AL
441 return NeedsUnpack;
442
773e2c1f
DK
443 if (S->CurrentState == pkgCache::State::UnPacked ||
444 S->CurrentState == pkgCache::State::HalfConfigured)
c6aa14e4
MV
445 // we leave triggers alone complettely. dpkg deals with
446 // them in a hard-to-predict manner and if they get
447 // resolved by dpkg before apt run dpkg --configure on
448 // the TriggersPending package dpkg returns a error
09fab244 449 //Pkg->CurrentState == pkgCache::State::TriggersAwaited
c6aa14e4 450 //Pkg->CurrentState == pkgCache::State::TriggersPending)
578bfd0a
AL
451 return NeedsConfigure;
452
773e2c1f
DK
453 if (S->CurrentState == pkgCache::State::HalfInstalled ||
454 S->InstState != pkgCache::State::Ok)
578bfd0a
AL
455 return NeedsUnpack;
456
457 return NeedsNothing;
458}
459 /*}}}*/
af29ffb4
MV
460// PkgIterator::CandVersion - Returns the candidate version string /*{{{*/
461// ---------------------------------------------------------------------
462/* Return string representing of the candidate version. */
463const char *
d3e8fbb3 464pkgCache::PkgIterator::CandVersion() const
af29ffb4
MV
465{
466 //TargetVer is empty, so don't use it.
749eb4cf 467 VerIterator version = pkgPolicy(Owner).GetCandidateVer(*this);
af29ffb4
MV
468 if (version.IsGood())
469 return version.VerStr();
470 return 0;
d3e8fbb3 471}
af29ffb4
MV
472 /*}}}*/
473// PkgIterator::CurVersion - Returns the current version string /*{{{*/
474// ---------------------------------------------------------------------
475/* Return string representing of the current version. */
476const char *
d3e8fbb3 477pkgCache::PkgIterator::CurVersion() const
af29ffb4
MV
478{
479 VerIterator version = CurrentVer();
480 if (version.IsGood())
481 return CurrentVer().VerStr();
482 return 0;
d3e8fbb3 483}
af29ffb4
MV
484 /*}}}*/
485// ostream operator to handle string representation of a package /*{{{*/
486// ---------------------------------------------------------------------
487/* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section)
1e3f4083 488 Note that the characters <|>() are all literal above. Versions will be omitted
af29ffb4
MV
489 if they provide no new information (e.g. there is no newer version than candidate)
490 If no version and/or section can be found "none" is used. */
491std::ostream&
8f3ba4e8 492operator<<(std::ostream& out, pkgCache::PkgIterator Pkg)
af29ffb4
MV
493{
494 if (Pkg.end() == true)
495 return out << "invalid package";
496
497 string current = string(Pkg.CurVersion() == 0 ? "none" : Pkg.CurVersion());
498 string candidate = string(Pkg.CandVersion() == 0 ? "none" : Pkg.CandVersion());
499 string newest = string(Pkg.VersionList().end() ? "none" : Pkg.VersionList().VerStr());
500
5dd4c8b8 501 out << Pkg.Name() << " [ " << Pkg.Arch() << " ] < " << current;
af29ffb4
MV
502 if (current != candidate)
503 out << " -> " << candidate;
504 if ( newest != "none" && candidate != newest)
505 out << " | " << newest;
7a669774
DK
506 if (Pkg->VersionList == 0)
507 out << " > ( none )";
508 else
509 out << " > ( " << string(Pkg.VersionList().Section()==0?"unknown":Pkg.VersionList().Section()) << " )";
af29ffb4
MV
510 return out;
511}
512 /*}}}*/
75ce2062
DK
513// PkgIterator::FullName - Returns Name and (maybe) Architecture /*{{{*/
514// ---------------------------------------------------------------------
515/* Returns a name:arch string */
516std::string pkgCache::PkgIterator::FullName(bool const &Pretty) const
517{
518 string fullname = Name();
519 if (Pretty == false ||
f6ce7ffc 520 (strcmp(Arch(), "all") != 0 && strcmp(Arch(), "any") != 0 &&
959470da 521 strcmp(Owner->NativeArch(), Arch()) != 0))
75ce2062
DK
522 return fullname.append(":").append(Arch());
523 return fullname;
524}
525 /*}}}*/
578bfd0a
AL
526// DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
527// ---------------------------------------------------------------------
528/* Currently critical deps are defined as depends, predepends and
308c7d30 529 conflicts (including dpkg's Breaks fields). */
e841200b 530bool pkgCache::DepIterator::IsCritical() const
578bfd0a 531{
359e46db 532 if (IsNegative() == true ||
71c9e95b
DK
533 S2->Type == pkgCache::Dep::Depends ||
534 S2->Type == pkgCache::Dep::PreDepends)
578bfd0a
AL
535 return true;
536 return false;
537}
538 /*}}}*/
df77d8a5
DK
539// DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
540// ---------------------------------------------------------------------
541/* Some dependencies are positive like Depends and Recommends, others
542 are negative like Conflicts which can and should be handled differently */
543bool pkgCache::DepIterator::IsNegative() const
544{
71c9e95b
DK
545 return S2->Type == Dep::DpkgBreaks ||
546 S2->Type == Dep::Conflicts ||
547 S2->Type == Dep::Obsoletes;
df77d8a5
DK
548}
549 /*}}}*/
578bfd0a
AL
550// DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
551// ---------------------------------------------------------------------
552/* This intellegently looks at dep target packages and tries to figure
553 out which package should be used. This is needed to nicely handle
554 provide mapping. If the target package has no other providing packages
555 then it returned. Otherwise the providing list is looked at to
556 see if there is one one unique providing package if so it is returned.
557 Otherwise true is returned and the target package is set. The return
b2e465d6
AL
558 result indicates whether the node should be expandable
559
560 In Conjunction with the DepCache the value of Result may not be
561 super-good since the policy may have made it uninstallable. Using
562 AllTargets is better in this case. */
e841200b 563bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) const
578bfd0a
AL
564{
565 Result = TargetPkg();
566
567 // No provides at all
568 if (Result->ProvidesList == 0)
569 return false;
570
571 // There is the Base package and the providing ones which is at least 2
572 if (Result->VersionList != 0)
573 return true;
574
575 /* We have to skip over indirect provisions of the package that
576 owns the dependency. For instance, if libc5-dev depends on the
577 virtual package libc-dev which is provided by libc5-dev and libc6-dev
578 we must ignore libc5-dev when considering the provides list. */
579 PrvIterator PStart = Result.ProvidesList();
f7f0d6c7 580 for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); ++PStart);
578bfd0a
AL
581
582 // Nothing but indirect self provides
583 if (PStart.end() == true)
584 return false;
585
586 // Check for single packages in the provides list
587 PrvIterator P = PStart;
f7f0d6c7 588 for (; P.end() != true; ++P)
578bfd0a
AL
589 {
590 // Skip over self provides
591 if (P.OwnerPkg() == ParentPkg())
592 continue;
593 if (PStart.OwnerPkg() != P.OwnerPkg())
594 break;
595 }
b2e465d6
AL
596
597 Result = PStart.OwnerPkg();
578bfd0a
AL
598
599 // Check for non dups
600 if (P.end() != true)
601 return true;
b2e465d6 602
578bfd0a
AL
603 return false;
604}
605 /*}}}*/
606// DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
607// ---------------------------------------------------------------------
b2e465d6 608/* This is a more useful version of TargetPkg() that follows versioned
578bfd0a 609 provides. It includes every possible package-version that could satisfy
fbfb2a7c
AL
610 the dependency. The last item in the list has a 0. The resulting pointer
611 must be delete [] 'd */
e841200b 612pkgCache::Version **pkgCache::DepIterator::AllTargets() const
578bfd0a
AL
613{
614 Version **Res = 0;
615 unsigned long Size =0;
616 while (1)
617 {
618 Version **End = Res;
619 PkgIterator DPkg = TargetPkg();
620
621 // Walk along the actual package providing versions
f7f0d6c7 622 for (VerIterator I = DPkg.VersionList(); I.end() == false; ++I)
578bfd0a 623 {
85434114 624 if (IsIgnorable(I.ParentPkg()) == true)
578bfd0a 625 continue;
887c6940 626 if (IsSatisfied(I) == false)
578bfd0a 627 continue;
85434114 628
578bfd0a
AL
629 Size++;
630 if (Res != 0)
631 *End++ = I;
632 }
633
634 // Follow all provides
f7f0d6c7 635 for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; ++I)
578bfd0a 636 {
85434114 637 if (IsIgnorable(I) == true)
578bfd0a 638 continue;
887c6940 639 if (IsSatisfied(I) == false)
85434114 640 continue;
5f909b67 641
578bfd0a
AL
642 Size++;
643 if (Res != 0)
644 *End++ = I.OwnerVer();
645 }
646
647 // Do it again and write it into the array
648 if (Res == 0)
649 {
650 Res = new Version *[Size+1];
651 Size = 0;
652 }
653 else
654 {
655 *End = 0;
656 break;
657 }
658 }
659
660 return Res;
661}
662 /*}}}*/
43d017d6
AL
663// DepIterator::GlobOr - Compute an OR group /*{{{*/
664// ---------------------------------------------------------------------
665/* This Takes an iterator, iterates past the current dependency grouping
666 and returns Start and End so that so End is the final element
667 in the group, if End == Start then D is End++ and End is the
668 dependency D was pointing to. Use in loops to iterate sensibly. */
669void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
670{
671 // Compute a single dependency element (glob or)
672 Start = *this;
673 End = *this;
018f1533 674 for (bool LastOR = true; end() == false && LastOR == true;)
43d017d6 675 {
71c9e95b 676 LastOR = (S2->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
3707fd4f 677 ++(*this);
43d017d6
AL
678 if (LastOR == true)
679 End = (*this);
680 }
681}
682 /*}}}*/
85434114
DK
683// DepIterator::IsIgnorable - should this packag/providr be ignored? /*{{{*/
684// ---------------------------------------------------------------------
685/* Deps like self-conflicts should be ignored as well as implicit conflicts
686 on virtual packages. */
fd23676e 687bool pkgCache::DepIterator::IsIgnorable(PkgIterator const &PT) const
85434114 688{
021626db
DK
689 if (IsNegative() == false)
690 return false;
691
fd23676e 692 pkgCache::PkgIterator const PP = ParentPkg();
021626db
DK
693 if (PP->Group != PT->Group)
694 return false;
695 // self-conflict
696 if (PP == PT)
697 return true;
fd23676e 698 pkgCache::VerIterator const PV = ParentVer();
021626db
DK
699 // ignore group-conflict on a M-A:same package - but not our implicit dependencies
700 // so that we can have M-A:same packages conflicting with their own real name
701 if ((PV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
8c7af4d4 702 return IsMultiArchImplicit() == false;
85434114
DK
703
704 return false;
705}
706bool pkgCache::DepIterator::IsIgnorable(PrvIterator const &Prv) const
707{
708 if (IsNegative() == false)
709 return false;
710
711 PkgIterator const Pkg = ParentPkg();
712 /* Provides may never be applied against the same package (or group)
713 if it is a conflicts. See the comment above. */
714 if (Prv.OwnerPkg()->Group == Pkg->Group)
715 return true;
716 // Implicit group-conflicts should not be applied on providers of other groups
8c7af4d4 717 if (IsMultiArchImplicit() && Prv.OwnerPkg()->Group != Pkg->Group)
85434114
DK
718 return true;
719
720 return false;
721}
722 /*}}}*/
887c6940
DK
723// DepIterator::IsSatisfied - check if a version satisfied the dependency /*{{{*/
724bool pkgCache::DepIterator::IsSatisfied(VerIterator const &Ver) const
725{
71c9e95b 726 return Owner->VS->CheckDep(Ver.VerStr(),S2->CompareOp,TargetVer());
887c6940
DK
727}
728bool pkgCache::DepIterator::IsSatisfied(PrvIterator const &Prv) const
729{
71c9e95b 730 return Owner->VS->CheckDep(Prv.ProvideVersion(),S2->CompareOp,TargetVer());
887c6940
DK
731}
732 /*}}}*/
8c7af4d4
DK
733// DepIterator::IsImplicit - added by the cache generation /*{{{*/
734bool pkgCache::DepIterator::IsImplicit() const
735{
736 if (IsMultiArchImplicit() == true)
737 return true;
738 if (IsNegative() || S2->Type == pkgCache::Dep::Replaces)
739 {
740 if ((S2->CompareOp & pkgCache::Dep::ArchSpecific) != pkgCache::Dep::ArchSpecific &&
741 strcmp(ParentPkg().Arch(), TargetPkg().Arch()) != 0)
742 return true;
743 }
744 return false;
745}
746 /*}}}*/
47f6d1b7
DK
747// ostream operator to handle string representation of a dependecy /*{{{*/
748// ---------------------------------------------------------------------
749/* */
8f3ba4e8 750std::ostream& operator<<(std::ostream& out, pkgCache::DepIterator D)
47f6d1b7
DK
751{
752 if (D.end() == true)
753 return out << "invalid dependency";
754
755 pkgCache::PkgIterator P = D.ParentPkg();
756 pkgCache::PkgIterator T = D.TargetPkg();
757
758 out << (P.end() ? "invalid pkg" : P.FullName(false)) << " " << D.DepType()
759 << " on ";
760 if (T.end() == true)
761 out << "invalid pkg";
762 else
763 out << T;
764
765 if (D->Version != 0)
766 out << " (" << D.CompType() << " " << D.TargetVer() << ")";
767
768 return out;
769}
770 /*}}}*/
578bfd0a
AL
771// VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
772// ---------------------------------------------------------------------
773/* This just looks over the version list to see if B is listed before A. In
774 most cases this will return in under 4 checks, ver lists are short. */
775int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
776{
777 // Check if they are equal
778 if (*this == B)
779 return 0;
780 if (end() == true)
781 return -1;
782 if (B.end() == true)
783 return 1;
784
785 /* Start at A and look for B. If B is found then A > B otherwise
786 B was before A so A < B */
787 VerIterator I = *this;
f7f0d6c7 788 for (;I.end() == false; ++I)
578bfd0a
AL
789 if (I == B)
790 return 1;
791 return -1;
792}
793 /*}}}*/
b518cca6
AL
794// VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
795// ---------------------------------------------------------------------
796/* */
0e6fe58e 797APT_PURE bool pkgCache::VerIterator::Downloadable() const
b518cca6
AL
798{
799 VerFileIterator Files = FileList();
f7f0d6c7 800 for (; Files.end() == false; ++Files)
b07aeb1a 801 if (Files.File().Flagged(pkgCache::Flag::NotSource) == false)
b518cca6
AL
802 return true;
803 return false;
804}
805 /*}}}*/
3c124dde
AL
806// VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
807// ---------------------------------------------------------------------
808/* This checks to see if any of the versions files are not NotAutomatic.
809 True if this version is selectable for automatic installation. */
0e6fe58e 810APT_PURE bool pkgCache::VerIterator::Automatic() const
3c124dde
AL
811{
812 VerFileIterator Files = FileList();
f7f0d6c7 813 for (; Files.end() == false; ++Files)
5ed56f93 814 // Do not check ButAutomaticUpgrades here as it is kind of automatic…
b07aeb1a 815 if (Files.File().Flagged(pkgCache::Flag::NotAutomatic) == false)
3c124dde
AL
816 return true;
817 return false;
818}
819 /*}}}*/
820// VerIterator::NewestFile - Return the newest file version relation /*{{{*/
821// ---------------------------------------------------------------------
822/* This looks at the version numbers associated with all of the sources
823 this version is in and returns the highest.*/
824pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const
825{
826 VerFileIterator Files = FileList();
827 VerFileIterator Highest = Files;
f7f0d6c7 828 for (; Files.end() == false; ++Files)
3c124dde 829 {
b2e465d6 830 if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0)
3c124dde
AL
831 Highest = Files;
832 }
833
834 return Highest;
835}
836 /*}}}*/
b2e465d6
AL
837// VerIterator::RelStr - Release description string /*{{{*/
838// ---------------------------------------------------------------------
839/* This describes the version from a release-centric manner. The output is a
840 list of Label:Version/Archive */
7f8c0eed
DK
841static std::string PkgFileIteratorToRelString(pkgCache::PkgFileIterator const &File)
842{
843 std::string Res;
844 if (File.Label() != 0)
845 Res = Res + File.Label() + ':';
846
847 if (File.Archive() != 0)
848 {
849 if (File.Version() == 0)
850 Res += File.Archive();
851 else
852 Res = Res + File.Version() + '/' + File.Archive();
853 }
854 else
855 {
856 // No release file, print the host name that this came from
857 if (File.Site() == 0 || File.Site()[0] == 0)
858 Res += "localhost";
859 else
860 Res += File.Site();
861 }
862 return Res;
863}
d4489d49 864string pkgCache::VerIterator::RelStr() const
b2e465d6 865{
7f8c0eed 866 std::vector<std::string> RelStrs;
f7f0d6c7 867 for (pkgCache::VerFileIterator I = this->FileList(); I.end() == false; ++I)
b2e465d6
AL
868 {
869 // Do not print 'not source' entries'
b07aeb1a
DK
870 pkgCache::PkgFileIterator const File = I.File();
871 if (File.Flagged(pkgCache::Flag::NotSource))
b2e465d6
AL
872 continue;
873
7f8c0eed
DK
874 std::string const RS = PkgFileIteratorToRelString(File);
875 if (std::find(RelStrs.begin(), RelStrs.end(), RS) != RelStrs.end())
b2e465d6 876 continue;
b2e465d6 877
7f8c0eed
DK
878 RelStrs.push_back(RS);
879 }
880 std::ostringstream os;
881 if (likely(RelStrs.empty() == false))
882 {
883 std::copy(RelStrs.begin(), RelStrs.end()-1, std::ostream_iterator<std::string>(os, ", "));
884 os << *RelStrs.rbegin();
5dd4c8b8 885 }
857e9c13 886 if (S->ParentPkg != 0)
7f8c0eed
DK
887 os << " [" << Arch() << "]";
888 return os.str();
b2e465d6
AL
889}
890 /*}}}*/
7a948ec7
DK
891// VerIterator::MultiArchType - string representing MultiArch flag /*{{{*/
892const char * pkgCache::VerIterator::MultiArchType() const
893{
894 if ((S->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
895 return "same";
896 else if ((S->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
897 return "foreign";
898 else if ((S->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
899 return "allowed";
900 return "none";
901}
902 /*}}}*/
b07aeb1a 903// RlsFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
578bfd0a
AL
904// ---------------------------------------------------------------------
905/* This stats the file and compares its stats with the ones that were
b07aeb1a 906 stored during generation. Date checks should probably also be
578bfd0a 907 included here. */
b07aeb1a 908bool pkgCache::RlsFileIterator::IsOk()
578bfd0a
AL
909{
910 struct stat Buf;
911 if (stat(FileName(),&Buf) != 0)
912 return false;
913
773e2c1f 914 if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime)
578bfd0a
AL
915 return false;
916
917 return true;
918}
919 /*}}}*/
b07aeb1a
DK
920// RlsFileIterator::RelStr - Return the release string /*{{{*/
921string pkgCache::RlsFileIterator::RelStr()
af87ab54
AL
922{
923 string Res;
924 if (Version() != 0)
925 Res = Res + (Res.empty() == true?"v=":",v=") + Version();
926 if (Origin() != 0)
927 Res = Res + (Res.empty() == true?"o=":",o=") + Origin();
928 if (Archive() != 0)
929 Res = Res + (Res.empty() == true?"a=":",a=") + Archive();
efc487fb
DK
930 if (Codename() != 0)
931 Res = Res + (Res.empty() == true?"n=":",n=") + Codename();
af87ab54
AL
932 if (Label() != 0)
933 Res = Res + (Res.empty() == true?"l=":",l=") + Label();
b07aeb1a
DK
934 return Res;
935}
936 /*}}}*/
937// PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
938// ---------------------------------------------------------------------
939/* This stats the file and compares its stats with the ones that were
940 stored during generation. Date checks should probably also be
941 included here. */
942bool pkgCache::PkgFileIterator::IsOk()
943{
944 struct stat Buf;
945 if (stat(FileName(),&Buf) != 0)
946 return false;
947
948 if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime)
949 return false;
950
951 return true;
952}
953 /*}}}*/
954string pkgCache::PkgFileIterator::RelStr() /*{{{*/
955{
956 std::string Res;
957 if (ReleaseFile() == 0)
958 {
959 if (Component() != 0)
960 Res = Res + (Res.empty() == true?"a=":",a=") + Component();
961 }
962 else
963 {
964 Res = ReleaseFile().RelStr();
965 if (Component() != 0)
966 Res = Res + (Res.empty() == true?"c=":",c=") + Component();
967 }
5dd4c8b8
DK
968 if (Architecture() != 0)
969 Res = Res + (Res.empty() == true?"b=":",b=") + Architecture();
af87ab54
AL
970 return Res;
971}
972 /*}}}*/
012b102a
MV
973// VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
974// ---------------------------------------------------------------------
d3e8fbb3 975/* return a DescIter for the current locale or the default if none is
012b102a
MV
976 * found
977 */
978pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescription() const
979{
45df0ad2
DK
980 std::vector<string> const lang = APT::Configuration::getLanguages();
981 for (std::vector<string>::const_iterator l = lang.begin();
f7f0d6c7 982 l != lang.end(); ++l)
45df0ad2 983 {
4b625b95
DK
984 pkgCache::DescIterator Desc = DescriptionList();
985 for (; Desc.end() == false; ++Desc)
0e7c3313 986 if (*l == Desc.LanguageCode())
45df0ad2 987 break;
4b625b95 988 if (Desc.end() == true)
0e7c3313
DK
989 {
990 if (*l == "en")
991 {
992 Desc = DescriptionList();
993 for (; Desc.end() == false; ++Desc)
994 if (strcmp(Desc.LanguageCode(), "") == 0)
995 break;
996 if (Desc.end() == true)
997 continue;
998 }
999 else
1000 continue;
1001 }
45df0ad2
DK
1002 return Desc;
1003 }
4b625b95
DK
1004 for (pkgCache::DescIterator Desc = DescriptionList();
1005 Desc.end() == false; ++Desc)
1006 if (strcmp(Desc.LanguageCode(), "") == 0)
1007 return Desc;
45df0ad2 1008 return DescriptionList();
d3e8fbb3 1009}
012b102a
MV
1010
1011 /*}}}*/
c8a4ce6c
DK
1012
1013pkgCache::~pkgCache() {}