pkgcache.cc 36 KB

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