apt  2.2.4
About: Apt (Advanced Package Tool) is a management system for software packages (Debian/Ubuntu). Release series 2.2.
  Fossies Dox: apt-2.2.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

pkgcache.cc
Go to the documentation of this file.
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4 
5  Package Cache - Accessor code for the cache
6 
7  Please see doc/apt-pkg/cache.sgml for a more detailed description of
8  this format. Also be sure to keep that file up-to-date!!
9 
10  This is the general utility functions for cache management. They provide
11  a complete set of accessor functions for the cache. The cacheiterators
12  header contains the STL-like iterators that can be used to easially
13  navigate the cache as well as seamlessly dereference the mmap'd
14  indexes. Use these always.
15 
16  The main class provides for ways to get package indexes and some
17  general lookup functions to start the iterators.
18 
19  ##################################################################### */
20  /*}}}*/
21 // Include Files /*{{{*/
22 #include <config.h>
23 
25 #include <apt-pkg/configuration.h>
26 #include <apt-pkg/error.h>
27 #include <apt-pkg/macros.h>
28 #include <apt-pkg/mmap.h>
29 #include <apt-pkg/pkgcache.h>
30 #include <apt-pkg/policy.h>
31 #include <apt-pkg/strutl.h>
32 #include <apt-pkg/version.h>
33 
34 #include <algorithm>
35 #include <sstream>
36 #include <string>
37 #include <vector>
38 #include <stddef.h>
39 #include <string.h>
40 #include <sys/stat.h>
41 #include <xxhash.h>
42 
43 #include <apti18n.h>
44  /*}}}*/
45 
46 using std::string;
47 using APT::StringView;
48 
49 
50 // Cache::Header::Header - Constructor /*{{{*/
51 // ---------------------------------------------------------------------
52 /* Simply initialize the header */
54 {
55 #define APT_HEADER_SET(X,Y) X = Y; static_assert(std::numeric_limits<decltype(X)>::max() > Y, "Size violation detected in pkgCache::Header")
56  APT_HEADER_SET(Signature, 0x98FE76DC);
57 
58  /* Whenever the structures change the major version should be bumped,
59  whenever the generator changes the minor version should be bumped. */
62  APT_HEADER_SET(Dirty, false);
63 
76 #undef APT_HEADER_SET
77 
78  GroupCount = 0;
79  PackageCount = 0;
80  VersionCount = 0;
81  DescriptionCount = 0;
82  DependsCount = 0;
83  DependsDataCount = 0;
84  ReleaseFileCount = 0;
85  PackageFileCount = 0;
86  VerFileCount = 0;
87  DescFileCount = 0;
88  ProvidesCount = 0;
89  MaxVerFileSize = 0;
90  MaxDescFileSize = 0;
91 
92  FileList = 0;
93  RlsFileList = 0;
94  VerSysName = 0;
95  Architecture = 0;
97  SetHashTableSize(_config->FindI("APT::Cache-HashTableSize", 196613));
98  memset(Pools,0,sizeof(Pools));
99 
100  CacheFileSize = 0;
101 }
102  /*}}}*/
103 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
104 // ---------------------------------------------------------------------
105 /* */
107 {
108  if (HeaderSz == Against.HeaderSz &&
109  GroupSz == Against.GroupSz &&
110  PackageSz == Against.PackageSz &&
111  ReleaseFileSz == Against.ReleaseFileSz &&
112  PackageFileSz == Against.PackageFileSz &&
113  VersionSz == Against.VersionSz &&
114  DescriptionSz == Against.DescriptionSz &&
115  DependencySz == Against.DependencySz &&
116  DependencyDataSz == Against.DependencyDataSz &&
117  VerFileSz == Against.VerFileSz &&
118  DescFileSz == Against.DescFileSz &&
119  ProvidesSz == Against.ProvidesSz)
120  return true;
121  return false;
122 }
123  /*}}}*/
124 
125 // Cache::pkgCache - Constructor /*{{{*/
126 // ---------------------------------------------------------------------
127 /* */
128 pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map), VS(nullptr), d(NULL)
129 {
130  // call getArchitectures() with cached=false to ensure that the
131  // architectures cache is re-evaluated. this is needed in cases
132  // when the APT::Architecture field changes between two cache creations
134  if (DoMap == true)
135  ReMap();
136 }
137  /*}}}*/
138 // Cache::ReMap - Reopen the cache file /*{{{*/
139 // ---------------------------------------------------------------------
140 /* If the file is already closed then this will open it open it. */
141 bool pkgCache::ReMap(bool const &Errorchecks)
142 {
143  // Apply the typecasts.
144  HeaderP = (Header *)Map.Data();
145  GrpP = (Group *)Map.Data();
146  PkgP = (Package *)Map.Data();
147  VerFileP = (VerFile *)Map.Data();
148  DescFileP = (DescFile *)Map.Data();
149  RlsFileP = (ReleaseFile *)Map.Data();
150  PkgFileP = (PackageFile *)Map.Data();
151  VerP = (Version *)Map.Data();
152  DescP = (Description *)Map.Data();
153  ProvideP = (Provides *)Map.Data();
154  DepP = (Dependency *)Map.Data();
156  StrP = (char *)Map.Data();
157 
158  if (Errorchecks == false)
159  return true;
160 
161  if (Map.Size() == 0 || HeaderP == 0)
162  return _error->Error(_("Empty package cache"));
163 
164  // Check the header
165  Header DefHeader;
166  if (HeaderP->Signature != DefHeader.Signature ||
167  HeaderP->Dirty == true)
168  return _error->Error(_("The package cache file is corrupted"));
169 
170  if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
171  HeaderP->MinorVersion != DefHeader.MinorVersion ||
172  HeaderP->CheckSizes(DefHeader) == false)
173  return _error->Error(_("The package cache file is an incompatible version"));
174 
175  if (HeaderP->VerSysName == 0 || HeaderP->Architecture == 0 || HeaderP->GetArchitectures() == 0)
176  return _error->Error(_("The package cache file is corrupted"));
177 
178  // Locate our VS..
180  return _error->Error(_("This APT does not support the versioning system '%s'"),StrP + HeaderP->VerSysName);
181 
182  // Check the architecture
183  std::vector<std::string> archs = APT::Configuration::getArchitectures();
184  std::string list = "";
185  for (auto const & arch : archs) {
186  if (!list.empty())
187  list.append(",");
188  list.append(arch);
189  }
190  if (_config->Find("APT::Architecture") != StrP + HeaderP->Architecture ||
191  list != StrP + HeaderP->GetArchitectures())
192  return _error->Error(_("The package cache was built for different architectures: %s vs %s"), StrP + HeaderP->GetArchitectures(), list.c_str());
193 
194 
195  auto hash = CacheHash();
196  if (_config->FindB("Debug::pkgCacheGen", false))
197  std::clog << "Opened cache with hash " << hash << ", expecting " << HeaderP->CacheFileSize << "\n";
198  if (hash != HeaderP->CacheFileSize)
199  return _error->Error(_("The package cache file is corrupted, it has the wrong hash"));
200 
201  return true;
202 }
203  /*}}}*/
204 // Cache::Hash - Hash a string /*{{{*/
205 // ---------------------------------------------------------------------
206 /* This is used to generate the hash entries for the HashTable. With my
207  package list from bo this function gets 94% table usage on a 512 item
208  table (480 used items) */
210 {
211  uint32_t Hash = 5381;
212  auto I = Str.begin();
213  auto End = Str.end();
214  for (; I + 7 < End; I += 8)
215  {
216  Hash = (33u * 33u * 33u * 33u * 33u * 33u * 33u * 33u * Hash +
217  33u * 33u * 33u * 33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[0]) +
218  33u * 33u * 33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[1]) +
219  33u * 33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[2]) +
220  33u * 33u * 33u * 33u * tolower_ascii_unsafe(I[3]) +
221  33u * 33u * 33u * tolower_ascii_unsafe(I[4]) +
222  33u * 33u * tolower_ascii_unsafe(I[5]) +
223  33u * tolower_ascii_unsafe(I[6]) +
224  tolower_ascii_unsafe(I[7]));
225  }
226  for (; I != End; ++I)
227  Hash = 33u * Hash + tolower_ascii_unsafe(*I);
228  return Hash % HeaderP->GetHashTableSize();
229 }
231 {
232  pkgCache::Header header = {};
233  XXH3_state_t *state = XXH3_createState();
234 
235  if (Map.Size() < sizeof(header))
236  return 0;
237 
238  XXH3_64bits_reset(state);
239  memcpy(&header, GetMap().Data(), sizeof(header));
240 
241  header.Dirty = false;
242  header.CacheFileSize = 0;
243 
244  XXH3_64bits_update(state,
245  reinterpret_cast<const unsigned char *>(PACKAGE_VERSION),
246  APT_ARRAY_SIZE(PACKAGE_VERSION));
247 
248  XXH3_64bits_update(state,
249  reinterpret_cast<const unsigned char *>(&header),
250  sizeof(header));
251 
252  if (Map.Size() > sizeof(header)) {
253  XXH3_64bits_update(state,
254  static_cast<const unsigned char *>(GetMap().Data()) + sizeof(header),
255  GetMap().Size() - sizeof(header));
256  }
257 
258  auto const digest = XXH3_64bits_digest(state);
259  XXH3_freeState(state);
260  return digest & 0xFFFFFFFF;
261 }
262  /*}}}*/
263 // Cache::FindPkg - Locate a package by name /*{{{*/
264 // ---------------------------------------------------------------------
265 /* Returns 0 on error, pointer to the package otherwise */
267  auto const found = Name.rfind(':');
268  if (found == string::npos)
269  return FindPkg(Name, "native");
270  auto const Arch = Name.substr(found+1);
271  /* Beware: This is specialcased to handle pkg:any in dependencies
272  as these are linked to virtual pkg:any named packages.
273  If you want any arch from a pkg, use FindPkg(pkg,"any") */
274  if (Arch == "any")
275  return FindPkg(Name, "any");
276  return FindPkg(Name.substr(0, found), Arch);
277 }
278  /*}}}*/
279 // Cache::FindPkg - Locate a package by name /*{{{*/
280 // ---------------------------------------------------------------------
281 /* Returns 0 on error, pointer to the package otherwise */
283  /* We make a detour via the GrpIterator here as
284  on a multi-arch environment a group is easier to
285  find than a package (less entries in the buckets) */
287  if (Grp.end() == true)
288  return PkgIterator(*this,0);
289 
290  return Grp.FindPkg(Arch);
291 }
292  /*}}}*/
293 // Cache::FindGrp - Locate a group by name /*{{{*/
294 // ---------------------------------------------------------------------
295 /* Returns End-Pointer on error, pointer to the group otherwise */
297  if (unlikely(Name.empty() == true))
298  return GrpIterator(*this,0);
299 
300  // Look at the hash bucket for the group
301  Group *Grp = GrpP + HeaderP->GrpHashTableP()[sHash(Name)];
302  for (; Grp != GrpP; Grp = GrpP + Grp->Next) {
303  int const cmp = StringViewCompareFast(Name, ViewString(Grp->Name));
304  if (cmp == 0)
305  return GrpIterator(*this, Grp);
306  else if (cmp < 0)
307  break;
308  }
309 
310  return GrpIterator(*this,0);
311 }
312  /*}}}*/
313 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
314 // ---------------------------------------------------------------------
315 /* This returns a string representation of the dependency compare
316  type in the weird debian style.. */
317 const char *pkgCache::CompTypeDeb(unsigned char Comp)
318 {
319  const char * const Ops[] = {"","<=",">=","<<",">>","=","!="};
320  if (unlikely((unsigned)(Comp & 0xF) >= sizeof(Ops)/sizeof(Ops[0])))
321  return "";
322  return Ops[Comp & 0xF];
323 }
324  /*}}}*/
325 // Cache::CompType - Return a string describing the compare type /*{{{*/
326 // ---------------------------------------------------------------------
327 /* This returns a string representation of the dependency compare
328  type */
329 const char *pkgCache::CompType(unsigned char Comp)
330 {
331  const char * const Ops[] = {"","<=",">=","<",">","=","!="};
332  if (unlikely((unsigned)(Comp & 0xF) >= sizeof(Ops)/sizeof(Ops[0])))
333  return "";
334  return Ops[Comp & 0xF];
335 }
336  /*}}}*/
337 // Cache::DepType - Return a string describing the dep type /*{{{*/
338 // ---------------------------------------------------------------------
339 /* */
340 const char *pkgCache::DepType(unsigned char Type)
341 {
342  const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
343  _("Recommends"),_("Conflicts"),_("Replaces"),
344  _("Obsoletes"),_("Breaks"), _("Enhances")};
345  if (Type < sizeof(Types)/sizeof(*Types))
346  return Types[Type];
347  return "";
348 }
349  /*}}}*/
350 // Cache::Priority - Convert a priority value to a string /*{{{*/
351 // ---------------------------------------------------------------------
352 /* */
353 const char *pkgCache::Priority(unsigned char Prio)
354 {
355  const char *Mapping[] = {0,_("required"),_("important"),_("standard"),
356  _("optional"),_("extra")};
357  if (Prio < APT_ARRAY_SIZE(Mapping))
358  return Mapping[Prio];
359  return 0;
360 }
361  /*}}}*/
362 // GrpIterator::FindPkg - Locate a package by arch /*{{{*/
363 // ---------------------------------------------------------------------
364 /* Returns an End-Pointer on error, pointer to the package otherwise */
365 pkgCache::PkgIterator pkgCache::GrpIterator::FindPkg(StringView Arch) const {
366  if (unlikely(IsGood() == false || S->FirstPackage == 0))
367  return PkgIterator(*Owner, 0);
368 
369  /* If we accept any package we simply return the "first"
370  package in this group */
371  if (Arch == "any")
372  return PkgIterator(*Owner, Owner->PkgP + S->FirstPackage);
373  if (Arch == "native" || Arch == "all")
374  Arch = Owner->NativeArch();
375 
376  // Iterate over the list to find the matching arch
377  for (pkgCache::Package *Pkg = PackageList(); Pkg != Owner->PkgP;
378  Pkg = Owner->PkgP + Pkg->NextPackage) {
379  if (Arch == Owner->ViewString(Pkg->Arch))
380  return PkgIterator(*Owner, Pkg);
381  if ((Owner->PkgP + S->LastPackage) == Pkg)
382  break;
383  }
384 
385  return PkgIterator(*Owner, 0);
386 }
387  /*}}}*/
388 // GrpIterator::FindPreferredPkg - Locate the "best" package /*{{{*/
389 // ---------------------------------------------------------------------
390 /* Returns an End-Pointer on error, pointer to the package otherwise */
391 pkgCache::PkgIterator pkgCache::GrpIterator::FindPreferredPkg(bool const &PreferNonVirtual) const {
392  pkgCache::PkgIterator Pkg = FindPkg(StringView("native", 6));
393  if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
394  return Pkg;
395 
396  std::vector<std::string> const archs = APT::Configuration::getArchitectures();
397  for (std::vector<std::string>::const_iterator a = archs.begin();
398  a != archs.end(); ++a) {
399  Pkg = FindPkg(*a);
400  if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
401  return Pkg;
402  }
403  // packages without an architecture
404  Pkg = FindPkg(StringView("none", 4));
405  if (Pkg.end() == false && (PreferNonVirtual == false || Pkg->VersionList != 0))
406  return Pkg;
407 
408  if (PreferNonVirtual == true)
409  return FindPreferredPkg(false);
410  return PkgIterator(*Owner, 0);
411 }
412  /*}}}*/
413 // GrpIterator::NextPkg - Locate the next package in the group /*{{{*/
414 // ---------------------------------------------------------------------
415 /* Returns an End-Pointer on error, pointer to the package otherwise.
416  We can't simply ++ to the next as the next package of the last will
417  be from a different group (with the same hash value) */
418 pkgCache::PkgIterator pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator const &LastPkg) const {
419  if (unlikely(IsGood() == false || S->FirstPackage == 0 ||
420  LastPkg.end() == true))
421  return PkgIterator(*Owner, 0);
422 
423  if (S->LastPackage == LastPkg.MapPointer())
424  return PkgIterator(*Owner, 0);
425 
426  return PkgIterator(*Owner, Owner->PkgP + LastPkg->NextPackage);
427 }
428  /*}}}*/
429 // GrpIterator::operator++ - Prefix incr /*{{{*/
430 // ---------------------------------------------------------------------
431 /* This will advance to the next logical group in the hash table. */
432 pkgCache::GrpIterator& pkgCache::GrpIterator::operator++()
433 {
434  // Follow the current links
435  if (S != Owner->GrpP)
436  S = Owner->GrpP + S->Next;
437 
438  // Follow the hash table
439  while (S == Owner->GrpP && (HashIndex+1) < (signed)Owner->HeaderP->GetHashTableSize())
440  {
441  ++HashIndex;
442  S = Owner->GrpP + Owner->HeaderP->GrpHashTableP()[HashIndex];
443  }
444  return *this;
445 }
446  /*}}}*/
447 // PkgIterator::operator++ - Prefix incr /*{{{*/
448 // ---------------------------------------------------------------------
449 /* This will advance to the next logical package in the hash table. */
450 pkgCache::PkgIterator& pkgCache::PkgIterator::operator++()
451 {
452  // Follow the current links
453  if (S != Owner->PkgP)
454  S = Owner->PkgP + S->NextPackage;
455 
456  // Follow the hash table
457  while (S == Owner->PkgP && (HashIndex+1) < (signed)Owner->HeaderP->GetHashTableSize())
458  {
459  ++HashIndex;
460  S = Owner->PkgP + Owner->HeaderP->PkgHashTableP()[HashIndex];
461  }
462  return *this;
463 }
464  /*}}}*/
465 pkgCache::DepIterator& pkgCache::DepIterator::operator++() /*{{{*/
466 {
467  if (S == Owner->DepP)
468  return *this;
469  S = Owner->DepP + (Type == DepVer ? S->NextDepends : S->NextRevDepends);
470  if (S == Owner->DepP)
471  S2 = Owner->DepDataP;
472  else
473  S2 = Owner->DepDataP + S->DependencyData;
474  return *this;
475 }
476  /*}}}*/
477 // PkgIterator::State - Check the State of the package /*{{{*/
478 // ---------------------------------------------------------------------
479 /* By this we mean if it is either cleanly installed or cleanly removed. */
480 pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
481 {
482  if (S->InstState == pkgCache::State::ReInstReq ||
483  S->InstState == pkgCache::State::HoldReInstReq)
484  return NeedsUnpack;
485 
486  if (S->CurrentState == pkgCache::State::UnPacked ||
487  S->CurrentState == pkgCache::State::HalfConfigured)
488  // we leave triggers alone completely. dpkg deals with
489  // them in a hard-to-predict manner and if they get
490  // resolved by dpkg before apt run dpkg --configure on
491  // the TriggersPending package dpkg returns a error
492  //Pkg->CurrentState == pkgCache::State::TriggersAwaited
493  //Pkg->CurrentState == pkgCache::State::TriggersPending)
494  return NeedsConfigure;
495 
496  if (S->CurrentState == pkgCache::State::HalfInstalled ||
497  S->InstState != pkgCache::State::Ok)
498  return NeedsUnpack;
499 
500  return NeedsNothing;
501 }
502  /*}}}*/
503 // PkgIterator::CurVersion - Returns the current version string /*{{{*/
504 // ---------------------------------------------------------------------
505 /* Return string representing of the current version. */
506 const char *
507 pkgCache::PkgIterator::CurVersion() const
508 {
509  VerIterator version = CurrentVer();
510  if (version.IsGood())
511  return CurrentVer().VerStr();
512  return 0;
513 }
514  /*}}}*/
515 // ostream operator to handle string representation of a package /*{{{*/
516 // ---------------------------------------------------------------------
517 /* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section)
518  Note that the characters <|>() are all literal above. Versions will be omitted
519  if they provide no new information (e.g. there is no newer version than candidate)
520  If no version and/or section can be found "none" is used. */
521 std::ostream&
522 operator<<(std::ostream& out, pkgCache::PkgIterator Pkg)
523 {
524  if (Pkg.end() == true)
525  return out << "invalid package";
526 
527  string current = string(Pkg.CurVersion() == 0 ? "none" : Pkg.CurVersion());
528  string newest = string(Pkg.VersionList().end() ? "none" : Pkg.VersionList().VerStr());
529 
530  out << Pkg.Name() << " [ " << Pkg.Arch() << " ] < " << current;
531  if ( newest != "none")
532  out << " | " << newest;
533  if (Pkg->VersionList == 0)
534  out << " > ( none )";
535  else
536  out << " > ( " << string(Pkg.VersionList().Section()==0?"unknown":Pkg.VersionList().Section()) << " )";
537  return out;
538 }
539  /*}}}*/
540 // PkgIterator::FullName - Returns Name and (maybe) Architecture /*{{{*/
541 // ---------------------------------------------------------------------
542 /* Returns a name:arch string */
543 std::string pkgCache::PkgIterator::FullName(bool const &Pretty) const
544 {
545  string fullname = Name();
546  if (Pretty == false ||
547  (strcmp(Arch(), "all") != 0 && strcmp(Arch(), "any") != 0 &&
548  strcmp(Owner->NativeArch(), Arch()) != 0))
549  return fullname.append(":").append(Arch());
550  return fullname;
551 }
552  /*}}}*/
553 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
554 // ---------------------------------------------------------------------
555 /* Currently critical deps are defined as depends, predepends and
556  conflicts (including dpkg's Breaks fields). */
557 bool pkgCache::DepIterator::IsCritical() const
558 {
559  if (IsNegative() == true ||
560  S2->Type == pkgCache::Dep::Depends ||
561  S2->Type == pkgCache::Dep::PreDepends)
562  return true;
563  return false;
564 }
565  /*}}}*/
566 // DepIterator::IsNegative - Returns true if the dep is a negative one /*{{{*/
567 // ---------------------------------------------------------------------
568 /* Some dependencies are positive like Depends and Recommends, others
569  are negative like Conflicts which can and should be handled differently */
570 bool pkgCache::DepIterator::IsNegative() const
571 {
572  return S2->Type == Dep::DpkgBreaks ||
573  S2->Type == Dep::Conflicts ||
574  S2->Type == Dep::Obsoletes;
575 }
576  /*}}}*/
577 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
578 // ---------------------------------------------------------------------
579 /* This intellegently looks at dep target packages and tries to figure
580  out which package should be used. This is needed to nicely handle
581  provide mapping. If the target package has no other providing packages
582  then it returned. Otherwise the providing list is looked at to
583  see if there is one unique providing package if so it is returned.
584  Otherwise true is returned and the target package is set. The return
585  result indicates whether the node should be expandable
586 
587  In Conjunction with the DepCache the value of Result may not be
588  super-good since the policy may have made it uninstallable. Using
589  AllTargets is better in this case. */
590 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) const
591 {
592  Result = TargetPkg();
593 
594  // No provides at all
595  if (Result->ProvidesList == 0)
596  return false;
597 
598  // There is the Base package and the providing ones which is at least 2
599  if (Result->VersionList != 0)
600  return true;
601 
602  /* We have to skip over indirect provisions of the package that
603  owns the dependency. For instance, if libc5-dev depends on the
604  virtual package libc-dev which is provided by libc5-dev and libc6-dev
605  we must ignore libc5-dev when considering the provides list. */
606  PrvIterator PStart = Result.ProvidesList();
607  for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); ++PStart);
608 
609  // Nothing but indirect self provides
610  if (PStart.end() == true)
611  return false;
612 
613  // Check for single packages in the provides list
614  PrvIterator P = PStart;
615  for (; P.end() != true; ++P)
616  {
617  // Skip over self provides
618  if (P.OwnerPkg() == ParentPkg())
619  continue;
620  if (PStart.OwnerPkg() != P.OwnerPkg())
621  break;
622  }
623 
624  Result = PStart.OwnerPkg();
625 
626  // Check for non dups
627  if (P.end() != true)
628  return true;
629 
630  return false;
631 }
632  /*}}}*/
633 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
634 // ---------------------------------------------------------------------
635 /* This is a more useful version of TargetPkg() that follows versioned
636  provides. It includes every possible package-version that could satisfy
637  the dependency. The last item in the list has a 0. The resulting pointer
638  must be delete [] 'd */
639 pkgCache::Version **pkgCache::DepIterator::AllTargets() const
640 {
641  Version **Res = 0;
642  unsigned long Size =0;
643  while (1)
644  {
645  Version **End = Res;
646  PkgIterator DPkg = TargetPkg();
647 
648  // Walk along the actual package providing versions
649  for (VerIterator I = DPkg.VersionList(); I.end() == false; ++I)
650  {
651  if (IsIgnorable(I.ParentPkg()) == true)
652  continue;
653  if (IsSatisfied(I) == false)
654  continue;
655 
656  Size++;
657  if (Res != 0)
658  *End++ = I;
659  }
660 
661  // Follow all provides
662  for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; ++I)
663  {
664  if (IsIgnorable(I) == true)
665  continue;
666  if (IsSatisfied(I) == false)
667  continue;
668 
669  Size++;
670  if (Res != 0)
671  *End++ = I.OwnerVer();
672  }
673 
674  // Do it again and write it into the array
675  if (Res == 0)
676  {
677  Res = new Version *[Size+1];
678  Size = 0;
679  }
680  else
681  {
682  *End = 0;
683  break;
684  }
685  }
686 
687  return Res;
688 }
689  /*}}}*/
690 // DepIterator::GlobOr - Compute an OR group /*{{{*/
691 // ---------------------------------------------------------------------
692 /* This Takes an iterator, iterates past the current dependency grouping
693  and returns Start and End so that so End is the final element
694  in the group, if End == Start then D is End++ and End is the
695  dependency D was pointing to. Use in loops to iterate sensibly. */
696 void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
697 {
698  // Compute a single dependency element (glob or)
699  Start = *this;
700  End = *this;
701  for (bool LastOR = true; end() == false && LastOR == true;)
702  {
703  LastOR = (S2->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
704  ++(*this);
705  if (LastOR == true)
706  End = (*this);
707  }
708 }
709  /*}}}*/
710 // DepIterator::IsIgnorable - should this packag/providr be ignored? /*{{{*/
711 // ---------------------------------------------------------------------
712 /* Deps like self-conflicts should be ignored as well as implicit conflicts
713  on virtual packages. */
714 bool pkgCache::DepIterator::IsIgnorable(PkgIterator const &PT) const
715 {
716  if (IsNegative() == false)
717  return false;
718 
719  pkgCache::PkgIterator const PP = ParentPkg();
720  if (PP->Group != PT->Group)
721  return false;
722  // self-conflict
723  if (PP == PT)
724  return true;
725  pkgCache::VerIterator const PV = ParentVer();
726  // ignore group-conflict on a M-A:same package - but not our implicit dependencies
727  // so that we can have M-A:same packages conflicting with their own real name
728  if ((PV->MultiArch & pkgCache::Version::Same) == pkgCache::Version::Same)
729  return IsMultiArchImplicit() == false;
730 
731  return false;
732 }
733 bool pkgCache::DepIterator::IsIgnorable(PrvIterator const &Prv) const
734 {
735  if (IsNegative() == false)
736  return false;
737 
738  PkgIterator const Pkg = ParentPkg();
739  /* Provides may never be applied against the same package (or group)
740  if it is a conflicts. See the comment above. */
741  if (Prv.OwnerPkg()->Group == Pkg->Group)
742  return true;
743  // Implicit group-conflicts should not be applied on providers of other groups
744  if (IsMultiArchImplicit() && Prv.OwnerPkg()->Group != Pkg->Group)
745  return true;
746 
747  return false;
748 }
749  /*}}}*/
750 // DepIterator::IsSatisfied - check if a version satisfied the dependency /*{{{*/
751 bool pkgCache::DepIterator::IsSatisfied(VerIterator const &Ver) const
752 {
753  return Owner->VS->CheckDep(Ver.VerStr(),S2->CompareOp,TargetVer());
754 }
755 bool pkgCache::DepIterator::IsSatisfied(PrvIterator const &Prv) const
756 {
757  return Owner->VS->CheckDep(Prv.ProvideVersion(),S2->CompareOp,TargetVer());
758 }
759  /*}}}*/
760 // DepIterator::IsImplicit - added by the cache generation /*{{{*/
761 bool pkgCache::DepIterator::IsImplicit() const
762 {
763  if (IsMultiArchImplicit() == true)
764  return true;
765  if (IsNegative() || S2->Type == pkgCache::Dep::Replaces)
766  {
767  if ((S2->CompareOp & pkgCache::Dep::ArchSpecific) != pkgCache::Dep::ArchSpecific &&
768  strcmp(ParentPkg().Arch(), TargetPkg().Arch()) != 0)
769  return true;
770  }
771  return false;
772 }
773  /*}}}*/
774 // ostream operator to handle string representation of a dependency /*{{{*/
775 // ---------------------------------------------------------------------
776 /* */
777 std::ostream& operator<<(std::ostream& out, pkgCache::DepIterator D)
778 {
779  if (D.end() == true)
780  return out << "invalid dependency";
781 
782  pkgCache::PkgIterator P = D.ParentPkg();
783  pkgCache::PkgIterator T = D.TargetPkg();
784 
785  out << (P.end() ? "invalid pkg" : P.FullName(false)) << " " << D.DepType()
786  << " on ";
788  if (T.end() == true)
789  out << "invalid pkg";
790  else
791  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 */
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::RelStr - Return the release string /*{{{*/
933 string pkgCache::RlsFileIterator::RelStr()
934 {
935  string Res;
936  if (Version() != 0)
937  Res = Res + (Res.empty() == true?"v=":",v=") + Version();
938  if (Origin() != 0)
939  Res = Res + (Res.empty() == true?"o=":",o=") + Origin();
940  if (Archive() != 0)
941  Res = Res + (Res.empty() == true?"a=":",a=") + Archive();
942  if (Codename() != 0)
943  Res = Res + (Res.empty() == true?"n=":",n=") + Codename();
944  if (Label() != 0)
945  Res = Res + (Res.empty() == true?"l=":",l=") + Label();
946  return Res;
947 }
948  /*}}}*/
949 string pkgCache::PkgFileIterator::RelStr() /*{{{*/
950 {
951  std::string Res;
952  if (ReleaseFile() == 0)
953  {
954  if (Component() != 0)
955  Res = Res + (Res.empty() == true?"a=":",a=") + Component();
956  }
957  else
958  {
959  Res = ReleaseFile().RelStr();
960  if (Component() != 0)
961  Res = Res + (Res.empty() == true?"c=":",c=") + Component();
962  }
963  if (Architecture() != 0)
964  Res = Res + (Res.empty() == true?"b=":",b=") + Architecture();
965  return Res;
966 }
967  /*}}}*/
968 // VerIterator::TranslatedDescriptionForLanguage - Return a DescIter for language/*{{{*/
969 // ---------------------------------------------------------------------
970 /* return a DescIter for the specified language
971  */
972 pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescriptionForLanguage(StringView lang) const
973 {
974  for (pkgCache::DescIterator Desc = DescriptionList(); Desc.end() == false; ++Desc)
975  if (lang == Desc.LanguageCode())
976  return Desc;
977 
978  if (lang == "en")
979  return TranslatedDescriptionForLanguage("");
980 
981  return DescIterator();
982 }
983 
984  /*}}}*/
985 // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
986 // ---------------------------------------------------------------------
987 /* return a DescIter for the current locale or the default if none is
988  * found
989  */
990 pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescription() const
991 {
992  std::vector<string> const lang = APT::Configuration::getLanguages();
993  for (std::vector<string>::const_iterator l = lang.begin();
994  l != lang.end(); ++l)
995  {
996  pkgCache::DescIterator Desc = TranslatedDescriptionForLanguage(*l);
997  if (Desc.IsGood())
998  return Desc;
999  }
1000 
1001  pkgCache::DescIterator Desc = TranslatedDescriptionForLanguage("");
1002  if (Desc.IsGood())
1003  return Desc;
1004 
1005  return DescriptionList();
1006 }
1007 
1008  /*}}}*/
1009 
static bool std::string const metaIndex const *const pkgAcqMetaClearSig *const pkgAcquire::Item *const I
Simple subset of std::string_view from C++17.
Definition: string_view.h:27
constexpr const char * begin() const
Definition: string_view.h:134
constexpr const char * end() const
Definition: string_view.h:135
int FindI(const char *Name, int const &Default=0) const
std::string Find(const char *Name, const char *Default=0) const
bool FindB(const char *Name, bool const &Default=false) const
Definition: mmap.h:41
void * Data()
Definition: mmap.h:63
unsigned long long Size()
Definition: mmap.h:64
map_id_t Hash(APT::StringView S) const
Definition: pkgcache.h:242
Provides * ProvideP
Definition: pkgcache.h:230
ReleaseFile * RlsFileP
Definition: pkgcache.h:226
void ReMap(void const *const oldMap, void *const newMap)
Dependency * DepP
Definition: pkgcache.h:231
PkgIterator FindPkg(APT::StringView Arch=APT::StringView("any", 3)) const
DependencyData * DepDataP
Definition: pkgcache.h:232
map_id_t sHash(APT::StringView S) const APT_PURE
Definition: pkgcache.cc:209
Package * PkgP
Definition: pkgcache.h:223
pkgVersioningSystem * VS
Definition: pkgcache.h:275
Version * VerP
Definition: pkgcache.h:228
Description * DescP
Definition: pkgcache.h:229
Group * GrpP
Definition: pkgcache.h:222
virtual ~pkgCache()
Definition: pkgcache.cc:1010
static const char * Priority(unsigned char Priority)
Definition: pkgcache.cc:353
DescFile * DescFileP
Definition: pkgcache.h:225
Header * HeaderP
Definition: pkgcache.h:221
const char * CompType() const
MMap & Map
Definition: pkgcache.h:215
char * StrP
Definition: pkgcache.h:233
MMap & GetMap()
Definition: pkgcache.h:238
enum pkgCache::@1 Type
bool MultiArchEnabled
Definition: pkgcache.h:287
PackageFile * PkgFileP
Definition: pkgcache.h:227
APT::StringView ViewString(map_stringitem_t idx) const
Definition: pkgcache.h:254
GrpIterator FindGrp(APT::StringView Name)
Definition: pkgcache.cc:296
VerFile * VerFileP
Definition: pkgcache.h:224
const char * Name() const
const char * DepType() const
void *const d
Definition: pkgcache.h:286
pkgCache(MMap *Map, bool DoMap=true)
Definition: pkgcache.cc:128
const char * Arch() const
static const char * CompTypeDeb(unsigned char Comp) APT_PURE
Definition: pkgcache.cc:317
APT_HIDDEN uint32_t CacheHash()
Definition: pkgcache.cc:230
static pkgVersioningSystem * GetVS(const char *Label) APT_PURE
Definition: version.cc:34
Configuration * _config
#define APT_ARRAY_SIZE(a)
Definition: macros.h:18
#define APT_IGNORE_DEPRECATED_PUSH
Definition: macros.h:103
#define APT_IGNORE_DEPRECATED_POP
Definition: macros.h:104
#define APT_PURE
Definition: macros.h:56
APT_PUBLIC std::vector< std::string > const getArchitectures(bool const &Cached=true)
Returns a vector of Architectures we support.
APT_PUBLIC std::vector< std::string > const getLanguages(bool const &All=false, bool const &Cached=true, char const **const Locale=0)
Returns a vector of Language Codes.
PackageContainer< std::list< pkgCache::PkgIterator > > PackageList
Definition: cacheset.h:681
static int StringViewCompareFast(StringView a, StringView b)
Faster comparison for string views (compare size before data)
Definition: string_view.h:145
static std::string PkgFileIteratorToRelString(pkgCache::PkgFileIterator const &File)
Definition: pkgcache.cc:870
std::ostream & operator<<(std::ostream &out, pkgCache::PkgIterator Pkg)
Definition: pkgcache.cc:522
#define APT_HEADER_SET(X, Y)
pkgCache - Structure definitions for the cache file
uint32_t map_id_t
Definition: pkgcache.h:93
information for a single dependency record
Definition: pkgcache.h:738
associates a description with a Translation file
Definition: pkgcache.h:607
datamember of a linked list of available description for a version
Definition: pkgcache.h:707
groups architecture depending packages together
Definition: pkgcache.h:415
map_stringitem_t Name
Name of the group.
Definition: pkgcache.h:417
map_pointer< Group > Next
Link to the next Group.
Definition: pkgcache.h:426
uint32_t GetHashTableSize() const
Definition: pkgcache.h:386
map_id_t ProvidesCount
Definition: pkgcache.h:344
map_id_t DependsDataCount
Definition: pkgcache.h:339
map_number_t PackageSz
Definition: pkgcache.h:318
map_number_t DescFileSz
Definition: pkgcache.h:327
map_number_t MajorVersion
Definition: pkgcache.h:300
bool CheckSizes(Header &Against) const APT_PURE
Definition: pkgcache.cc:106
uint32_t Signature
Signature information.
Definition: pkgcache.h:298
map_stringitem_t GetArchitectures() const
Definition: pkgcache.h:388
map_number_t GroupSz
Definition: pkgcache.h:317
map_fileid_t DescFileCount
Definition: pkgcache.h:343
DynamicMMap::Pool Pools[2 *12]
The Pool structures manage the allocation pools that the generator uses.
Definition: pkgcache.h:373
void SetHashTableSize(unsigned int const sz)
Definition: pkgcache.h:387
map_number_t ReleaseFileSz
Definition: pkgcache.h:319
map_fileid_t ReleaseFileCount
Definition: pkgcache.h:340
map_filesize_small_t CacheFileSize
Hash of the file (TODO: Rename)
Definition: pkgcache.h:397
map_number_t PackageFileSz
Definition: pkgcache.h:320
map_id_t DescriptionCount
Definition: pkgcache.h:337
map_number_t VersionSz
Definition: pkgcache.h:321
map_number_t DescriptionSz
Definition: pkgcache.h:322
map_number_t ProvidesSz
Definition: pkgcache.h:325
uint16_t HeaderSz
Size of structure values.
Definition: pkgcache.h:316
map_stringitem_t VerSysName
String representing the version system used.
Definition: pkgcache.h:355
map_filesize_t MaxVerFileSize
The maximum size of a raw entry from the original Package file.
Definition: pkgcache.h:361
map_number_t VerFileSz
Definition: pkgcache.h:326
map_id_t VersionCount
Definition: pkgcache.h:336
void SetArchitectures(map_stringitem_t const idx)
Definition: pkgcache.h:389
map_number_t DependencyDataSz
Definition: pkgcache.h:324
map_filesize_t MaxDescFileSize
The maximum size of a raw entry from the original Translation file.
Definition: pkgcache.h:363
map_pointer< ReleaseFile > RlsFileList
index of the first ReleaseFile structure
Definition: pkgcache.h:352
map_id_t PackageCount
Definition: pkgcache.h:335
map_fileid_t VerFileCount
Definition: pkgcache.h:342
map_stringitem_t Architecture
native architecture the cache was built against
Definition: pkgcache.h:357
map_number_t DependencySz
Definition: pkgcache.h:323
map_fileid_t PackageFileCount
Definition: pkgcache.h:341
map_id_t DependsCount
Definition: pkgcache.h:338
map_number_t MinorVersion
Definition: pkgcache.h:301
map_id_t GroupCount
Structure counts.
Definition: pkgcache.h:334
map_pointer< PackageFile > FileList
index of the first PackageFile structure
Definition: pkgcache.h:350
bool Dirty
indicates if the cache should be erased
Definition: pkgcache.h:307
stores information about the files used to generate the cache
Definition: pkgcache.h:552
contains information for a single unique package
Definition: pkgcache.h:450
handles virtual packages
Definition: pkgcache.h:780
stores information about the release files used to generate the cache
Definition: pkgcache.h:508
associates a version with a PackageFile
Definition: pkgcache.h:593
information for a single version of a package
Definition: pkgcache.h:625
static int tolower_ascii_unsafe(int const c)
Definition: strutl.h:132