]>
Commit | Line | Data |
---|---|---|
578bfd0a AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
f55a958f | 3 | // $Id: pkgcache.cc,v 1.2 1998/07/04 05:57:35 jgg Exp $ |
578bfd0a AL |
4 | /* ###################################################################### |
5 | ||
6 | Package Cache - Accessor code for the cache | |
7 | ||
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!! | |
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 /*{{{*/ | |
23 | #include <pkglib/pkgcache.h> | |
24 | #include <pkglib/version.h> | |
25 | #include <pkglib/error.h> | |
26 | #include <system.h> | |
27 | ||
28 | #include <string> | |
29 | #include <sys/stat.h> | |
30 | #include <unistd.h> | |
31 | /*}}}*/ | |
32 | ||
33 | // Cache::Header::Header - Constructor /*{{{*/ | |
34 | // --------------------------------------------------------------------- | |
35 | /* Simply initialize the header */ | |
36 | pkgCache::Header::Header() | |
37 | { | |
38 | Signature = 0x98FE76DC; | |
39 | ||
40 | /* Whenever the structures change the major version should be bumped, | |
41 | whenever the generator changes the minor version should be bumped. */ | |
42 | MajorVersion = 2; | |
43 | MinorVersion = 0; | |
44 | Dirty = true; | |
45 | ||
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); | |
52 | ||
53 | PackageCount = 0; | |
54 | VersionCount = 0; | |
55 | DependsCount = 0; | |
56 | PackageFileCount = 0; | |
57 | ||
58 | FileList = 0; | |
59 | StringList = 0; | |
60 | memset(HashTable,0,sizeof(HashTable)); | |
61 | memset(Pools,0,sizeof(Pools)); | |
62 | } | |
63 | /*}}}*/ | |
64 | // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/ | |
65 | // --------------------------------------------------------------------- | |
66 | /* */ | |
67 | bool pkgCache::Header::CheckSizes(Header &Against) const | |
68 | { | |
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) | |
75 | return true; | |
76 | return false; | |
77 | } | |
78 | /*}}}*/ | |
79 | ||
80 | // Cache::pkgCache - Constructor /*{{{*/ | |
81 | // --------------------------------------------------------------------- | |
82 | /* */ | |
83 | pkgCache::pkgCache(MMap &Map) : Map(Map) | |
84 | { | |
85 | ReMap(); | |
86 | } | |
87 | /*}}}*/ | |
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() | |
92 | { | |
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(); | |
102 | ||
103 | cout << "Size is " << Map.Size() << endl; | |
104 | if (Map.Size() == 0) | |
105 | return false; | |
106 | ||
107 | // Check the header | |
108 | Header DefHeader; | |
109 | if (HeaderP->Signature != DefHeader.Signature || | |
110 | HeaderP->Dirty == true) | |
111 | return _error->Error("The package cache file is corrupted"); | |
112 | ||
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"); | |
117 | ||
118 | return true; | |
119 | } | |
120 | /*}}}*/ | |
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) | |
127 | { | |
128 | unsigned long Hash = 0; | |
129 | for (const char *I = Str.begin(); I != Str.end(); I++) | |
130 | Hash += *I * ((Str.end() - I + 1)); | |
131 | Header H; | |
132 | return Hash % _count(H.HashTable); | |
133 | } | |
134 | ||
135 | unsigned long pkgCache::sHash(const char *Str) | |
136 | { | |
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)); | |
141 | Header H; | |
142 | return Hash % _count(H.HashTable); | |
143 | } | |
144 | ||
145 | /*}}}*/ | |
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) | |
150 | { | |
151 | // Look at the hash bucket | |
152 | Package *Pkg = PkgP + HeaderP->HashTable[Hash(Name)]; | |
153 | for (; Pkg != PkgP; Pkg = PkgP + Pkg->NextPackage) | |
154 | { | |
155 | if (Pkg->Name != 0 && StrP + Pkg->Name == Name) | |
156 | return PkgIterator(*this,Pkg); | |
157 | } | |
158 | return PkgIterator(*this,0); | |
159 | } | |
160 | /*}}}*/ | |
161 | ||
f55a958f AL |
162 | // Bases for iterator classes /*{{{*/ |
163 | void pkgCache::VerIterator::_dummy() {} | |
164 | void pkgCache::DepIterator::_dummy() {} | |
165 | void pkgCache::PrvIterator::_dummy() {} | |
166 | /*}}}*/ | |
167 | // PkgIterator::operator ++ - Postfix incr /*{{{*/ | |
578bfd0a AL |
168 | // --------------------------------------------------------------------- |
169 | /* This will advance to the next logical package in the hash table. */ | |
170 | void pkgCache::PkgIterator::operator ++(int) | |
171 | { | |
172 | // Follow the current links | |
173 | if (Pkg != Owner->PkgP) | |
174 | Pkg = Owner->PkgP + Pkg->NextPackage; | |
175 | ||
176 | // Follow the hash table | |
177 | while (Pkg == Owner->PkgP && HashIndex < (signed)_count(Owner->HeaderP->HashTable)) | |
178 | { | |
179 | HashIndex++; | |
180 | Pkg = Owner->PkgP + Owner->HeaderP->HashTable[HashIndex]; | |
181 | } | |
182 | }; | |
183 | /*}}}*/ | |
578bfd0a AL |
184 | // PkgIterator::State - Check the State of the package /*{{{*/ |
185 | // --------------------------------------------------------------------- | |
186 | /* By this we mean if it is either cleanly installed or cleanly removed. */ | |
187 | pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const | |
188 | { | |
f55a958f AL |
189 | if (Pkg->CurrentState == UnPacked || |
190 | Pkg->CurrentState == HalfConfigured) | |
578bfd0a AL |
191 | return NeedsConfigure; |
192 | ||
f55a958f AL |
193 | if (Pkg->CurrentState == UnInstalled || |
194 | Pkg->CurrentState == HalfInstalled || | |
195 | Pkg->InstState != Ok) | |
578bfd0a AL |
196 | return NeedsUnpack; |
197 | ||
198 | return NeedsNothing; | |
199 | } | |
200 | /*}}}*/ | |
201 | // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/ | |
202 | // --------------------------------------------------------------------- | |
203 | /* Currently critical deps are defined as depends, predepends and | |
204 | conflicts. */ | |
205 | bool pkgCache::DepIterator::IsCritical() | |
206 | { | |
f55a958f AL |
207 | if (Dep->Type == Conflicts || Dep->Type == Depends || |
208 | Dep->Type == PreDepends) | |
578bfd0a AL |
209 | return true; |
210 | return false; | |
211 | } | |
212 | /*}}}*/ | |
213 | // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/ | |
214 | // --------------------------------------------------------------------- | |
215 | /* This intellegently looks at dep target packages and tries to figure | |
216 | out which package should be used. This is needed to nicely handle | |
217 | provide mapping. If the target package has no other providing packages | |
218 | then it returned. Otherwise the providing list is looked at to | |
219 | see if there is one one unique providing package if so it is returned. | |
220 | Otherwise true is returned and the target package is set. The return | |
221 | result indicates whether the node should be expandable */ | |
222 | bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) | |
223 | { | |
224 | Result = TargetPkg(); | |
225 | ||
226 | // No provides at all | |
227 | if (Result->ProvidesList == 0) | |
228 | return false; | |
229 | ||
230 | // There is the Base package and the providing ones which is at least 2 | |
231 | if (Result->VersionList != 0) | |
232 | return true; | |
233 | ||
234 | /* We have to skip over indirect provisions of the package that | |
235 | owns the dependency. For instance, if libc5-dev depends on the | |
236 | virtual package libc-dev which is provided by libc5-dev and libc6-dev | |
237 | we must ignore libc5-dev when considering the provides list. */ | |
238 | PrvIterator PStart = Result.ProvidesList(); | |
239 | for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++); | |
240 | ||
241 | // Nothing but indirect self provides | |
242 | if (PStart.end() == true) | |
243 | return false; | |
244 | ||
245 | // Check for single packages in the provides list | |
246 | PrvIterator P = PStart; | |
247 | for (; P.end() != true; P++) | |
248 | { | |
249 | // Skip over self provides | |
250 | if (P.OwnerPkg() == ParentPkg()) | |
251 | continue; | |
252 | if (PStart.OwnerPkg() != P.OwnerPkg()) | |
253 | break; | |
254 | } | |
255 | ||
256 | // Check for non dups | |
257 | if (P.end() != true) | |
258 | return true; | |
259 | Result = PStart.OwnerPkg(); | |
260 | return false; | |
261 | } | |
262 | /*}}}*/ | |
263 | // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/ | |
264 | // --------------------------------------------------------------------- | |
265 | /* This is a more usefull version of TargetPkg() that follows versioned | |
266 | provides. It includes every possible package-version that could satisfy | |
267 | the dependency. The last item in the list has a 0. */ | |
268 | pkgCache::Version **pkgCache::DepIterator::AllTargets() | |
269 | { | |
270 | Version **Res = 0; | |
271 | unsigned long Size =0; | |
272 | while (1) | |
273 | { | |
274 | Version **End = Res; | |
275 | PkgIterator DPkg = TargetPkg(); | |
276 | ||
277 | // Walk along the actual package providing versions | |
278 | for (VerIterator I = DPkg.VersionList(); I.end() == false; I++) | |
279 | { | |
280 | if (pkgCheckDep(TargetVer(),I.VerStr(),Dep->CompareOp) == false) | |
281 | continue; | |
282 | ||
f55a958f | 283 | if (Dep->Type == Conflicts && ParentPkg() == I.ParentPkg()) |
578bfd0a AL |
284 | continue; |
285 | ||
286 | Size++; | |
287 | if (Res != 0) | |
288 | *End++ = I; | |
289 | } | |
290 | ||
291 | // Follow all provides | |
292 | for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++) | |
293 | { | |
294 | if (pkgCheckDep(TargetVer(),I.ProvideVersion(),Dep->CompareOp) == false) | |
295 | continue; | |
296 | ||
f55a958f | 297 | if (Dep->Type == Conflicts && ParentPkg() == I.OwnerPkg()) |
578bfd0a AL |
298 | continue; |
299 | ||
300 | Size++; | |
301 | if (Res != 0) | |
302 | *End++ = I.OwnerVer(); | |
303 | } | |
304 | ||
305 | // Do it again and write it into the array | |
306 | if (Res == 0) | |
307 | { | |
308 | Res = new Version *[Size+1]; | |
309 | Size = 0; | |
310 | } | |
311 | else | |
312 | { | |
313 | *End = 0; | |
314 | break; | |
315 | } | |
316 | } | |
317 | ||
318 | return Res; | |
319 | } | |
320 | /*}}}*/ | |
321 | // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/ | |
322 | // --------------------------------------------------------------------- | |
323 | /* This just looks over the version list to see if B is listed before A. In | |
324 | most cases this will return in under 4 checks, ver lists are short. */ | |
325 | int pkgCache::VerIterator::CompareVer(const VerIterator &B) const | |
326 | { | |
327 | // Check if they are equal | |
328 | if (*this == B) | |
329 | return 0; | |
330 | if (end() == true) | |
331 | return -1; | |
332 | if (B.end() == true) | |
333 | return 1; | |
334 | ||
335 | /* Start at A and look for B. If B is found then A > B otherwise | |
336 | B was before A so A < B */ | |
337 | VerIterator I = *this; | |
338 | for (;I.end() == false; I++) | |
339 | if (I == B) | |
340 | return 1; | |
341 | return -1; | |
342 | } | |
343 | /*}}}*/ | |
344 | // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/ | |
345 | // --------------------------------------------------------------------- | |
346 | /* This stats the file and compares its stats with the ones that were | |
347 | stored during generation. Date checks should probably also be | |
348 | included here. */ | |
349 | bool pkgCache::PkgFileIterator::IsOk() | |
350 | { | |
351 | struct stat Buf; | |
352 | if (stat(FileName(),&Buf) != 0) | |
353 | return false; | |
354 | ||
355 | if (Buf.st_size != (signed)File->Size || Buf.st_mtime != File->mtime) | |
356 | return false; | |
357 | ||
358 | return true; | |
359 | } | |
360 | /*}}}*/ |