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