]>
git.saurik.com Git - apt.git/blob - apt-pkg/pkgcache.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: pkgcache.cc,v 1.37 2003/02/10 01:40:58 doogie Exp $
4 /* ######################################################################
6 Package Cache - Accessor code for the cache
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!!
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.
17 The main class provides for ways to get package indexes and some
18 general lookup functions to start the iterators.
20 ##################################################################### */
22 // Include Files /*{{{*/
23 #include <apt-pkg/pkgcache.h>
24 #include <apt-pkg/version.h>
25 #include <apt-pkg/error.h>
26 #include <apt-pkg/strutl.h>
27 #include <apt-pkg/configuration.h>
41 // Cache::Header::Header - Constructor /*{{{*/
42 // ---------------------------------------------------------------------
43 /* Simply initialize the header */
44 pkgCache::Header::Header()
46 Signature
= 0x98FE76DC;
48 /* Whenever the structures change the major version should be bumped,
49 whenever the generator changes the minor version should be bumped. */
54 HeaderSz
= sizeof(pkgCache::Header
);
55 PackageSz
= sizeof(pkgCache::Package
);
56 PackageFileSz
= sizeof(pkgCache::PackageFile
);
57 VersionSz
= sizeof(pkgCache::Version
);
58 DependencySz
= sizeof(pkgCache::Dependency
);
59 ProvidesSz
= sizeof(pkgCache::Provides
);
60 VerFileSz
= sizeof(pkgCache::VerFile
);
74 memset(HashTable
,0,sizeof(HashTable
));
75 memset(Pools
,0,sizeof(Pools
));
78 // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
79 // ---------------------------------------------------------------------
81 bool pkgCache::Header::CheckSizes(Header
&Against
) const
83 if (HeaderSz
== Against
.HeaderSz
&&
84 PackageSz
== Against
.PackageSz
&&
85 PackageFileSz
== Against
.PackageFileSz
&&
86 VersionSz
== Against
.VersionSz
&&
87 DependencySz
== Against
.DependencySz
&&
88 VerFileSz
== Against
.VerFileSz
&&
89 ProvidesSz
== Against
.ProvidesSz
)
95 // Cache::pkgCache - Constructor /*{{{*/
96 // ---------------------------------------------------------------------
98 pkgCache::pkgCache(MMap
*Map
, bool DoMap
) : Map(*Map
)
104 // Cache::ReMap - Reopen the cache file /*{{{*/
105 // ---------------------------------------------------------------------
106 /* If the file is already closed then this will open it open it. */
107 bool pkgCache::ReMap()
109 // Apply the typecasts.
110 HeaderP
= (Header
*)Map
.Data();
111 PkgP
= (Package
*)Map
.Data();
112 VerFileP
= (VerFile
*)Map
.Data();
113 PkgFileP
= (PackageFile
*)Map
.Data();
114 VerP
= (Version
*)Map
.Data();
115 ProvideP
= (Provides
*)Map
.Data();
116 DepP
= (Dependency
*)Map
.Data();
117 StringItemP
= (StringItem
*)Map
.Data();
118 StrP
= (char *)Map
.Data();
120 if (Map
.Size() == 0 || HeaderP
== 0)
121 return _error
->Error(_("Empty package cache"));
125 if (HeaderP
->Signature
!= DefHeader
.Signature
||
126 HeaderP
->Dirty
== true)
127 return _error
->Error(_("The package cache file is corrupted"));
129 if (HeaderP
->MajorVersion
!= DefHeader
.MajorVersion
||
130 HeaderP
->MinorVersion
!= DefHeader
.MinorVersion
||
131 HeaderP
->CheckSizes(DefHeader
) == false)
132 return _error
->Error(_("The package cache file is an incompatible version"));
135 if (HeaderP
->VerSysName
== 0 ||
136 (VS
= pkgVersioningSystem::GetVS(StrP
+ HeaderP
->VerSysName
)) == 0)
137 return _error
->Error(_("This APT does not support the versioning system '%s'"),StrP
+ HeaderP
->VerSysName
);
139 // Chcek the arhcitecture
140 if (HeaderP
->Architecture
== 0 ||
141 _config
->Find("APT::Architecture") != StrP
+ HeaderP
->Architecture
)
142 return _error
->Error(_("The package cache was built for a different architecture"));
146 // Cache::Hash - Hash a string /*{{{*/
147 // ---------------------------------------------------------------------
148 /* This is used to generate the hash entries for the HashTable. With my
149 package list from bo this function gets 94% table usage on a 512 item
150 table (480 used items) */
151 unsigned long pkgCache::sHash(const string
&Str
) const
153 unsigned long Hash
= 0;
154 for (string::const_iterator I
= Str
.begin(); I
!= Str
.end(); I
++)
155 Hash
= 5*Hash
+ tolower(*I
);
156 return Hash
% _count(HeaderP
->HashTable
);
159 unsigned long pkgCache::sHash(const char *Str
) const
161 unsigned long Hash
= 0;
162 for (const char *I
= Str
; *I
!= 0; I
++)
163 Hash
= 5*Hash
+ tolower(*I
);
164 return Hash
% _count(HeaderP
->HashTable
);
168 // Cache::FindPkg - Locate a package by name /*{{{*/
169 // ---------------------------------------------------------------------
170 /* Returns 0 on error, pointer to the package otherwise */
171 pkgCache::PkgIterator
pkgCache::FindPkg(const string
&Name
)
173 // Look at the hash bucket
174 Package
*Pkg
= PkgP
+ HeaderP
->HashTable
[Hash(Name
)];
175 for (; Pkg
!= PkgP
; Pkg
= PkgP
+ Pkg
->NextPackage
)
177 if (Pkg
->Name
!= 0 && StrP
[Pkg
->Name
] == Name
[0] &&
178 stringcasecmp(Name
,StrP
+ Pkg
->Name
) == 0)
179 return PkgIterator(*this,Pkg
);
181 return PkgIterator(*this,0);
184 // Cache::CompTypeDeb - Return a string describing the compare type /*{{{*/
185 // ---------------------------------------------------------------------
186 /* This returns a string representation of the dependency compare
187 type in the weird debian style.. */
188 const char *pkgCache::CompTypeDeb(unsigned char Comp
)
190 const char *Ops
[] = {"","<=",">=","<<",">>","=","!="};
191 if ((unsigned)(Comp
& 0xF) < 7)
192 return Ops
[Comp
& 0xF];
196 // Cache::CompType - Return a string describing the compare type /*{{{*/
197 // ---------------------------------------------------------------------
198 /* This returns a string representation of the dependency compare
200 const char *pkgCache::CompType(unsigned char Comp
)
202 const char *Ops
[] = {"","<=",">=","<",">","=","!="};
203 if ((unsigned)(Comp
& 0xF) < 7)
204 return Ops
[Comp
& 0xF];
208 // Cache::DepType - Return a string describing the dep type /*{{{*/
209 // ---------------------------------------------------------------------
211 const char *pkgCache::DepType(unsigned char Type
)
213 const char *Types
[] = {"",_("Depends"),_("PreDepends"),_("Suggests"),
214 _("Recommends"),_("Conflicts"),_("Replaces"),
221 // Cache::Priority - Convert a priority value to a string /*{{{*/
222 // ---------------------------------------------------------------------
224 const char *pkgCache::Priority(unsigned char Prio
)
226 const char *Mapping
[] = {0,_("important"),_("required"),_("standard"),
227 _("optional"),_("extra")};
228 if (Prio
< _count(Mapping
))
229 return Mapping
[Prio
];
234 // Bases for iterator classes /*{{{*/
235 void pkgCache::VerIterator::_dummy() {}
236 void pkgCache::DepIterator::_dummy() {}
237 void pkgCache::PrvIterator::_dummy() {}
239 // PkgIterator::operator ++ - Postfix incr /*{{{*/
240 // ---------------------------------------------------------------------
241 /* This will advance to the next logical package in the hash table. */
242 void pkgCache::PkgIterator::operator ++(int)
244 // Follow the current links
245 if (Pkg
!= Owner
->PkgP
)
246 Pkg
= Owner
->PkgP
+ Pkg
->NextPackage
;
248 // Follow the hash table
249 while (Pkg
== Owner
->PkgP
&& (HashIndex
+1) < (signed)_count(Owner
->HeaderP
->HashTable
))
252 Pkg
= Owner
->PkgP
+ Owner
->HeaderP
->HashTable
[HashIndex
];
256 // PkgIterator::State - Check the State of the package /*{{{*/
257 // ---------------------------------------------------------------------
258 /* By this we mean if it is either cleanly installed or cleanly removed. */
259 pkgCache::PkgIterator::OkState
pkgCache::PkgIterator::State() const
261 if (Pkg
->InstState
== pkgCache::State::ReInstReq
||
262 Pkg
->InstState
== pkgCache::State::HoldReInstReq
)
265 if (Pkg
->CurrentState
== pkgCache::State::UnPacked
||
266 Pkg
->CurrentState
== pkgCache::State::HalfConfigured
)
267 return NeedsConfigure
;
269 if (Pkg
->CurrentState
== pkgCache::State::HalfInstalled
||
270 Pkg
->InstState
!= pkgCache::State::Ok
)
276 // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
277 // ---------------------------------------------------------------------
278 /* Currently critical deps are defined as depends, predepends and
280 bool pkgCache::DepIterator::IsCritical()
282 if (Dep
->Type
== pkgCache::Dep::Conflicts
||
283 Dep
->Type
== pkgCache::Dep::Obsoletes
||
284 Dep
->Type
== pkgCache::Dep::Depends
||
285 Dep
->Type
== pkgCache::Dep::PreDepends
)
290 // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
291 // ---------------------------------------------------------------------
292 /* This intellegently looks at dep target packages and tries to figure
293 out which package should be used. This is needed to nicely handle
294 provide mapping. If the target package has no other providing packages
295 then it returned. Otherwise the providing list is looked at to
296 see if there is one one unique providing package if so it is returned.
297 Otherwise true is returned and the target package is set. The return
298 result indicates whether the node should be expandable
300 In Conjunction with the DepCache the value of Result may not be
301 super-good since the policy may have made it uninstallable. Using
302 AllTargets is better in this case. */
303 bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator
&Result
)
305 Result
= TargetPkg();
307 // No provides at all
308 if (Result
->ProvidesList
== 0)
311 // There is the Base package and the providing ones which is at least 2
312 if (Result
->VersionList
!= 0)
315 /* We have to skip over indirect provisions of the package that
316 owns the dependency. For instance, if libc5-dev depends on the
317 virtual package libc-dev which is provided by libc5-dev and libc6-dev
318 we must ignore libc5-dev when considering the provides list. */
319 PrvIterator PStart
= Result
.ProvidesList();
320 for (; PStart
.end() != true && PStart
.OwnerPkg() == ParentPkg(); PStart
++);
322 // Nothing but indirect self provides
323 if (PStart
.end() == true)
326 // Check for single packages in the provides list
327 PrvIterator P
= PStart
;
328 for (; P
.end() != true; P
++)
330 // Skip over self provides
331 if (P
.OwnerPkg() == ParentPkg())
333 if (PStart
.OwnerPkg() != P
.OwnerPkg())
337 Result
= PStart
.OwnerPkg();
339 // Check for non dups
346 // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
347 // ---------------------------------------------------------------------
348 /* This is a more useful version of TargetPkg() that follows versioned
349 provides. It includes every possible package-version that could satisfy
350 the dependency. The last item in the list has a 0. The resulting pointer
351 must be delete [] 'd */
352 pkgCache::Version
**pkgCache::DepIterator::AllTargets()
355 unsigned long Size
=0;
359 PkgIterator DPkg
= TargetPkg();
361 // Walk along the actual package providing versions
362 for (VerIterator I
= DPkg
.VersionList(); I
.end() == false; I
++)
364 if (Owner
->VS
->CheckDep(I
.VerStr(),Dep
->CompareOp
,TargetVer()) == false)
367 if ((Dep
->Type
== pkgCache::Dep::Conflicts
||
368 Dep
->Type
== pkgCache::Dep::Obsoletes
) &&
369 ParentPkg() == I
.ParentPkg())
377 // Follow all provides
378 for (PrvIterator I
= DPkg
.ProvidesList(); I
.end() == false; I
++)
380 if (Owner
->VS
->CheckDep(I
.ProvideVersion(),Dep
->CompareOp
,TargetVer()) == false)
383 if ((Dep
->Type
== pkgCache::Dep::Conflicts
||
384 Dep
->Type
== pkgCache::Dep::Obsoletes
) &&
385 ParentPkg() == I
.OwnerPkg())
390 *End
++ = I
.OwnerVer();
393 // Do it again and write it into the array
396 Res
= new Version
*[Size
+1];
409 // DepIterator::GlobOr - Compute an OR group /*{{{*/
410 // ---------------------------------------------------------------------
411 /* This Takes an iterator, iterates past the current dependency grouping
412 and returns Start and End so that so End is the final element
413 in the group, if End == Start then D is End++ and End is the
414 dependency D was pointing to. Use in loops to iterate sensibly. */
415 void pkgCache::DepIterator::GlobOr(DepIterator
&Start
,DepIterator
&End
)
417 // Compute a single dependency element (glob or)
420 for (bool LastOR
= true; end() == false && LastOR
== true;)
422 LastOR
= (Dep
->CompareOp
& pkgCache::Dep::Or
) == pkgCache::Dep::Or
;
429 // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
430 // ---------------------------------------------------------------------
431 /* This just looks over the version list to see if B is listed before A. In
432 most cases this will return in under 4 checks, ver lists are short. */
433 int pkgCache::VerIterator::CompareVer(const VerIterator
&B
) const
435 // Check if they are equal
443 /* Start at A and look for B. If B is found then A > B otherwise
444 B was before A so A < B */
445 VerIterator I
= *this;
446 for (;I
.end() == false; I
++)
452 // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
453 // ---------------------------------------------------------------------
455 bool pkgCache::VerIterator::Downloadable() const
457 VerFileIterator Files
= FileList();
458 for (; Files
.end() == false; Files
++)
459 if ((Files
.File()->Flags
& pkgCache::Flag::NotSource
) != pkgCache::Flag::NotSource
)
464 // VerIterator::Automatic - Check if this version is 'automatic' /*{{{*/
465 // ---------------------------------------------------------------------
466 /* This checks to see if any of the versions files are not NotAutomatic.
467 True if this version is selectable for automatic installation. */
468 bool pkgCache::VerIterator::Automatic() const
470 VerFileIterator Files
= FileList();
471 for (; Files
.end() == false; Files
++)
472 if ((Files
.File()->Flags
& pkgCache::Flag::NotAutomatic
) != pkgCache::Flag::NotAutomatic
)
477 // VerIterator::NewestFile - Return the newest file version relation /*{{{*/
478 // ---------------------------------------------------------------------
479 /* This looks at the version numbers associated with all of the sources
480 this version is in and returns the highest.*/
481 pkgCache::VerFileIterator
pkgCache::VerIterator::NewestFile() const
483 VerFileIterator Files
= FileList();
484 VerFileIterator Highest
= Files
;
485 for (; Files
.end() == false; Files
++)
487 if (Owner
->VS
->CmpReleaseVer(Files
.File().Version(),Highest
.File().Version()) > 0)
494 // VerIterator::RelStr - Release description string /*{{{*/
495 // ---------------------------------------------------------------------
496 /* This describes the version from a release-centric manner. The output is a
497 list of Label:Version/Archive */
498 string
pkgCache::VerIterator::RelStr()
502 for (pkgCache::VerFileIterator I
= this->FileList(); I
.end() == false; I
++)
504 // Do not print 'not source' entries'
505 pkgCache::PkgFileIterator File
= I
.File();
506 if ((File
->Flags
& pkgCache::Flag::NotSource
) == pkgCache::Flag::NotSource
)
509 // See if we have already printed this out..
511 for (pkgCache::VerFileIterator J
= this->FileList(); I
!= J
; J
++)
513 pkgCache::PkgFileIterator File2
= J
.File();
514 if (File2
->Label
== 0 || File
->Label
== 0)
517 if (strcmp(File
.Label(),File2
.Label()) != 0)
520 if (File2
->Version
== File
->Version
)
525 if (File2
->Version
== 0 || File
->Version
== 0)
527 if (strcmp(File
.Version(),File2
.Version()) == 0)
539 if (File
->Label
!= 0)
540 Res
= Res
+ File
.Label() + ':';
542 if (File
->Archive
!= 0)
544 if (File
->Version
== 0)
545 Res
+= File
.Archive();
547 Res
= Res
+ File
.Version() + '/' + File
.Archive();
551 // No release file, print the host name that this came from
552 if (File
->Site
== 0 || File
.Site()[0] == 0)
561 // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
562 // ---------------------------------------------------------------------
563 /* This stats the file and compares its stats with the ones that were
564 stored during generation. Date checks should probably also be
566 bool pkgCache::PkgFileIterator::IsOk()
569 if (stat(FileName(),&Buf
) != 0)
572 if (Buf
.st_size
!= (signed)File
->Size
|| Buf
.st_mtime
!= File
->mtime
)
578 // PkgFileIterator::RelStr - Return the release string /*{{{*/
579 // ---------------------------------------------------------------------
581 string
pkgCache::PkgFileIterator::RelStr()
585 Res
= Res
+ (Res
.empty() == true?"v=":",v=") + Version();
587 Res
= Res
+ (Res
.empty() == true?"o=":",o=") + Origin();
589 Res
= Res
+ (Res
.empty() == true?"a=":",a=") + Archive();
591 Res
= Res
+ (Res
.empty() == true?"l=":",l=") + Label();
592 if (Component() != 0)
593 Res
= Res
+ (Res
.empty() == true?"c=":",c=") + Component();