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