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