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