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