pkgcache.cc 36 KB

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