merged from debian-apt
[ntk/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
11 This is the general utility functions for cache managment. They provide
12 a complete set of accessor functions for the cache. The cacheiterators
13 header contains the STL-like iterators that can be used to easially
14 navigate the cache as well as seemlessly dereference the mmap'd
15 indexes. Use these always.
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 /*{{{*/
094a497d 23#include <apt-pkg/pkgcache.h>
af29ffb4 24#include <apt-pkg/policy.h>
094a497d
AL
25#include <apt-pkg/version.h>
26#include <apt-pkg/error.h>
231fea14 27#include <apt-pkg/strutl.h>
b2e465d6 28#include <apt-pkg/configuration.h>
45df0ad2 29#include <apt-pkg/aptconfiguration.h>
5c0d3668 30#include <apt-pkg/macros.h>
578bfd0a 31
b2e465d6
AL
32#include <apti18n.h>
33
578bfd0a
AL
34#include <string>
35#include <sys/stat.h>
36#include <unistd.h>
1ae93c94 37
851a45a8 38#include <ctype.h>
578bfd0a
AL
39 /*}}}*/
40
851a45a8
AL
41using std::string;
42
012b102a 43
578bfd0a
AL
44// Cache::Header::Header - Constructor /*{{{*/
45// ---------------------------------------------------------------------
46/* Simply initialize the header */
47pkgCache::Header::Header()
48{
49 Signature = 0x98FE76DC;
50
51 /* Whenever the structures change the major version should be bumped,
52 whenever the generator changes the minor version should be bumped. */
f8ae7e8b 53 MajorVersion = 8;
6a3da7a6 54 MinorVersion = 0;
b2e465d6 55 Dirty = false;
578bfd0a
AL
56
57 HeaderSz = sizeof(pkgCache::Header);
52c41485 58 GroupSz = sizeof(pkgCache::Group);
578bfd0a
AL
59 PackageSz = sizeof(pkgCache::Package);
60 PackageFileSz = sizeof(pkgCache::PackageFile);
61 VersionSz = sizeof(pkgCache::Version);
a52f938b 62 DescriptionSz = sizeof(pkgCache::Description);
578bfd0a
AL
63 DependencySz = sizeof(pkgCache::Dependency);
64 ProvidesSz = sizeof(pkgCache::Provides);
dcb79bae 65 VerFileSz = sizeof(pkgCache::VerFile);
a52f938b 66 DescFileSz = sizeof(pkgCache::DescFile);
dcb79bae 67
52c41485 68 GroupCount = 0;
578bfd0a
AL
69 PackageCount = 0;
70 VersionCount = 0;
a52f938b 71 DescriptionCount = 0;
578bfd0a
AL
72 DependsCount = 0;
73 PackageFileCount = 0;
a7e66b17 74 VerFileCount = 0;
a52f938b 75 DescFileCount = 0;
a7e66b17 76 ProvidesCount = 0;
ad00ae81 77 MaxVerFileSize = 0;
a52f938b 78 MaxDescFileSize = 0;
578bfd0a
AL
79
80 FileList = 0;
81 StringList = 0;
b2e465d6
AL
82 VerSysName = 0;
83 Architecture = 0;
5bf15716
DK
84 memset(PkgHashTable,0,sizeof(PkgHashTable));
85 memset(GrpHashTable,0,sizeof(GrpHashTable));
578bfd0a 86 memset(Pools,0,sizeof(Pools));
0688ccd8
JAK
87
88 CacheFileSize = 0;
578bfd0a
AL
89}
90 /*}}}*/
91// Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
92// ---------------------------------------------------------------------
93/* */
94bool pkgCache::Header::CheckSizes(Header &Against) const
95{
96 if (HeaderSz == Against.HeaderSz &&
52c41485 97 GroupSz == Against.GroupSz &&
578bfd0a
AL
98 PackageSz == Against.PackageSz &&
99 PackageFileSz == Against.PackageFileSz &&
100 VersionSz == Against.VersionSz &&
a52f938b 101 DescriptionSz == Against.DescriptionSz &&
dcb79bae
AL
102 DependencySz == Against.DependencySz &&
103 VerFileSz == Against.VerFileSz &&
a52f938b 104 DescFileSz == Against.DescFileSz &&
578bfd0a
AL
105 ProvidesSz == Against.ProvidesSz)
106 return true;
107 return false;
108}
109 /*}}}*/
110
111// Cache::pkgCache - Constructor /*{{{*/
112// ---------------------------------------------------------------------
113/* */
b2e465d6 114pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map)
578bfd0a 115{
4cbf323f
MV
116 // call getArchitectures() with cached=false to ensure that the
117 // architectures cache is re-evaulated. this is needed in cases
118 // when the APT::Architecture field changes between two cache creations
119 MultiArchEnabled = APT::Configuration::getArchitectures(false).size() > 1;
b2e465d6
AL
120 if (DoMap == true)
121 ReMap();
578bfd0a
AL
122}
123 /*}}}*/
124// Cache::ReMap - Reopen the cache file /*{{{*/
125// ---------------------------------------------------------------------
126/* If the file is already closed then this will open it open it. */
a9fe5928 127bool pkgCache::ReMap(bool const &Errorchecks)
578bfd0a
AL
128{
129 // Apply the typecasts.
130 HeaderP = (Header *)Map.Data();
5bf15716 131 GrpP = (Group *)Map.Data();
578bfd0a 132 PkgP = (Package *)Map.Data();
dcb79bae 133 VerFileP = (VerFile *)Map.Data();
a52f938b 134 DescFileP = (DescFile *)Map.Data();
578bfd0a
AL
135 PkgFileP = (PackageFile *)Map.Data();
136 VerP = (Version *)Map.Data();
a52f938b 137 DescP = (Description *)Map.Data();
578bfd0a
AL
138 ProvideP = (Provides *)Map.Data();
139 DepP = (Dependency *)Map.Data();
140 StringItemP = (StringItem *)Map.Data();
141 StrP = (char *)Map.Data();
142
a9fe5928
DK
143 if (Errorchecks == false)
144 return true;
145
b2e465d6
AL
146 if (Map.Size() == 0 || HeaderP == 0)
147 return _error->Error(_("Empty package cache"));
578bfd0a
AL
148
149 // Check the header
150 Header DefHeader;
151 if (HeaderP->Signature != DefHeader.Signature ||
152 HeaderP->Dirty == true)
b2e465d6 153 return _error->Error(_("The package cache file is corrupted"));
578bfd0a
AL
154
155 if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
156 HeaderP->MinorVersion != DefHeader.MinorVersion ||
157 HeaderP->CheckSizes(DefHeader) == false)
b2e465d6
AL
158 return _error->Error(_("The package cache file is an incompatible version"));
159
7d79339f
JAK
160 if (Map.Size() < HeaderP->CacheFileSize)
161 return _error->Error(_("The package cache file is corrupted, it is too small"));
162
b2e465d6
AL
163 // Locate our VS..
164 if (HeaderP->VerSysName == 0 ||
165 (VS = pkgVersioningSystem::GetVS(StrP + HeaderP->VerSysName)) == 0)
db0db9fe 166 return _error->Error(_("This APT does not support the versioning system '%s'"),StrP + HeaderP->VerSysName);
b2e465d6
AL
167
168 // Chcek the arhcitecture
169 if (HeaderP->Architecture == 0 ||
170 _config->Find("APT::Architecture") != StrP + HeaderP->Architecture)
bac2e715 171 return _error->Error(_("The package cache was built for a different architecture"));
578bfd0a
AL
172 return true;
173}
174 /*}}}*/
175// Cache::Hash - Hash a string /*{{{*/
176// ---------------------------------------------------------------------
177/* This is used to generate the hash entries for the HashTable. With my
178 package list from bo this function gets 94% table usage on a 512 item
179 table (480 used items) */
171c75f1 180unsigned long pkgCache::sHash(const string &Str) const
578bfd0a
AL
181{
182 unsigned long Hash = 0;
851a45a8 183 for (string::const_iterator I = Str.begin(); I != Str.end(); I++)
4e86942a 184 Hash = 5*Hash + tolower_ascii(*I);
5bf15716 185 return Hash % _count(HeaderP->PkgHashTable);
578bfd0a
AL
186}
187
f9eec0e7 188unsigned long pkgCache::sHash(const char *Str) const
578bfd0a
AL
189{
190 unsigned long Hash = 0;
f9eec0e7 191 for (const char *I = Str; *I != 0; I++)
4e86942a 192 Hash = 5*Hash + tolower_ascii(*I);
5bf15716 193 return Hash % _count(HeaderP->PkgHashTable);
578bfd0a
AL
194}
195
8d4c859d
DK
196 /*}}}*/
197// Cache::SingleArchFindPkg - Locate a package by name /*{{{*/
198// ---------------------------------------------------------------------
199/* Returns 0 on error, pointer to the package otherwise
200 The multiArch enabled methods will fallback to this one as it is (a bit)
201 faster for single arch environments and realworld is mostly singlearch… */
202pkgCache::PkgIterator pkgCache::SingleArchFindPkg(const string &Name)
203{
204 // Look at the hash bucket
205 Package *Pkg = PkgP + HeaderP->PkgHashTable[Hash(Name)];
206 for (; Pkg != PkgP; Pkg = PkgP + Pkg->NextPackage)
207 {
208 if (Pkg->Name != 0 && StrP[Pkg->Name] == Name[0] &&
209 stringcasecmp(Name,StrP + Pkg->Name) == 0)
210 return PkgIterator(*this,Pkg);
211 }
212 return PkgIterator(*this,0);
213}
578bfd0a
AL
214 /*}}}*/
215// Cache::FindPkg - Locate a package by name /*{{{*/
216// ---------------------------------------------------------------------
217/* Returns 0 on error, pointer to the package otherwise */
25396fb0
DK
218pkgCache::PkgIterator pkgCache::FindPkg(const string &Name) {
219 size_t const found = Name.find(':');
220 if (found == string::npos)
254b8ebb
DK
221 {
222 if (MultiArchCache() == false)
223 return SingleArchFindPkg(Name);
224 else
225 return FindPkg(Name, "native");
226 }
4d174dc8 227 string const Arch = Name.substr(found+1);
eddc9dd0
DK
228 /* Beware: This is specialcased to handle pkg:any in dependencies as
229 these are linked to virtual pkg:any named packages with all archs.
230 If you want any arch from a given pkg, use FindPkg(pkg,arch) */
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
DK
239pkgCache::PkgIterator pkgCache::FindPkg(const string &Name, string const &Arch) {
240 if (MultiArchCache() == false) {
61d15f91 241 if (Arch == "native" || Arch == "all" || Arch == "any" ||
959470da 242 Arch == NativeArch())
8d4c859d
DK
243 return SingleArchFindPkg(Name);
244 else
245 return PkgIterator(*this,0);
246 }
5bf15716
DK
247 /* We make a detour via the GrpIterator here as
248 on a multi-arch environment a group is easier to
249 find than a package (less entries in the buckets) */
250 pkgCache::GrpIterator Grp = FindGrp(Name);
251 if (Grp.end() == true)
252 return PkgIterator(*this,0);
253
254 return Grp.FindPkg(Arch);
255}
256 /*}}}*/
257// Cache::FindGrp - Locate a group by name /*{{{*/
258// ---------------------------------------------------------------------
259/* Returns End-Pointer on error, pointer to the group otherwise */
260pkgCache::GrpIterator pkgCache::FindGrp(const string &Name) {
261 if (unlikely(Name.empty() == true))
262 return GrpIterator(*this,0);
263
264 // Look at the hash bucket for the group
265 Group *Grp = GrpP + HeaderP->GrpHashTable[sHash(Name)];
266 for (; Grp != GrpP; Grp = GrpP + Grp->Next) {
267 if (Grp->Name != 0 && StrP[Grp->Name] == Name[0] &&
268 stringcasecmp(Name, StrP + Grp->Name) == 0)
269 return GrpIterator(*this, Grp);
270 }
271
272 return GrpIterator(*this,0);
578bfd0a
AL
273}
274 /*}}}*/
b2e465d6
AL
275// Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
276// ---------------------------------------------------------------------
277/* This returns a string representation of the dependency compare
278 type in the weird debian style.. */
279const char *pkgCache::CompTypeDeb(unsigned char Comp)
280{
281 const char *Ops[] = {"","<=",">=","<<",">>","=","!="};
282 if ((unsigned)(Comp & 0xF) < 7)
283 return Ops[Comp & 0xF];
284 return "";
285}
286 /*}}}*/
287// Cache::CompType - Return a string describing the compare type /*{{{*/
288// ---------------------------------------------------------------------
289/* This returns a string representation of the dependency compare
290 type */
291const char *pkgCache::CompType(unsigned char Comp)
292{
293 const char *Ops[] = {"","<=",">=","<",">","=","!="};
294 if ((unsigned)(Comp & 0xF) < 7)
295 return Ops[Comp & 0xF];
296 return "";
297}
298 /*}}}*/
299// Cache::DepType - Return a string describing the dep type /*{{{*/
300// ---------------------------------------------------------------------
301/* */
302const char *pkgCache::DepType(unsigned char Type)
303{
304 const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
305 _("Recommends"),_("Conflicts"),_("Replaces"),
f8ae7e8b 306 _("Obsoletes"),_("Breaks"), _("Enhances")};
308c7d30 307 if (Type < sizeof(Types)/sizeof(*Types))
b2e465d6
AL
308 return Types[Type];
309 return "";
310}
311 /*}}}*/
0149949b
AL
312// Cache::Priority - Convert a priority value to a string /*{{{*/
313// ---------------------------------------------------------------------
314/* */
315const char *pkgCache::Priority(unsigned char Prio)
316{
b2e465d6
AL
317 const char *Mapping[] = {0,_("important"),_("required"),_("standard"),
318 _("optional"),_("extra")};
0149949b
AL
319 if (Prio < _count(Mapping))
320 return Mapping[Prio];
321 return 0;
322}
323 /*}}}*/
5bf15716
DK
324// GrpIterator::FindPkg - Locate a package by arch /*{{{*/
325// ---------------------------------------------------------------------
326/* Returns an End-Pointer on error, pointer to the package otherwise */
e841200b 327pkgCache::PkgIterator pkgCache::GrpIterator::FindPkg(string Arch) const {
5bf15716
DK
328 if (unlikely(IsGood() == false || S->FirstPackage == 0))
329 return PkgIterator(*Owner, 0);
330
60dcec6d
DK
331 /* If we accept any package we simply return the "first"
332 package in this group (the last one added). */
333 if (Arch == "any")
334 return PkgIterator(*Owner, Owner->PkgP + S->FirstPackage);
335
959470da 336 char const* const myArch = Owner->NativeArch();
5bf15716
DK
337 /* Most of the time the package for our native architecture is
338 the one we add at first to the cache, but this would be the
339 last one we check, so we do it now. */
566046f4 340 if (Arch == "native" || Arch == myArch || Arch == "all") {
5bf15716 341 pkgCache::Package *Pkg = Owner->PkgP + S->LastPackage;
959470da 342 if (strcasecmp(myArch, Owner->StrP + Pkg->Arch) == 0)
5bf15716 343 return PkgIterator(*Owner, Pkg);
959470da 344 Arch = myArch;
5bf15716
DK
345 }
346
5bf15716
DK
347 /* Iterate over the list to find the matching arch
348 unfortunately this list includes "package noise"
349 (= different packages with same calculated hash),
350 so we need to check the name also */
351 for (pkgCache::Package *Pkg = PackageList(); Pkg != Owner->PkgP;
352 Pkg = Owner->PkgP + Pkg->NextPackage) {
353 if (S->Name == Pkg->Name &&
354 stringcasecmp(Arch, Owner->StrP + Pkg->Arch) == 0)
355 return PkgIterator(*Owner, Pkg);
356 if ((Owner->PkgP + S->LastPackage) == Pkg)
357 break;
358 }
359
bd2fb30a
DK
360 return PkgIterator(*Owner, 0);
361}
362 /*}}}*/
363// GrpIterator::FindPreferredPkg - Locate the "best" package /*{{{*/
364// ---------------------------------------------------------------------
365/* Returns an End-Pointer on error, pointer to the package otherwise */
3db58cf4 366pkgCache::PkgIterator pkgCache::GrpIterator::FindPreferredPkg(bool const &PreferNonVirtual) const {
bd2fb30a 367 pkgCache::PkgIterator Pkg = FindPkg("native");
3db58cf4 368 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
bd2fb30a
DK
369 return Pkg;
370
371 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
372 for (std::vector<std::string>::const_iterator a = archs.begin();
373 a != archs.end(); ++a) {
374 Pkg = FindPkg(*a);
3db58cf4 375 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
bd2fb30a
DK
376 return Pkg;
377 }
378
3db58cf4
DK
379 if (PreferNonVirtual == true)
380 return FindPreferredPkg(false);
5bf15716
DK
381 return PkgIterator(*Owner, 0);
382}
383 /*}}}*/
384// GrpIterator::NextPkg - Locate the next package in the group /*{{{*/
385// ---------------------------------------------------------------------
386/* Returns an End-Pointer on error, pointer to the package otherwise.
c408e01e
DK
387 We can't simply ++ to the next as the next package of the last will
388 be from a different group (with the same hash value) */
e841200b 389pkgCache::PkgIterator pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator const &LastPkg) const {
5bf15716
DK
390 if (unlikely(IsGood() == false || S->FirstPackage == 0 ||
391 LastPkg.end() == true))
392 return PkgIterator(*Owner, 0);
393
c408e01e
DK
394 if (S->LastPackage == LastPkg.Index())
395 return PkgIterator(*Owner, 0);
5bf15716 396
c408e01e 397 return PkgIterator(*Owner, Owner->PkgP + LastPkg->NextPackage);
5bf15716
DK
398}
399 /*}}}*/
25396fb0
DK
400// GrpIterator::operator ++ - Postfix incr /*{{{*/
401// ---------------------------------------------------------------------
402/* This will advance to the next logical group in the hash table. */
403void pkgCache::GrpIterator::operator ++(int)
404{
405 // Follow the current links
406 if (S != Owner->GrpP)
407 S = Owner->GrpP + S->Next;
408
409 // Follow the hash table
410 while (S == Owner->GrpP && (HashIndex+1) < (signed)_count(Owner->HeaderP->GrpHashTable))
411 {
412 HashIndex++;
413 S = Owner->GrpP + Owner->HeaderP->GrpHashTable[HashIndex];
414 }
415};
f55a958f
AL
416 /*}}}*/
417// PkgIterator::operator ++ - Postfix incr /*{{{*/
578bfd0a
AL
418// ---------------------------------------------------------------------
419/* This will advance to the next logical package in the hash table. */
420void pkgCache::PkgIterator::operator ++(int)
421{
422 // Follow the current links
773e2c1f
DK
423 if (S != Owner->PkgP)
424 S = Owner->PkgP + S->NextPackage;
b2e465d6 425
578bfd0a 426 // Follow the hash table
5bf15716 427 while (S == Owner->PkgP && (HashIndex+1) < (signed)_count(Owner->HeaderP->PkgHashTable))
578bfd0a
AL
428 {
429 HashIndex++;
5bf15716 430 S = Owner->PkgP + Owner->HeaderP->PkgHashTable[HashIndex];
578bfd0a
AL
431 }
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 *
464pkgCache::PkgIterator::CandVersion() const
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;
471};
472 /*}}}*/
473// PkgIterator::CurVersion - Returns the current version string /*{{{*/
474// ---------------------------------------------------------------------
475/* Return string representing of the current version. */
476const char *
477pkgCache::PkgIterator::CurVersion() const
478{
479 VerIterator version = CurrentVer();
480 if (version.IsGood())
481 return CurrentVer().VerStr();
482 return 0;
483};
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)
488 Note that the characters <|>() are all literal above. Versions will be ommited
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&
492operator<<(ostream& out, pkgCache::PkgIterator Pkg)
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;
506 out << " > ( " << string(Pkg.Section()==0?"none":Pkg.Section()) << " )";
507 return out;
508}
509 /*}}}*/
75ce2062
DK
510// PkgIterator::FullName - Returns Name and (maybe) Architecture /*{{{*/
511// ---------------------------------------------------------------------
512/* Returns a name:arch string */
513std::string pkgCache::PkgIterator::FullName(bool const &Pretty) const
514{
515 string fullname = Name();
516 if (Pretty == false ||
959470da
DK
517 (strcmp(Arch(), "all") != 0 &&
518 strcmp(Owner->NativeArch(), Arch()) != 0))
75ce2062
DK
519 return fullname.append(":").append(Arch());
520 return fullname;
521}
522 /*}}}*/
578bfd0a
AL
523// DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
524// ---------------------------------------------------------------------
525/* Currently critical deps are defined as depends, predepends and
308c7d30 526 conflicts (including dpkg's Breaks fields). */
e841200b 527bool pkgCache::DepIterator::IsCritical() const
578bfd0a 528{
359e46db 529 if (IsNegative() == true ||
773e2c1f
DK
530 S->Type == pkgCache::Dep::Depends ||
531 S->Type == pkgCache::Dep::PreDepends)
578bfd0a
AL
532 return true;
533 return false;
534}
535 /*}}}*/
df77d8a5
DK
536// DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
537// ---------------------------------------------------------------------
538/* Some dependencies are positive like Depends and Recommends, others
539 are negative like Conflicts which can and should be handled differently */
540bool pkgCache::DepIterator::IsNegative() const
541{
542 return S->Type == Dep::DpkgBreaks ||
543 S->Type == Dep::Conflicts ||
544 S->Type == Dep::Obsoletes;
545}
546 /*}}}*/
578bfd0a
AL
547// DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
548// ---------------------------------------------------------------------
549/* This intellegently looks at dep target packages and tries to figure
550 out which package should be used. This is needed to nicely handle
551 provide mapping. If the target package has no other providing packages
552 then it returned. Otherwise the providing list is looked at to
553 see if there is one one unique providing package if so it is returned.
554 Otherwise true is returned and the target package is set. The return
b2e465d6
AL
555 result indicates whether the node should be expandable
556
557 In Conjunction with the DepCache the value of Result may not be
558 super-good since the policy may have made it uninstallable. Using
559 AllTargets is better in this case. */
e841200b 560bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) const
578bfd0a
AL
561{
562 Result = TargetPkg();
563
564 // No provides at all
565 if (Result->ProvidesList == 0)
566 return false;
567
568 // There is the Base package and the providing ones which is at least 2
569 if (Result->VersionList != 0)
570 return true;
571
572 /* We have to skip over indirect provisions of the package that
573 owns the dependency. For instance, if libc5-dev depends on the
574 virtual package libc-dev which is provided by libc5-dev and libc6-dev
575 we must ignore libc5-dev when considering the provides list. */
576 PrvIterator PStart = Result.ProvidesList();
577 for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++);
578
579 // Nothing but indirect self provides
580 if (PStart.end() == true)
581 return false;
582
583 // Check for single packages in the provides list
584 PrvIterator P = PStart;
585 for (; P.end() != true; P++)
586 {
587 // Skip over self provides
588 if (P.OwnerPkg() == ParentPkg())
589 continue;
590 if (PStart.OwnerPkg() != P.OwnerPkg())
591 break;
592 }
b2e465d6
AL
593
594 Result = PStart.OwnerPkg();
578bfd0a
AL
595
596 // Check for non dups
597 if (P.end() != true)
598 return true;
b2e465d6 599
578bfd0a
AL
600 return false;
601}
602 /*}}}*/
603// DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
604// ---------------------------------------------------------------------
b2e465d6 605/* This is a more useful version of TargetPkg() that follows versioned
578bfd0a 606 provides. It includes every possible package-version that could satisfy
fbfb2a7c
AL
607 the dependency. The last item in the list has a 0. The resulting pointer
608 must be delete [] 'd */
e841200b 609pkgCache::Version **pkgCache::DepIterator::AllTargets() const
578bfd0a
AL
610{
611 Version **Res = 0;
612 unsigned long Size =0;
613 while (1)
614 {
615 Version **End = Res;
616 PkgIterator DPkg = TargetPkg();
617
618 // Walk along the actual package providing versions
619 for (VerIterator I = DPkg.VersionList(); I.end() == false; I++)
620 {
773e2c1f 621 if (Owner->VS->CheckDep(I.VerStr(),S->CompareOp,TargetVer()) == false)
578bfd0a
AL
622 continue;
623
359e46db 624 if (IsNegative() == true &&
f07b5628 625 ParentPkg() == I.ParentPkg())
578bfd0a
AL
626 continue;
627
628 Size++;
629 if (Res != 0)
630 *End++ = I;
631 }
632
633 // Follow all provides
634 for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++)
635 {
773e2c1f 636 if (Owner->VS->CheckDep(I.ProvideVersion(),S->CompareOp,TargetVer()) == false)
578bfd0a
AL
637 continue;
638
359e46db 639 if (IsNegative() == true &&
f07b5628 640 ParentPkg() == I.OwnerPkg())
578bfd0a
AL
641 continue;
642
643 Size++;
644 if (Res != 0)
645 *End++ = I.OwnerVer();
646 }
647
648 // Do it again and write it into the array
649 if (Res == 0)
650 {
651 Res = new Version *[Size+1];
652 Size = 0;
653 }
654 else
655 {
656 *End = 0;
657 break;
658 }
659 }
660
661 return Res;
662}
663 /*}}}*/
43d017d6
AL
664// DepIterator::GlobOr - Compute an OR group /*{{{*/
665// ---------------------------------------------------------------------
666/* This Takes an iterator, iterates past the current dependency grouping
667 and returns Start and End so that so End is the final element
668 in the group, if End == Start then D is End++ and End is the
669 dependency D was pointing to. Use in loops to iterate sensibly. */
670void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
671{
672 // Compute a single dependency element (glob or)
673 Start = *this;
674 End = *this;
018f1533 675 for (bool LastOR = true; end() == false && LastOR == true;)
43d017d6 676 {
773e2c1f 677 LastOR = (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
018f1533 678 (*this)++;
43d017d6
AL
679 if (LastOR == true)
680 End = (*this);
681 }
682}
683 /*}}}*/
47f6d1b7
DK
684// ostream operator to handle string representation of a dependecy /*{{{*/
685// ---------------------------------------------------------------------
686/* */
687std::ostream& operator<<(ostream& out, pkgCache::DepIterator D)
688{
689 if (D.end() == true)
690 return out << "invalid dependency";
691
692 pkgCache::PkgIterator P = D.ParentPkg();
693 pkgCache::PkgIterator T = D.TargetPkg();
694
695 out << (P.end() ? "invalid pkg" : P.FullName(false)) << " " << D.DepType()
696 << " on ";
697 if (T.end() == true)
698 out << "invalid pkg";
699 else
700 out << T;
701
702 if (D->Version != 0)
703 out << " (" << D.CompType() << " " << D.TargetVer() << ")";
704
705 return out;
706}
707 /*}}}*/
578bfd0a
AL
708// VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
709// ---------------------------------------------------------------------
710/* This just looks over the version list to see if B is listed before A. In
711 most cases this will return in under 4 checks, ver lists are short. */
712int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
713{
714 // Check if they are equal
715 if (*this == B)
716 return 0;
717 if (end() == true)
718 return -1;
719 if (B.end() == true)
720 return 1;
721
722 /* Start at A and look for B. If B is found then A > B otherwise
723 B was before A so A < B */
724 VerIterator I = *this;
725 for (;I.end() == false; I++)
726 if (I == B)
727 return 1;
728 return -1;
729}
730 /*}}}*/
b518cca6
AL
731// VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
732// ---------------------------------------------------------------------
733/* */
734bool pkgCache::VerIterator::Downloadable() const
735{
736 VerFileIterator Files = FileList();
737 for (; Files.end() == false; Files++)
f07b5628 738 if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource)
b518cca6
AL
739 return true;
740 return false;
741}
742 /*}}}*/
3c124dde
AL
743// VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
744// ---------------------------------------------------------------------
745/* This checks to see if any of the versions files are not NotAutomatic.
746 True if this version is selectable for automatic installation. */
747bool pkgCache::VerIterator::Automatic() const
748{
749 VerFileIterator Files = FileList();
750 for (; Files.end() == false; Files++)
5ed56f93 751 // Do not check ButAutomaticUpgrades here as it is kind of automatic…
3c124dde
AL
752 if ((Files.File()->Flags & pkgCache::Flag::NotAutomatic) != pkgCache::Flag::NotAutomatic)
753 return true;
754 return false;
755}
756 /*}}}*/
757// VerIterator::NewestFile - Return the newest file version relation /*{{{*/
758// ---------------------------------------------------------------------
759/* This looks at the version numbers associated with all of the sources
760 this version is in and returns the highest.*/
761pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const
762{
763 VerFileIterator Files = FileList();
764 VerFileIterator Highest = Files;
765 for (; Files.end() == false; Files++)
766 {
b2e465d6 767 if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0)
3c124dde
AL
768 Highest = Files;
769 }
770
771 return Highest;
772}
773 /*}}}*/
b2e465d6
AL
774// VerIterator::RelStr - Release description string /*{{{*/
775// ---------------------------------------------------------------------
776/* This describes the version from a release-centric manner. The output is a
777 list of Label:Version/Archive */
d4489d49 778string pkgCache::VerIterator::RelStr() const
b2e465d6
AL
779{
780 bool First = true;
781 string Res;
782 for (pkgCache::VerFileIterator I = this->FileList(); I.end() == false; I++)
783 {
784 // Do not print 'not source' entries'
785 pkgCache::PkgFileIterator File = I.File();
786 if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
787 continue;
788
789 // See if we have already printed this out..
790 bool Seen = false;
791 for (pkgCache::VerFileIterator J = this->FileList(); I != J; J++)
792 {
793 pkgCache::PkgFileIterator File2 = J.File();
794 if (File2->Label == 0 || File->Label == 0)
795 continue;
796
797 if (strcmp(File.Label(),File2.Label()) != 0)
798 continue;
799
800 if (File2->Version == File->Version)
801 {
802 Seen = true;
803 break;
804 }
10639577 805 if (File2->Version == 0 || File->Version == 0)
b2e465d6
AL
806 break;
807 if (strcmp(File.Version(),File2.Version()) == 0)
808 Seen = true;
809 }
810
811 if (Seen == true)
812 continue;
813
814 if (First == false)
815 Res += ", ";
816 else
817 First = false;
818
819 if (File->Label != 0)
820 Res = Res + File.Label() + ':';
821
822 if (File->Archive != 0)
823 {
824 if (File->Version == 0)
825 Res += File.Archive();
826 else
827 Res = Res + File.Version() + '/' + File.Archive();
828 }
829 else
830 {
831 // No release file, print the host name that this came from
832 if (File->Site == 0 || File.Site()[0] == 0)
833 Res += "localhost";
834 else
835 Res += File.Site();
836 }
5dd4c8b8 837 }
857e9c13 838 if (S->ParentPkg != 0)
5dd4c8b8 839 Res.append(" [").append(Arch()).append("]");
b2e465d6
AL
840 return Res;
841}
842 /*}}}*/
578bfd0a
AL
843// PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
844// ---------------------------------------------------------------------
845/* This stats the file and compares its stats with the ones that were
846 stored during generation. Date checks should probably also be
847 included here. */
848bool pkgCache::PkgFileIterator::IsOk()
849{
850 struct stat Buf;
851 if (stat(FileName(),&Buf) != 0)
852 return false;
853
773e2c1f 854 if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime)
578bfd0a
AL
855 return false;
856
857 return true;
858}
859 /*}}}*/
af87ab54
AL
860// PkgFileIterator::RelStr - Return the release string /*{{{*/
861// ---------------------------------------------------------------------
862/* */
863string pkgCache::PkgFileIterator::RelStr()
864{
865 string Res;
866 if (Version() != 0)
867 Res = Res + (Res.empty() == true?"v=":",v=") + Version();
868 if (Origin() != 0)
869 Res = Res + (Res.empty() == true?"o=":",o=") + Origin();
870 if (Archive() != 0)
871 Res = Res + (Res.empty() == true?"a=":",a=") + Archive();
efc487fb
DK
872 if (Codename() != 0)
873 Res = Res + (Res.empty() == true?"n=":",n=") + Codename();
af87ab54
AL
874 if (Label() != 0)
875 Res = Res + (Res.empty() == true?"l=":",l=") + Label();
876 if (Component() != 0)
877 Res = Res + (Res.empty() == true?"c=":",c=") + Component();
5dd4c8b8
DK
878 if (Architecture() != 0)
879 Res = Res + (Res.empty() == true?"b=":",b=") + Architecture();
af87ab54
AL
880 return Res;
881}
882 /*}}}*/
012b102a
MV
883// VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
884// ---------------------------------------------------------------------
885/* return a DescIter for the current locale or the default if none is
886 * found
887 */
888pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescription() const
889{
45df0ad2
DK
890 std::vector<string> const lang = APT::Configuration::getLanguages();
891 for (std::vector<string>::const_iterator l = lang.begin();
892 l != lang.end(); l++)
893 {
4b625b95
DK
894 pkgCache::DescIterator Desc = DescriptionList();
895 for (; Desc.end() == false; ++Desc)
f824d95c
DK
896 if (*l == Desc.LanguageCode() ||
897 (*l == "en" && strcmp(Desc.LanguageCode(),"") == 0))
45df0ad2 898 break;
4b625b95
DK
899 if (Desc.end() == true)
900 continue;
45df0ad2
DK
901 return Desc;
902 }
4b625b95
DK
903 for (pkgCache::DescIterator Desc = DescriptionList();
904 Desc.end() == false; ++Desc)
905 if (strcmp(Desc.LanguageCode(), "") == 0)
906 return Desc;
45df0ad2 907 return DescriptionList();
012b102a
MV
908};
909
910 /*}}}*/