]> git.saurik.com Git - apt.git/blob - apt-pkg/pkgcache.cc
just-in-time creation for (explicit) negative deps
[apt.git] / apt-pkg / pkgcache.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: pkgcache.cc,v 1.37 2003/02/10 01:40:58 doogie Exp $
4 /* ######################################################################
5
6 Package Cache - Accessor code for the cache
7
8 Please see doc/apt-pkg/cache.sgml for a more detailed description of
9 this format. Also be sure to keep that file up-to-date!!
10
11 This is the general utility functions for cache management. 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 /*{{{*/
23 #include<config.h>
24
25 #include <apt-pkg/pkgcache.h>
26 #include <apt-pkg/policy.h>
27 #include <apt-pkg/version.h>
28 #include <apt-pkg/error.h>
29 #include <apt-pkg/strutl.h>
30 #include <apt-pkg/configuration.h>
31 #include <apt-pkg/aptconfiguration.h>
32 #include <apt-pkg/mmap.h>
33 #include <apt-pkg/macros.h>
34
35 #include <stddef.h>
36 #include <string.h>
37 #include <ostream>
38 #include <vector>
39 #include <string>
40 #include <sys/stat.h>
41
42 #include <apti18n.h>
43 /*}}}*/
44
45 using std::string;
46
47
48 // Cache::Header::Header - Constructor /*{{{*/
49 // ---------------------------------------------------------------------
50 /* Simply initialize the header */
51 pkgCache::Header::Header()
52 {
53 #define APT_HEADER_SET(X,Y) X = Y; static_assert(std::numeric_limits<decltype(X)>::max() > Y, "Size violation detected in pkgCache::Header")
54 APT_HEADER_SET(Signature, 0x98FE76DC);
55
56 /* Whenever the structures change the major version should be bumped,
57 whenever the generator changes the minor version should be bumped. */
58 APT_HEADER_SET(MajorVersion, 10);
59 APT_HEADER_SET(MinorVersion, 0);
60 APT_HEADER_SET(Dirty, false);
61
62 APT_HEADER_SET(HeaderSz, sizeof(pkgCache::Header));
63 APT_HEADER_SET(GroupSz, sizeof(pkgCache::Group));
64 APT_HEADER_SET(PackageSz, sizeof(pkgCache::Package));
65 APT_HEADER_SET(ReleaseFileSz, sizeof(pkgCache::ReleaseFile));
66 APT_HEADER_SET(PackageFileSz, sizeof(pkgCache::PackageFile));
67 APT_HEADER_SET(VersionSz, sizeof(pkgCache::Version));
68 APT_HEADER_SET(DescriptionSz, sizeof(pkgCache::Description));
69 APT_HEADER_SET(DependencySz, sizeof(pkgCache::Dependency));
70 APT_HEADER_SET(DependencyDataSz, sizeof(pkgCache::DependencyData));
71 APT_HEADER_SET(ProvidesSz, sizeof(pkgCache::Provides));
72 APT_HEADER_SET(VerFileSz, sizeof(pkgCache::VerFile));
73 APT_HEADER_SET(DescFileSz, sizeof(pkgCache::DescFile));
74 #undef APT_HEADER_SET
75
76 GroupCount = 0;
77 PackageCount = 0;
78 VersionCount = 0;
79 DescriptionCount = 0;
80 DependsCount = 0;
81 DependsDataCount = 0;
82 ReleaseFileCount = 0;
83 PackageFileCount = 0;
84 VerFileCount = 0;
85 DescFileCount = 0;
86 ProvidesCount = 0;
87 MaxVerFileSize = 0;
88 MaxDescFileSize = 0;
89
90 FileList = 0;
91 RlsFileList = 0;
92 VerSysName = 0;
93 Architecture = 0;
94 SetArchitectures(0);
95 SetHashTableSize(_config->FindI("APT::Cache-HashTableSize", 10 * 1048));
96 memset(Pools,0,sizeof(Pools));
97
98 CacheFileSize = 0;
99 }
100 /*}}}*/
101 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
102 // ---------------------------------------------------------------------
103 /* */
104 bool pkgCache::Header::CheckSizes(Header &Against) const
105 {
106 if (HeaderSz == Against.HeaderSz &&
107 GroupSz == Against.GroupSz &&
108 PackageSz == Against.PackageSz &&
109 ReleaseFileSz == Against.ReleaseFileSz &&
110 PackageFileSz == Against.PackageFileSz &&
111 VersionSz == Against.VersionSz &&
112 DescriptionSz == Against.DescriptionSz &&
113 DependencySz == Against.DependencySz &&
114 DependencyDataSz == Against.DependencyDataSz &&
115 VerFileSz == Against.VerFileSz &&
116 DescFileSz == Against.DescFileSz &&
117 ProvidesSz == Against.ProvidesSz)
118 return true;
119 return false;
120 }
121 /*}}}*/
122
123 // Cache::pkgCache - Constructor /*{{{*/
124 // ---------------------------------------------------------------------
125 /* */
126 APT_IGNORE_DEPRECATED_PUSH
127 pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map), d(NULL)
128 {
129 // call getArchitectures() with cached=false to ensure that the
130 // architectures cache is re-evaulated. this is needed in cases
131 // when the APT::Architecture field changes between two cache creations
132 MultiArchEnabled = APT::Configuration::getArchitectures(false).size() > 1;
133 if (DoMap == true)
134 ReMap();
135 }
136 APT_IGNORE_DEPRECATED_POP
137 /*}}}*/
138 // Cache::ReMap - Reopen the cache file /*{{{*/
139 // ---------------------------------------------------------------------
140 /* If the file is already closed then this will open it open it. */
141 bool pkgCache::ReMap(bool const &Errorchecks)
142 {
143 // Apply the typecasts.
144 HeaderP = (Header *)Map.Data();
145 GrpP = (Group *)Map.Data();
146 PkgP = (Package *)Map.Data();
147 VerFileP = (VerFile *)Map.Data();
148 DescFileP = (DescFile *)Map.Data();
149 RlsFileP = (ReleaseFile *)Map.Data();
150 PkgFileP = (PackageFile *)Map.Data();
151 VerP = (Version *)Map.Data();
152 DescP = (Description *)Map.Data();
153 ProvideP = (Provides *)Map.Data();
154 DepP = (Dependency *)Map.Data();
155 DepDataP = (DependencyData *)Map.Data();
156 StrP = (char *)Map.Data();
157
158 if (Errorchecks == false)
159 return true;
160
161 if (Map.Size() == 0 || HeaderP == 0)
162 return _error->Error(_("Empty package cache"));
163
164 // Check the header
165 Header DefHeader;
166 if (HeaderP->Signature != DefHeader.Signature ||
167 HeaderP->Dirty == true)
168 return _error->Error(_("The package cache file is corrupted"));
169
170 if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
171 HeaderP->MinorVersion != DefHeader.MinorVersion ||
172 HeaderP->CheckSizes(DefHeader) == false)
173 return _error->Error(_("The package cache file is an incompatible version"));
174
175 if (Map.Size() < HeaderP->CacheFileSize)
176 return _error->Error(_("The package cache file is corrupted, it is too small"));
177
178 if (HeaderP->VerSysName == 0 || HeaderP->Architecture == 0 || HeaderP->GetArchitectures() == 0)
179 return _error->Error(_("The package cache file is corrupted"));
180
181 // Locate our VS..
182 if ((VS = pkgVersioningSystem::GetVS(StrP + HeaderP->VerSysName)) == 0)
183 return _error->Error(_("This APT does not support the versioning system '%s'"),StrP + HeaderP->VerSysName);
184
185 // Check the architecture
186 std::vector<std::string> archs = APT::Configuration::getArchitectures();
187 std::vector<std::string>::const_iterator a = archs.begin();
188 std::string list = *a;
189 for (++a; a != archs.end(); ++a)
190 list.append(",").append(*a);
191 if (_config->Find("APT::Architecture") != StrP + HeaderP->Architecture ||
192 list != StrP + HeaderP->GetArchitectures())
193 return _error->Error(_("The package cache was built for different architectures: %s vs %s"), StrP + HeaderP->GetArchitectures(), list.c_str());
194
195 return true;
196 }
197 /*}}}*/
198 // Cache::Hash - Hash a string /*{{{*/
199 // ---------------------------------------------------------------------
200 /* This is used to generate the hash entries for the HashTable. With my
201 package list from bo this function gets 94% table usage on a 512 item
202 table (480 used items) */
203 map_id_t pkgCache::sHash(const string &Str) const
204 {
205 unsigned long Hash = 0;
206 for (string::const_iterator I = Str.begin(); I != Str.end(); ++I)
207 Hash = 41 * Hash + tolower_ascii(*I);
208 return Hash % HeaderP->GetHashTableSize();
209 }
210
211 map_id_t pkgCache::sHash(const char *Str) const
212 {
213 unsigned long Hash = tolower_ascii(*Str);
214 for (const char *I = Str + 1; *I != 0; ++I)
215 Hash = 41 * Hash + tolower_ascii(*I);
216 return Hash % HeaderP->GetHashTableSize();
217 }
218 /*}}}*/
219 // Cache::SingleArchFindPkg - Locate a package by name /*{{{*/
220 // ---------------------------------------------------------------------
221 /* Returns 0 on error, pointer to the package otherwise
222 The multiArch enabled methods will fallback to this one as it is (a bit)
223 faster for single arch environments and realworld is mostly singlearch… */
224 pkgCache::PkgIterator pkgCache::SingleArchFindPkg(const string &Name)
225 {
226 // Look at the hash bucket
227 Package *Pkg = PkgP + HeaderP->PkgHashTableP()[Hash(Name)];
228 for (; Pkg != PkgP; Pkg = PkgP + Pkg->NextPackage)
229 {
230 int const cmp = strcmp(Name.c_str(), StrP + (GrpP + Pkg->Group)->Name);
231 if (cmp == 0)
232 return PkgIterator(*this, Pkg);
233 else if (cmp < 0)
234 break;
235 }
236 return PkgIterator(*this,0);
237 }
238 /*}}}*/
239 // Cache::FindPkg - Locate a package by name /*{{{*/
240 // ---------------------------------------------------------------------
241 /* Returns 0 on error, pointer to the package otherwise */
242 pkgCache::PkgIterator pkgCache::FindPkg(const string &Name) {
243 size_t const found = Name.find(':');
244 if (found == string::npos)
245 return FindPkg(Name, "native");
246 string const Arch = Name.substr(found+1);
247 /* Beware: This is specialcased to handle pkg:any in dependencies as
248 these are linked to virtual pkg:any named packages with all archs.
249 If you want any arch from a given pkg, use FindPkg(pkg,arch) */
250 if (Arch == "any")
251 return FindPkg(Name, "any");
252 return FindPkg(Name.substr(0, found), Arch);
253 }
254 /*}}}*/
255 // Cache::FindPkg - Locate a package by name /*{{{*/
256 // ---------------------------------------------------------------------
257 /* Returns 0 on error, pointer to the package otherwise */
258 pkgCache::PkgIterator pkgCache::FindPkg(const string &Name, string const &Arch) {
259 /* We make a detour via the GrpIterator here as
260 on a multi-arch environment a group is easier to
261 find than a package (less entries in the buckets) */
262 pkgCache::GrpIterator Grp = FindGrp(Name);
263 if (Grp.end() == true)
264 return PkgIterator(*this,0);
265
266 return Grp.FindPkg(Arch);
267 }
268 /*}}}*/
269 // Cache::FindGrp - Locate a group by name /*{{{*/
270 // ---------------------------------------------------------------------
271 /* Returns End-Pointer on error, pointer to the group otherwise */
272 pkgCache::GrpIterator pkgCache::FindGrp(const string &Name) {
273 if (unlikely(Name.empty() == true))
274 return GrpIterator(*this,0);
275
276 // Look at the hash bucket for the group
277 Group *Grp = GrpP + HeaderP->GrpHashTableP()[sHash(Name)];
278 for (; Grp != GrpP; Grp = GrpP + Grp->Next) {
279 int const cmp = strcmp(Name.c_str(), StrP + Grp->Name);
280 if (cmp == 0)
281 return GrpIterator(*this, Grp);
282 else if (cmp < 0)
283 break;
284 }
285
286 return GrpIterator(*this,0);
287 }
288 /*}}}*/
289 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
290 // ---------------------------------------------------------------------
291 /* This returns a string representation of the dependency compare
292 type in the weird debian style.. */
293 const char *pkgCache::CompTypeDeb(unsigned char Comp)
294 {
295 const char * const Ops[] = {"","<=",">=","<<",">>","=","!="};
296 if (unlikely((unsigned)(Comp & 0xF) >= sizeof(Ops)/sizeof(Ops[0])))
297 return "";
298 return Ops[Comp & 0xF];
299 }
300 /*}}}*/
301 // Cache::CompType - Return a string describing the compare type /*{{{*/
302 // ---------------------------------------------------------------------
303 /* This returns a string representation of the dependency compare
304 type */
305 const char *pkgCache::CompType(unsigned char Comp)
306 {
307 const char * const Ops[] = {"","<=",">=","<",">","=","!="};
308 if (unlikely((unsigned)(Comp & 0xF) >= sizeof(Ops)/sizeof(Ops[0])))
309 return "";
310 return Ops[Comp & 0xF];
311 }
312 /*}}}*/
313 // Cache::DepType - Return a string describing the dep type /*{{{*/
314 // ---------------------------------------------------------------------
315 /* */
316 const char *pkgCache::DepType(unsigned char Type)
317 {
318 const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
319 _("Recommends"),_("Conflicts"),_("Replaces"),
320 _("Obsoletes"),_("Breaks"), _("Enhances")};
321 if (Type < sizeof(Types)/sizeof(*Types))
322 return Types[Type];
323 return "";
324 }
325 /*}}}*/
326 // Cache::Priority - Convert a priority value to a string /*{{{*/
327 // ---------------------------------------------------------------------
328 /* */
329 const char *pkgCache::Priority(unsigned char Prio)
330 {
331 const char *Mapping[] = {0,_("important"),_("required"),_("standard"),
332 _("optional"),_("extra")};
333 if (Prio < _count(Mapping))
334 return Mapping[Prio];
335 return 0;
336 }
337 /*}}}*/
338 // GrpIterator::FindPkg - Locate a package by arch /*{{{*/
339 // ---------------------------------------------------------------------
340 /* Returns an End-Pointer on error, pointer to the package otherwise */
341 pkgCache::PkgIterator pkgCache::GrpIterator::FindPkg(string Arch) const {
342 if (unlikely(IsGood() == false || S->FirstPackage == 0))
343 return PkgIterator(*Owner, 0);
344
345 /* If we accept any package we simply return the "first"
346 package in this group (the last one added). */
347 if (Arch == "any")
348 return PkgIterator(*Owner, Owner->PkgP + S->FirstPackage);
349
350 char const* const myArch = Owner->NativeArch();
351 /* Most of the time the package for our native architecture is
352 the one we add at first to the cache, but this would be the
353 last one we check, so we do it now. */
354 if (Arch == "native" || Arch == myArch || Arch == "all") {
355 pkgCache::Package *Pkg = Owner->PkgP + S->LastPackage;
356 if (strcmp(myArch, Owner->StrP + Pkg->Arch) == 0)
357 return PkgIterator(*Owner, Pkg);
358 Arch = myArch;
359 }
360
361 // Iterate over the list to find the matching arch
362 for (pkgCache::Package *Pkg = PackageList(); Pkg != Owner->PkgP;
363 Pkg = Owner->PkgP + Pkg->NextPackage) {
364 if (stringcmp(Arch, Owner->StrP + Pkg->Arch) == 0)
365 return PkgIterator(*Owner, Pkg);
366 if ((Owner->PkgP + S->LastPackage) == Pkg)
367 break;
368 }
369
370 return PkgIterator(*Owner, 0);
371 }
372 /*}}}*/
373 // GrpIterator::FindPreferredPkg - Locate the "best" package /*{{{*/
374 // ---------------------------------------------------------------------
375 /* Returns an End-Pointer on error, pointer to the package otherwise */
376 pkgCache::PkgIterator pkgCache::GrpIterator::FindPreferredPkg(bool const &PreferNonVirtual) const {
377 pkgCache::PkgIterator Pkg = FindPkg("native");
378 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
379 return Pkg;
380
381 std::vector<std::string> const archs = APT::Configuration::getArchitectures();
382 for (std::vector<std::string>::const_iterator a = archs.begin();
383 a != archs.end(); ++a) {
384 Pkg = FindPkg(*a);
385 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
386 return Pkg;
387 }
388 // packages without an architecture
389 Pkg = FindPkg("none");
390 if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
391 return Pkg;
392
393 if (PreferNonVirtual == true)
394 return FindPreferredPkg(false);
395 return PkgIterator(*Owner, 0);
396 }
397 /*}}}*/
398 // GrpIterator::NextPkg - Locate the next package in the group /*{{{*/
399 // ---------------------------------------------------------------------
400 /* Returns an End-Pointer on error, pointer to the package otherwise.
401 We can't simply ++ to the next as the next package of the last will
402 be from a different group (with the same hash value) */
403 pkgCache::PkgIterator pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator const &LastPkg) const {
404 if (unlikely(IsGood() == false || S->FirstPackage == 0 ||
405 LastPkg.end() == true))
406 return PkgIterator(*Owner, 0);
407
408 if (S->LastPackage == LastPkg.Index())
409 return PkgIterator(*Owner, 0);
410
411 return PkgIterator(*Owner, Owner->PkgP + LastPkg->NextPackage);
412 }
413 /*}}}*/
414 // GrpIterator::operator++ - Prefix incr /*{{{*/
415 // ---------------------------------------------------------------------
416 /* This will advance to the next logical group in the hash table. */
417 pkgCache::GrpIterator& pkgCache::GrpIterator::operator++()
418 {
419 // Follow the current links
420 if (S != Owner->GrpP)
421 S = Owner->GrpP + S->Next;
422
423 // Follow the hash table
424 while (S == Owner->GrpP && (HashIndex+1) < (signed)Owner->HeaderP->GetHashTableSize())
425 {
426 ++HashIndex;
427 S = Owner->GrpP + Owner->HeaderP->GrpHashTableP()[HashIndex];
428 }
429 return *this;
430 }
431 /*}}}*/
432 // PkgIterator::operator++ - Prefix incr /*{{{*/
433 // ---------------------------------------------------------------------
434 /* This will advance to the next logical package in the hash table. */
435 pkgCache::PkgIterator& pkgCache::PkgIterator::operator++()
436 {
437 // Follow the current links
438 if (S != Owner->PkgP)
439 S = Owner->PkgP + S->NextPackage;
440
441 // Follow the hash table
442 while (S == Owner->PkgP && (HashIndex+1) < (signed)Owner->HeaderP->GetHashTableSize())
443 {
444 ++HashIndex;
445 S = Owner->PkgP + Owner->HeaderP->PkgHashTableP()[HashIndex];
446 }
447 return *this;
448 }
449 /*}}}*/
450 pkgCache::DepIterator& pkgCache::DepIterator::operator++() /*{{{*/
451 {
452 if (S == Owner->DepP)
453 return *this;
454 S = Owner->DepP + (Type == DepVer ? S->NextDepends : S->NextRevDepends);
455 if (S == Owner->DepP)
456 S2 = Owner->DepDataP;
457 else
458 S2 = Owner->DepDataP + S->DependencyData;
459 return *this;
460 }
461 /*}}}*/
462 // PkgIterator::State - Check the State of the package /*{{{*/
463 // ---------------------------------------------------------------------
464 /* By this we mean if it is either cleanly installed or cleanly removed. */
465 pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
466 {
467 if (S->InstState == pkgCache::State::ReInstReq ||
468 S->InstState == pkgCache::State::HoldReInstReq)
469 return NeedsUnpack;
470
471 if (S->CurrentState == pkgCache::State::UnPacked ||
472 S->CurrentState == pkgCache::State::HalfConfigured)
473 // we leave triggers alone complettely. dpkg deals with
474 // them in a hard-to-predict manner and if they get
475 // resolved by dpkg before apt run dpkg --configure on
476 // the TriggersPending package dpkg returns a error
477 //Pkg->CurrentState == pkgCache::State::TriggersAwaited
478 //Pkg->CurrentState == pkgCache::State::TriggersPending)
479 return NeedsConfigure;
480
481 if (S->CurrentState == pkgCache::State::HalfInstalled ||
482 S->InstState != pkgCache::State::Ok)
483 return NeedsUnpack;
484
485 return NeedsNothing;
486 }
487 /*}}}*/
488 // PkgIterator::CandVersion - Returns the candidate version string /*{{{*/
489 // ---------------------------------------------------------------------
490 /* Return string representing of the candidate version. */
491 const char *
492 pkgCache::PkgIterator::CandVersion() const
493 {
494 //TargetVer is empty, so don't use it.
495 VerIterator version = pkgPolicy(Owner).GetCandidateVer(*this);
496 if (version.IsGood())
497 return version.VerStr();
498 return 0;
499 }
500 /*}}}*/
501 // PkgIterator::CurVersion - Returns the current version string /*{{{*/
502 // ---------------------------------------------------------------------
503 /* Return string representing of the current version. */
504 const char *
505 pkgCache::PkgIterator::CurVersion() const
506 {
507 VerIterator version = CurrentVer();
508 if (version.IsGood())
509 return CurrentVer().VerStr();
510 return 0;
511 }
512 /*}}}*/
513 // ostream operator to handle string representation of a package /*{{{*/
514 // ---------------------------------------------------------------------
515 /* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section)
516 Note that the characters <|>() are all literal above. Versions will be omitted
517 if they provide no new information (e.g. there is no newer version than candidate)
518 If no version and/or section can be found "none" is used. */
519 std::ostream&
520 operator<<(std::ostream& out, pkgCache::PkgIterator Pkg)
521 {
522 if (Pkg.end() == true)
523 return out << "invalid package";
524
525 string current = string(Pkg.CurVersion() == 0 ? "none" : Pkg.CurVersion());
526 string candidate = string(Pkg.CandVersion() == 0 ? "none" : Pkg.CandVersion());
527 string newest = string(Pkg.VersionList().end() ? "none" : Pkg.VersionList().VerStr());
528
529 out << Pkg.Name() << " [ " << Pkg.Arch() << " ] < " << current;
530 if (current != candidate)
531 out << " -> " << candidate;
532 if ( newest != "none" && candidate != newest)
533 out << " | " << newest;
534 if (Pkg->VersionList == 0)
535 out << " > ( none )";
536 else
537 out << " > ( " << string(Pkg.VersionList().Section()==0?"unknown":Pkg.VersionList().Section()) << " )";
538 return out;
539 }
540 /*}}}*/
541 // PkgIterator::FullName - Returns Name and (maybe) Architecture /*{{{*/
542 // ---------------------------------------------------------------------
543 /* Returns a name:arch string */
544 std::string pkgCache::PkgIterator::FullName(bool const &Pretty) const
545 {
546 string fullname = Name();
547 if (Pretty == false ||
548 (strcmp(Arch(), "all") != 0 &&
549 strcmp(Owner->NativeArch(), Arch()) != 0))
550 return fullname.append(":").append(Arch());
551 return fullname;
552 }
553 /*}}}*/
554 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
555 // ---------------------------------------------------------------------
556 /* Currently critical deps are defined as depends, predepends and
557 conflicts (including dpkg's Breaks fields). */
558 bool pkgCache::DepIterator::IsCritical() const
559 {
560 if (IsNegative() == true ||
561 S2->Type == pkgCache::Dep::Depends ||
562 S2->Type == pkgCache::Dep::PreDepends)
563 return true;
564 return false;
565 }
566 /*}}}*/
567 // DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
568 // ---------------------------------------------------------------------
569 /* Some dependencies are positive like Depends and Recommends, others
570 are negative like Conflicts which can and should be handled differently */
571 bool pkgCache::DepIterator::IsNegative() const
572 {
573 return S2->Type == Dep::DpkgBreaks ||
574 S2->Type == Dep::Conflicts ||
575 S2->Type == Dep::Obsoletes;
576 }
577 /*}}}*/
578 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
579 // ---------------------------------------------------------------------
580 /* This intellegently looks at dep target packages and tries to figure
581 out which package should be used. This is needed to nicely handle
582 provide mapping. If the target package has no other providing packages
583 then it returned. Otherwise the providing list is looked at to
584 see if there is one one unique providing package if so it is returned.
585 Otherwise true is returned and the target package is set. The return
586 result indicates whether the node should be expandable
587
588 In Conjunction with the DepCache the value of Result may not be
589 super-good since the policy may have made it uninstallable. Using
590 AllTargets is better in this case. */
591 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) const
592 {
593 Result = TargetPkg();
594
595 // No provides at all
596 if (Result->ProvidesList == 0)
597 return false;
598
599 // There is the Base package and the providing ones which is at least 2
600 if (Result->VersionList != 0)
601 return true;
602
603 /* We have to skip over indirect provisions of the package that
604 owns the dependency. For instance, if libc5-dev depends on the
605 virtual package libc-dev which is provided by libc5-dev and libc6-dev
606 we must ignore libc5-dev when considering the provides list. */
607 PrvIterator PStart = Result.ProvidesList();
608 for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); ++PStart);
609
610 // Nothing but indirect self provides
611 if (PStart.end() == true)
612 return false;
613
614 // Check for single packages in the provides list
615 PrvIterator P = PStart;
616 for (; P.end() != true; ++P)
617 {
618 // Skip over self provides
619 if (P.OwnerPkg() == ParentPkg())
620 continue;
621 if (PStart.OwnerPkg() != P.OwnerPkg())
622 break;
623 }
624
625 Result = PStart.OwnerPkg();
626
627 // Check for non dups
628 if (P.end() != true)
629 return true;
630
631 return false;
632 }
633 /*}}}*/
634 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
635 // ---------------------------------------------------------------------
636 /* This is a more useful version of TargetPkg() that follows versioned
637 provides. It includes every possible package-version that could satisfy
638 the dependency. The last item in the list has a 0. The resulting pointer
639 must be delete [] 'd */
640 pkgCache::Version **pkgCache::DepIterator::AllTargets() const
641 {
642 Version **Res = 0;
643 unsigned long Size =0;
644 while (1)
645 {
646 Version **End = Res;
647 PkgIterator DPkg = TargetPkg();
648
649 // Walk along the actual package providing versions
650 for (VerIterator I = DPkg.VersionList(); I.end() == false; ++I)
651 {
652 if (IsIgnorable(I.ParentPkg()) == true)
653 continue;
654 if (IsSatisfied(I) == false)
655 continue;
656
657 Size++;
658 if (Res != 0)
659 *End++ = I;
660 }
661
662 // Follow all provides
663 for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; ++I)
664 {
665 if (IsIgnorable(I) == true)
666 continue;
667 if (IsSatisfied(I) == false)
668 continue;
669
670 Size++;
671 if (Res != 0)
672 *End++ = I.OwnerVer();
673 }
674
675 // Do it again and write it into the array
676 if (Res == 0)
677 {
678 Res = new Version *[Size+1];
679 Size = 0;
680 }
681 else
682 {
683 *End = 0;
684 break;
685 }
686 }
687
688 return Res;
689 }
690 /*}}}*/
691 // DepIterator::GlobOr - Compute an OR group /*{{{*/
692 // ---------------------------------------------------------------------
693 /* This Takes an iterator, iterates past the current dependency grouping
694 and returns Start and End so that so End is the final element
695 in the group, if End == Start then D is End++ and End is the
696 dependency D was pointing to. Use in loops to iterate sensibly. */
697 void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
698 {
699 // Compute a single dependency element (glob or)
700 Start = *this;
701 End = *this;
702 for (bool LastOR = true; end() == false && LastOR == true;)
703 {
704 LastOR = (S2->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
705 ++(*this);
706 if (LastOR == true)
707 End = (*this);
708 }
709 }
710 /*}}}*/
711 // DepIterator::IsIgnorable - should this packag/providr be ignored? /*{{{*/
712 // ---------------------------------------------------------------------
713 /* Deps like self-conflicts should be ignored as well as implicit conflicts
714 on virtual packages. */
715 bool pkgCache::DepIterator::IsIgnorable(PkgIterator const &PT) const
716 {
717 if (IsNegative() == false)
718 return false;
719
720 pkgCache::PkgIterator const PP = ParentPkg();
721 if (PP->Group != PT->Group)
722 return false;
723 // self-conflict
724 if (PP == PT)
725 return true;
726 pkgCache::VerIterator const PV = ParentVer();
727 // ignore group-conflict on a M-A:same package - but not our implicit dependencies
728 // so that we can have M-A:same packages conflicting with their own real name
729 if ((PV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
730 return IsMultiArchImplicit() == false;
731
732 return false;
733 }
734 bool pkgCache::DepIterator::IsIgnorable(PrvIterator const &Prv) const
735 {
736 if (IsNegative() == false)
737 return false;
738
739 PkgIterator const Pkg = ParentPkg();
740 /* Provides may never be applied against the same package (or group)
741 if it is a conflicts. See the comment above. */
742 if (Prv.OwnerPkg()->Group == Pkg->Group)
743 return true;
744 // Implicit group-conflicts should not be applied on providers of other groups
745 if (IsMultiArchImplicit() && Prv.OwnerPkg()->Group != Pkg->Group)
746 return true;
747
748 return false;
749 }
750 /*}}}*/
751 // DepIterator::IsSatisfied - check if a version satisfied the dependency /*{{{*/
752 bool pkgCache::DepIterator::IsSatisfied(VerIterator const &Ver) const
753 {
754 return Owner->VS->CheckDep(Ver.VerStr(),S2->CompareOp,TargetVer());
755 }
756 bool pkgCache::DepIterator::IsSatisfied(PrvIterator const &Prv) const
757 {
758 return Owner->VS->CheckDep(Prv.ProvideVersion(),S2->CompareOp,TargetVer());
759 }
760 /*}}}*/
761 // DepIterator::IsImplicit - added by the cache generation /*{{{*/
762 bool pkgCache::DepIterator::IsImplicit() const
763 {
764 if (IsMultiArchImplicit() == true)
765 return true;
766 if (IsNegative() || S2->Type == pkgCache::Dep::Replaces)
767 {
768 if ((S2->CompareOp & pkgCache::Dep::ArchSpecific) != pkgCache::Dep::ArchSpecific &&
769 strcmp(ParentPkg().Arch(), TargetPkg().Arch()) != 0)
770 return true;
771 }
772 return false;
773 }
774 /*}}}*/
775 // ostream operator to handle string representation of a dependecy /*{{{*/
776 // ---------------------------------------------------------------------
777 /* */
778 std::ostream& operator<<(std::ostream& out, pkgCache::DepIterator D)
779 {
780 if (D.end() == true)
781 return out << "invalid dependency";
782
783 pkgCache::PkgIterator P = D.ParentPkg();
784 pkgCache::PkgIterator T = D.TargetPkg();
785
786 out << (P.end() ? "invalid pkg" : P.FullName(false)) << " " << D.DepType()
787 << " on ";
788 if (T.end() == true)
789 out << "invalid pkg";
790 else
791 out << T;
792
793 if (D->Version != 0)
794 out << " (" << D.CompType() << " " << D.TargetVer() << ")";
795
796 return out;
797 }
798 /*}}}*/
799 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
800 // ---------------------------------------------------------------------
801 /* This just looks over the version list to see if B is listed before A. In
802 most cases this will return in under 4 checks, ver lists are short. */
803 int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
804 {
805 // Check if they are equal
806 if (*this == B)
807 return 0;
808 if (end() == true)
809 return -1;
810 if (B.end() == true)
811 return 1;
812
813 /* Start at A and look for B. If B is found then A > B otherwise
814 B was before A so A < B */
815 VerIterator I = *this;
816 for (;I.end() == false; ++I)
817 if (I == B)
818 return 1;
819 return -1;
820 }
821 /*}}}*/
822 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
823 // ---------------------------------------------------------------------
824 /* */
825 APT_PURE bool pkgCache::VerIterator::Downloadable() const
826 {
827 VerFileIterator Files = FileList();
828 for (; Files.end() == false; ++Files)
829 if (Files.File().Flagged(pkgCache::Flag::NotSource) == false)
830 return true;
831 return false;
832 }
833 /*}}}*/
834 // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
835 // ---------------------------------------------------------------------
836 /* This checks to see if any of the versions files are not NotAutomatic.
837 True if this version is selectable for automatic installation. */
838 APT_PURE bool pkgCache::VerIterator::Automatic() const
839 {
840 VerFileIterator Files = FileList();
841 for (; Files.end() == false; ++Files)
842 // Do not check ButAutomaticUpgrades here as it is kind of automatic…
843 if (Files.File().Flagged(pkgCache::Flag::NotAutomatic) == false)
844 return true;
845 return false;
846 }
847 /*}}}*/
848 // VerIterator::NewestFile - Return the newest file version relation /*{{{*/
849 // ---------------------------------------------------------------------
850 /* This looks at the version numbers associated with all of the sources
851 this version is in and returns the highest.*/
852 pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const
853 {
854 VerFileIterator Files = FileList();
855 VerFileIterator Highest = Files;
856 for (; Files.end() == false; ++Files)
857 {
858 if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0)
859 Highest = Files;
860 }
861
862 return Highest;
863 }
864 /*}}}*/
865 // VerIterator::RelStr - Release description string /*{{{*/
866 // ---------------------------------------------------------------------
867 /* This describes the version from a release-centric manner. The output is a
868 list of Label:Version/Archive */
869 string pkgCache::VerIterator::RelStr() const
870 {
871 bool First = true;
872 string Res;
873 for (pkgCache::VerFileIterator I = this->FileList(); I.end() == false; ++I)
874 {
875 // Do not print 'not source' entries'
876 pkgCache::PkgFileIterator const File = I.File();
877 if (File.Flagged(pkgCache::Flag::NotSource))
878 continue;
879
880 // See if we have already printed this out..
881 bool Seen = false;
882 for (pkgCache::VerFileIterator J = this->FileList(); I != J; ++J)
883 {
884 pkgCache::PkgFileIterator const File2 = J.File();
885 if (File2.Label() == 0 || File.Label() == 0)
886 continue;
887
888 if (strcmp(File.Label(),File2.Label()) != 0)
889 continue;
890
891 if (File2.Version() == File.Version())
892 {
893 Seen = true;
894 break;
895 }
896 if (File2.Version() == 0 || File.Version() == 0)
897 break;
898 if (strcmp(File.Version(),File2.Version()) == 0)
899 Seen = true;
900 }
901
902 if (Seen == true)
903 continue;
904
905 if (First == false)
906 Res += ", ";
907 else
908 First = false;
909
910 if (File.Label() != 0)
911 Res = Res + File.Label() + ':';
912
913 if (File.Archive() != 0)
914 {
915 if (File.Version() == 0)
916 Res += File.Archive();
917 else
918 Res = Res + File.Version() + '/' + File.Archive();
919 }
920 else
921 {
922 // No release file, print the host name that this came from
923 if (File.Site() == 0 || File.Site()[0] == 0)
924 Res += "localhost";
925 else
926 Res += File.Site();
927 }
928 }
929 if (S->ParentPkg != 0)
930 Res.append(" [").append(Arch()).append("]");
931 return Res;
932 }
933 /*}}}*/
934 // VerIterator::MultiArchType - string representing MultiArch flag /*{{{*/
935 const char * pkgCache::VerIterator::MultiArchType() const
936 {
937 if ((S->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
938 return "same";
939 else if ((S->MultiArch & pkgCache::Version::Foreign) == pkgCache::Version::Foreign)
940 return "foreign";
941 else if ((S->MultiArch & pkgCache::Version::Allowed) == pkgCache::Version::Allowed)
942 return "allowed";
943 return "none";
944 }
945 /*}}}*/
946 // RlsFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
947 // ---------------------------------------------------------------------
948 /* This stats the file and compares its stats with the ones that were
949 stored during generation. Date checks should probably also be
950 included here. */
951 bool pkgCache::RlsFileIterator::IsOk()
952 {
953 struct stat Buf;
954 if (stat(FileName(),&Buf) != 0)
955 return false;
956
957 if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime)
958 return false;
959
960 return true;
961 }
962 /*}}}*/
963 // RlsFileIterator::RelStr - Return the release string /*{{{*/
964 string pkgCache::RlsFileIterator::RelStr()
965 {
966 string Res;
967 if (Version() != 0)
968 Res = Res + (Res.empty() == true?"v=":",v=") + Version();
969 if (Origin() != 0)
970 Res = Res + (Res.empty() == true?"o=":",o=") + Origin();
971 if (Archive() != 0)
972 Res = Res + (Res.empty() == true?"a=":",a=") + Archive();
973 if (Codename() != 0)
974 Res = Res + (Res.empty() == true?"n=":",n=") + Codename();
975 if (Label() != 0)
976 Res = Res + (Res.empty() == true?"l=":",l=") + Label();
977 return Res;
978 }
979 /*}}}*/
980 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
981 // ---------------------------------------------------------------------
982 /* This stats the file and compares its stats with the ones that were
983 stored during generation. Date checks should probably also be
984 included here. */
985 bool pkgCache::PkgFileIterator::IsOk()
986 {
987 struct stat Buf;
988 if (stat(FileName(),&Buf) != 0)
989 return false;
990
991 if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime)
992 return false;
993
994 return true;
995 }
996 /*}}}*/
997 string pkgCache::PkgFileIterator::RelStr() /*{{{*/
998 {
999 std::string Res;
1000 if (ReleaseFile() == 0)
1001 {
1002 if (Component() != 0)
1003 Res = Res + (Res.empty() == true?"a=":",a=") + Component();
1004 }
1005 else
1006 {
1007 Res = ReleaseFile().RelStr();
1008 if (Component() != 0)
1009 Res = Res + (Res.empty() == true?"c=":",c=") + Component();
1010 }
1011 if (Architecture() != 0)
1012 Res = Res + (Res.empty() == true?"b=":",b=") + Architecture();
1013 return Res;
1014 }
1015 /*}}}*/
1016 // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
1017 // ---------------------------------------------------------------------
1018 /* return a DescIter for the current locale or the default if none is
1019 * found
1020 */
1021 pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescription() const
1022 {
1023 std::vector<string> const lang = APT::Configuration::getLanguages();
1024 for (std::vector<string>::const_iterator l = lang.begin();
1025 l != lang.end(); ++l)
1026 {
1027 pkgCache::DescIterator Desc = DescriptionList();
1028 for (; Desc.end() == false; ++Desc)
1029 if (*l == Desc.LanguageCode())
1030 break;
1031 if (Desc.end() == true)
1032 {
1033 if (*l == "en")
1034 {
1035 Desc = DescriptionList();
1036 for (; Desc.end() == false; ++Desc)
1037 if (strcmp(Desc.LanguageCode(), "") == 0)
1038 break;
1039 if (Desc.end() == true)
1040 continue;
1041 }
1042 else
1043 continue;
1044 }
1045 return Desc;
1046 }
1047 for (pkgCache::DescIterator Desc = DescriptionList();
1048 Desc.end() == false; ++Desc)
1049 if (strcmp(Desc.LanguageCode(), "") == 0)
1050 return Desc;
1051 return DescriptionList();
1052 }
1053
1054 /*}}}*/
1055
1056 pkgCache::~pkgCache() {}