pkgcache.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: pkgcache.cc,v 1.8 1998/07/19 04:22:00 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. 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->CurrentState == State::UnPacked ||
  188. Pkg->CurrentState == State::HalfConfigured)
  189. return NeedsConfigure;
  190. if (Pkg->CurrentState == State::UnInstalled ||
  191. Pkg->CurrentState == State::HalfInstalled ||
  192. Pkg->InstState != State::Ok)
  193. return NeedsUnpack;
  194. return NeedsNothing;
  195. }
  196. /*}}}*/
  197. // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
  198. // ---------------------------------------------------------------------
  199. /* Currently critical deps are defined as depends, predepends and
  200. conflicts. */
  201. bool pkgCache::DepIterator::IsCritical()
  202. {
  203. if (Dep->Type == Dep::Conflicts || Dep->Type == Dep::Depends ||
  204. Dep->Type == Dep::PreDepends)
  205. return true;
  206. return false;
  207. }
  208. /*}}}*/
  209. // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
  210. // ---------------------------------------------------------------------
  211. /* This intellegently looks at dep target packages and tries to figure
  212. out which package should be used. This is needed to nicely handle
  213. provide mapping. If the target package has no other providing packages
  214. then it returned. Otherwise the providing list is looked at to
  215. see if there is one one unique providing package if so it is returned.
  216. Otherwise true is returned and the target package is set. The return
  217. result indicates whether the node should be expandable */
  218. bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result)
  219. {
  220. Result = TargetPkg();
  221. // No provides at all
  222. if (Result->ProvidesList == 0)
  223. return false;
  224. // There is the Base package and the providing ones which is at least 2
  225. if (Result->VersionList != 0)
  226. return true;
  227. /* We have to skip over indirect provisions of the package that
  228. owns the dependency. For instance, if libc5-dev depends on the
  229. virtual package libc-dev which is provided by libc5-dev and libc6-dev
  230. we must ignore libc5-dev when considering the provides list. */
  231. PrvIterator PStart = Result.ProvidesList();
  232. for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++);
  233. // Nothing but indirect self provides
  234. if (PStart.end() == true)
  235. return false;
  236. // Check for single packages in the provides list
  237. PrvIterator P = PStart;
  238. for (; P.end() != true; P++)
  239. {
  240. // Skip over self provides
  241. if (P.OwnerPkg() == ParentPkg())
  242. continue;
  243. if (PStart.OwnerPkg() != P.OwnerPkg())
  244. break;
  245. }
  246. // Check for non dups
  247. if (P.end() != true)
  248. return true;
  249. Result = PStart.OwnerPkg();
  250. return false;
  251. }
  252. /*}}}*/
  253. // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
  254. // ---------------------------------------------------------------------
  255. /* This is a more usefull version of TargetPkg() that follows versioned
  256. provides. It includes every possible package-version that could satisfy
  257. the dependency. The last item in the list has a 0. */
  258. pkgCache::Version **pkgCache::DepIterator::AllTargets()
  259. {
  260. Version **Res = 0;
  261. unsigned long Size =0;
  262. while (1)
  263. {
  264. Version **End = Res;
  265. PkgIterator DPkg = TargetPkg();
  266. // Walk along the actual package providing versions
  267. for (VerIterator I = DPkg.VersionList(); I.end() == false; I++)
  268. {
  269. if (pkgCheckDep(TargetVer(),I.VerStr(),Dep->CompareOp) == false)
  270. continue;
  271. if (Dep->Type == Dep::Conflicts && ParentPkg() == I.ParentPkg())
  272. continue;
  273. Size++;
  274. if (Res != 0)
  275. *End++ = I;
  276. }
  277. // Follow all provides
  278. for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++)
  279. {
  280. if (pkgCheckDep(TargetVer(),I.ProvideVersion(),Dep->CompareOp) == false)
  281. continue;
  282. if (Dep->Type == Dep::Conflicts && ParentPkg() == I.OwnerPkg())
  283. continue;
  284. Size++;
  285. if (Res != 0)
  286. *End++ = I.OwnerVer();
  287. }
  288. // Do it again and write it into the array
  289. if (Res == 0)
  290. {
  291. Res = new Version *[Size+1];
  292. Size = 0;
  293. }
  294. else
  295. {
  296. *End = 0;
  297. break;
  298. }
  299. }
  300. return Res;
  301. }
  302. /*}}}*/
  303. // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
  304. // ---------------------------------------------------------------------
  305. /* This just looks over the version list to see if B is listed before A. In
  306. most cases this will return in under 4 checks, ver lists are short. */
  307. int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
  308. {
  309. // Check if they are equal
  310. if (*this == B)
  311. return 0;
  312. if (end() == true)
  313. return -1;
  314. if (B.end() == true)
  315. return 1;
  316. /* Start at A and look for B. If B is found then A > B otherwise
  317. B was before A so A < B */
  318. VerIterator I = *this;
  319. for (;I.end() == false; I++)
  320. if (I == B)
  321. return 1;
  322. return -1;
  323. }
  324. /*}}}*/
  325. // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
  326. // ---------------------------------------------------------------------
  327. /* */
  328. bool pkgCache::VerIterator::Downloadable() const
  329. {
  330. VerFileIterator Files = FileList();
  331. for (; Files.end() == false; Files++)
  332. if ((Files.File()->Flags & Flag::NotSource) != Flag::NotSource)
  333. return true;
  334. return false;
  335. }
  336. /*}}}*/
  337. // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
  338. // ---------------------------------------------------------------------
  339. /* This stats the file and compares its stats with the ones that were
  340. stored during generation. Date checks should probably also be
  341. included here. */
  342. bool pkgCache::PkgFileIterator::IsOk()
  343. {
  344. struct stat Buf;
  345. if (stat(FileName(),&Buf) != 0)
  346. return false;
  347. if (Buf.st_size != (signed)File->Size || Buf.st_mtime != File->mtime)
  348. return false;
  349. return true;
  350. }
  351. /*}}}*/