pkgcache.cc 31 KB

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