pkgcache.cc 36 KB

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