pkgcache.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcache.cc,v 1.7 1998/07/12 23:58:32 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 = 0;
  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. FileList = 0;
  53. StringList = 0;
  54. memset(HashTable,0,sizeof(HashTable));
  55. memset(Pools,0,sizeof(Pools));
  56. }
  57. /*}}}*/
  58. // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* */
  61. bool pkgCache::Header::CheckSizes(Header &Against) const
  62. {
  63. if (HeaderSz == Against.HeaderSz &&
  64. PackageSz == Against.PackageSz &&
  65. PackageFileSz == Against.PackageFileSz &&
  66. VersionSz == Against.VersionSz &&
  67. DependencySz == Against.DependencySz &&
  68. VerFileSz == Against.VerFileSz &&
  69. ProvidesSz == Against.ProvidesSz)
  70. return true;
  71. return false;
  72. }
  73. /*}}}*/
  74. // Cache::pkgCache - Constructor /*{{{*/
  75. // ---------------------------------------------------------------------
  76. /* */
  77. pkgCache::pkgCache(MMap &Map) : Map(Map)
  78. {
  79. ReMap();
  80. }
  81. /*}}}*/
  82. // Cache::ReMap - Reopen the cache file /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* If the file is already closed then this will open it open it. */
  85. bool pkgCache::ReMap()
  86. {
  87. // Apply the typecasts.
  88. HeaderP = (Header *)Map.Data();
  89. PkgP = (Package *)Map.Data();
  90. VerFileP = (VerFile *)Map.Data();
  91. PkgFileP = (PackageFile *)Map.Data();
  92. VerP = (Version *)Map.Data();
  93. ProvideP = (Provides *)Map.Data();
  94. DepP = (Dependency *)Map.Data();
  95. StringItemP = (StringItem *)Map.Data();
  96. StrP = (char *)Map.Data();
  97. if (Map.Size() == 0)
  98. return false;
  99. // Check the header
  100. Header DefHeader;
  101. if (HeaderP->Signature != DefHeader.Signature ||
  102. HeaderP->Dirty == true)
  103. return _error->Error("The package cache file is corrupted");
  104. if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
  105. HeaderP->MinorVersion != DefHeader.MinorVersion ||
  106. HeaderP->CheckSizes(DefHeader) == false)
  107. return _error->Error("The package cache file is an incompatible version");
  108. return true;
  109. }
  110. /*}}}*/
  111. // Cache::Hash - Hash a string /*{{{*/
  112. // ---------------------------------------------------------------------
  113. /* This is used to generate the hash entries for the HashTable. With my
  114. package list from bo this function gets 94% table usage on a 512 item
  115. table (480 used items) */
  116. unsigned long pkgCache::sHash(string Str)
  117. {
  118. unsigned long Hash = 0;
  119. for (const char *I = Str.begin(); I != Str.end(); I++)
  120. Hash += *I * ((Str.end() - I + 1));
  121. Header H;
  122. return Hash % _count(H.HashTable);
  123. }
  124. unsigned long pkgCache::sHash(const char *Str)
  125. {
  126. unsigned long Hash = 0;
  127. const char *End = Str + strlen(Str);
  128. for (const char *I = Str; I != End; I++)
  129. Hash += *I * ((End - I + 1));
  130. Header H;
  131. return Hash % _count(H.HashTable);
  132. }
  133. /*}}}*/
  134. // Cache::FindPkg - Locate a package by name /*{{{*/
  135. // ---------------------------------------------------------------------
  136. /* Returns 0 on error, pointer to the package otherwise */
  137. pkgCache::PkgIterator pkgCache::FindPkg(string Name)
  138. {
  139. // Look at the hash bucket
  140. Package *Pkg = PkgP + HeaderP->HashTable[Hash(Name)];
  141. for (; Pkg != PkgP; Pkg = PkgP + Pkg->NextPackage)
  142. {
  143. if (Pkg->Name != 0 && StrP + Pkg->Name == Name)
  144. return PkgIterator(*this,Pkg);
  145. }
  146. return PkgIterator(*this,0);
  147. }
  148. /*}}}*/
  149. // Cache::Priority - Convert a priority value to a string /*{{{*/
  150. // ---------------------------------------------------------------------
  151. /* */
  152. const char *pkgCache::Priority(unsigned char Prio)
  153. {
  154. const char *Mapping[] = {0,"important","required","standard","optional","extra"};
  155. if (Prio < _count(Mapping))
  156. return Mapping[Prio];
  157. return 0;
  158. }
  159. /*}}}*/
  160. // Bases for iterator classes /*{{{*/
  161. void pkgCache::VerIterator::_dummy() {}
  162. void pkgCache::DepIterator::_dummy() {}
  163. void pkgCache::PrvIterator::_dummy() {}
  164. /*}}}*/
  165. // PkgIterator::operator ++ - Postfix incr /*{{{*/
  166. // ---------------------------------------------------------------------
  167. /* This will advance to the next logical package in the hash table. */
  168. void pkgCache::PkgIterator::operator ++(int)
  169. {
  170. // Follow the current links
  171. if (Pkg != Owner->PkgP)
  172. Pkg = Owner->PkgP + Pkg->NextPackage;
  173. // Follow the hash table
  174. while (Pkg == Owner->PkgP && HashIndex < (signed)_count(Owner->HeaderP->HashTable))
  175. {
  176. HashIndex++;
  177. Pkg = Owner->PkgP + Owner->HeaderP->HashTable[HashIndex];
  178. }
  179. };
  180. /*}}}*/
  181. // PkgIterator::State - Check the State of the package /*{{{*/
  182. // ---------------------------------------------------------------------
  183. /* By this we mean if it is either cleanly installed or cleanly removed. */
  184. pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
  185. {
  186. if (Pkg->CurrentState == State::UnPacked ||
  187. Pkg->CurrentState == State::HalfConfigured)
  188. return NeedsConfigure;
  189. if (Pkg->CurrentState == State::UnInstalled ||
  190. Pkg->CurrentState == State::HalfInstalled ||
  191. Pkg->InstState != State::Ok)
  192. return NeedsUnpack;
  193. return NeedsNothing;
  194. }
  195. /*}}}*/
  196. // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
  197. // ---------------------------------------------------------------------
  198. /* Currently critical deps are defined as depends, predepends and
  199. conflicts. */
  200. bool pkgCache::DepIterator::IsCritical()
  201. {
  202. if (Dep->Type == Dep::Conflicts || Dep->Type == Dep::Depends ||
  203. Dep->Type == Dep::PreDepends)
  204. return true;
  205. return false;
  206. }
  207. /*}}}*/
  208. // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
  209. // ---------------------------------------------------------------------
  210. /* This intellegently looks at dep target packages and tries to figure
  211. out which package should be used. This is needed to nicely handle
  212. provide mapping. If the target package has no other providing packages
  213. then it returned. Otherwise the providing list is looked at to
  214. see if there is one one unique providing package if so it is returned.
  215. Otherwise true is returned and the target package is set. The return
  216. result indicates whether the node should be expandable */
  217. bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result)
  218. {
  219. Result = TargetPkg();
  220. // No provides at all
  221. if (Result->ProvidesList == 0)
  222. return false;
  223. // There is the Base package and the providing ones which is at least 2
  224. if (Result->VersionList != 0)
  225. return true;
  226. /* We have to skip over indirect provisions of the package that
  227. owns the dependency. For instance, if libc5-dev depends on the
  228. virtual package libc-dev which is provided by libc5-dev and libc6-dev
  229. we must ignore libc5-dev when considering the provides list. */
  230. PrvIterator PStart = Result.ProvidesList();
  231. for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++);
  232. // Nothing but indirect self provides
  233. if (PStart.end() == true)
  234. return false;
  235. // Check for single packages in the provides list
  236. PrvIterator P = PStart;
  237. for (; P.end() != true; P++)
  238. {
  239. // Skip over self provides
  240. if (P.OwnerPkg() == ParentPkg())
  241. continue;
  242. if (PStart.OwnerPkg() != P.OwnerPkg())
  243. break;
  244. }
  245. // Check for non dups
  246. if (P.end() != true)
  247. return true;
  248. Result = PStart.OwnerPkg();
  249. return false;
  250. }
  251. /*}}}*/
  252. // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
  253. // ---------------------------------------------------------------------
  254. /* This is a more usefull version of TargetPkg() that follows versioned
  255. provides. It includes every possible package-version that could satisfy
  256. the dependency. The last item in the list has a 0. */
  257. pkgCache::Version **pkgCache::DepIterator::AllTargets()
  258. {
  259. Version **Res = 0;
  260. unsigned long Size =0;
  261. while (1)
  262. {
  263. Version **End = Res;
  264. PkgIterator DPkg = TargetPkg();
  265. // Walk along the actual package providing versions
  266. for (VerIterator I = DPkg.VersionList(); I.end() == false; I++)
  267. {
  268. if (pkgCheckDep(TargetVer(),I.VerStr(),Dep->CompareOp) == false)
  269. continue;
  270. if (Dep->Type == Dep::Conflicts && ParentPkg() == I.ParentPkg())
  271. continue;
  272. Size++;
  273. if (Res != 0)
  274. *End++ = I;
  275. }
  276. // Follow all provides
  277. for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++)
  278. {
  279. if (pkgCheckDep(TargetVer(),I.ProvideVersion(),Dep->CompareOp) == false)
  280. continue;
  281. if (Dep->Type == Dep::Conflicts && ParentPkg() == I.OwnerPkg())
  282. continue;
  283. Size++;
  284. if (Res != 0)
  285. *End++ = I.OwnerVer();
  286. }
  287. // Do it again and write it into the array
  288. if (Res == 0)
  289. {
  290. Res = new Version *[Size+1];
  291. Size = 0;
  292. }
  293. else
  294. {
  295. *End = 0;
  296. break;
  297. }
  298. }
  299. return Res;
  300. }
  301. /*}}}*/
  302. // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
  303. // ---------------------------------------------------------------------
  304. /* This just looks over the version list to see if B is listed before A. In
  305. most cases this will return in under 4 checks, ver lists are short. */
  306. int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
  307. {
  308. // Check if they are equal
  309. if (*this == B)
  310. return 0;
  311. if (end() == true)
  312. return -1;
  313. if (B.end() == true)
  314. return 1;
  315. /* Start at A and look for B. If B is found then A > B otherwise
  316. B was before A so A < B */
  317. VerIterator I = *this;
  318. for (;I.end() == false; I++)
  319. if (I == B)
  320. return 1;
  321. return -1;
  322. }
  323. /*}}}*/
  324. // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
  325. // ---------------------------------------------------------------------
  326. /* */
  327. bool pkgCache::VerIterator::Downloadable() const
  328. {
  329. VerFileIterator Files = FileList();
  330. for (; Files.end() == false; Files++)
  331. if ((Files.File()->Flags & Flag::NotSource) != Flag::NotSource)
  332. return true;
  333. return false;
  334. }
  335. /*}}}*/
  336. // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
  337. // ---------------------------------------------------------------------
  338. /* This stats the file and compares its stats with the ones that were
  339. stored during generation. Date checks should probably also be
  340. included here. */
  341. bool pkgCache::PkgFileIterator::IsOk()
  342. {
  343. struct stat Buf;
  344. if (stat(FileName(),&Buf) != 0)
  345. return false;
  346. if (Buf.st_size != (signed)File->Size || Buf.st_mtime != File->mtime)
  347. return false;
  348. return true;
  349. }
  350. /*}}}*/