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