pkgcache.cc 36 KB

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