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