pkgcache.cc 36 KB

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