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