]>
Commit | Line | Data |
---|---|---|
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 /*{{{*/ | |
094a497d | 23 | #include <apt-pkg/pkgcache.h> |
af29ffb4 | 24 | #include <apt-pkg/policy.h> |
094a497d AL |
25 | #include <apt-pkg/version.h> |
26 | #include <apt-pkg/error.h> | |
231fea14 | 27 | #include <apt-pkg/strutl.h> |
b2e465d6 | 28 | #include <apt-pkg/configuration.h> |
45df0ad2 | 29 | #include <apt-pkg/aptconfiguration.h> |
578bfd0a | 30 | |
b2e465d6 AL |
31 | #include <apti18n.h> |
32 | ||
578bfd0a AL |
33 | #include <string> |
34 | #include <sys/stat.h> | |
35 | #include <unistd.h> | |
1ae93c94 | 36 | |
851a45a8 | 37 | #include <ctype.h> |
1ae93c94 | 38 | #include <system.h> |
578bfd0a AL |
39 | /*}}}*/ |
40 | ||
851a45a8 AL |
41 | using std::string; |
42 | ||
012b102a | 43 | |
578bfd0a AL |
44 | // Cache::Header::Header - Constructor /*{{{*/ |
45 | // --------------------------------------------------------------------- | |
46 | /* Simply initialize the header */ | |
47 | pkgCache::Header::Header() | |
48 | { | |
49 | Signature = 0x98FE76DC; | |
50 | ||
51 | /* Whenever the structures change the major version should be bumped, | |
52 | whenever the generator changes the minor version should be bumped. */ | |
f8ae7e8b | 53 | MajorVersion = 8; |
6a3da7a6 | 54 | MinorVersion = 0; |
b2e465d6 | 55 | Dirty = false; |
578bfd0a AL |
56 | |
57 | HeaderSz = sizeof(pkgCache::Header); | |
58 | PackageSz = sizeof(pkgCache::Package); | |
59 | PackageFileSz = sizeof(pkgCache::PackageFile); | |
60 | VersionSz = sizeof(pkgCache::Version); | |
a52f938b | 61 | DescriptionSz = sizeof(pkgCache::Description); |
578bfd0a AL |
62 | DependencySz = sizeof(pkgCache::Dependency); |
63 | ProvidesSz = sizeof(pkgCache::Provides); | |
dcb79bae | 64 | VerFileSz = sizeof(pkgCache::VerFile); |
a52f938b | 65 | DescFileSz = sizeof(pkgCache::DescFile); |
dcb79bae | 66 | |
578bfd0a AL |
67 | PackageCount = 0; |
68 | VersionCount = 0; | |
a52f938b | 69 | DescriptionCount = 0; |
578bfd0a AL |
70 | DependsCount = 0; |
71 | PackageFileCount = 0; | |
a7e66b17 | 72 | VerFileCount = 0; |
a52f938b | 73 | DescFileCount = 0; |
a7e66b17 | 74 | ProvidesCount = 0; |
ad00ae81 | 75 | MaxVerFileSize = 0; |
a52f938b | 76 | MaxDescFileSize = 0; |
578bfd0a AL |
77 | |
78 | FileList = 0; | |
79 | StringList = 0; | |
b2e465d6 AL |
80 | VerSysName = 0; |
81 | Architecture = 0; | |
5bf15716 DK |
82 | memset(PkgHashTable,0,sizeof(PkgHashTable)); |
83 | memset(GrpHashTable,0,sizeof(GrpHashTable)); | |
578bfd0a AL |
84 | memset(Pools,0,sizeof(Pools)); |
85 | } | |
86 | /*}}}*/ | |
87 | // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/ | |
88 | // --------------------------------------------------------------------- | |
89 | /* */ | |
90 | bool pkgCache::Header::CheckSizes(Header &Against) const | |
91 | { | |
92 | if (HeaderSz == Against.HeaderSz && | |
93 | PackageSz == Against.PackageSz && | |
94 | PackageFileSz == Against.PackageFileSz && | |
95 | VersionSz == Against.VersionSz && | |
a52f938b | 96 | DescriptionSz == Against.DescriptionSz && |
dcb79bae AL |
97 | DependencySz == Against.DependencySz && |
98 | VerFileSz == Against.VerFileSz && | |
a52f938b | 99 | DescFileSz == Against.DescFileSz && |
578bfd0a AL |
100 | ProvidesSz == Against.ProvidesSz) |
101 | return true; | |
102 | return false; | |
103 | } | |
104 | /*}}}*/ | |
105 | ||
106 | // Cache::pkgCache - Constructor /*{{{*/ | |
107 | // --------------------------------------------------------------------- | |
108 | /* */ | |
b2e465d6 | 109 | pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map) |
578bfd0a | 110 | { |
b2e465d6 AL |
111 | if (DoMap == true) |
112 | ReMap(); | |
578bfd0a AL |
113 | } |
114 | /*}}}*/ | |
115 | // Cache::ReMap - Reopen the cache file /*{{{*/ | |
116 | // --------------------------------------------------------------------- | |
117 | /* If the file is already closed then this will open it open it. */ | |
118 | bool pkgCache::ReMap() | |
119 | { | |
120 | // Apply the typecasts. | |
121 | HeaderP = (Header *)Map.Data(); | |
5bf15716 | 122 | GrpP = (Group *)Map.Data(); |
578bfd0a | 123 | PkgP = (Package *)Map.Data(); |
dcb79bae | 124 | VerFileP = (VerFile *)Map.Data(); |
a52f938b | 125 | DescFileP = (DescFile *)Map.Data(); |
578bfd0a AL |
126 | PkgFileP = (PackageFile *)Map.Data(); |
127 | VerP = (Version *)Map.Data(); | |
a52f938b | 128 | DescP = (Description *)Map.Data(); |
578bfd0a AL |
129 | ProvideP = (Provides *)Map.Data(); |
130 | DepP = (Dependency *)Map.Data(); | |
131 | StringItemP = (StringItem *)Map.Data(); | |
132 | StrP = (char *)Map.Data(); | |
133 | ||
b2e465d6 AL |
134 | if (Map.Size() == 0 || HeaderP == 0) |
135 | return _error->Error(_("Empty package cache")); | |
578bfd0a AL |
136 | |
137 | // Check the header | |
138 | Header DefHeader; | |
139 | if (HeaderP->Signature != DefHeader.Signature || | |
140 | HeaderP->Dirty == true) | |
b2e465d6 | 141 | return _error->Error(_("The package cache file is corrupted")); |
578bfd0a AL |
142 | |
143 | if (HeaderP->MajorVersion != DefHeader.MajorVersion || | |
144 | HeaderP->MinorVersion != DefHeader.MinorVersion || | |
145 | HeaderP->CheckSizes(DefHeader) == false) | |
b2e465d6 AL |
146 | return _error->Error(_("The package cache file is an incompatible version")); |
147 | ||
148 | // Locate our VS.. | |
149 | if (HeaderP->VerSysName == 0 || | |
150 | (VS = pkgVersioningSystem::GetVS(StrP + HeaderP->VerSysName)) == 0) | |
db0db9fe | 151 | return _error->Error(_("This APT does not support the versioning system '%s'"),StrP + HeaderP->VerSysName); |
b2e465d6 AL |
152 | |
153 | // Chcek the arhcitecture | |
154 | if (HeaderP->Architecture == 0 || | |
155 | _config->Find("APT::Architecture") != StrP + HeaderP->Architecture) | |
bac2e715 | 156 | return _error->Error(_("The package cache was built for a different architecture")); |
578bfd0a AL |
157 | return true; |
158 | } | |
159 | /*}}}*/ | |
160 | // Cache::Hash - Hash a string /*{{{*/ | |
161 | // --------------------------------------------------------------------- | |
162 | /* This is used to generate the hash entries for the HashTable. With my | |
163 | package list from bo this function gets 94% table usage on a 512 item | |
164 | table (480 used items) */ | |
171c75f1 | 165 | unsigned long pkgCache::sHash(const string &Str) const |
578bfd0a AL |
166 | { |
167 | unsigned long Hash = 0; | |
851a45a8 | 168 | for (string::const_iterator I = Str.begin(); I != Str.end(); I++) |
4e86942a | 169 | Hash = 5*Hash + tolower_ascii(*I); |
5bf15716 | 170 | return Hash % _count(HeaderP->PkgHashTable); |
578bfd0a AL |
171 | } |
172 | ||
f9eec0e7 | 173 | unsigned long pkgCache::sHash(const char *Str) const |
578bfd0a AL |
174 | { |
175 | unsigned long Hash = 0; | |
f9eec0e7 | 176 | for (const char *I = Str; *I != 0; I++) |
4e86942a | 177 | Hash = 5*Hash + tolower_ascii(*I); |
5bf15716 | 178 | return Hash % _count(HeaderP->PkgHashTable); |
578bfd0a AL |
179 | } |
180 | ||
181 | /*}}}*/ | |
182 | // Cache::FindPkg - Locate a package by name /*{{{*/ | |
183 | // --------------------------------------------------------------------- | |
184 | /* Returns 0 on error, pointer to the package otherwise */ | |
25396fb0 DK |
185 | pkgCache::PkgIterator pkgCache::FindPkg(const string &Name) { |
186 | size_t const found = Name.find(':'); | |
187 | if (found == string::npos) | |
188 | return FindPkg(Name, "native"); | |
189 | return FindPkg(Name.substr(0, found), Name.substr(found+1, string::npos)); | |
190 | } | |
191 | /*}}}*/ | |
192 | // Cache::FindPkg - Locate a package by name /*{{{*/ | |
193 | // --------------------------------------------------------------------- | |
194 | /* Returns 0 on error, pointer to the package otherwise */ | |
5bf15716 DK |
195 | pkgCache::PkgIterator pkgCache::FindPkg(const string &Name, string Arch) { |
196 | /* We make a detour via the GrpIterator here as | |
197 | on a multi-arch environment a group is easier to | |
198 | find than a package (less entries in the buckets) */ | |
199 | pkgCache::GrpIterator Grp = FindGrp(Name); | |
200 | if (Grp.end() == true) | |
201 | return PkgIterator(*this,0); | |
202 | ||
203 | return Grp.FindPkg(Arch); | |
204 | } | |
205 | /*}}}*/ | |
206 | // Cache::FindGrp - Locate a group by name /*{{{*/ | |
207 | // --------------------------------------------------------------------- | |
208 | /* Returns End-Pointer on error, pointer to the group otherwise */ | |
209 | pkgCache::GrpIterator pkgCache::FindGrp(const string &Name) { | |
210 | if (unlikely(Name.empty() == true)) | |
211 | return GrpIterator(*this,0); | |
212 | ||
213 | // Look at the hash bucket for the group | |
214 | Group *Grp = GrpP + HeaderP->GrpHashTable[sHash(Name)]; | |
215 | for (; Grp != GrpP; Grp = GrpP + Grp->Next) { | |
216 | if (Grp->Name != 0 && StrP[Grp->Name] == Name[0] && | |
217 | stringcasecmp(Name, StrP + Grp->Name) == 0) | |
218 | return GrpIterator(*this, Grp); | |
219 | } | |
220 | ||
221 | return GrpIterator(*this,0); | |
578bfd0a AL |
222 | } |
223 | /*}}}*/ | |
b2e465d6 AL |
224 | // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/ |
225 | // --------------------------------------------------------------------- | |
226 | /* This returns a string representation of the dependency compare | |
227 | type in the weird debian style.. */ | |
228 | const char *pkgCache::CompTypeDeb(unsigned char Comp) | |
229 | { | |
230 | const char *Ops[] = {"","<=",">=","<<",">>","=","!="}; | |
231 | if ((unsigned)(Comp & 0xF) < 7) | |
232 | return Ops[Comp & 0xF]; | |
233 | return ""; | |
234 | } | |
235 | /*}}}*/ | |
236 | // Cache::CompType - Return a string describing the compare type /*{{{*/ | |
237 | // --------------------------------------------------------------------- | |
238 | /* This returns a string representation of the dependency compare | |
239 | type */ | |
240 | const char *pkgCache::CompType(unsigned char Comp) | |
241 | { | |
242 | const char *Ops[] = {"","<=",">=","<",">","=","!="}; | |
243 | if ((unsigned)(Comp & 0xF) < 7) | |
244 | return Ops[Comp & 0xF]; | |
245 | return ""; | |
246 | } | |
247 | /*}}}*/ | |
248 | // Cache::DepType - Return a string describing the dep type /*{{{*/ | |
249 | // --------------------------------------------------------------------- | |
250 | /* */ | |
251 | const char *pkgCache::DepType(unsigned char Type) | |
252 | { | |
253 | const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"), | |
254 | _("Recommends"),_("Conflicts"),_("Replaces"), | |
f8ae7e8b | 255 | _("Obsoletes"),_("Breaks"), _("Enhances")}; |
308c7d30 | 256 | if (Type < sizeof(Types)/sizeof(*Types)) |
b2e465d6 AL |
257 | return Types[Type]; |
258 | return ""; | |
259 | } | |
260 | /*}}}*/ | |
0149949b AL |
261 | // Cache::Priority - Convert a priority value to a string /*{{{*/ |
262 | // --------------------------------------------------------------------- | |
263 | /* */ | |
264 | const char *pkgCache::Priority(unsigned char Prio) | |
265 | { | |
b2e465d6 AL |
266 | const char *Mapping[] = {0,_("important"),_("required"),_("standard"), |
267 | _("optional"),_("extra")}; | |
0149949b AL |
268 | if (Prio < _count(Mapping)) |
269 | return Mapping[Prio]; | |
270 | return 0; | |
271 | } | |
272 | /*}}}*/ | |
5bf15716 DK |
273 | // GrpIterator::FindPkg - Locate a package by arch /*{{{*/ |
274 | // --------------------------------------------------------------------- | |
275 | /* Returns an End-Pointer on error, pointer to the package otherwise */ | |
276 | pkgCache::PkgIterator pkgCache::GrpIterator::FindPkg(string Arch) { | |
277 | if (unlikely(IsGood() == false || S->FirstPackage == 0)) | |
278 | return PkgIterator(*Owner, 0); | |
279 | ||
280 | static string const myArch = _config->Find("APT::Architecture"); | |
281 | /* Most of the time the package for our native architecture is | |
282 | the one we add at first to the cache, but this would be the | |
283 | last one we check, so we do it now. */ | |
284 | if (Arch == "native" || Arch == myArch) { | |
285 | Arch = myArch; | |
286 | pkgCache::Package *Pkg = Owner->PkgP + S->LastPackage; | |
287 | if (stringcasecmp(Arch, Owner->StrP + Pkg->Arch) == 0) | |
288 | return PkgIterator(*Owner, Pkg); | |
289 | } | |
290 | ||
291 | /* If we accept any package we simply return the "first" | |
292 | package in this group (the last one added). */ | |
293 | if (Arch == "any") | |
294 | return PkgIterator(*Owner, Owner->PkgP + S->FirstPackage); | |
295 | ||
296 | /* Iterate over the list to find the matching arch | |
297 | unfortunately this list includes "package noise" | |
298 | (= different packages with same calculated hash), | |
299 | so we need to check the name also */ | |
300 | for (pkgCache::Package *Pkg = PackageList(); Pkg != Owner->PkgP; | |
301 | Pkg = Owner->PkgP + Pkg->NextPackage) { | |
302 | if (S->Name == Pkg->Name && | |
303 | stringcasecmp(Arch, Owner->StrP + Pkg->Arch) == 0) | |
304 | return PkgIterator(*Owner, Pkg); | |
305 | if ((Owner->PkgP + S->LastPackage) == Pkg) | |
306 | break; | |
307 | } | |
308 | ||
309 | return PkgIterator(*Owner, 0); | |
310 | } | |
311 | /*}}}*/ | |
312 | // GrpIterator::NextPkg - Locate the next package in the group /*{{{*/ | |
313 | // --------------------------------------------------------------------- | |
314 | /* Returns an End-Pointer on error, pointer to the package otherwise. | |
315 | We can't simply ++ to the next as the list of packages includes | |
316 | "package noise" (= packages with the same hash value but different name) */ | |
317 | pkgCache::PkgIterator pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator const &LastPkg) { | |
318 | if (unlikely(IsGood() == false || S->FirstPackage == 0 || | |
319 | LastPkg.end() == true)) | |
320 | return PkgIterator(*Owner, 0); | |
321 | ||
322 | // Iterate over the list to find the next package | |
323 | pkgCache::Package *Pkg = Owner->PkgP + LastPkg.Index(); | |
324 | Pkg = Owner->PkgP + Pkg->NextPackage; | |
325 | for (; Pkg != Owner->PkgP; Pkg = Owner->PkgP + Pkg->NextPackage) { | |
326 | if (S->Name == Pkg->Name) | |
327 | return PkgIterator(*Owner, Pkg); | |
328 | if ((Owner->PkgP + S->LastPackage) == Pkg) | |
329 | break; | |
330 | } | |
331 | ||
332 | return PkgIterator(*Owner, 0); | |
333 | } | |
334 | /*}}}*/ | |
25396fb0 DK |
335 | // GrpIterator::operator ++ - Postfix incr /*{{{*/ |
336 | // --------------------------------------------------------------------- | |
337 | /* This will advance to the next logical group in the hash table. */ | |
338 | void pkgCache::GrpIterator::operator ++(int) | |
339 | { | |
340 | // Follow the current links | |
341 | if (S != Owner->GrpP) | |
342 | S = Owner->GrpP + S->Next; | |
343 | ||
344 | // Follow the hash table | |
345 | while (S == Owner->GrpP && (HashIndex+1) < (signed)_count(Owner->HeaderP->GrpHashTable)) | |
346 | { | |
347 | HashIndex++; | |
348 | S = Owner->GrpP + Owner->HeaderP->GrpHashTable[HashIndex]; | |
349 | } | |
350 | }; | |
351 | /*}}}*/ | |
f55a958f | 352 | // PkgIterator::operator ++ - Postfix incr /*{{{*/ |
578bfd0a AL |
353 | // --------------------------------------------------------------------- |
354 | /* This will advance to the next logical package in the hash table. */ | |
355 | void pkgCache::PkgIterator::operator ++(int) | |
356 | { | |
357 | // Follow the current links | |
773e2c1f DK |
358 | if (S != Owner->PkgP) |
359 | S = Owner->PkgP + S->NextPackage; | |
b2e465d6 | 360 | |
578bfd0a | 361 | // Follow the hash table |
5bf15716 | 362 | while (S == Owner->PkgP && (HashIndex+1) < (signed)_count(Owner->HeaderP->PkgHashTable)) |
578bfd0a AL |
363 | { |
364 | HashIndex++; | |
5bf15716 | 365 | S = Owner->PkgP + Owner->HeaderP->PkgHashTable[HashIndex]; |
578bfd0a AL |
366 | } |
367 | }; | |
368 | /*}}}*/ | |
578bfd0a AL |
369 | // PkgIterator::State - Check the State of the package /*{{{*/ |
370 | // --------------------------------------------------------------------- | |
371 | /* By this we mean if it is either cleanly installed or cleanly removed. */ | |
372 | pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const | |
d38b7b3d | 373 | { |
773e2c1f DK |
374 | if (S->InstState == pkgCache::State::ReInstReq || |
375 | S->InstState == pkgCache::State::HoldReInstReq) | |
c7c1b0f6 AL |
376 | return NeedsUnpack; |
377 | ||
773e2c1f DK |
378 | if (S->CurrentState == pkgCache::State::UnPacked || |
379 | S->CurrentState == pkgCache::State::HalfConfigured) | |
c6aa14e4 MV |
380 | // we leave triggers alone complettely. dpkg deals with |
381 | // them in a hard-to-predict manner and if they get | |
382 | // resolved by dpkg before apt run dpkg --configure on | |
383 | // the TriggersPending package dpkg returns a error | |
09fab244 | 384 | //Pkg->CurrentState == pkgCache::State::TriggersAwaited |
c6aa14e4 | 385 | //Pkg->CurrentState == pkgCache::State::TriggersPending) |
578bfd0a AL |
386 | return NeedsConfigure; |
387 | ||
773e2c1f DK |
388 | if (S->CurrentState == pkgCache::State::HalfInstalled || |
389 | S->InstState != pkgCache::State::Ok) | |
578bfd0a AL |
390 | return NeedsUnpack; |
391 | ||
392 | return NeedsNothing; | |
393 | } | |
394 | /*}}}*/ | |
af29ffb4 MV |
395 | // PkgIterator::CandVersion - Returns the candidate version string /*{{{*/ |
396 | // --------------------------------------------------------------------- | |
397 | /* Return string representing of the candidate version. */ | |
398 | const char * | |
399 | pkgCache::PkgIterator::CandVersion() const | |
400 | { | |
401 | //TargetVer is empty, so don't use it. | |
857e9c13 | 402 | VerIterator version = pkgPolicy(Owner).GetCandidateVer(*this); |
af29ffb4 MV |
403 | if (version.IsGood()) |
404 | return version.VerStr(); | |
405 | return 0; | |
406 | }; | |
407 | /*}}}*/ | |
408 | // PkgIterator::CurVersion - Returns the current version string /*{{{*/ | |
409 | // --------------------------------------------------------------------- | |
410 | /* Return string representing of the current version. */ | |
411 | const char * | |
412 | pkgCache::PkgIterator::CurVersion() const | |
413 | { | |
414 | VerIterator version = CurrentVer(); | |
415 | if (version.IsGood()) | |
416 | return CurrentVer().VerStr(); | |
417 | return 0; | |
418 | }; | |
419 | /*}}}*/ | |
420 | // ostream operator to handle string representation of a package /*{{{*/ | |
421 | // --------------------------------------------------------------------- | |
422 | /* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section) | |
423 | Note that the characters <|>() are all literal above. Versions will be ommited | |
424 | if they provide no new information (e.g. there is no newer version than candidate) | |
425 | If no version and/or section can be found "none" is used. */ | |
426 | std::ostream& | |
427 | operator<<(ostream& out, pkgCache::PkgIterator Pkg) | |
428 | { | |
429 | if (Pkg.end() == true) | |
430 | return out << "invalid package"; | |
431 | ||
432 | string current = string(Pkg.CurVersion() == 0 ? "none" : Pkg.CurVersion()); | |
433 | string candidate = string(Pkg.CandVersion() == 0 ? "none" : Pkg.CandVersion()); | |
434 | string newest = string(Pkg.VersionList().end() ? "none" : Pkg.VersionList().VerStr()); | |
435 | ||
5dd4c8b8 | 436 | out << Pkg.Name() << " [ " << Pkg.Arch() << " ] < " << current; |
af29ffb4 MV |
437 | if (current != candidate) |
438 | out << " -> " << candidate; | |
439 | if ( newest != "none" && candidate != newest) | |
440 | out << " | " << newest; | |
441 | out << " > ( " << string(Pkg.Section()==0?"none":Pkg.Section()) << " )"; | |
442 | return out; | |
443 | } | |
444 | /*}}}*/ | |
578bfd0a AL |
445 | // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/ |
446 | // --------------------------------------------------------------------- | |
447 | /* Currently critical deps are defined as depends, predepends and | |
308c7d30 | 448 | conflicts (including dpkg's Breaks fields). */ |
578bfd0a AL |
449 | bool pkgCache::DepIterator::IsCritical() |
450 | { | |
773e2c1f DK |
451 | if (S->Type == pkgCache::Dep::Conflicts || |
452 | S->Type == pkgCache::Dep::DpkgBreaks || | |
453 | S->Type == pkgCache::Dep::Obsoletes || | |
454 | S->Type == pkgCache::Dep::Depends || | |
455 | S->Type == pkgCache::Dep::PreDepends) | |
578bfd0a AL |
456 | return true; |
457 | return false; | |
458 | } | |
459 | /*}}}*/ | |
460 | // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/ | |
461 | // --------------------------------------------------------------------- | |
462 | /* This intellegently looks at dep target packages and tries to figure | |
463 | out which package should be used. This is needed to nicely handle | |
464 | provide mapping. If the target package has no other providing packages | |
465 | then it returned. Otherwise the providing list is looked at to | |
466 | see if there is one one unique providing package if so it is returned. | |
467 | Otherwise true is returned and the target package is set. The return | |
b2e465d6 AL |
468 | result indicates whether the node should be expandable |
469 | ||
470 | In Conjunction with the DepCache the value of Result may not be | |
471 | super-good since the policy may have made it uninstallable. Using | |
472 | AllTargets is better in this case. */ | |
578bfd0a AL |
473 | bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) |
474 | { | |
475 | Result = TargetPkg(); | |
476 | ||
477 | // No provides at all | |
478 | if (Result->ProvidesList == 0) | |
479 | return false; | |
480 | ||
481 | // There is the Base package and the providing ones which is at least 2 | |
482 | if (Result->VersionList != 0) | |
483 | return true; | |
484 | ||
485 | /* We have to skip over indirect provisions of the package that | |
486 | owns the dependency. For instance, if libc5-dev depends on the | |
487 | virtual package libc-dev which is provided by libc5-dev and libc6-dev | |
488 | we must ignore libc5-dev when considering the provides list. */ | |
489 | PrvIterator PStart = Result.ProvidesList(); | |
490 | for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++); | |
491 | ||
492 | // Nothing but indirect self provides | |
493 | if (PStart.end() == true) | |
494 | return false; | |
495 | ||
496 | // Check for single packages in the provides list | |
497 | PrvIterator P = PStart; | |
498 | for (; P.end() != true; P++) | |
499 | { | |
500 | // Skip over self provides | |
501 | if (P.OwnerPkg() == ParentPkg()) | |
502 | continue; | |
503 | if (PStart.OwnerPkg() != P.OwnerPkg()) | |
504 | break; | |
505 | } | |
b2e465d6 AL |
506 | |
507 | Result = PStart.OwnerPkg(); | |
578bfd0a AL |
508 | |
509 | // Check for non dups | |
510 | if (P.end() != true) | |
511 | return true; | |
b2e465d6 | 512 | |
578bfd0a AL |
513 | return false; |
514 | } | |
515 | /*}}}*/ | |
516 | // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/ | |
517 | // --------------------------------------------------------------------- | |
b2e465d6 | 518 | /* This is a more useful version of TargetPkg() that follows versioned |
578bfd0a | 519 | provides. It includes every possible package-version that could satisfy |
fbfb2a7c AL |
520 | the dependency. The last item in the list has a 0. The resulting pointer |
521 | must be delete [] 'd */ | |
578bfd0a AL |
522 | pkgCache::Version **pkgCache::DepIterator::AllTargets() |
523 | { | |
524 | Version **Res = 0; | |
525 | unsigned long Size =0; | |
526 | while (1) | |
527 | { | |
528 | Version **End = Res; | |
529 | PkgIterator DPkg = TargetPkg(); | |
530 | ||
531 | // Walk along the actual package providing versions | |
532 | for (VerIterator I = DPkg.VersionList(); I.end() == false; I++) | |
533 | { | |
773e2c1f | 534 | if (Owner->VS->CheckDep(I.VerStr(),S->CompareOp,TargetVer()) == false) |
578bfd0a AL |
535 | continue; |
536 | ||
773e2c1f DK |
537 | if ((S->Type == pkgCache::Dep::Conflicts || |
538 | S->Type == pkgCache::Dep::DpkgBreaks || | |
539 | S->Type == pkgCache::Dep::Obsoletes) && | |
f07b5628 | 540 | ParentPkg() == I.ParentPkg()) |
578bfd0a AL |
541 | continue; |
542 | ||
543 | Size++; | |
544 | if (Res != 0) | |
545 | *End++ = I; | |
546 | } | |
547 | ||
548 | // Follow all provides | |
549 | for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++) | |
550 | { | |
773e2c1f | 551 | if (Owner->VS->CheckDep(I.ProvideVersion(),S->CompareOp,TargetVer()) == false) |
578bfd0a AL |
552 | continue; |
553 | ||
773e2c1f DK |
554 | if ((S->Type == pkgCache::Dep::Conflicts || |
555 | S->Type == pkgCache::Dep::DpkgBreaks || | |
556 | S->Type == pkgCache::Dep::Obsoletes) && | |
f07b5628 | 557 | ParentPkg() == I.OwnerPkg()) |
578bfd0a AL |
558 | continue; |
559 | ||
560 | Size++; | |
561 | if (Res != 0) | |
562 | *End++ = I.OwnerVer(); | |
563 | } | |
564 | ||
565 | // Do it again and write it into the array | |
566 | if (Res == 0) | |
567 | { | |
568 | Res = new Version *[Size+1]; | |
569 | Size = 0; | |
570 | } | |
571 | else | |
572 | { | |
573 | *End = 0; | |
574 | break; | |
575 | } | |
576 | } | |
577 | ||
578 | return Res; | |
579 | } | |
580 | /*}}}*/ | |
43d017d6 AL |
581 | // DepIterator::GlobOr - Compute an OR group /*{{{*/ |
582 | // --------------------------------------------------------------------- | |
583 | /* This Takes an iterator, iterates past the current dependency grouping | |
584 | and returns Start and End so that so End is the final element | |
585 | in the group, if End == Start then D is End++ and End is the | |
586 | dependency D was pointing to. Use in loops to iterate sensibly. */ | |
587 | void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End) | |
588 | { | |
589 | // Compute a single dependency element (glob or) | |
590 | Start = *this; | |
591 | End = *this; | |
018f1533 | 592 | for (bool LastOR = true; end() == false && LastOR == true;) |
43d017d6 | 593 | { |
773e2c1f | 594 | LastOR = (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; |
018f1533 | 595 | (*this)++; |
43d017d6 AL |
596 | if (LastOR == true) |
597 | End = (*this); | |
598 | } | |
599 | } | |
600 | /*}}}*/ | |
578bfd0a AL |
601 | // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/ |
602 | // --------------------------------------------------------------------- | |
603 | /* This just looks over the version list to see if B is listed before A. In | |
604 | most cases this will return in under 4 checks, ver lists are short. */ | |
605 | int pkgCache::VerIterator::CompareVer(const VerIterator &B) const | |
606 | { | |
607 | // Check if they are equal | |
608 | if (*this == B) | |
609 | return 0; | |
610 | if (end() == true) | |
611 | return -1; | |
612 | if (B.end() == true) | |
613 | return 1; | |
614 | ||
615 | /* Start at A and look for B. If B is found then A > B otherwise | |
616 | B was before A so A < B */ | |
617 | VerIterator I = *this; | |
618 | for (;I.end() == false; I++) | |
619 | if (I == B) | |
620 | return 1; | |
621 | return -1; | |
622 | } | |
623 | /*}}}*/ | |
b518cca6 AL |
624 | // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/ |
625 | // --------------------------------------------------------------------- | |
626 | /* */ | |
627 | bool pkgCache::VerIterator::Downloadable() const | |
628 | { | |
629 | VerFileIterator Files = FileList(); | |
630 | for (; Files.end() == false; Files++) | |
f07b5628 | 631 | if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource) |
b518cca6 AL |
632 | return true; |
633 | return false; | |
634 | } | |
635 | /*}}}*/ | |
3c124dde AL |
636 | // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/ |
637 | // --------------------------------------------------------------------- | |
638 | /* This checks to see if any of the versions files are not NotAutomatic. | |
639 | True if this version is selectable for automatic installation. */ | |
640 | bool pkgCache::VerIterator::Automatic() const | |
641 | { | |
642 | VerFileIterator Files = FileList(); | |
643 | for (; Files.end() == false; Files++) | |
644 | if ((Files.File()->Flags & pkgCache::Flag::NotAutomatic) != pkgCache::Flag::NotAutomatic) | |
645 | return true; | |
646 | return false; | |
647 | } | |
648 | /*}}}*/ | |
649 | // VerIterator::NewestFile - Return the newest file version relation /*{{{*/ | |
650 | // --------------------------------------------------------------------- | |
651 | /* This looks at the version numbers associated with all of the sources | |
652 | this version is in and returns the highest.*/ | |
653 | pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const | |
654 | { | |
655 | VerFileIterator Files = FileList(); | |
656 | VerFileIterator Highest = Files; | |
657 | for (; Files.end() == false; Files++) | |
658 | { | |
b2e465d6 | 659 | if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0) |
3c124dde AL |
660 | Highest = Files; |
661 | } | |
662 | ||
663 | return Highest; | |
664 | } | |
665 | /*}}}*/ | |
b2e465d6 AL |
666 | // VerIterator::RelStr - Release description string /*{{{*/ |
667 | // --------------------------------------------------------------------- | |
668 | /* This describes the version from a release-centric manner. The output is a | |
669 | list of Label:Version/Archive */ | |
670 | string pkgCache::VerIterator::RelStr() | |
671 | { | |
672 | bool First = true; | |
673 | string Res; | |
674 | for (pkgCache::VerFileIterator I = this->FileList(); I.end() == false; I++) | |
675 | { | |
676 | // Do not print 'not source' entries' | |
677 | pkgCache::PkgFileIterator File = I.File(); | |
678 | if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource) | |
679 | continue; | |
680 | ||
681 | // See if we have already printed this out.. | |
682 | bool Seen = false; | |
683 | for (pkgCache::VerFileIterator J = this->FileList(); I != J; J++) | |
684 | { | |
685 | pkgCache::PkgFileIterator File2 = J.File(); | |
686 | if (File2->Label == 0 || File->Label == 0) | |
687 | continue; | |
688 | ||
689 | if (strcmp(File.Label(),File2.Label()) != 0) | |
690 | continue; | |
691 | ||
692 | if (File2->Version == File->Version) | |
693 | { | |
694 | Seen = true; | |
695 | break; | |
696 | } | |
10639577 | 697 | if (File2->Version == 0 || File->Version == 0) |
b2e465d6 AL |
698 | break; |
699 | if (strcmp(File.Version(),File2.Version()) == 0) | |
700 | Seen = true; | |
701 | } | |
702 | ||
703 | if (Seen == true) | |
704 | continue; | |
705 | ||
706 | if (First == false) | |
707 | Res += ", "; | |
708 | else | |
709 | First = false; | |
710 | ||
711 | if (File->Label != 0) | |
712 | Res = Res + File.Label() + ':'; | |
713 | ||
714 | if (File->Archive != 0) | |
715 | { | |
716 | if (File->Version == 0) | |
717 | Res += File.Archive(); | |
718 | else | |
719 | Res = Res + File.Version() + '/' + File.Archive(); | |
720 | } | |
721 | else | |
722 | { | |
723 | // No release file, print the host name that this came from | |
724 | if (File->Site == 0 || File.Site()[0] == 0) | |
725 | Res += "localhost"; | |
726 | else | |
727 | Res += File.Site(); | |
728 | } | |
5dd4c8b8 | 729 | } |
857e9c13 | 730 | if (S->ParentPkg != 0) |
5dd4c8b8 | 731 | Res.append(" [").append(Arch()).append("]"); |
b2e465d6 AL |
732 | return Res; |
733 | } | |
734 | /*}}}*/ | |
578bfd0a AL |
735 | // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/ |
736 | // --------------------------------------------------------------------- | |
737 | /* This stats the file and compares its stats with the ones that were | |
738 | stored during generation. Date checks should probably also be | |
739 | included here. */ | |
740 | bool pkgCache::PkgFileIterator::IsOk() | |
741 | { | |
742 | struct stat Buf; | |
743 | if (stat(FileName(),&Buf) != 0) | |
744 | return false; | |
745 | ||
773e2c1f | 746 | if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime) |
578bfd0a AL |
747 | return false; |
748 | ||
749 | return true; | |
750 | } | |
751 | /*}}}*/ | |
af87ab54 AL |
752 | // PkgFileIterator::RelStr - Return the release string /*{{{*/ |
753 | // --------------------------------------------------------------------- | |
754 | /* */ | |
755 | string pkgCache::PkgFileIterator::RelStr() | |
756 | { | |
757 | string Res; | |
758 | if (Version() != 0) | |
759 | Res = Res + (Res.empty() == true?"v=":",v=") + Version(); | |
760 | if (Origin() != 0) | |
761 | Res = Res + (Res.empty() == true?"o=":",o=") + Origin(); | |
762 | if (Archive() != 0) | |
763 | Res = Res + (Res.empty() == true?"a=":",a=") + Archive(); | |
efc487fb DK |
764 | if (Codename() != 0) |
765 | Res = Res + (Res.empty() == true?"n=":",n=") + Codename(); | |
af87ab54 AL |
766 | if (Label() != 0) |
767 | Res = Res + (Res.empty() == true?"l=":",l=") + Label(); | |
768 | if (Component() != 0) | |
769 | Res = Res + (Res.empty() == true?"c=":",c=") + Component(); | |
5dd4c8b8 DK |
770 | if (Architecture() != 0) |
771 | Res = Res + (Res.empty() == true?"b=":",b=") + Architecture(); | |
af87ab54 AL |
772 | return Res; |
773 | } | |
774 | /*}}}*/ | |
012b102a MV |
775 | // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/ |
776 | // --------------------------------------------------------------------- | |
777 | /* return a DescIter for the current locale or the default if none is | |
778 | * found | |
779 | */ | |
780 | pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescription() const | |
781 | { | |
45df0ad2 DK |
782 | std::vector<string> const lang = APT::Configuration::getLanguages(); |
783 | for (std::vector<string>::const_iterator l = lang.begin(); | |
784 | l != lang.end(); l++) | |
785 | { | |
786 | pkgCache::DescIterator DescDefault = DescriptionList(); | |
787 | pkgCache::DescIterator Desc = DescDefault; | |
788 | ||
789 | for (; Desc.end() == false; Desc++) | |
790 | if (*l == Desc.LanguageCode()) | |
791 | break; | |
792 | if (Desc.end() == true) | |
793 | Desc = DescDefault; | |
794 | return Desc; | |
795 | } | |
796 | ||
797 | return DescriptionList(); | |
012b102a MV |
798 | }; |
799 | ||
800 | /*}}}*/ |