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