]>
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"); | |
4d174dc8 DK |
189 | string const Arch = Name.substr(found+1); |
190 | if (Arch == "any") | |
191 | return FindPkg(Name, "any"); | |
192 | return FindPkg(Name.substr(0, found), Arch); | |
25396fb0 DK |
193 | } |
194 | /*}}}*/ | |
195 | // Cache::FindPkg - Locate a package by name /*{{{*/ | |
196 | // --------------------------------------------------------------------- | |
197 | /* Returns 0 on error, pointer to the package otherwise */ | |
5bf15716 DK |
198 | pkgCache::PkgIterator pkgCache::FindPkg(const string &Name, string Arch) { |
199 | /* We make a detour via the GrpIterator here as | |
200 | on a multi-arch environment a group is easier to | |
201 | find than a package (less entries in the buckets) */ | |
202 | pkgCache::GrpIterator Grp = FindGrp(Name); | |
203 | if (Grp.end() == true) | |
204 | return PkgIterator(*this,0); | |
205 | ||
206 | return Grp.FindPkg(Arch); | |
207 | } | |
208 | /*}}}*/ | |
209 | // Cache::FindGrp - Locate a group by name /*{{{*/ | |
210 | // --------------------------------------------------------------------- | |
211 | /* Returns End-Pointer on error, pointer to the group otherwise */ | |
212 | pkgCache::GrpIterator pkgCache::FindGrp(const string &Name) { | |
213 | if (unlikely(Name.empty() == true)) | |
214 | return GrpIterator(*this,0); | |
215 | ||
216 | // Look at the hash bucket for the group | |
217 | Group *Grp = GrpP + HeaderP->GrpHashTable[sHash(Name)]; | |
218 | for (; Grp != GrpP; Grp = GrpP + Grp->Next) { | |
219 | if (Grp->Name != 0 && StrP[Grp->Name] == Name[0] && | |
220 | stringcasecmp(Name, StrP + Grp->Name) == 0) | |
221 | return GrpIterator(*this, Grp); | |
222 | } | |
223 | ||
224 | return GrpIterator(*this,0); | |
578bfd0a AL |
225 | } |
226 | /*}}}*/ | |
b2e465d6 AL |
227 | // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/ |
228 | // --------------------------------------------------------------------- | |
229 | /* This returns a string representation of the dependency compare | |
230 | type in the weird debian style.. */ | |
231 | const char *pkgCache::CompTypeDeb(unsigned char Comp) | |
232 | { | |
233 | const char *Ops[] = {"","<=",">=","<<",">>","=","!="}; | |
234 | if ((unsigned)(Comp & 0xF) < 7) | |
235 | return Ops[Comp & 0xF]; | |
236 | return ""; | |
237 | } | |
238 | /*}}}*/ | |
239 | // Cache::CompType - Return a string describing the compare type /*{{{*/ | |
240 | // --------------------------------------------------------------------- | |
241 | /* This returns a string representation of the dependency compare | |
242 | type */ | |
243 | const char *pkgCache::CompType(unsigned char Comp) | |
244 | { | |
245 | const char *Ops[] = {"","<=",">=","<",">","=","!="}; | |
246 | if ((unsigned)(Comp & 0xF) < 7) | |
247 | return Ops[Comp & 0xF]; | |
248 | return ""; | |
249 | } | |
250 | /*}}}*/ | |
251 | // Cache::DepType - Return a string describing the dep type /*{{{*/ | |
252 | // --------------------------------------------------------------------- | |
253 | /* */ | |
254 | const char *pkgCache::DepType(unsigned char Type) | |
255 | { | |
256 | const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"), | |
257 | _("Recommends"),_("Conflicts"),_("Replaces"), | |
f8ae7e8b | 258 | _("Obsoletes"),_("Breaks"), _("Enhances")}; |
308c7d30 | 259 | if (Type < sizeof(Types)/sizeof(*Types)) |
b2e465d6 AL |
260 | return Types[Type]; |
261 | return ""; | |
262 | } | |
263 | /*}}}*/ | |
0149949b AL |
264 | // Cache::Priority - Convert a priority value to a string /*{{{*/ |
265 | // --------------------------------------------------------------------- | |
266 | /* */ | |
267 | const char *pkgCache::Priority(unsigned char Prio) | |
268 | { | |
b2e465d6 AL |
269 | const char *Mapping[] = {0,_("important"),_("required"),_("standard"), |
270 | _("optional"),_("extra")}; | |
0149949b AL |
271 | if (Prio < _count(Mapping)) |
272 | return Mapping[Prio]; | |
273 | return 0; | |
274 | } | |
275 | /*}}}*/ | |
5bf15716 DK |
276 | // GrpIterator::FindPkg - Locate a package by arch /*{{{*/ |
277 | // --------------------------------------------------------------------- | |
278 | /* Returns an End-Pointer on error, pointer to the package otherwise */ | |
279 | pkgCache::PkgIterator pkgCache::GrpIterator::FindPkg(string Arch) { | |
280 | if (unlikely(IsGood() == false || S->FirstPackage == 0)) | |
281 | return PkgIterator(*Owner, 0); | |
282 | ||
283 | static string const myArch = _config->Find("APT::Architecture"); | |
284 | /* Most of the time the package for our native architecture is | |
285 | the one we add at first to the cache, but this would be the | |
286 | last one we check, so we do it now. */ | |
287 | if (Arch == "native" || Arch == myArch) { | |
288 | Arch = myArch; | |
289 | pkgCache::Package *Pkg = Owner->PkgP + S->LastPackage; | |
290 | if (stringcasecmp(Arch, Owner->StrP + Pkg->Arch) == 0) | |
291 | return PkgIterator(*Owner, Pkg); | |
292 | } | |
293 | ||
294 | /* If we accept any package we simply return the "first" | |
295 | package in this group (the last one added). */ | |
296 | if (Arch == "any") | |
297 | return PkgIterator(*Owner, Owner->PkgP + S->FirstPackage); | |
298 | ||
299 | /* Iterate over the list to find the matching arch | |
300 | unfortunately this list includes "package noise" | |
301 | (= different packages with same calculated hash), | |
302 | so we need to check the name also */ | |
303 | for (pkgCache::Package *Pkg = PackageList(); Pkg != Owner->PkgP; | |
304 | Pkg = Owner->PkgP + Pkg->NextPackage) { | |
305 | if (S->Name == Pkg->Name && | |
306 | stringcasecmp(Arch, Owner->StrP + Pkg->Arch) == 0) | |
307 | return PkgIterator(*Owner, Pkg); | |
308 | if ((Owner->PkgP + S->LastPackage) == Pkg) | |
309 | break; | |
310 | } | |
311 | ||
312 | return PkgIterator(*Owner, 0); | |
313 | } | |
314 | /*}}}*/ | |
315 | // GrpIterator::NextPkg - Locate the next package in the group /*{{{*/ | |
316 | // --------------------------------------------------------------------- | |
317 | /* Returns an End-Pointer on error, pointer to the package otherwise. | |
318 | We can't simply ++ to the next as the list of packages includes | |
319 | "package noise" (= packages with the same hash value but different name) */ | |
320 | pkgCache::PkgIterator pkgCache::GrpIterator::NextPkg(pkgCache::PkgIterator const &LastPkg) { | |
321 | if (unlikely(IsGood() == false || S->FirstPackage == 0 || | |
322 | LastPkg.end() == true)) | |
323 | return PkgIterator(*Owner, 0); | |
324 | ||
325 | // Iterate over the list to find the next package | |
326 | pkgCache::Package *Pkg = Owner->PkgP + LastPkg.Index(); | |
327 | Pkg = Owner->PkgP + Pkg->NextPackage; | |
328 | for (; Pkg != Owner->PkgP; Pkg = Owner->PkgP + Pkg->NextPackage) { | |
329 | if (S->Name == Pkg->Name) | |
330 | return PkgIterator(*Owner, Pkg); | |
331 | if ((Owner->PkgP + S->LastPackage) == Pkg) | |
332 | break; | |
333 | } | |
334 | ||
335 | return PkgIterator(*Owner, 0); | |
336 | } | |
337 | /*}}}*/ | |
25396fb0 DK |
338 | // GrpIterator::operator ++ - Postfix incr /*{{{*/ |
339 | // --------------------------------------------------------------------- | |
340 | /* This will advance to the next logical group in the hash table. */ | |
341 | void pkgCache::GrpIterator::operator ++(int) | |
342 | { | |
343 | // Follow the current links | |
344 | if (S != Owner->GrpP) | |
345 | S = Owner->GrpP + S->Next; | |
346 | ||
347 | // Follow the hash table | |
348 | while (S == Owner->GrpP && (HashIndex+1) < (signed)_count(Owner->HeaderP->GrpHashTable)) | |
349 | { | |
350 | HashIndex++; | |
351 | S = Owner->GrpP + Owner->HeaderP->GrpHashTable[HashIndex]; | |
352 | } | |
353 | }; | |
354 | /*}}}*/ | |
f55a958f | 355 | // PkgIterator::operator ++ - Postfix incr /*{{{*/ |
578bfd0a AL |
356 | // --------------------------------------------------------------------- |
357 | /* This will advance to the next logical package in the hash table. */ | |
358 | void pkgCache::PkgIterator::operator ++(int) | |
359 | { | |
360 | // Follow the current links | |
773e2c1f DK |
361 | if (S != Owner->PkgP) |
362 | S = Owner->PkgP + S->NextPackage; | |
b2e465d6 | 363 | |
578bfd0a | 364 | // Follow the hash table |
5bf15716 | 365 | while (S == Owner->PkgP && (HashIndex+1) < (signed)_count(Owner->HeaderP->PkgHashTable)) |
578bfd0a AL |
366 | { |
367 | HashIndex++; | |
5bf15716 | 368 | S = Owner->PkgP + Owner->HeaderP->PkgHashTable[HashIndex]; |
578bfd0a AL |
369 | } |
370 | }; | |
371 | /*}}}*/ | |
578bfd0a AL |
372 | // PkgIterator::State - Check the State of the package /*{{{*/ |
373 | // --------------------------------------------------------------------- | |
374 | /* By this we mean if it is either cleanly installed or cleanly removed. */ | |
375 | pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const | |
d38b7b3d | 376 | { |
773e2c1f DK |
377 | if (S->InstState == pkgCache::State::ReInstReq || |
378 | S->InstState == pkgCache::State::HoldReInstReq) | |
c7c1b0f6 AL |
379 | return NeedsUnpack; |
380 | ||
773e2c1f DK |
381 | if (S->CurrentState == pkgCache::State::UnPacked || |
382 | S->CurrentState == pkgCache::State::HalfConfigured) | |
c6aa14e4 MV |
383 | // we leave triggers alone complettely. dpkg deals with |
384 | // them in a hard-to-predict manner and if they get | |
385 | // resolved by dpkg before apt run dpkg --configure on | |
386 | // the TriggersPending package dpkg returns a error | |
09fab244 | 387 | //Pkg->CurrentState == pkgCache::State::TriggersAwaited |
c6aa14e4 | 388 | //Pkg->CurrentState == pkgCache::State::TriggersPending) |
578bfd0a AL |
389 | return NeedsConfigure; |
390 | ||
773e2c1f DK |
391 | if (S->CurrentState == pkgCache::State::HalfInstalled || |
392 | S->InstState != pkgCache::State::Ok) | |
578bfd0a AL |
393 | return NeedsUnpack; |
394 | ||
395 | return NeedsNothing; | |
396 | } | |
397 | /*}}}*/ | |
af29ffb4 MV |
398 | // PkgIterator::CandVersion - Returns the candidate version string /*{{{*/ |
399 | // --------------------------------------------------------------------- | |
400 | /* Return string representing of the candidate version. */ | |
401 | const char * | |
402 | pkgCache::PkgIterator::CandVersion() const | |
403 | { | |
404 | //TargetVer is empty, so don't use it. | |
857e9c13 | 405 | VerIterator version = pkgPolicy(Owner).GetCandidateVer(*this); |
af29ffb4 MV |
406 | if (version.IsGood()) |
407 | return version.VerStr(); | |
408 | return 0; | |
409 | }; | |
410 | /*}}}*/ | |
411 | // PkgIterator::CurVersion - Returns the current version string /*{{{*/ | |
412 | // --------------------------------------------------------------------- | |
413 | /* Return string representing of the current version. */ | |
414 | const char * | |
415 | pkgCache::PkgIterator::CurVersion() const | |
416 | { | |
417 | VerIterator version = CurrentVer(); | |
418 | if (version.IsGood()) | |
419 | return CurrentVer().VerStr(); | |
420 | return 0; | |
421 | }; | |
422 | /*}}}*/ | |
423 | // ostream operator to handle string representation of a package /*{{{*/ | |
424 | // --------------------------------------------------------------------- | |
425 | /* Output name < cur.rent.version -> candid.ate.version | new.est.version > (section) | |
426 | Note that the characters <|>() are all literal above. Versions will be ommited | |
427 | if they provide no new information (e.g. there is no newer version than candidate) | |
428 | If no version and/or section can be found "none" is used. */ | |
429 | std::ostream& | |
430 | operator<<(ostream& out, pkgCache::PkgIterator Pkg) | |
431 | { | |
432 | if (Pkg.end() == true) | |
433 | return out << "invalid package"; | |
434 | ||
435 | string current = string(Pkg.CurVersion() == 0 ? "none" : Pkg.CurVersion()); | |
436 | string candidate = string(Pkg.CandVersion() == 0 ? "none" : Pkg.CandVersion()); | |
437 | string newest = string(Pkg.VersionList().end() ? "none" : Pkg.VersionList().VerStr()); | |
438 | ||
5dd4c8b8 | 439 | out << Pkg.Name() << " [ " << Pkg.Arch() << " ] < " << current; |
af29ffb4 MV |
440 | if (current != candidate) |
441 | out << " -> " << candidate; | |
442 | if ( newest != "none" && candidate != newest) | |
443 | out << " | " << newest; | |
444 | out << " > ( " << string(Pkg.Section()==0?"none":Pkg.Section()) << " )"; | |
445 | return out; | |
446 | } | |
447 | /*}}}*/ | |
578bfd0a AL |
448 | // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/ |
449 | // --------------------------------------------------------------------- | |
450 | /* Currently critical deps are defined as depends, predepends and | |
308c7d30 | 451 | conflicts (including dpkg's Breaks fields). */ |
578bfd0a AL |
452 | bool pkgCache::DepIterator::IsCritical() |
453 | { | |
773e2c1f DK |
454 | if (S->Type == pkgCache::Dep::Conflicts || |
455 | S->Type == pkgCache::Dep::DpkgBreaks || | |
456 | S->Type == pkgCache::Dep::Obsoletes || | |
457 | S->Type == pkgCache::Dep::Depends || | |
458 | S->Type == pkgCache::Dep::PreDepends) | |
578bfd0a AL |
459 | return true; |
460 | return false; | |
461 | } | |
462 | /*}}}*/ | |
463 | // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/ | |
464 | // --------------------------------------------------------------------- | |
465 | /* This intellegently looks at dep target packages and tries to figure | |
466 | out which package should be used. This is needed to nicely handle | |
467 | provide mapping. If the target package has no other providing packages | |
468 | then it returned. Otherwise the providing list is looked at to | |
469 | see if there is one one unique providing package if so it is returned. | |
470 | Otherwise true is returned and the target package is set. The return | |
b2e465d6 AL |
471 | result indicates whether the node should be expandable |
472 | ||
473 | In Conjunction with the DepCache the value of Result may not be | |
474 | super-good since the policy may have made it uninstallable. Using | |
475 | AllTargets is better in this case. */ | |
578bfd0a AL |
476 | bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result) |
477 | { | |
478 | Result = TargetPkg(); | |
479 | ||
480 | // No provides at all | |
481 | if (Result->ProvidesList == 0) | |
482 | return false; | |
483 | ||
484 | // There is the Base package and the providing ones which is at least 2 | |
485 | if (Result->VersionList != 0) | |
486 | return true; | |
487 | ||
488 | /* We have to skip over indirect provisions of the package that | |
489 | owns the dependency. For instance, if libc5-dev depends on the | |
490 | virtual package libc-dev which is provided by libc5-dev and libc6-dev | |
491 | we must ignore libc5-dev when considering the provides list. */ | |
492 | PrvIterator PStart = Result.ProvidesList(); | |
493 | for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++); | |
494 | ||
495 | // Nothing but indirect self provides | |
496 | if (PStart.end() == true) | |
497 | return false; | |
498 | ||
499 | // Check for single packages in the provides list | |
500 | PrvIterator P = PStart; | |
501 | for (; P.end() != true; P++) | |
502 | { | |
503 | // Skip over self provides | |
504 | if (P.OwnerPkg() == ParentPkg()) | |
505 | continue; | |
506 | if (PStart.OwnerPkg() != P.OwnerPkg()) | |
507 | break; | |
508 | } | |
b2e465d6 AL |
509 | |
510 | Result = PStart.OwnerPkg(); | |
578bfd0a AL |
511 | |
512 | // Check for non dups | |
513 | if (P.end() != true) | |
514 | return true; | |
b2e465d6 | 515 | |
578bfd0a AL |
516 | return false; |
517 | } | |
518 | /*}}}*/ | |
519 | // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/ | |
520 | // --------------------------------------------------------------------- | |
b2e465d6 | 521 | /* This is a more useful version of TargetPkg() that follows versioned |
578bfd0a | 522 | provides. It includes every possible package-version that could satisfy |
fbfb2a7c AL |
523 | the dependency. The last item in the list has a 0. The resulting pointer |
524 | must be delete [] 'd */ | |
578bfd0a AL |
525 | pkgCache::Version **pkgCache::DepIterator::AllTargets() |
526 | { | |
527 | Version **Res = 0; | |
528 | unsigned long Size =0; | |
529 | while (1) | |
530 | { | |
531 | Version **End = Res; | |
532 | PkgIterator DPkg = TargetPkg(); | |
533 | ||
534 | // Walk along the actual package providing versions | |
535 | for (VerIterator I = DPkg.VersionList(); I.end() == false; I++) | |
536 | { | |
773e2c1f | 537 | if (Owner->VS->CheckDep(I.VerStr(),S->CompareOp,TargetVer()) == false) |
578bfd0a AL |
538 | continue; |
539 | ||
773e2c1f DK |
540 | if ((S->Type == pkgCache::Dep::Conflicts || |
541 | S->Type == pkgCache::Dep::DpkgBreaks || | |
542 | S->Type == pkgCache::Dep::Obsoletes) && | |
f07b5628 | 543 | ParentPkg() == I.ParentPkg()) |
578bfd0a AL |
544 | continue; |
545 | ||
546 | Size++; | |
547 | if (Res != 0) | |
548 | *End++ = I; | |
549 | } | |
550 | ||
551 | // Follow all provides | |
552 | for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++) | |
553 | { | |
773e2c1f | 554 | if (Owner->VS->CheckDep(I.ProvideVersion(),S->CompareOp,TargetVer()) == false) |
578bfd0a AL |
555 | continue; |
556 | ||
773e2c1f DK |
557 | if ((S->Type == pkgCache::Dep::Conflicts || |
558 | S->Type == pkgCache::Dep::DpkgBreaks || | |
559 | S->Type == pkgCache::Dep::Obsoletes) && | |
f07b5628 | 560 | ParentPkg() == I.OwnerPkg()) |
578bfd0a AL |
561 | continue; |
562 | ||
563 | Size++; | |
564 | if (Res != 0) | |
565 | *End++ = I.OwnerVer(); | |
566 | } | |
567 | ||
568 | // Do it again and write it into the array | |
569 | if (Res == 0) | |
570 | { | |
571 | Res = new Version *[Size+1]; | |
572 | Size = 0; | |
573 | } | |
574 | else | |
575 | { | |
576 | *End = 0; | |
577 | break; | |
578 | } | |
579 | } | |
580 | ||
581 | return Res; | |
582 | } | |
583 | /*}}}*/ | |
43d017d6 AL |
584 | // DepIterator::GlobOr - Compute an OR group /*{{{*/ |
585 | // --------------------------------------------------------------------- | |
586 | /* This Takes an iterator, iterates past the current dependency grouping | |
587 | and returns Start and End so that so End is the final element | |
588 | in the group, if End == Start then D is End++ and End is the | |
589 | dependency D was pointing to. Use in loops to iterate sensibly. */ | |
590 | void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End) | |
591 | { | |
592 | // Compute a single dependency element (glob or) | |
593 | Start = *this; | |
594 | End = *this; | |
018f1533 | 595 | for (bool LastOR = true; end() == false && LastOR == true;) |
43d017d6 | 596 | { |
773e2c1f | 597 | LastOR = (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or; |
018f1533 | 598 | (*this)++; |
43d017d6 AL |
599 | if (LastOR == true) |
600 | End = (*this); | |
601 | } | |
602 | } | |
603 | /*}}}*/ | |
578bfd0a AL |
604 | // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/ |
605 | // --------------------------------------------------------------------- | |
606 | /* This just looks over the version list to see if B is listed before A. In | |
607 | most cases this will return in under 4 checks, ver lists are short. */ | |
608 | int pkgCache::VerIterator::CompareVer(const VerIterator &B) const | |
609 | { | |
610 | // Check if they are equal | |
611 | if (*this == B) | |
612 | return 0; | |
613 | if (end() == true) | |
614 | return -1; | |
615 | if (B.end() == true) | |
616 | return 1; | |
617 | ||
618 | /* Start at A and look for B. If B is found then A > B otherwise | |
619 | B was before A so A < B */ | |
620 | VerIterator I = *this; | |
621 | for (;I.end() == false; I++) | |
622 | if (I == B) | |
623 | return 1; | |
624 | return -1; | |
625 | } | |
626 | /*}}}*/ | |
b518cca6 AL |
627 | // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/ |
628 | // --------------------------------------------------------------------- | |
629 | /* */ | |
630 | bool pkgCache::VerIterator::Downloadable() const | |
631 | { | |
632 | VerFileIterator Files = FileList(); | |
633 | for (; Files.end() == false; Files++) | |
f07b5628 | 634 | if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource) |
b518cca6 AL |
635 | return true; |
636 | return false; | |
637 | } | |
638 | /*}}}*/ | |
3c124dde AL |
639 | // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/ |
640 | // --------------------------------------------------------------------- | |
641 | /* This checks to see if any of the versions files are not NotAutomatic. | |
642 | True if this version is selectable for automatic installation. */ | |
643 | bool pkgCache::VerIterator::Automatic() const | |
644 | { | |
645 | VerFileIterator Files = FileList(); | |
646 | for (; Files.end() == false; Files++) | |
647 | if ((Files.File()->Flags & pkgCache::Flag::NotAutomatic) != pkgCache::Flag::NotAutomatic) | |
648 | return true; | |
649 | return false; | |
650 | } | |
651 | /*}}}*/ | |
803ea2a8 DK |
652 | // VerIterator::Pseudo - Check if this version is a pseudo one /*{{{*/ |
653 | // --------------------------------------------------------------------- | |
654 | /* Sometimes you have the need to express dependencies with versions | |
655 | which doesn't really exist or exist multiply times for "different" | |
656 | packages. We need these versions for dependency resolution but they | |
657 | are a problem everytime we need to download/install something. */ | |
658 | bool pkgCache::VerIterator::Pseudo() const | |
659 | { | |
42d71ab5 DK |
660 | if (S->MultiArch == pkgCache::Version::All && |
661 | strcmp(Arch(true),"all") != 0) | |
662 | { | |
663 | GrpIterator const Grp = ParentPkg().Group(); | |
664 | return (Grp->LastPackage != Grp->FirstPackage); | |
665 | } | |
666 | return false; | |
803ea2a8 DK |
667 | } |
668 | /*}}}*/ | |
3c124dde AL |
669 | // VerIterator::NewestFile - Return the newest file version relation /*{{{*/ |
670 | // --------------------------------------------------------------------- | |
671 | /* This looks at the version numbers associated with all of the sources | |
672 | this version is in and returns the highest.*/ | |
673 | pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const | |
674 | { | |
675 | VerFileIterator Files = FileList(); | |
676 | VerFileIterator Highest = Files; | |
677 | for (; Files.end() == false; Files++) | |
678 | { | |
b2e465d6 | 679 | if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0) |
3c124dde AL |
680 | Highest = Files; |
681 | } | |
682 | ||
683 | return Highest; | |
684 | } | |
685 | /*}}}*/ | |
b2e465d6 AL |
686 | // VerIterator::RelStr - Release description string /*{{{*/ |
687 | // --------------------------------------------------------------------- | |
688 | /* This describes the version from a release-centric manner. The output is a | |
689 | list of Label:Version/Archive */ | |
690 | string pkgCache::VerIterator::RelStr() | |
691 | { | |
692 | bool First = true; | |
693 | string Res; | |
694 | for (pkgCache::VerFileIterator I = this->FileList(); I.end() == false; I++) | |
695 | { | |
696 | // Do not print 'not source' entries' | |
697 | pkgCache::PkgFileIterator File = I.File(); | |
698 | if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource) | |
699 | continue; | |
700 | ||
701 | // See if we have already printed this out.. | |
702 | bool Seen = false; | |
703 | for (pkgCache::VerFileIterator J = this->FileList(); I != J; J++) | |
704 | { | |
705 | pkgCache::PkgFileIterator File2 = J.File(); | |
706 | if (File2->Label == 0 || File->Label == 0) | |
707 | continue; | |
708 | ||
709 | if (strcmp(File.Label(),File2.Label()) != 0) | |
710 | continue; | |
711 | ||
712 | if (File2->Version == File->Version) | |
713 | { | |
714 | Seen = true; | |
715 | break; | |
716 | } | |
10639577 | 717 | if (File2->Version == 0 || File->Version == 0) |
b2e465d6 AL |
718 | break; |
719 | if (strcmp(File.Version(),File2.Version()) == 0) | |
720 | Seen = true; | |
721 | } | |
722 | ||
723 | if (Seen == true) | |
724 | continue; | |
725 | ||
726 | if (First == false) | |
727 | Res += ", "; | |
728 | else | |
729 | First = false; | |
730 | ||
731 | if (File->Label != 0) | |
732 | Res = Res + File.Label() + ':'; | |
733 | ||
734 | if (File->Archive != 0) | |
735 | { | |
736 | if (File->Version == 0) | |
737 | Res += File.Archive(); | |
738 | else | |
739 | Res = Res + File.Version() + '/' + File.Archive(); | |
740 | } | |
741 | else | |
742 | { | |
743 | // No release file, print the host name that this came from | |
744 | if (File->Site == 0 || File.Site()[0] == 0) | |
745 | Res += "localhost"; | |
746 | else | |
747 | Res += File.Site(); | |
748 | } | |
5dd4c8b8 | 749 | } |
857e9c13 | 750 | if (S->ParentPkg != 0) |
5dd4c8b8 | 751 | Res.append(" [").append(Arch()).append("]"); |
b2e465d6 AL |
752 | return Res; |
753 | } | |
754 | /*}}}*/ | |
578bfd0a AL |
755 | // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/ |
756 | // --------------------------------------------------------------------- | |
757 | /* This stats the file and compares its stats with the ones that were | |
758 | stored during generation. Date checks should probably also be | |
759 | included here. */ | |
760 | bool pkgCache::PkgFileIterator::IsOk() | |
761 | { | |
762 | struct stat Buf; | |
763 | if (stat(FileName(),&Buf) != 0) | |
764 | return false; | |
765 | ||
773e2c1f | 766 | if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime) |
578bfd0a AL |
767 | return false; |
768 | ||
769 | return true; | |
770 | } | |
771 | /*}}}*/ | |
af87ab54 AL |
772 | // PkgFileIterator::RelStr - Return the release string /*{{{*/ |
773 | // --------------------------------------------------------------------- | |
774 | /* */ | |
775 | string pkgCache::PkgFileIterator::RelStr() | |
776 | { | |
777 | string Res; | |
778 | if (Version() != 0) | |
779 | Res = Res + (Res.empty() == true?"v=":",v=") + Version(); | |
780 | if (Origin() != 0) | |
781 | Res = Res + (Res.empty() == true?"o=":",o=") + Origin(); | |
782 | if (Archive() != 0) | |
783 | Res = Res + (Res.empty() == true?"a=":",a=") + Archive(); | |
efc487fb DK |
784 | if (Codename() != 0) |
785 | Res = Res + (Res.empty() == true?"n=":",n=") + Codename(); | |
af87ab54 AL |
786 | if (Label() != 0) |
787 | Res = Res + (Res.empty() == true?"l=":",l=") + Label(); | |
788 | if (Component() != 0) | |
789 | Res = Res + (Res.empty() == true?"c=":",c=") + Component(); | |
5dd4c8b8 DK |
790 | if (Architecture() != 0) |
791 | Res = Res + (Res.empty() == true?"b=":",b=") + Architecture(); | |
af87ab54 AL |
792 | return Res; |
793 | } | |
794 | /*}}}*/ | |
012b102a MV |
795 | // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/ |
796 | // --------------------------------------------------------------------- | |
797 | /* return a DescIter for the current locale or the default if none is | |
798 | * found | |
799 | */ | |
800 | pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescription() const | |
801 | { | |
45df0ad2 DK |
802 | std::vector<string> const lang = APT::Configuration::getLanguages(); |
803 | for (std::vector<string>::const_iterator l = lang.begin(); | |
804 | l != lang.end(); l++) | |
805 | { | |
806 | pkgCache::DescIterator DescDefault = DescriptionList(); | |
807 | pkgCache::DescIterator Desc = DescDefault; | |
808 | ||
809 | for (; Desc.end() == false; Desc++) | |
810 | if (*l == Desc.LanguageCode()) | |
811 | break; | |
812 | if (Desc.end() == true) | |
813 | Desc = DescDefault; | |
814 | return Desc; | |
815 | } | |
816 | ||
817 | return DescriptionList(); | |
012b102a MV |
818 | }; |
819 | ||
820 | /*}}}*/ |