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