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