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