]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
231fea14 | 3 | // $Id: pkgcache.cc,v 1.30 1999/12/05 05:37:45 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> | |
231fea14 | 30 | #include <apt-pkg/strutl.h> |
578bfd0a AL |
31 | #include <system.h> |
32 | ||
33 | #include <string> | |
34 | #include <sys/stat.h> | |
35 | #include <unistd.h> | |
36 | /*}}}*/ | |
37 | ||
38 | // Cache::Header::Header - Constructor /*{{{*/ | |
39 | // --------------------------------------------------------------------- | |
40 | /* Simply initialize the header */ | |
41 | pkgCache::Header::Header() | |
42 | { | |
43 | Signature = 0x98FE76DC; | |
44 | ||
45 | /* Whenever the structures change the major version should be bumped, | |
46 | whenever the generator changes the minor version should be bumped. */ | |
204fbdcc | 47 | MajorVersion = 3; |
231fea14 | 48 | MinorVersion = 5; |
578bfd0a AL |
49 | Dirty = true; |
50 | ||
51 | HeaderSz = sizeof(pkgCache::Header); | |
52 | PackageSz = sizeof(pkgCache::Package); | |
53 | PackageFileSz = sizeof(pkgCache::PackageFile); | |
54 | VersionSz = sizeof(pkgCache::Version); | |
55 | DependencySz = sizeof(pkgCache::Dependency); | |
56 | ProvidesSz = sizeof(pkgCache::Provides); | |
dcb79bae AL |
57 | VerFileSz = sizeof(pkgCache::VerFile); |
58 | ||
578bfd0a AL |
59 | PackageCount = 0; |
60 | VersionCount = 0; | |
61 | DependsCount = 0; | |
62 | PackageFileCount = 0; | |
a7e66b17 AL |
63 | VerFileCount = 0; |
64 | ProvidesCount = 0; | |
ad00ae81 | 65 | MaxVerFileSize = 0; |
578bfd0a AL |
66 | |
67 | FileList = 0; | |
68 | StringList = 0; | |
69 | memset(HashTable,0,sizeof(HashTable)); | |
70 | memset(Pools,0,sizeof(Pools)); | |
71 | } | |
72 | /*}}}*/ | |
73 | // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/ | |
74 | // --------------------------------------------------------------------- | |
75 | /* */ | |
76 | bool pkgCache::Header::CheckSizes(Header &Against) const | |
77 | { | |
78 | if (HeaderSz == Against.HeaderSz && | |
79 | PackageSz == Against.PackageSz && | |
80 | PackageFileSz == Against.PackageFileSz && | |
81 | VersionSz == Against.VersionSz && | |
dcb79bae AL |
82 | DependencySz == Against.DependencySz && |
83 | VerFileSz == Against.VerFileSz && | |
578bfd0a AL |
84 | ProvidesSz == Against.ProvidesSz) |
85 | return true; | |
86 | return false; | |
87 | } | |
88 | /*}}}*/ | |
89 | ||
90 | // Cache::pkgCache - Constructor /*{{{*/ | |
91 | // --------------------------------------------------------------------- | |
92 | /* */ | |
93 | pkgCache::pkgCache(MMap &Map) : Map(Map) | |
94 | { | |
95 | ReMap(); | |
96 | } | |
97 | /*}}}*/ | |
98 | // Cache::ReMap - Reopen the cache file /*{{{*/ | |
99 | // --------------------------------------------------------------------- | |
100 | /* If the file is already closed then this will open it open it. */ | |
101 | bool pkgCache::ReMap() | |
102 | { | |
103 | // Apply the typecasts. | |
104 | HeaderP = (Header *)Map.Data(); | |
105 | PkgP = (Package *)Map.Data(); | |
dcb79bae | 106 | VerFileP = (VerFile *)Map.Data(); |
578bfd0a AL |
107 | PkgFileP = (PackageFile *)Map.Data(); |
108 | VerP = (Version *)Map.Data(); | |
109 | ProvideP = (Provides *)Map.Data(); | |
110 | DepP = (Dependency *)Map.Data(); | |
111 | StringItemP = (StringItem *)Map.Data(); | |
112 | StrP = (char *)Map.Data(); | |
113 | ||
578bfd0a AL |
114 | if (Map.Size() == 0) |
115 | return false; | |
116 | ||
117 | // Check the header | |
118 | Header DefHeader; | |
119 | if (HeaderP->Signature != DefHeader.Signature || | |
120 | HeaderP->Dirty == true) | |
121 | return _error->Error("The package cache file is corrupted"); | |
122 | ||
123 | if (HeaderP->MajorVersion != DefHeader.MajorVersion || | |
124 | HeaderP->MinorVersion != DefHeader.MinorVersion || | |
125 | HeaderP->CheckSizes(DefHeader) == false) | |
126 | return _error->Error("The package cache file is an incompatible version"); | |
127 | ||
128 | return true; | |
129 | } | |
130 | /*}}}*/ | |
131 | // Cache::Hash - Hash a string /*{{{*/ | |
132 | // --------------------------------------------------------------------- | |
133 | /* This is used to generate the hash entries for the HashTable. With my | |
134 | package list from bo this function gets 94% table usage on a 512 item | |
135 | table (480 used items) */ | |
f9eec0e7 | 136 | unsigned long pkgCache::sHash(string Str) const |
578bfd0a AL |
137 | { |
138 | unsigned long Hash = 0; | |
139 | for (const char *I = Str.begin(); I != Str.end(); I++) | |
231fea14 | 140 | Hash = 5*Hash + tolower(*I); |
f9eec0e7 | 141 | return Hash % _count(HeaderP->HashTable); |
578bfd0a AL |
142 | } |
143 | ||
f9eec0e7 | 144 | unsigned long pkgCache::sHash(const char *Str) const |
578bfd0a AL |
145 | { |
146 | unsigned long Hash = 0; | |
f9eec0e7 | 147 | for (const char *I = Str; *I != 0; I++) |
231fea14 | 148 | Hash = 5*Hash + tolower(*I); |
f9eec0e7 | 149 | return Hash % _count(HeaderP->HashTable); |
578bfd0a AL |
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 | { | |
c1a22377 | 162 | if (Pkg->Name != 0 && StrP[Pkg->Name] == Name[0] && |
231fea14 | 163 | stringcasecmp(Name.begin(),Name.end(),StrP + Pkg->Name) == 0) |
578bfd0a AL |
164 | return PkgIterator(*this,Pkg); |
165 | } | |
166 | return PkgIterator(*this,0); | |
167 | } | |
168 | /*}}}*/ | |
0149949b AL |
169 | // Cache::Priority - Convert a priority value to a string /*{{{*/ |
170 | // --------------------------------------------------------------------- | |
171 | /* */ | |
172 | const char *pkgCache::Priority(unsigned char Prio) | |
173 | { | |
174 | const char *Mapping[] = {0,"important","required","standard","optional","extra"}; | |
175 | if (Prio < _count(Mapping)) | |
176 | return Mapping[Prio]; | |
177 | return 0; | |
178 | } | |
179 | /*}}}*/ | |
1fcbfcb8 AL |
180 | // Cache::GetCandidateVer - Returns the Candidate install version /*{{{*/ |
181 | // --------------------------------------------------------------------- | |
182 | /* The default just returns the highest available version that is not | |
183 | a source and automatic */ | |
184 | pkgCache::VerIterator pkgCache::GetCandidateVer(PkgIterator Pkg, | |
185 | bool AllowCurrent) | |
186 | { | |
187 | /* Not source/not automatic versions cannot be a candidate version | |
188 | unless they are already installed */ | |
189 | VerIterator Last(*this,0); | |
190 | ||
191 | for (VerIterator I = Pkg.VersionList(); I.end() == false; I++) | |
192 | { | |
193 | if (Pkg.CurrentVer() == I && AllowCurrent == true) | |
194 | return I; | |
195 | ||
196 | for (VerFileIterator J = I.FileList(); J.end() == false; J++) | |
197 | { | |
198 | if ((J.File()->Flags & Flag::NotSource) != 0) | |
199 | continue; | |
200 | ||
201 | /* Stash the highest version of a not-automatic source, we use it | |
202 | if there is nothing better */ | |
203 | if ((J.File()->Flags & Flag::NotAutomatic) != 0) | |
204 | { | |
205 | if (Last.end() == true) | |
206 | Last = I; | |
207 | continue; | |
208 | } | |
209 | ||
210 | return I; | |
211 | } | |
212 | } | |
213 | ||
214 | return Last; | |
215 | } | |
216 | /*}}}*/ | |
578bfd0a | 217 | |
f55a958f AL |
218 | // Bases for iterator classes /*{{{*/ |
219 | void pkgCache::VerIterator::_dummy() {} | |
220 | void pkgCache::DepIterator::_dummy() {} | |
221 | void pkgCache::PrvIterator::_dummy() {} | |
222 | /*}}}*/ | |
223 | // PkgIterator::operator ++ - Postfix incr /*{{{*/ | |
578bfd0a AL |
224 | // --------------------------------------------------------------------- |
225 | /* This will advance to the next logical package in the hash table. */ | |
226 | void pkgCache::PkgIterator::operator ++(int) | |
227 | { | |
228 | // Follow the current links | |
229 | if (Pkg != Owner->PkgP) | |
230 | Pkg = Owner->PkgP + Pkg->NextPackage; | |
231 | ||
232 | // Follow the hash table | |
233 | while (Pkg == Owner->PkgP && HashIndex < (signed)_count(Owner->HeaderP->HashTable)) | |
234 | { | |
235 | HashIndex++; | |
236 | Pkg = Owner->PkgP + Owner->HeaderP->HashTable[HashIndex]; | |
237 | } | |
238 | }; | |
239 | /*}}}*/ | |
578bfd0a AL |
240 | // PkgIterator::State - Check the State of the package /*{{{*/ |
241 | // --------------------------------------------------------------------- | |
242 | /* By this we mean if it is either cleanly installed or cleanly removed. */ | |
243 | pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const | |
d38b7b3d | 244 | { |
c7c1b0f6 AL |
245 | if (Pkg->InstState == pkgCache::State::ReInstReq || |
246 | Pkg->InstState == pkgCache::State::HoldReInstReq) | |
247 | return NeedsUnpack; | |
248 | ||
7d4f285b AL |
249 | if (Pkg->CurrentState == pkgCache::State::UnPacked || |
250 | Pkg->CurrentState == pkgCache::State::HalfConfigured) | |
578bfd0a AL |
251 | return NeedsConfigure; |
252 | ||
a005475e | 253 | if (Pkg->CurrentState == pkgCache::State::HalfInstalled || |
7d4f285b | 254 | Pkg->InstState != pkgCache::State::Ok) |
578bfd0a AL |
255 | return NeedsUnpack; |
256 | ||
257 | return NeedsNothing; | |
258 | } | |
259 | /*}}}*/ | |
260 | // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/ | |
261 | // --------------------------------------------------------------------- | |
262 | /* Currently critical deps are defined as depends, predepends and | |
263 | conflicts. */ | |
264 | bool pkgCache::DepIterator::IsCritical() | |
265 | { | |
f07b5628 AL |
266 | if (Dep->Type == pkgCache::Dep::Conflicts || |
267 | Dep->Type == pkgCache::Dep::Depends || | |
268 | Dep->Type == pkgCache::Dep::PreDepends) | |
578bfd0a AL |
269 | return true; |
270 | return false; | |
271 | } | |
272 | /*}}}*/ | |
273 | // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/ | |
274 | // --------------------------------------------------------------------- | |
275 | /* This intellegently looks at dep target packages and tries to figure | |
276 | out which package should be used. This is needed to nicely handle | |
277 | provide mapping. If the target package has no other providing packages | |
278 | then it returned. Otherwise the providing list is looked at to | |
279 | see if there is one one unique providing package if so it is returned. | |
280 | Otherwise true is returned and the target package is set. The return | |
281 | result indicates whether the node should be expandable */ | |
282 | bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) | |
283 | { | |
284 | Result = TargetPkg(); | |
285 | ||
286 | // No provides at all | |
287 | if (Result->ProvidesList == 0) | |
288 | return false; | |
289 | ||
290 | // There is the Base package and the providing ones which is at least 2 | |
291 | if (Result->VersionList != 0) | |
292 | return true; | |
293 | ||
294 | /* We have to skip over indirect provisions of the package that | |
295 | owns the dependency. For instance, if libc5-dev depends on the | |
296 | virtual package libc-dev which is provided by libc5-dev and libc6-dev | |
297 | we must ignore libc5-dev when considering the provides list. */ | |
298 | PrvIterator PStart = Result.ProvidesList(); | |
299 | for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++); | |
300 | ||
301 | // Nothing but indirect self provides | |
302 | if (PStart.end() == true) | |
303 | return false; | |
304 | ||
305 | // Check for single packages in the provides list | |
306 | PrvIterator P = PStart; | |
307 | for (; P.end() != true; P++) | |
308 | { | |
309 | // Skip over self provides | |
310 | if (P.OwnerPkg() == ParentPkg()) | |
311 | continue; | |
312 | if (PStart.OwnerPkg() != P.OwnerPkg()) | |
313 | break; | |
314 | } | |
315 | ||
316 | // Check for non dups | |
317 | if (P.end() != true) | |
318 | return true; | |
319 | Result = PStart.OwnerPkg(); | |
320 | return false; | |
321 | } | |
322 | /*}}}*/ | |
323 | // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/ | |
324 | // --------------------------------------------------------------------- | |
325 | /* This is a more usefull version of TargetPkg() that follows versioned | |
326 | provides. It includes every possible package-version that could satisfy | |
fbfb2a7c AL |
327 | the dependency. The last item in the list has a 0. The resulting pointer |
328 | must be delete [] 'd */ | |
578bfd0a AL |
329 | pkgCache::Version **pkgCache::DepIterator::AllTargets() |
330 | { | |
331 | Version **Res = 0; | |
332 | unsigned long Size =0; | |
333 | while (1) | |
334 | { | |
335 | Version **End = Res; | |
336 | PkgIterator DPkg = TargetPkg(); | |
337 | ||
338 | // Walk along the actual package providing versions | |
339 | for (VerIterator I = DPkg.VersionList(); I.end() == false; I++) | |
340 | { | |
341 | if (pkgCheckDep(TargetVer(),I.VerStr(),Dep->CompareOp) == false) | |
342 | continue; | |
343 | ||
f07b5628 AL |
344 | if (Dep->Type == pkgCache::Dep::Conflicts && |
345 | ParentPkg() == I.ParentPkg()) | |
578bfd0a AL |
346 | continue; |
347 | ||
348 | Size++; | |
349 | if (Res != 0) | |
350 | *End++ = I; | |
351 | } | |
352 | ||
353 | // Follow all provides | |
354 | for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++) | |
355 | { | |
356 | if (pkgCheckDep(TargetVer(),I.ProvideVersion(),Dep->CompareOp) == false) | |
357 | continue; | |
358 | ||
f07b5628 AL |
359 | if (Dep->Type == pkgCache::Dep::Conflicts && |
360 | ParentPkg() == I.OwnerPkg()) | |
578bfd0a AL |
361 | continue; |
362 | ||
363 | Size++; | |
364 | if (Res != 0) | |
365 | *End++ = I.OwnerVer(); | |
366 | } | |
367 | ||
368 | // Do it again and write it into the array | |
369 | if (Res == 0) | |
370 | { | |
371 | Res = new Version *[Size+1]; | |
372 | Size = 0; | |
373 | } | |
374 | else | |
375 | { | |
376 | *End = 0; | |
377 | break; | |
378 | } | |
379 | } | |
380 | ||
381 | return Res; | |
382 | } | |
383 | /*}}}*/ | |
0a8e3465 AL |
384 | // DepIterator::CompType - Return a string describing the compare type /*{{{*/ |
385 | // --------------------------------------------------------------------- | |
386 | /* This returns a string representation of the dependency compare | |
387 | type */ | |
388 | const char *pkgCache::DepIterator::CompType() | |
389 | { | |
390 | const char *Ops[] = {"","<=",">=","<",">","=","!="}; | |
43d017d6 | 391 | if ((unsigned)(Dep->CompareOp & 0xF) < 7) |
0a8e3465 AL |
392 | return Ops[Dep->CompareOp & 0xF]; |
393 | return ""; | |
394 | } | |
395 | /*}}}*/ | |
43d017d6 AL |
396 | // DepIterator::DepType - Return a string describing the dep type /*{{{*/ |
397 | // --------------------------------------------------------------------- | |
398 | /* */ | |
399 | const char *pkgCache::DepIterator::DepType() | |
400 | { | |
401 | const char *Types[] = {"","Depends","PreDepends","Suggests", | |
402 | "Recommends","Conflicts","Replaces"}; | |
403 | if (Dep->Type < 7) | |
404 | return Types[Dep->Type]; | |
405 | return ""; | |
406 | } | |
407 | /*}}}*/ | |
408 | // DepIterator::GlobOr - Compute an OR group /*{{{*/ | |
409 | // --------------------------------------------------------------------- | |
410 | /* This Takes an iterator, iterates past the current dependency grouping | |
411 | and returns Start and End so that so End is the final element | |
412 | in the group, if End == Start then D is End++ and End is the | |
413 | dependency D was pointing to. Use in loops to iterate sensibly. */ | |
414 | void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End) | |
415 | { | |
416 | // Compute a single dependency element (glob or) | |
417 | Start = *this; | |
418 | End = *this; | |
018f1533 | 419 | for (bool LastOR = true; end() == false && LastOR == true;) |
43d017d6 AL |
420 | { |
421 | LastOR = (Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; | |
018f1533 | 422 | (*this)++; |
43d017d6 AL |
423 | if (LastOR == true) |
424 | End = (*this); | |
425 | } | |
426 | } | |
427 | /*}}}*/ | |
578bfd0a AL |
428 | // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/ |
429 | // --------------------------------------------------------------------- | |
430 | /* This just looks over the version list to see if B is listed before A. In | |
431 | most cases this will return in under 4 checks, ver lists are short. */ | |
432 | int pkgCache::VerIterator::CompareVer(const VerIterator &B) const | |
433 | { | |
434 | // Check if they are equal | |
435 | if (*this == B) | |
436 | return 0; | |
437 | if (end() == true) | |
438 | return -1; | |
439 | if (B.end() == true) | |
440 | return 1; | |
441 | ||
442 | /* Start at A and look for B. If B is found then A > B otherwise | |
443 | B was before A so A < B */ | |
444 | VerIterator I = *this; | |
445 | for (;I.end() == false; I++) | |
446 | if (I == B) | |
447 | return 1; | |
448 | return -1; | |
449 | } | |
450 | /*}}}*/ | |
b518cca6 AL |
451 | // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/ |
452 | // --------------------------------------------------------------------- | |
453 | /* */ | |
454 | bool pkgCache::VerIterator::Downloadable() const | |
455 | { | |
456 | VerFileIterator Files = FileList(); | |
457 | for (; Files.end() == false; Files++) | |
f07b5628 | 458 | if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource) |
b518cca6 AL |
459 | return true; |
460 | return false; | |
461 | } | |
462 | /*}}}*/ | |
c9807169 AL |
463 | // VerIterator::PriorityType - Return a string describing the priority /*{{{*/ |
464 | // --------------------------------------------------------------------- | |
465 | /* */ | |
466 | const char *pkgCache::VerIterator::PriorityType() | |
467 | { | |
468 | const char *Types[] = {"","Important","Required","Standard", | |
469 | "Optional","Extra"}; | |
470 | if (Ver->Priority < 6) | |
471 | return Types[Ver->Priority]; | |
472 | return ""; | |
473 | } | |
474 | /*}}}*/ | |
3c124dde AL |
475 | // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/ |
476 | // --------------------------------------------------------------------- | |
477 | /* This checks to see if any of the versions files are not NotAutomatic. | |
478 | True if this version is selectable for automatic installation. */ | |
479 | bool pkgCache::VerIterator::Automatic() const | |
480 | { | |
481 | VerFileIterator Files = FileList(); | |
482 | for (; Files.end() == false; Files++) | |
483 | if ((Files.File()->Flags & pkgCache::Flag::NotAutomatic) != pkgCache::Flag::NotAutomatic) | |
484 | return true; | |
485 | return false; | |
486 | } | |
487 | /*}}}*/ | |
488 | // VerIterator::NewestFile - Return the newest file version relation /*{{{*/ | |
489 | // --------------------------------------------------------------------- | |
490 | /* This looks at the version numbers associated with all of the sources | |
491 | this version is in and returns the highest.*/ | |
492 | pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const | |
493 | { | |
494 | VerFileIterator Files = FileList(); | |
495 | VerFileIterator Highest = Files; | |
496 | for (; Files.end() == false; Files++) | |
497 | { | |
498 | if (pkgVersionCompare(Files.File().Version(),Highest.File().Version()) > 0) | |
499 | Highest = Files; | |
500 | } | |
501 | ||
502 | return Highest; | |
503 | } | |
504 | /*}}}*/ | |
578bfd0a AL |
505 | // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/ |
506 | // --------------------------------------------------------------------- | |
507 | /* This stats the file and compares its stats with the ones that were | |
508 | stored during generation. Date checks should probably also be | |
509 | included here. */ | |
510 | bool pkgCache::PkgFileIterator::IsOk() | |
511 | { | |
512 | struct stat Buf; | |
513 | if (stat(FileName(),&Buf) != 0) | |
514 | return false; | |
515 | ||
516 | if (Buf.st_size != (signed)File->Size || Buf.st_mtime != File->mtime) | |
517 | return false; | |
518 | ||
519 | return true; | |
520 | } | |
521 | /*}}}*/ |