pkgcache.cc 17 KB

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