pkgcache.cc 31 KB

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