pkgcache.cc 15 KB

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