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