]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcache.cc
e2602c88f213ebf5140ee1906e93b7065ba4312a
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcache.cc,v 1.3 1998/07/04 22:32:11 jgg Exp $
4 /* ######################################################################
6 Package Cache - Accessor code for the cache
8 Please see doc/pkglib/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 <pkglib/pkgcache.h>
24 #include <pkglib/version.h>
25 #include <pkglib/error.h>
33 // Cache::Header::Header - Constructor /*{{{*/
34 // ---------------------------------------------------------------------
35 /* Simply initialize the header */
36 pkgCache::Header::Header()
38 Signature
= 0x98FE76DC;
40 /* Whenever the structures change the major version should be bumped,
41 whenever the generator changes the minor version should be bumped. */
46 HeaderSz
= sizeof(pkgCache::Header
);
47 PackageSz
= sizeof(pkgCache::Package
);
48 PackageFileSz
= sizeof(pkgCache::PackageFile
);
49 VersionSz
= sizeof(pkgCache::Version
);
50 DependencySz
= sizeof(pkgCache::Dependency
);
51 ProvidesSz
= sizeof(pkgCache::Provides
);
60 memset(HashTable
,0,sizeof(HashTable
));
61 memset(Pools
,0,sizeof(Pools
));
64 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
65 // ---------------------------------------------------------------------
67 bool pkgCache::Header::CheckSizes(Header
&Against
) const
69 if (HeaderSz
== Against
.HeaderSz
&&
70 PackageSz
== Against
.PackageSz
&&
71 PackageFileSz
== Against
.PackageFileSz
&&
72 VersionSz
== Against
.VersionSz
&&
73 DependencySz
== Against
.DependencySz
&&
74 ProvidesSz
== Against
.ProvidesSz
)
80 // Cache::pkgCache - Constructor /*{{{*/
81 // ---------------------------------------------------------------------
83 pkgCache::pkgCache(MMap
&Map
) : Map(Map
)
88 // Cache::ReMap - Reopen the cache file /*{{{*/
89 // ---------------------------------------------------------------------
90 /* If the file is already closed then this will open it open it. */
91 bool pkgCache::ReMap()
93 // Apply the typecasts.
94 HeaderP
= (Header
*)Map
.Data();
95 PkgP
= (Package
*)Map
.Data();
96 PkgFileP
= (PackageFile
*)Map
.Data();
97 VerP
= (Version
*)Map
.Data();
98 ProvideP
= (Provides
*)Map
.Data();
99 DepP
= (Dependency
*)Map
.Data();
100 StringItemP
= (StringItem
*)Map
.Data();
101 StrP
= (char *)Map
.Data();
103 cout
<< "Size is " << Map
.Size() << endl
;
109 if (HeaderP
->Signature
!= DefHeader
.Signature
||
110 HeaderP
->Dirty
== true)
111 return _error
->Error("The package cache file is corrupted");
113 if (HeaderP
->MajorVersion
!= DefHeader
.MajorVersion
||
114 HeaderP
->MinorVersion
!= DefHeader
.MinorVersion
||
115 HeaderP
->CheckSizes(DefHeader
) == false)
116 return _error
->Error("The package cache file is an incompatible version");
121 // Cache::Hash - Hash a string /*{{{*/
122 // ---------------------------------------------------------------------
123 /* This is used to generate the hash entries for the HashTable. With my
124 package list from bo this function gets 94% table usage on a 512 item
125 table (480 used items) */
126 unsigned long pkgCache::sHash(string Str
)
128 unsigned long Hash
= 0;
129 for (const char *I
= Str
.begin(); I
!= Str
.end(); I
++)
130 Hash
+= *I
* ((Str
.end() - I
+ 1));
132 return Hash
% _count(H
.HashTable
);
135 unsigned long pkgCache::sHash(const char *Str
)
137 unsigned long Hash
= 0;
138 const char *End
= Str
+ strlen(Str
);
139 for (const char *I
= Str
; I
!= End
; I
++)
140 Hash
+= *I
* ((End
- I
+ 1));
142 return Hash
% _count(H
.HashTable
);
146 // Cache::FindPkg - Locate a package by name /*{{{*/
147 // ---------------------------------------------------------------------
148 /* Returns 0 on error, pointer to the package otherwise */
149 pkgCache::PkgIterator
pkgCache::FindPkg(string Name
)
151 // Look at the hash bucket
152 Package
*Pkg
= PkgP
+ HeaderP
->HashTable
[Hash(Name
)];
153 for (; Pkg
!= PkgP
; Pkg
= PkgP
+ Pkg
->NextPackage
)
155 if (Pkg
->Name
!= 0 && StrP
+ Pkg
->Name
== Name
)
156 return PkgIterator(*this,Pkg
);
158 return PkgIterator(*this,0);
161 // Cache::Priority - Convert a priority value to a string /*{{{*/
162 // ---------------------------------------------------------------------
164 const char *pkgCache::Priority(unsigned char Prio
)
166 const char *Mapping
[] = {0,"important","required","standard","optional","extra"};
167 if (Prio
< _count(Mapping
))
168 return Mapping
[Prio
];
173 // Bases for iterator classes /*{{{*/
174 void pkgCache::VerIterator::_dummy() {}
175 void pkgCache::DepIterator::_dummy() {}
176 void pkgCache::PrvIterator::_dummy() {}
178 // PkgIterator::operator ++ - Postfix incr /*{{{*/
179 // ---------------------------------------------------------------------
180 /* This will advance to the next logical package in the hash table. */
181 void pkgCache::PkgIterator::operator ++(int)
183 // Follow the current links
184 if (Pkg
!= Owner
->PkgP
)
185 Pkg
= Owner
->PkgP
+ Pkg
->NextPackage
;
187 // Follow the hash table
188 while (Pkg
== Owner
->PkgP
&& HashIndex
< (signed)_count(Owner
->HeaderP
->HashTable
))
191 Pkg
= Owner
->PkgP
+ Owner
->HeaderP
->HashTable
[HashIndex
];
195 // PkgIterator::State - Check the State of the package /*{{{*/
196 // ---------------------------------------------------------------------
197 /* By this we mean if it is either cleanly installed or cleanly removed. */
198 pkgCache::PkgIterator::OkState
pkgCache::PkgIterator::State() const
200 if (Pkg
->CurrentState
== UnPacked
||
201 Pkg
->CurrentState
== HalfConfigured
)
202 return NeedsConfigure
;
204 if (Pkg
->CurrentState
== UnInstalled
||
205 Pkg
->CurrentState
== HalfInstalled
||
206 Pkg
->InstState
!= Ok
)
212 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
213 // ---------------------------------------------------------------------
214 /* Currently critical deps are defined as depends, predepends and
216 bool pkgCache::DepIterator::IsCritical()
218 if (Dep
->Type
== Conflicts
|| Dep
->Type
== Depends
||
219 Dep
->Type
== PreDepends
)
224 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
225 // ---------------------------------------------------------------------
226 /* This intellegently looks at dep target packages and tries to figure
227 out which package should be used. This is needed to nicely handle
228 provide mapping. If the target package has no other providing packages
229 then it returned. Otherwise the providing list is looked at to
230 see if there is one one unique providing package if so it is returned.
231 Otherwise true is returned and the target package is set. The return
232 result indicates whether the node should be expandable */
233 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator
&Result
)
235 Result
= TargetPkg();
237 // No provides at all
238 if (Result
->ProvidesList
== 0)
241 // There is the Base package and the providing ones which is at least 2
242 if (Result
->VersionList
!= 0)
245 /* We have to skip over indirect provisions of the package that
246 owns the dependency. For instance, if libc5-dev depends on the
247 virtual package libc-dev which is provided by libc5-dev and libc6-dev
248 we must ignore libc5-dev when considering the provides list. */
249 PrvIterator PStart
= Result
.ProvidesList();
250 for (; PStart
.end() != true && PStart
.OwnerPkg() == ParentPkg(); PStart
++);
252 // Nothing but indirect self provides
253 if (PStart
.end() == true)
256 // Check for single packages in the provides list
257 PrvIterator P
= PStart
;
258 for (; P
.end() != true; P
++)
260 // Skip over self provides
261 if (P
.OwnerPkg() == ParentPkg())
263 if (PStart
.OwnerPkg() != P
.OwnerPkg())
267 // Check for non dups
270 Result
= PStart
.OwnerPkg();
274 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
275 // ---------------------------------------------------------------------
276 /* This is a more usefull version of TargetPkg() that follows versioned
277 provides. It includes every possible package-version that could satisfy
278 the dependency. The last item in the list has a 0. */
279 pkgCache::Version
**pkgCache::DepIterator::AllTargets()
282 unsigned long Size
=0;
286 PkgIterator DPkg
= TargetPkg();
288 // Walk along the actual package providing versions
289 for (VerIterator I
= DPkg
.VersionList(); I
.end() == false; I
++)
291 if (pkgCheckDep(TargetVer(),I
.VerStr(),Dep
->CompareOp
) == false)
294 if (Dep
->Type
== Conflicts
&& ParentPkg() == I
.ParentPkg())
302 // Follow all provides
303 for (PrvIterator I
= DPkg
.ProvidesList(); I
.end() == false; I
++)
305 if (pkgCheckDep(TargetVer(),I
.ProvideVersion(),Dep
->CompareOp
) == false)
308 if (Dep
->Type
== Conflicts
&& ParentPkg() == I
.OwnerPkg())
313 *End
++ = I
.OwnerVer();
316 // Do it again and write it into the array
319 Res
= new Version
*[Size
+1];
332 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
333 // ---------------------------------------------------------------------
334 /* This just looks over the version list to see if B is listed before A. In
335 most cases this will return in under 4 checks, ver lists are short. */
336 int pkgCache::VerIterator::CompareVer(const VerIterator
&B
) const
338 // Check if they are equal
346 /* Start at A and look for B. If B is found then A > B otherwise
347 B was before A so A < B */
348 VerIterator I
= *this;
349 for (;I
.end() == false; I
++)
355 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
356 // ---------------------------------------------------------------------
357 /* This stats the file and compares its stats with the ones that were
358 stored during generation. Date checks should probably also be
360 bool pkgCache::PkgFileIterator::IsOk()
363 if (stat(FileName(),&Buf
) != 0)
366 if (Buf
.st_size
!= (signed)File
->Size
|| Buf
.st_mtime
!= File
->mtime
)