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

630 lines
21 KiB

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