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