You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

910 lines
31 KiB

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