pkgcache.cc 37 KB

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