]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcache.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcache.cc,v 1.8 1998/07/19 04:22:00 jgg 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 /*{{{*/
24 #pragma implementation "apt-pkg/pkgcache.h"
25 #pragma implementation "apt-pkg/cacheiterators.h"
27 #include <apt-pkg/pkgcache.h>
28 #include <apt-pkg/version.h>
29 #include <apt-pkg/error.h>
37 // Cache::Header::Header - Constructor /*{{{*/
38 // ---------------------------------------------------------------------
39 /* Simply initialize the header */
40 pkgCache::Header::Header()
42 Signature
= 0x98FE76DC;
44 /* Whenever the structures change the major version should be bumped,
45 whenever the generator changes the minor version should be bumped. */
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
);
56 VerFileSz
= sizeof(pkgCache::VerFile
);
66 memset(HashTable
,0,sizeof(HashTable
));
67 memset(Pools
,0,sizeof(Pools
));
70 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
71 // ---------------------------------------------------------------------
73 bool pkgCache::Header::CheckSizes(Header
&Against
) const
75 if (HeaderSz
== Against
.HeaderSz
&&
76 PackageSz
== Against
.PackageSz
&&
77 PackageFileSz
== Against
.PackageFileSz
&&
78 VersionSz
== Against
.VersionSz
&&
79 DependencySz
== Against
.DependencySz
&&
80 VerFileSz
== Against
.VerFileSz
&&
81 ProvidesSz
== Against
.ProvidesSz
)
87 // Cache::pkgCache - Constructor /*{{{*/
88 // ---------------------------------------------------------------------
90 pkgCache::pkgCache(MMap
&Map
) : Map(Map
)
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()
100 // Apply the typecasts.
101 HeaderP
= (Header
*)Map
.Data();
102 PkgP
= (Package
*)Map
.Data();
103 VerFileP
= (VerFile
*)Map
.Data();
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();
116 if (HeaderP
->Signature
!= DefHeader
.Signature
||
117 HeaderP
->Dirty
== true)
118 return _error
->Error("The package cache file is corrupted");
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");
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
)
135 unsigned long Hash
= 0;
136 for (const char *I
= Str
.begin(); I
!= Str
.end(); I
++)
137 Hash
+= *I
* ((Str
.end() - I
+ 1));
139 return Hash
% _count(H
.HashTable
);
142 unsigned long pkgCache::sHash(const char *Str
)
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));
149 return Hash
% _count(H
.HashTable
);
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
)
158 // Look at the hash bucket
159 Package
*Pkg
= PkgP
+ HeaderP
->HashTable
[Hash(Name
)];
160 for (; Pkg
!= PkgP
; Pkg
= PkgP
+ Pkg
->NextPackage
)
162 if (Pkg
->Name
!= 0 && StrP
+ Pkg
->Name
== Name
)
163 return PkgIterator(*this,Pkg
);
165 return PkgIterator(*this,0);
168 // Cache::Priority - Convert a priority value to a string /*{{{*/
169 // ---------------------------------------------------------------------
171 const char *pkgCache::Priority(unsigned char Prio
)
173 const char *Mapping
[] = {0,"important","required","standard","optional","extra"};
174 if (Prio
< _count(Mapping
))
175 return Mapping
[Prio
];
180 // Bases for iterator classes /*{{{*/
181 void pkgCache::VerIterator::_dummy() {}
182 void pkgCache::DepIterator::_dummy() {}
183 void pkgCache::PrvIterator::_dummy() {}
185 // PkgIterator::operator ++ - Postfix incr /*{{{*/
186 // ---------------------------------------------------------------------
187 /* This will advance to the next logical package in the hash table. */
188 void pkgCache::PkgIterator::operator ++(int)
190 // Follow the current links
191 if (Pkg
!= Owner
->PkgP
)
192 Pkg
= Owner
->PkgP
+ Pkg
->NextPackage
;
194 // Follow the hash table
195 while (Pkg
== Owner
->PkgP
&& HashIndex
< (signed)_count(Owner
->HeaderP
->HashTable
))
198 Pkg
= Owner
->PkgP
+ Owner
->HeaderP
->HashTable
[HashIndex
];
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
207 if (Pkg
->CurrentState
== State::UnPacked
||
208 Pkg
->CurrentState
== State::HalfConfigured
)
209 return NeedsConfigure
;
211 if (Pkg
->CurrentState
== State::UnInstalled
||
212 Pkg
->CurrentState
== State::HalfInstalled
||
213 Pkg
->InstState
!= State::Ok
)
219 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
220 // ---------------------------------------------------------------------
221 /* Currently critical deps are defined as depends, predepends and
223 bool pkgCache::DepIterator::IsCritical()
225 if (Dep
->Type
== Dep::Conflicts
|| Dep
->Type
== Dep::Depends
||
226 Dep
->Type
== Dep::PreDepends
)
231 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
232 // ---------------------------------------------------------------------
233 /* This intellegently looks at dep target packages and tries to figure
234 out which package should be used. This is needed to nicely handle
235 provide mapping. If the target package has no other providing packages
236 then it returned. Otherwise the providing list is looked at to
237 see if there is one one unique providing package if so it is returned.
238 Otherwise true is returned and the target package is set. The return
239 result indicates whether the node should be expandable */
240 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator
&Result
)
242 Result
= TargetPkg();
244 // No provides at all
245 if (Result
->ProvidesList
== 0)
248 // There is the Base package and the providing ones which is at least 2
249 if (Result
->VersionList
!= 0)
252 /* We have to skip over indirect provisions of the package that
253 owns the dependency. For instance, if libc5-dev depends on the
254 virtual package libc-dev which is provided by libc5-dev and libc6-dev
255 we must ignore libc5-dev when considering the provides list. */
256 PrvIterator PStart
= Result
.ProvidesList();
257 for (; PStart
.end() != true && PStart
.OwnerPkg() == ParentPkg(); PStart
++);
259 // Nothing but indirect self provides
260 if (PStart
.end() == true)
263 // Check for single packages in the provides list
264 PrvIterator P
= PStart
;
265 for (; P
.end() != true; P
++)
267 // Skip over self provides
268 if (P
.OwnerPkg() == ParentPkg())
270 if (PStart
.OwnerPkg() != P
.OwnerPkg())
274 // Check for non dups
277 Result
= PStart
.OwnerPkg();
281 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
282 // ---------------------------------------------------------------------
283 /* This is a more usefull version of TargetPkg() that follows versioned
284 provides. It includes every possible package-version that could satisfy
285 the dependency. The last item in the list has a 0. */
286 pkgCache::Version
**pkgCache::DepIterator::AllTargets()
289 unsigned long Size
=0;
293 PkgIterator DPkg
= TargetPkg();
295 // Walk along the actual package providing versions
296 for (VerIterator I
= DPkg
.VersionList(); I
.end() == false; I
++)
298 if (pkgCheckDep(TargetVer(),I
.VerStr(),Dep
->CompareOp
) == false)
301 if (Dep
->Type
== Dep::Conflicts
&& ParentPkg() == I
.ParentPkg())
309 // Follow all provides
310 for (PrvIterator I
= DPkg
.ProvidesList(); I
.end() == false; I
++)
312 if (pkgCheckDep(TargetVer(),I
.ProvideVersion(),Dep
->CompareOp
) == false)
315 if (Dep
->Type
== Dep::Conflicts
&& ParentPkg() == I
.OwnerPkg())
320 *End
++ = I
.OwnerVer();
323 // Do it again and write it into the array
326 Res
= new Version
*[Size
+1];
339 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
340 // ---------------------------------------------------------------------
341 /* This just looks over the version list to see if B is listed before A. In
342 most cases this will return in under 4 checks, ver lists are short. */
343 int pkgCache::VerIterator::CompareVer(const VerIterator
&B
) const
345 // Check if they are equal
353 /* Start at A and look for B. If B is found then A > B otherwise
354 B was before A so A < B */
355 VerIterator I
= *this;
356 for (;I
.end() == false; I
++)
362 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
363 // ---------------------------------------------------------------------
365 bool pkgCache::VerIterator::Downloadable() const
367 VerFileIterator Files
= FileList();
368 for (; Files
.end() == false; Files
++)
369 if ((Files
.File()->Flags
& Flag::NotSource
) != Flag::NotSource
)
374 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
375 // ---------------------------------------------------------------------
376 /* This stats the file and compares its stats with the ones that were
377 stored during generation. Date checks should probably also be
379 bool pkgCache::PkgFileIterator::IsOk()
382 if (stat(FileName(),&Buf
) != 0)
385 if (Buf
.st_size
!= (signed)File
->Size
|| Buf
.st_mtime
!= File
->mtime
)