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