pkgcache.cc 36 KB

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