pkgcache.cc 36 KB

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