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