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