pkgcache.cc 36 KB

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