pkgcache.cc 30 KB

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