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