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