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