]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcache.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcache.cc,v 1.37 2003/02/10 01:40:58 doogie Exp $
4 /* ######################################################################
6 Package Cache - Accessor code for the cache
8 Please see doc/apt-pkg/cache.sgml for a more detailed description of
9 this format. Also be sure to keep that file up-to-date!!
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.
17 The main class provides for ways to get package indexes and some
18 general lookup functions to start the iterators.
20 ##################################################################### */
22 // Include Files /*{{{*/
23 #include <apt-pkg/pkgcache.h>
24 #include <apt-pkg/indexfile.h>
25 #include <apt-pkg/version.h>
26 #include <apt-pkg/error.h>
27 #include <apt-pkg/strutl.h>
28 #include <apt-pkg/configuration.h>
43 // Cache::Header::Header - Constructor /*{{{*/
44 // ---------------------------------------------------------------------
45 /* Simply initialize the header */
46 pkgCache::Header::Header()
48 Signature
= 0x98FE76DC;
50 /* Whenever the structures change the major version should be bumped,
51 whenever the generator changes the minor version should be bumped. */
56 HeaderSz
= sizeof(pkgCache::Header
);
57 PackageSz
= sizeof(pkgCache::Package
);
58 PackageFileSz
= sizeof(pkgCache::PackageFile
);
59 VersionSz
= sizeof(pkgCache::Version
);
60 DescriptionSz
= sizeof(pkgCache::Description
);
61 DependencySz
= sizeof(pkgCache::Dependency
);
62 ProvidesSz
= sizeof(pkgCache::Provides
);
63 VerFileSz
= sizeof(pkgCache::VerFile
);
64 DescFileSz
= sizeof(pkgCache::DescFile
);
81 memset(HashTable
,0,sizeof(HashTable
));
82 memset(Pools
,0,sizeof(Pools
));
85 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
86 // ---------------------------------------------------------------------
88 bool pkgCache::Header::CheckSizes(Header
&Against
) const
90 if (HeaderSz
== Against
.HeaderSz
&&
91 PackageSz
== Against
.PackageSz
&&
92 PackageFileSz
== Against
.PackageFileSz
&&
93 VersionSz
== Against
.VersionSz
&&
94 DescriptionSz
== Against
.DescriptionSz
&&
95 DependencySz
== Against
.DependencySz
&&
96 VerFileSz
== Against
.VerFileSz
&&
97 DescFileSz
== Against
.DescFileSz
&&
98 ProvidesSz
== Against
.ProvidesSz
)
104 // Cache::pkgCache - Constructor /*{{{*/
105 // ---------------------------------------------------------------------
107 pkgCache::pkgCache(MMap
*Map
, bool DoMap
) : Map(*Map
)
113 // Cache::ReMap - Reopen the cache file /*{{{*/
114 // ---------------------------------------------------------------------
115 /* If the file is already closed then this will open it open it. */
116 bool pkgCache::ReMap()
118 // Apply the typecasts.
119 HeaderP
= (Header
*)Map
.Data();
120 PkgP
= (Package
*)Map
.Data();
121 VerFileP
= (VerFile
*)Map
.Data();
122 DescFileP
= (DescFile
*)Map
.Data();
123 PkgFileP
= (PackageFile
*)Map
.Data();
124 VerP
= (Version
*)Map
.Data();
125 DescP
= (Description
*)Map
.Data();
126 ProvideP
= (Provides
*)Map
.Data();
127 DepP
= (Dependency
*)Map
.Data();
128 StringItemP
= (StringItem
*)Map
.Data();
129 StrP
= (char *)Map
.Data();
131 if (Map
.Size() == 0 || HeaderP
== 0)
132 return _error
->Error(_("Empty package cache"));
136 if (HeaderP
->Signature
!= DefHeader
.Signature
||
137 HeaderP
->Dirty
== true)
138 return _error
->Error(_("The package cache file is corrupted"));
140 if (HeaderP
->MajorVersion
!= DefHeader
.MajorVersion
||
141 HeaderP
->MinorVersion
!= DefHeader
.MinorVersion
||
142 HeaderP
->CheckSizes(DefHeader
) == false)
143 return _error
->Error(_("The package cache file is an incompatible version"));
146 if (HeaderP
->VerSysName
== 0 ||
147 (VS
= pkgVersioningSystem::GetVS(StrP
+ HeaderP
->VerSysName
)) == 0)
148 return _error
->Error(_("This APT does not support the versioning system '%s'"),StrP
+ HeaderP
->VerSysName
);
150 // Chcek the arhcitecture
151 if (HeaderP
->Architecture
== 0 ||
152 _config
->Find("APT::Architecture") != StrP
+ HeaderP
->Architecture
)
153 return _error
->Error(_("The package cache was built for a different architecture"));
157 // Cache::Hash - Hash a string /*{{{*/
158 // ---------------------------------------------------------------------
159 /* This is used to generate the hash entries for the HashTable. With my
160 package list from bo this function gets 94% table usage on a 512 item
161 table (480 used items) */
162 unsigned long pkgCache::sHash(const string
&Str
) const
164 unsigned long Hash
= 0;
165 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); I
++)
166 Hash
= 5*Hash
+ tolower(*I
);
167 return Hash
% _count(HeaderP
->HashTable
);
170 unsigned long pkgCache::sHash(const char *Str
) const
172 unsigned long Hash
= 0;
173 for (const char *I
= Str
; *I
!= 0; I
++)
174 Hash
= 5*Hash
+ tolower(*I
);
175 return Hash
% _count(HeaderP
->HashTable
);
179 // Cache::FindPkg - Locate a package by name /*{{{*/
180 // ---------------------------------------------------------------------
181 /* Returns 0 on error, pointer to the package otherwise */
182 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
)
184 // Look at the hash bucket
185 Package
*Pkg
= PkgP
+ HeaderP
->HashTable
[Hash(Name
)];
186 for (; Pkg
!= PkgP
; Pkg
= PkgP
+ Pkg
->NextPackage
)
188 if (Pkg
->Name
!= 0 && StrP
[Pkg
->Name
] == Name
[0] &&
189 stringcasecmp(Name
,StrP
+ Pkg
->Name
) == 0)
190 return PkgIterator(*this,Pkg
);
192 return PkgIterator(*this,0);
195 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
196 // ---------------------------------------------------------------------
197 /* This returns a string representation of the dependency compare
198 type in the weird debian style.. */
199 const char *pkgCache::CompTypeDeb(unsigned char Comp
)
201 const char *Ops
[] = {"","<=",">=","<<",">>","=","!="};
202 if ((unsigned)(Comp
& 0xF) < 7)
203 return Ops
[Comp
& 0xF];
207 // Cache::CompType - Return a string describing the compare type /*{{{*/
208 // ---------------------------------------------------------------------
209 /* This returns a string representation of the dependency compare
211 const char *pkgCache::CompType(unsigned char Comp
)
213 const char *Ops
[] = {"","<=",">=","<",">","=","!="};
214 if ((unsigned)(Comp
& 0xF) < 7)
215 return Ops
[Comp
& 0xF];
219 // Cache::DepType - Return a string describing the dep type /*{{{*/
220 // ---------------------------------------------------------------------
222 const char *pkgCache::DepType(unsigned char Type
)
224 const char *Types
[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
225 _("Recommends"),_("Conflicts"),_("Replaces"),
232 // Cache::Priority - Convert a priority value to a string /*{{{*/
233 // ---------------------------------------------------------------------
235 const char *pkgCache::Priority(unsigned char Prio
)
237 const char *Mapping
[] = {0,_("important"),_("required"),_("standard"),
238 _("optional"),_("extra")};
239 if (Prio
< _count(Mapping
))
240 return Mapping
[Prio
];
244 // Bases for iterator classes /*{{{*/
245 void pkgCache::VerIterator::_dummy() {}
246 void pkgCache::DepIterator::_dummy() {}
247 void pkgCache::PrvIterator::_dummy() {}
248 void pkgCache::DescIterator::_dummy() {}
250 // PkgIterator::operator ++ - Postfix incr /*{{{*/
251 // ---------------------------------------------------------------------
252 /* This will advance to the next logical package in the hash table. */
253 void pkgCache::PkgIterator::operator ++(int)
255 // Follow the current links
256 if (Pkg
!= Owner
->PkgP
)
257 Pkg
= Owner
->PkgP
+ Pkg
->NextPackage
;
259 // Follow the hash table
260 while (Pkg
== Owner
->PkgP
&& (HashIndex
+1) < (signed)_count(Owner
->HeaderP
->HashTable
))
263 Pkg
= Owner
->PkgP
+ Owner
->HeaderP
->HashTable
[HashIndex
];
267 // PkgIterator::State - Check the State of the package /*{{{*/
268 // ---------------------------------------------------------------------
269 /* By this we mean if it is either cleanly installed or cleanly removed. */
270 pkgCache::PkgIterator::OkState
pkgCache::PkgIterator::State() const
272 if (Pkg
->InstState
== pkgCache::State::ReInstReq
||
273 Pkg
->InstState
== pkgCache::State::HoldReInstReq
)
276 if (Pkg
->CurrentState
== pkgCache::State::UnPacked
||
277 Pkg
->CurrentState
== pkgCache::State::HalfConfigured
)
278 return NeedsConfigure
;
280 if (Pkg
->CurrentState
== pkgCache::State::HalfInstalled
||
281 Pkg
->InstState
!= pkgCache::State::Ok
)
287 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
288 // ---------------------------------------------------------------------
289 /* Currently critical deps are defined as depends, predepends and
291 bool pkgCache::DepIterator::IsCritical()
293 if (Dep
->Type
== pkgCache::Dep::Conflicts
||
294 Dep
->Type
== pkgCache::Dep::Obsoletes
||
295 Dep
->Type
== pkgCache::Dep::Depends
||
296 Dep
->Type
== pkgCache::Dep::PreDepends
)
301 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
302 // ---------------------------------------------------------------------
303 /* This intellegently looks at dep target packages and tries to figure
304 out which package should be used. This is needed to nicely handle
305 provide mapping. If the target package has no other providing packages
306 then it returned. Otherwise the providing list is looked at to
307 see if there is one one unique providing package if so it is returned.
308 Otherwise true is returned and the target package is set. The return
309 result indicates whether the node should be expandable
311 In Conjunction with the DepCache the value of Result may not be
312 super-good since the policy may have made it uninstallable. Using
313 AllTargets is better in this case. */
314 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator
&Result
)
316 Result
= TargetPkg();
318 // No provides at all
319 if (Result
->ProvidesList
== 0)
322 // There is the Base package and the providing ones which is at least 2
323 if (Result
->VersionList
!= 0)
326 /* We have to skip over indirect provisions of the package that
327 owns the dependency. For instance, if libc5-dev depends on the
328 virtual package libc-dev which is provided by libc5-dev and libc6-dev
329 we must ignore libc5-dev when considering the provides list. */
330 PrvIterator PStart
= Result
.ProvidesList();
331 for (; PStart
.end() != true && PStart
.OwnerPkg() == ParentPkg(); PStart
++);
333 // Nothing but indirect self provides
334 if (PStart
.end() == true)
337 // Check for single packages in the provides list
338 PrvIterator P
= PStart
;
339 for (; P
.end() != true; P
++)
341 // Skip over self provides
342 if (P
.OwnerPkg() == ParentPkg())
344 if (PStart
.OwnerPkg() != P
.OwnerPkg())
348 Result
= PStart
.OwnerPkg();
350 // Check for non dups
357 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
358 // ---------------------------------------------------------------------
359 /* This is a more useful version of TargetPkg() that follows versioned
360 provides. It includes every possible package-version that could satisfy
361 the dependency. The last item in the list has a 0. The resulting pointer
362 must be delete [] 'd */
363 pkgCache::Version
**pkgCache::DepIterator::AllTargets()
366 unsigned long Size
=0;
370 PkgIterator DPkg
= TargetPkg();
372 // Walk along the actual package providing versions
373 for (VerIterator I
= DPkg
.VersionList(); I
.end() == false; I
++)
375 if (Owner
->VS
->CheckDep(I
.VerStr(),Dep
->CompareOp
,TargetVer()) == false)
378 if ((Dep
->Type
== pkgCache::Dep::Conflicts
||
379 Dep
->Type
== pkgCache::Dep::Obsoletes
) &&
380 ParentPkg() == I
.ParentPkg())
388 // Follow all provides
389 for (PrvIterator I
= DPkg
.ProvidesList(); I
.end() == false; I
++)
391 if (Owner
->VS
->CheckDep(I
.ProvideVersion(),Dep
->CompareOp
,TargetVer()) == false)
394 if ((Dep
->Type
== pkgCache::Dep::Conflicts
||
395 Dep
->Type
== pkgCache::Dep::Obsoletes
) &&
396 ParentPkg() == I
.OwnerPkg())
401 *End
++ = I
.OwnerVer();
404 // Do it again and write it into the array
407 Res
= new Version
*[Size
+1];
420 // DepIterator::GlobOr - Compute an OR group /*{{{*/
421 // ---------------------------------------------------------------------
422 /* This Takes an iterator, iterates past the current dependency grouping
423 and returns Start and End so that so End is the final element
424 in the group, if End == Start then D is End++ and End is the
425 dependency D was pointing to. Use in loops to iterate sensibly. */
426 void pkgCache::DepIterator::GlobOr(DepIterator
&Start
,DepIterator
&End
)
428 // Compute a single dependency element (glob or)
431 for (bool LastOR
= true; end() == false && LastOR
== true;)
433 LastOR
= (Dep
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
440 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
441 // ---------------------------------------------------------------------
442 /* This just looks over the version list to see if B is listed before A. In
443 most cases this will return in under 4 checks, ver lists are short. */
444 int pkgCache::VerIterator::CompareVer(const VerIterator
&B
) const
446 // Check if they are equal
454 /* Start at A and look for B. If B is found then A > B otherwise
455 B was before A so A < B */
456 VerIterator I
= *this;
457 for (;I
.end() == false; I
++)
463 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
464 // ---------------------------------------------------------------------
466 bool pkgCache::VerIterator::Downloadable() const
468 VerFileIterator Files
= FileList();
469 for (; Files
.end() == false; Files
++)
470 if ((Files
.File()->Flags
& pkgCache::Flag::NotSource
) != pkgCache::Flag::NotSource
)
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
481 VerFileIterator Files
= FileList();
482 for (; Files
.end() == false; Files
++)
483 if ((Files
.File()->Flags
& pkgCache::Flag::NotAutomatic
) != pkgCache::Flag::NotAutomatic
)
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
494 VerFileIterator Files
= FileList();
495 VerFileIterator Highest
= Files
;
496 for (; Files
.end() == false; Files
++)
498 if (Owner
->VS
->CmpReleaseVer(Files
.File().Version(),Highest
.File().Version()) > 0)
505 // VerIterator::RelStr - Release description string /*{{{*/
506 // ---------------------------------------------------------------------
507 /* This describes the version from a release-centric manner. The output is a
508 list of Label:Version/Archive */
509 string
pkgCache::VerIterator::RelStr()
513 for (pkgCache::VerFileIterator I
= this->FileList(); I
.end() == false; I
++)
515 // Do not print 'not source' entries'
516 pkgCache::PkgFileIterator File
= I
.File();
517 if ((File
->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
)
520 // See if we have already printed this out..
522 for (pkgCache::VerFileIterator J
= this->FileList(); I
!= J
; J
++)
524 pkgCache::PkgFileIterator File2
= J
.File();
525 if (File2
->Label
== 0 || File
->Label
== 0)
528 if (strcmp(File
.Label(),File2
.Label()) != 0)
531 if (File2
->Version
== File
->Version
)
536 if (File2
->Version
== 0 || File
->Version
== 0)
538 if (strcmp(File
.Version(),File2
.Version()) == 0)
550 if (File
->Label
!= 0)
551 Res
= Res
+ File
.Label() + ':';
553 if (File
->Archive
!= 0)
555 if (File
->Version
== 0)
556 Res
+= File
.Archive();
558 Res
= Res
+ File
.Version() + '/' + File
.Archive();
562 // No release file, print the host name that this came from
563 if (File
->Site
== 0 || File
.Site()[0] == 0)
572 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
573 // ---------------------------------------------------------------------
574 /* This stats the file and compares its stats with the ones that were
575 stored during generation. Date checks should probably also be
577 bool pkgCache::PkgFileIterator::IsOk()
580 if (stat(FileName(),&Buf
) != 0)
583 if (Buf
.st_size
!= (signed)File
->Size
|| Buf
.st_mtime
!= File
->mtime
)
589 // PkgFileIterator::RelStr - Return the release string /*{{{*/
590 // ---------------------------------------------------------------------
592 string
pkgCache::PkgFileIterator::RelStr()
596 Res
= Res
+ (Res
.empty() == true?"v=":",v=") + Version();
598 Res
= Res
+ (Res
.empty() == true?"o=":",o=") + Origin();
600 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Archive();
602 Res
= Res
+ (Res
.empty() == true?"l=":",l=") + Label();
603 if (Component() != 0)
604 Res
= Res
+ (Res
.empty() == true?"c=":",c=") + Component();
608 // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
609 // ---------------------------------------------------------------------
610 /* return a DescIter for the current locale or the default if none is
613 pkgCache::DescIterator
pkgCache::VerIterator::TranslatedDescription() const
615 pkgCache::DescIterator DescDefault
= DescriptionList();
616 pkgCache::DescIterator Desc
= DescDefault
;
617 for (; Desc
.end() == false; Desc
++)
618 if (pkgIndexFile::LanguageCode() == Desc
.LanguageCode())
620 if (Desc
.end() == true) Desc
= DescDefault
;