pkgcache.cc 36 KB

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