pkgcache.cc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcache.cc,v 1.34 2001/04/29 05:13:51 jgg 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. #ifdef __GNUG__
  19. #pragma implementation "apt-pkg/pkgcache.h"
  20. #pragma implementation "apt-pkg/cacheiterators.h"
  21. #endif
  22. #include <apt-pkg/pkgcache.h>
  23. #include <apt-pkg/version.h>
  24. #include <apt-pkg/error.h>
  25. #include <apt-pkg/strutl.h>
  26. #include <apt-pkg/configuration.h>
  27. #include <apti18n.h>
  28. #include <string>
  29. #include <sys/stat.h>
  30. #include <unistd.h>
  31. #include <system.h>
  32. /*}}}*/
  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 = 3;
  42. MinorVersion = 5;
  43. Dirty = false;
  44. HeaderSz = sizeof(pkgCache::Header);
  45. PackageSz = sizeof(pkgCache::Package);
  46. PackageFileSz = sizeof(pkgCache::PackageFile);
  47. VersionSz = sizeof(pkgCache::Version);
  48. DependencySz = sizeof(pkgCache::Dependency);
  49. ProvidesSz = sizeof(pkgCache::Provides);
  50. VerFileSz = sizeof(pkgCache::VerFile);
  51. PackageCount = 0;
  52. VersionCount = 0;
  53. DependsCount = 0;
  54. PackageFileCount = 0;
  55. VerFileCount = 0;
  56. ProvidesCount = 0;
  57. MaxVerFileSize = 0;
  58. FileList = 0;
  59. StringList = 0;
  60. VerSysName = 0;
  61. Architecture = 0;
  62. memset(HashTable,0,sizeof(HashTable));
  63. memset(Pools,0,sizeof(Pools));
  64. }
  65. /*}}}*/
  66. // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
  67. // ---------------------------------------------------------------------
  68. /* */
  69. bool pkgCache::Header::CheckSizes(Header &Against) const
  70. {
  71. if (HeaderSz == Against.HeaderSz &&
  72. PackageSz == Against.PackageSz &&
  73. PackageFileSz == Against.PackageFileSz &&
  74. VersionSz == Against.VersionSz &&
  75. DependencySz == Against.DependencySz &&
  76. VerFileSz == Against.VerFileSz &&
  77. ProvidesSz == Against.ProvidesSz)
  78. return true;
  79. return false;
  80. }
  81. /*}}}*/
  82. // Cache::pkgCache - Constructor /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* */
  85. pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map)
  86. {
  87. if (DoMap == true)
  88. ReMap();
  89. }
  90. /*}}}*/
  91. // Cache::ReMap - Reopen the cache file /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* If the file is already closed then this will open it open it. */
  94. bool pkgCache::ReMap()
  95. {
  96. // Apply the typecasts.
  97. HeaderP = (Header *)Map.Data();
  98. PkgP = (Package *)Map.Data();
  99. VerFileP = (VerFile *)Map.Data();
  100. PkgFileP = (PackageFile *)Map.Data();
  101. VerP = (Version *)Map.Data();
  102. ProvideP = (Provides *)Map.Data();
  103. DepP = (Dependency *)Map.Data();
  104. StringItemP = (StringItem *)Map.Data();
  105. StrP = (char *)Map.Data();
  106. if (Map.Size() == 0 || HeaderP == 0)
  107. return _error->Error(_("Empty package cache"));
  108. // Check the header
  109. Header DefHeader;
  110. if (HeaderP->Signature != DefHeader.Signature ||
  111. HeaderP->Dirty == true)
  112. return _error->Error(_("The package cache file is corrupted"));
  113. if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
  114. HeaderP->MinorVersion != DefHeader.MinorVersion ||
  115. HeaderP->CheckSizes(DefHeader) == false)
  116. return _error->Error(_("The package cache file is an incompatible version"));
  117. // Locate our VS..
  118. if (HeaderP->VerSysName == 0 ||
  119. (VS = pkgVersioningSystem::GetVS(StrP + HeaderP->VerSysName)) == 0)
  120. return _error->Error(_("This APT does not support the Versioning System '%s'"),StrP + HeaderP->VerSysName);
  121. // Chcek the arhcitecture
  122. if (HeaderP->Architecture == 0 ||
  123. _config->Find("APT::Architecture") != StrP + HeaderP->Architecture)
  124. return _error->Error(_("The package cache was build for a different architecture"));
  125. return true;
  126. }
  127. /*}}}*/
  128. // Cache::Hash - Hash a string /*{{{*/
  129. // ---------------------------------------------------------------------
  130. /* This is used to generate the hash entries for the HashTable. With my
  131. package list from bo this function gets 94% table usage on a 512 item
  132. table (480 used items) */
  133. unsigned long pkgCache::sHash(string Str) const
  134. {
  135. unsigned long Hash = 0;
  136. for (const char *I = Str.begin(); I != Str.end(); I++)
  137. Hash = 5*Hash + tolower(*I);
  138. return Hash % _count(HeaderP->HashTable);
  139. }
  140. unsigned long pkgCache::sHash(const char *Str) const
  141. {
  142. unsigned long Hash = 0;
  143. for (const char *I = Str; *I != 0; I++)
  144. Hash = 5*Hash + tolower(*I);
  145. return Hash % _count(HeaderP->HashTable);
  146. }
  147. /*}}}*/
  148. // Cache::FindPkg - Locate a package by name /*{{{*/
  149. // ---------------------------------------------------------------------
  150. /* Returns 0 on error, pointer to the package otherwise */
  151. pkgCache::PkgIterator pkgCache::FindPkg(string Name)
  152. {
  153. // Look at the hash bucket
  154. Package *Pkg = PkgP + HeaderP->HashTable[Hash(Name)];
  155. for (; Pkg != PkgP; Pkg = PkgP + Pkg->NextPackage)
  156. {
  157. if (Pkg->Name != 0 && StrP[Pkg->Name] == Name[0] &&
  158. stringcasecmp(Name.begin(),Name.end(),StrP + Pkg->Name) == 0)
  159. return PkgIterator(*this,Pkg);
  160. }
  161. return PkgIterator(*this,0);
  162. }
  163. /*}}}*/
  164. // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* This returns a string representation of the dependency compare
  167. type in the weird debian style.. */
  168. const char *pkgCache::CompTypeDeb(unsigned char Comp)
  169. {
  170. const char *Ops[] = {"","<=",">=","<<",">>","=","!="};
  171. if ((unsigned)(Comp & 0xF) < 7)
  172. return Ops[Comp & 0xF];
  173. return "";
  174. }
  175. /*}}}*/
  176. // Cache::CompType - Return a string describing the compare type /*{{{*/
  177. // ---------------------------------------------------------------------
  178. /* This returns a string representation of the dependency compare
  179. type */
  180. const char *pkgCache::CompType(unsigned char Comp)
  181. {
  182. const char *Ops[] = {"","<=",">=","<",">","=","!="};
  183. if ((unsigned)(Comp & 0xF) < 7)
  184. return Ops[Comp & 0xF];
  185. return "";
  186. }
  187. /*}}}*/
  188. // Cache::DepType - Return a string describing the dep type /*{{{*/
  189. // ---------------------------------------------------------------------
  190. /* */
  191. const char *pkgCache::DepType(unsigned char Type)
  192. {
  193. const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
  194. _("Recommends"),_("Conflicts"),_("Replaces"),
  195. _("Obsoletes")};
  196. if (Type < 8)
  197. return Types[Type];
  198. return "";
  199. }
  200. /*}}}*/
  201. // Cache::Priority - Convert a priority value to a string /*{{{*/
  202. // ---------------------------------------------------------------------
  203. /* */
  204. const char *pkgCache::Priority(unsigned char Prio)
  205. {
  206. const char *Mapping[] = {0,_("important"),_("required"),_("standard"),
  207. _("optional"),_("extra")};
  208. if (Prio < _count(Mapping))
  209. return Mapping[Prio];
  210. return 0;
  211. }
  212. /*}}}*/
  213. // Bases for iterator classes /*{{{*/
  214. void pkgCache::VerIterator::_dummy() {}
  215. void pkgCache::DepIterator::_dummy() {}
  216. void pkgCache::PrvIterator::_dummy() {}
  217. /*}}}*/
  218. // PkgIterator::operator ++ - Postfix incr /*{{{*/
  219. // ---------------------------------------------------------------------
  220. /* This will advance to the next logical package in the hash table. */
  221. void pkgCache::PkgIterator::operator ++(int)
  222. {
  223. // Follow the current links
  224. if (Pkg != Owner->PkgP)
  225. Pkg = Owner->PkgP + Pkg->NextPackage;
  226. // Follow the hash table
  227. while (Pkg == Owner->PkgP && (HashIndex+1) < (signed)_count(Owner->HeaderP->HashTable))
  228. {
  229. HashIndex++;
  230. Pkg = Owner->PkgP + Owner->HeaderP->HashTable[HashIndex];
  231. }
  232. };
  233. /*}}}*/
  234. // PkgIterator::State - Check the State of the package /*{{{*/
  235. // ---------------------------------------------------------------------
  236. /* By this we mean if it is either cleanly installed or cleanly removed. */
  237. pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
  238. {
  239. if (Pkg->InstState == pkgCache::State::ReInstReq ||
  240. Pkg->InstState == pkgCache::State::HoldReInstReq)
  241. return NeedsUnpack;
  242. if (Pkg->CurrentState == pkgCache::State::UnPacked ||
  243. Pkg->CurrentState == pkgCache::State::HalfConfigured)
  244. return NeedsConfigure;
  245. if (Pkg->CurrentState == pkgCache::State::HalfInstalled ||
  246. Pkg->InstState != pkgCache::State::Ok)
  247. return NeedsUnpack;
  248. return NeedsNothing;
  249. }
  250. /*}}}*/
  251. // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
  252. // ---------------------------------------------------------------------
  253. /* Currently critical deps are defined as depends, predepends and
  254. conflicts. */
  255. bool pkgCache::DepIterator::IsCritical()
  256. {
  257. if (Dep->Type == pkgCache::Dep::Conflicts ||
  258. Dep->Type == pkgCache::Dep::Obsoletes ||
  259. Dep->Type == pkgCache::Dep::Depends ||
  260. Dep->Type == pkgCache::Dep::PreDepends)
  261. return true;
  262. return false;
  263. }
  264. /*}}}*/
  265. // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
  266. // ---------------------------------------------------------------------
  267. /* This intellegently looks at dep target packages and tries to figure
  268. out which package should be used. This is needed to nicely handle
  269. provide mapping. If the target package has no other providing packages
  270. then it returned. Otherwise the providing list is looked at to
  271. see if there is one one unique providing package if so it is returned.
  272. Otherwise true is returned and the target package is set. The return
  273. result indicates whether the node should be expandable
  274. In Conjunction with the DepCache the value of Result may not be
  275. super-good since the policy may have made it uninstallable. Using
  276. AllTargets is better in this case. */
  277. bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result)
  278. {
  279. Result = TargetPkg();
  280. // No provides at all
  281. if (Result->ProvidesList == 0)
  282. return false;
  283. // There is the Base package and the providing ones which is at least 2
  284. if (Result->VersionList != 0)
  285. return true;
  286. /* We have to skip over indirect provisions of the package that
  287. owns the dependency. For instance, if libc5-dev depends on the
  288. virtual package libc-dev which is provided by libc5-dev and libc6-dev
  289. we must ignore libc5-dev when considering the provides list. */
  290. PrvIterator PStart = Result.ProvidesList();
  291. for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++);
  292. // Nothing but indirect self provides
  293. if (PStart.end() == true)
  294. return false;
  295. // Check for single packages in the provides list
  296. PrvIterator P = PStart;
  297. for (; P.end() != true; P++)
  298. {
  299. // Skip over self provides
  300. if (P.OwnerPkg() == ParentPkg())
  301. continue;
  302. if (PStart.OwnerPkg() != P.OwnerPkg())
  303. break;
  304. }
  305. Result = PStart.OwnerPkg();
  306. // Check for non dups
  307. if (P.end() != true)
  308. return true;
  309. return false;
  310. }
  311. /*}}}*/
  312. // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
  313. // ---------------------------------------------------------------------
  314. /* This is a more useful version of TargetPkg() that follows versioned
  315. provides. It includes every possible package-version that could satisfy
  316. the dependency. The last item in the list has a 0. The resulting pointer
  317. must be delete [] 'd */
  318. pkgCache::Version **pkgCache::DepIterator::AllTargets()
  319. {
  320. Version **Res = 0;
  321. unsigned long Size =0;
  322. while (1)
  323. {
  324. Version **End = Res;
  325. PkgIterator DPkg = TargetPkg();
  326. // Walk along the actual package providing versions
  327. for (VerIterator I = DPkg.VersionList(); I.end() == false; I++)
  328. {
  329. if (Owner->VS->CheckDep(I.VerStr(),Dep->CompareOp,TargetVer()) == false)
  330. continue;
  331. if ((Dep->Type == pkgCache::Dep::Conflicts ||
  332. Dep->Type == pkgCache::Dep::Obsoletes) &&
  333. ParentPkg() == I.ParentPkg())
  334. continue;
  335. Size++;
  336. if (Res != 0)
  337. *End++ = I;
  338. }
  339. // Follow all provides
  340. for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++)
  341. {
  342. if (Owner->VS->CheckDep(I.ProvideVersion(),Dep->CompareOp,TargetVer()) == false)
  343. continue;
  344. if ((Dep->Type == pkgCache::Dep::Conflicts ||
  345. Dep->Type == pkgCache::Dep::Obsoletes) &&
  346. ParentPkg() == I.OwnerPkg())
  347. continue;
  348. Size++;
  349. if (Res != 0)
  350. *End++ = I.OwnerVer();
  351. }
  352. // Do it again and write it into the array
  353. if (Res == 0)
  354. {
  355. Res = new Version *[Size+1];
  356. Size = 0;
  357. }
  358. else
  359. {
  360. *End = 0;
  361. break;
  362. }
  363. }
  364. return Res;
  365. }
  366. /*}}}*/
  367. // DepIterator::GlobOr - Compute an OR group /*{{{*/
  368. // ---------------------------------------------------------------------
  369. /* This Takes an iterator, iterates past the current dependency grouping
  370. and returns Start and End so that so End is the final element
  371. in the group, if End == Start then D is End++ and End is the
  372. dependency D was pointing to. Use in loops to iterate sensibly. */
  373. void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
  374. {
  375. // Compute a single dependency element (glob or)
  376. Start = *this;
  377. End = *this;
  378. for (bool LastOR = true; end() == false && LastOR == true;)
  379. {
  380. LastOR = (Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
  381. (*this)++;
  382. if (LastOR == true)
  383. End = (*this);
  384. }
  385. }
  386. /*}}}*/
  387. // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
  388. // ---------------------------------------------------------------------
  389. /* This just looks over the version list to see if B is listed before A. In
  390. most cases this will return in under 4 checks, ver lists are short. */
  391. int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
  392. {
  393. // Check if they are equal
  394. if (*this == B)
  395. return 0;
  396. if (end() == true)
  397. return -1;
  398. if (B.end() == true)
  399. return 1;
  400. /* Start at A and look for B. If B is found then A > B otherwise
  401. B was before A so A < B */
  402. VerIterator I = *this;
  403. for (;I.end() == false; I++)
  404. if (I == B)
  405. return 1;
  406. return -1;
  407. }
  408. /*}}}*/
  409. // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
  410. // ---------------------------------------------------------------------
  411. /* */
  412. bool pkgCache::VerIterator::Downloadable() const
  413. {
  414. VerFileIterator Files = FileList();
  415. for (; Files.end() == false; Files++)
  416. if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource)
  417. return true;
  418. return false;
  419. }
  420. /*}}}*/
  421. // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
  422. // ---------------------------------------------------------------------
  423. /* This checks to see if any of the versions files are not NotAutomatic.
  424. True if this version is selectable for automatic installation. */
  425. bool pkgCache::VerIterator::Automatic() const
  426. {
  427. VerFileIterator Files = FileList();
  428. for (; Files.end() == false; Files++)
  429. if ((Files.File()->Flags & pkgCache::Flag::NotAutomatic) != pkgCache::Flag::NotAutomatic)
  430. return true;
  431. return false;
  432. }
  433. /*}}}*/
  434. // VerIterator::NewestFile - Return the newest file version relation /*{{{*/
  435. // ---------------------------------------------------------------------
  436. /* This looks at the version numbers associated with all of the sources
  437. this version is in and returns the highest.*/
  438. pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const
  439. {
  440. VerFileIterator Files = FileList();
  441. VerFileIterator Highest = Files;
  442. for (; Files.end() == false; Files++)
  443. {
  444. if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0)
  445. Highest = Files;
  446. }
  447. return Highest;
  448. }
  449. /*}}}*/
  450. // VerIterator::RelStr - Release description string /*{{{*/
  451. // ---------------------------------------------------------------------
  452. /* This describes the version from a release-centric manner. The output is a
  453. list of Label:Version/Archive */
  454. string pkgCache::VerIterator::RelStr()
  455. {
  456. bool First = true;
  457. string Res;
  458. for (pkgCache::VerFileIterator I = this->FileList(); I.end() == false; I++)
  459. {
  460. // Do not print 'not source' entries'
  461. pkgCache::PkgFileIterator File = I.File();
  462. if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
  463. continue;
  464. // See if we have already printed this out..
  465. bool Seen = false;
  466. for (pkgCache::VerFileIterator J = this->FileList(); I != J; J++)
  467. {
  468. pkgCache::PkgFileIterator File2 = J.File();
  469. if (File2->Label == 0 || File->Label == 0)
  470. continue;
  471. if (strcmp(File.Label(),File2.Label()) != 0)
  472. continue;
  473. if (File2->Version == File->Version)
  474. {
  475. Seen = true;
  476. break;
  477. }
  478. if (File2->Version == 0 || File->Version == 0)
  479. break;
  480. if (strcmp(File.Version(),File2.Version()) == 0)
  481. Seen = true;
  482. }
  483. if (Seen == true)
  484. continue;
  485. if (First == false)
  486. Res += ", ";
  487. else
  488. First = false;
  489. if (File->Label != 0)
  490. Res = Res + File.Label() + ':';
  491. if (File->Archive != 0)
  492. {
  493. if (File->Version == 0)
  494. Res += File.Archive();
  495. else
  496. Res = Res + File.Version() + '/' + File.Archive();
  497. }
  498. else
  499. {
  500. // No release file, print the host name that this came from
  501. if (File->Site == 0 || File.Site()[0] == 0)
  502. Res += "localhost";
  503. else
  504. Res += File.Site();
  505. }
  506. }
  507. return Res;
  508. }
  509. /*}}}*/
  510. // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
  511. // ---------------------------------------------------------------------
  512. /* This stats the file and compares its stats with the ones that were
  513. stored during generation. Date checks should probably also be
  514. included here. */
  515. bool pkgCache::PkgFileIterator::IsOk()
  516. {
  517. struct stat Buf;
  518. if (stat(FileName(),&Buf) != 0)
  519. return false;
  520. if (Buf.st_size != (signed)File->Size || Buf.st_mtime != File->mtime)
  521. return false;
  522. return true;
  523. }
  524. /*}}}*/
  525. // PkgFileIterator::RelStr - Return the release string /*{{{*/
  526. // ---------------------------------------------------------------------
  527. /* */
  528. string pkgCache::PkgFileIterator::RelStr()
  529. {
  530. string Res;
  531. if (Version() != 0)
  532. Res = Res + (Res.empty() == true?"v=":",v=") + Version();
  533. if (Origin() != 0)
  534. Res = Res + (Res.empty() == true?"o=":",o=") + Origin();
  535. if (Archive() != 0)
  536. Res = Res + (Res.empty() == true?"a=":",a=") + Archive();
  537. if (Label() != 0)
  538. Res = Res + (Res.empty() == true?"l=":",l=") + Label();
  539. if (Component() != 0)
  540. Res = Res + (Res.empty() == true?"c=":",c=") + Component();
  541. return Res;
  542. }
  543. /*}}}*/