pkgcache.cc 28 KB

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