pkgcache.cc 36 KB

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