pkgcache.cc 37 KB

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