merge Goswin Brederlow "support download of index files for different archs"
[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
31 #include <apti18n.h>
32
33 #include <string>
34 #include <sys/stat.h>
35 #include <unistd.h>
36
37 #include <ctype.h>
38 #include <system.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 PackageSz = sizeof(pkgCache::Package);
59 PackageFileSz = sizeof(pkgCache::PackageFile);
60 VersionSz = sizeof(pkgCache::Version);
61 DescriptionSz = sizeof(pkgCache::Description);
62 DependencySz = sizeof(pkgCache::Dependency);
63 ProvidesSz = sizeof(pkgCache::Provides);
64 VerFileSz = sizeof(pkgCache::VerFile);
65 DescFileSz = sizeof(pkgCache::DescFile);
66
67 PackageCount = 0;
68 VersionCount = 0;
69 DescriptionCount = 0;
70 DependsCount = 0;
71 PackageFileCount = 0;
72 VerFileCount = 0;
73 DescFileCount = 0;
74 ProvidesCount = 0;
75 MaxVerFileSize = 0;
76 MaxDescFileSize = 0;
77
78 FileList = 0;
79 StringList = 0;
80 VerSysName = 0;
81 Architecture = 0;
82 memset(PkgHashTable,0,sizeof(PkgHashTable));
83 memset(GrpHashTable,0,sizeof(GrpHashTable));
84 memset(Pools,0,sizeof(Pools));
85 }
86 /*}}}*/
87 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
88 // ---------------------------------------------------------------------
89 /* */
90 bool pkgCache::Header::CheckSizes(Header &Against) const
91 {
92 if (HeaderSz == Against.HeaderSz &&
93 PackageSz == Against.PackageSz &&
94 PackageFileSz == Against.PackageFileSz &&
95 VersionSz == Against.VersionSz &&
96 DescriptionSz == Against.DescriptionSz &&
97 DependencySz == Against.DependencySz &&
98 VerFileSz == Against.VerFileSz &&
99 DescFileSz == Against.DescFileSz &&
100 ProvidesSz == Against.ProvidesSz)
101 return true;
102 return false;
103 }
104 /*}}}*/
105
106 // Cache::pkgCache - Constructor /*{{{*/
107 // ---------------------------------------------------------------------
108 /* */
109 pkgCache::pkgCache(MMap *Map, bool DoMap) : Map(*Map)
110 {
111 if (DoMap == true)
112 ReMap();
113 }
114 /*}}}*/
115 // Cache::ReMap - Reopen the cache file /*{{{*/
116 // ---------------------------------------------------------------------
117 /* If the file is already closed then this will open it open it. */
118 bool pkgCache::ReMap()
119 {
120 // Apply the typecasts.
121 HeaderP = (Header *)Map.Data();
122 GrpP = (Group *)Map.Data();
123 PkgP = (Package *)Map.Data();
124 VerFileP = (VerFile *)Map.Data();
125 DescFileP = (DescFile *)Map.Data();
126 PkgFileP = (PackageFile *)Map.Data();
127 VerP = (Version *)Map.Data();
128 DescP = (Description *)Map.Data();
129 ProvideP = (Provides *)Map.Data();
130 DepP = (Dependency *)Map.Data();
131 StringItemP = (StringItem *)Map.Data();
132 StrP = (char *)Map.Data();
133
134 if (Map.Size() == 0 || HeaderP == 0)
135 return _error->Error(_("Empty package cache"));
136
137 // Check the header
138 Header DefHeader;
139 if (HeaderP->Signature != DefHeader.Signature ||
140 HeaderP->Dirty == true)
141 return _error->Error(_("The package cache file is corrupted"));
142
143 if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
144 HeaderP->MinorVersion != DefHeader.MinorVersion ||
145 HeaderP->CheckSizes(DefHeader) == false)
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)
151 return _error->Error(_("This APT does not support the versioning system '%s'"),StrP + HeaderP->VerSysName);
152
153 // Chcek the arhcitecture
154 if (HeaderP->Architecture == 0 ||
155 _config->Find("APT::Architecture") != StrP + HeaderP->Architecture)
156 return _error->Error(_("The package cache was built for a different architecture"));
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) */
165 unsigned long pkgCache::sHash(const string &Str) const
166 {
167 unsigned long Hash = 0;
168 for (string::const_iterator I = Str.begin(); I != Str.end(); I++)
169 Hash = 5*Hash + tolower_ascii(*I);
170 return Hash % _count(HeaderP->PkgHashTable);
171 }
172
173 unsigned long pkgCache::sHash(const char *Str) const
174 {
175 unsigned long Hash = 0;
176 for (const char *I = Str; *I != 0; I++)
177 Hash = 5*Hash + tolower_ascii(*I);
178 return Hash % _count(HeaderP->PkgHashTable);
179 }
180
181 /*}}}*/
182 // Cache::FindPkg - Locate a package by name /*{{{*/
183 // ---------------------------------------------------------------------
184 /* Returns 0 on error, pointer to the package otherwise */
185 pkgCache::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 */
199 pkgCache::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);
212 }
213 /*}}}*/
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.. */
218 const 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 */
230 const 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 /* */
241 const char *pkgCache::DepType(unsigned char Type)
242 {
243 const char *Types[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
244 _("Recommends"),_("Conflicts"),_("Replaces"),
245 _("Obsoletes"),_("Breaks"), _("Enhances")};
246 if (Type < sizeof(Types)/sizeof(*Types))
247 return Types[Type];
248 return "";
249 }
250 /*}}}*/
251 // Cache::Priority - Convert a priority value to a string /*{{{*/
252 // ---------------------------------------------------------------------
253 /* */
254 const char *pkgCache::Priority(unsigned char Prio)
255 {
256 const char *Mapping[] = {0,_("important"),_("required"),_("standard"),
257 _("optional"),_("extra")};
258 if (Prio < _count(Mapping))
259 return Mapping[Prio];
260 return 0;
261 }
262 /*}}}*/
263 // GrpIterator::FindPkg - Locate a package by arch /*{{{*/
264 // ---------------------------------------------------------------------
265 /* Returns an End-Pointer on error, pointer to the package otherwise */
266 pkgCache::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) */
307 pkgCache::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 /*}}}*/
325 // PkgIterator::operator ++ - Postfix incr /*{{{*/
326 // ---------------------------------------------------------------------
327 /* This will advance to the next logical package in the hash table. */
328 void pkgCache::PkgIterator::operator ++(int)
329 {
330 // Follow the current links
331 if (S != Owner->PkgP)
332 S = Owner->PkgP + S->NextPackage;
333
334 // Follow the hash table
335 while (S == Owner->PkgP && (HashIndex+1) < (signed)_count(Owner->HeaderP->PkgHashTable))
336 {
337 HashIndex++;
338 S = Owner->PkgP + Owner->HeaderP->PkgHashTable[HashIndex];
339 }
340 };
341 /*}}}*/
342 // PkgIterator::State - Check the State of the package /*{{{*/
343 // ---------------------------------------------------------------------
344 /* By this we mean if it is either cleanly installed or cleanly removed. */
345 pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
346 {
347 if (S->InstState == pkgCache::State::ReInstReq ||
348 S->InstState == pkgCache::State::HoldReInstReq)
349 return NeedsUnpack;
350
351 if (S->CurrentState == pkgCache::State::UnPacked ||
352 S->CurrentState == pkgCache::State::HalfConfigured)
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
357 //Pkg->CurrentState == pkgCache::State::TriggersAwaited
358 //Pkg->CurrentState == pkgCache::State::TriggersPending)
359 return NeedsConfigure;
360
361 if (S->CurrentState == pkgCache::State::HalfInstalled ||
362 S->InstState != pkgCache::State::Ok)
363 return NeedsUnpack;
364
365 return NeedsNothing;
366 }
367 /*}}}*/
368 // PkgIterator::CandVersion - Returns the candidate version string /*{{{*/
369 // ---------------------------------------------------------------------
370 /* Return string representing of the candidate version. */
371 const char *
372 pkgCache::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. */
384 const char *
385 pkgCache::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. */
399 std::ostream&
400 operator<<(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() << " [ " << Pkg.Arch() << " ] < " << 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 /*}}}*/
418 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
419 // ---------------------------------------------------------------------
420 /* Currently critical deps are defined as depends, predepends and
421 conflicts (including dpkg's Breaks fields). */
422 bool pkgCache::DepIterator::IsCritical()
423 {
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)
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
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. */
446 bool 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 }
479
480 Result = PStart.OwnerPkg();
481
482 // Check for non dups
483 if (P.end() != true)
484 return true;
485
486 return false;
487 }
488 /*}}}*/
489 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
490 // ---------------------------------------------------------------------
491 /* This is a more useful version of TargetPkg() that follows versioned
492 provides. It includes every possible package-version that could satisfy
493 the dependency. The last item in the list has a 0. The resulting pointer
494 must be delete [] 'd */
495 pkgCache::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 {
507 if (Owner->VS->CheckDep(I.VerStr(),S->CompareOp,TargetVer()) == false)
508 continue;
509
510 if ((S->Type == pkgCache::Dep::Conflicts ||
511 S->Type == pkgCache::Dep::DpkgBreaks ||
512 S->Type == pkgCache::Dep::Obsoletes) &&
513 ParentPkg() == I.ParentPkg())
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 {
524 if (Owner->VS->CheckDep(I.ProvideVersion(),S->CompareOp,TargetVer()) == false)
525 continue;
526
527 if ((S->Type == pkgCache::Dep::Conflicts ||
528 S->Type == pkgCache::Dep::DpkgBreaks ||
529 S->Type == pkgCache::Dep::Obsoletes) &&
530 ParentPkg() == I.OwnerPkg())
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 /*}}}*/
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. */
560 void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
561 {
562 // Compute a single dependency element (glob or)
563 Start = *this;
564 End = *this;
565 for (bool LastOR = true; end() == false && LastOR == true;)
566 {
567 LastOR = (S->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
568 (*this)++;
569 if (LastOR == true)
570 End = (*this);
571 }
572 }
573 /*}}}*/
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. */
578 int 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 /*}}}*/
597 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
598 // ---------------------------------------------------------------------
599 /* */
600 bool pkgCache::VerIterator::Downloadable() const
601 {
602 VerFileIterator Files = FileList();
603 for (; Files.end() == false; Files++)
604 if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource)
605 return true;
606 return false;
607 }
608 /*}}}*/
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. */
613 bool 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.*/
626 pkgCache::VerFileIterator pkgCache::VerIterator::NewestFile() const
627 {
628 VerFileIterator Files = FileList();
629 VerFileIterator Highest = Files;
630 for (; Files.end() == false; Files++)
631 {
632 if (Owner->VS->CmpReleaseVer(Files.File().Version(),Highest.File().Version()) > 0)
633 Highest = Files;
634 }
635
636 return Highest;
637 }
638 /*}}}*/
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 */
643 string 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 }
670 if (File2->Version == 0 || File->Version == 0)
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 if (S->Arch != 0)
704 Res.append(" [").append(Arch()).append("]");
705 return Res;
706 }
707 /*}}}*/
708 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
709 // ---------------------------------------------------------------------
710 /* This stats the file and compares its stats with the ones that were
711 stored during generation. Date checks should probably also be
712 included here. */
713 bool pkgCache::PkgFileIterator::IsOk()
714 {
715 struct stat Buf;
716 if (stat(FileName(),&Buf) != 0)
717 return false;
718
719 if (Buf.st_size != (signed)S->Size || Buf.st_mtime != S->mtime)
720 return false;
721
722 return true;
723 }
724 /*}}}*/
725 // PkgFileIterator::RelStr - Return the release string /*{{{*/
726 // ---------------------------------------------------------------------
727 /* */
728 string pkgCache::PkgFileIterator::RelStr()
729 {
730 string Res;
731 if (Version() != 0)
732 Res = Res + (Res.empty() == true?"v=":",v=") + Version();
733 if (Origin() != 0)
734 Res = Res + (Res.empty() == true?"o=":",o=") + Origin();
735 if (Archive() != 0)
736 Res = Res + (Res.empty() == true?"a=":",a=") + Archive();
737 if (Codename() != 0)
738 Res = Res + (Res.empty() == true?"n=":",n=") + Codename();
739 if (Label() != 0)
740 Res = Res + (Res.empty() == true?"l=":",l=") + Label();
741 if (Component() != 0)
742 Res = Res + (Res.empty() == true?"c=":",c=") + Component();
743 if (Architecture() != 0)
744 Res = Res + (Res.empty() == true?"b=":",b=") + Architecture();
745 return Res;
746 }
747 /*}}}*/
748 // VerIterator::TranslatedDescription - Return the a DescIter for locale/*{{{*/
749 // ---------------------------------------------------------------------
750 /* return a DescIter for the current locale or the default if none is
751 * found
752 */
753 pkgCache::DescIterator pkgCache::VerIterator::TranslatedDescription() const
754 {
755 std::vector<string> const lang = APT::Configuration::getLanguages();
756 for (std::vector<string>::const_iterator l = lang.begin();
757 l != lang.end(); l++)
758 {
759 pkgCache::DescIterator DescDefault = DescriptionList();
760 pkgCache::DescIterator Desc = DescDefault;
761
762 for (; Desc.end() == false; Desc++)
763 if (*l == Desc.LanguageCode())
764 break;
765 if (Desc.end() == true)
766 Desc = DescDefault;
767 return Desc;
768 }
769
770 return DescriptionList();
771 };
772
773 /*}}}*/