| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454 |
- // -*- mode: cpp; mode: fold -*-
- // Description /*{{{*/
- // $Id: pkgcache.cc,v 1.18 1998/11/27 00:07:24 jgg Exp $
- /* ######################################################################
-
- Package Cache - Accessor code for the cache
-
- Please see doc/apt-pkg/cache.sgml for a more detailed description of
- this format. Also be sure to keep that file up-to-date!!
-
- This is the general utility functions for cache managment. They provide
- a complete set of accessor functions for the cache. The cacheiterators
- header contains the STL-like iterators that can be used to easially
- navigate the cache as well as seemlessly dereference the mmap'd
- indexes. Use these always.
-
- The main class provides for ways to get package indexes and some
- general lookup functions to start the iterators.
- ##################################################################### */
- /*}}}*/
- // Include Files /*{{{*/
- #ifdef __GNUG__
- #pragma implementation "apt-pkg/pkgcache.h"
- #pragma implementation "apt-pkg/cacheiterators.h"
- #endif
- #include <apt-pkg/pkgcache.h>
- #include <apt-pkg/version.h>
- #include <apt-pkg/error.h>
- #include <system.h>
- #include <string>
- #include <sys/stat.h>
- #include <unistd.h>
- /*}}}*/
- // Cache::Header::Header - Constructor /*{{{*/
- // ---------------------------------------------------------------------
- /* Simply initialize the header */
- pkgCache::Header::Header()
- {
- Signature = 0x98FE76DC;
-
- /* Whenever the structures change the major version should be bumped,
- whenever the generator changes the minor version should be bumped. */
- MajorVersion = 2;
- MinorVersion = 2;
- Dirty = true;
-
- HeaderSz = sizeof(pkgCache::Header);
- PackageSz = sizeof(pkgCache::Package);
- PackageFileSz = sizeof(pkgCache::PackageFile);
- VersionSz = sizeof(pkgCache::Version);
- DependencySz = sizeof(pkgCache::Dependency);
- ProvidesSz = sizeof(pkgCache::Provides);
- VerFileSz = sizeof(pkgCache::VerFile);
-
- PackageCount = 0;
- VersionCount = 0;
- DependsCount = 0;
- PackageFileCount = 0;
- MaxVerFileSize = 0;
-
- FileList = 0;
- StringList = 0;
- memset(HashTable,0,sizeof(HashTable));
- memset(Pools,0,sizeof(Pools));
- }
- /*}}}*/
- // Cache::Header::CheckSizes - Check if the two headers have same *sz /*{{{*/
- // ---------------------------------------------------------------------
- /* */
- bool pkgCache::Header::CheckSizes(Header &Against) const
- {
- if (HeaderSz == Against.HeaderSz &&
- PackageSz == Against.PackageSz &&
- PackageFileSz == Against.PackageFileSz &&
- VersionSz == Against.VersionSz &&
- DependencySz == Against.DependencySz &&
- VerFileSz == Against.VerFileSz &&
- ProvidesSz == Against.ProvidesSz)
- return true;
- return false;
- }
- /*}}}*/
- // Cache::pkgCache - Constructor /*{{{*/
- // ---------------------------------------------------------------------
- /* */
- pkgCache::pkgCache(MMap &Map) : Map(Map)
- {
- ReMap();
- }
- /*}}}*/
- // Cache::ReMap - Reopen the cache file /*{{{*/
- // ---------------------------------------------------------------------
- /* If the file is already closed then this will open it open it. */
- bool pkgCache::ReMap()
- {
- // Apply the typecasts.
- HeaderP = (Header *)Map.Data();
- PkgP = (Package *)Map.Data();
- VerFileP = (VerFile *)Map.Data();
- PkgFileP = (PackageFile *)Map.Data();
- VerP = (Version *)Map.Data();
- ProvideP = (Provides *)Map.Data();
- DepP = (Dependency *)Map.Data();
- StringItemP = (StringItem *)Map.Data();
- StrP = (char *)Map.Data();
- if (Map.Size() == 0)
- return false;
-
- // Check the header
- Header DefHeader;
- if (HeaderP->Signature != DefHeader.Signature ||
- HeaderP->Dirty == true)
- return _error->Error("The package cache file is corrupted");
-
- if (HeaderP->MajorVersion != DefHeader.MajorVersion ||
- HeaderP->MinorVersion != DefHeader.MinorVersion ||
- HeaderP->CheckSizes(DefHeader) == false)
- return _error->Error("The package cache file is an incompatible version");
-
- return true;
- }
- /*}}}*/
- // Cache::Hash - Hash a string /*{{{*/
- // ---------------------------------------------------------------------
- /* This is used to generate the hash entries for the HashTable. With my
- package list from bo this function gets 94% table usage on a 512 item
- table (480 used items) */
- unsigned long pkgCache::sHash(string Str)
- {
- unsigned long Hash = 0;
- for (const char *I = Str.begin(); I != Str.end(); I++)
- Hash += *I * ((Str.end() - I + 1));
- Header H;
- return Hash % _count(H.HashTable);
- }
- unsigned long pkgCache::sHash(const char *Str)
- {
- unsigned long Hash = 0;
- const char *End = Str + strlen(Str);
- for (const char *I = Str; I != End; I++)
- Hash += *I * ((End - I + 1));
- Header H;
- return Hash % _count(H.HashTable);
- }
- /*}}}*/
- // Cache::FindPkg - Locate a package by name /*{{{*/
- // ---------------------------------------------------------------------
- /* Returns 0 on error, pointer to the package otherwise */
- pkgCache::PkgIterator pkgCache::FindPkg(string Name)
- {
- // Look at the hash bucket
- Package *Pkg = PkgP + HeaderP->HashTable[Hash(Name)];
- for (; Pkg != PkgP; Pkg = PkgP + Pkg->NextPackage)
- {
- if (Pkg->Name != 0 && StrP + Pkg->Name == Name)
- return PkgIterator(*this,Pkg);
- }
- return PkgIterator(*this,0);
- }
- /*}}}*/
- // Cache::Priority - Convert a priority value to a string /*{{{*/
- // ---------------------------------------------------------------------
- /* */
- const char *pkgCache::Priority(unsigned char Prio)
- {
- const char *Mapping[] = {0,"important","required","standard","optional","extra"};
- if (Prio < _count(Mapping))
- return Mapping[Prio];
- return 0;
- }
- /*}}}*/
- // Bases for iterator classes /*{{{*/
- void pkgCache::VerIterator::_dummy() {}
- void pkgCache::DepIterator::_dummy() {}
- void pkgCache::PrvIterator::_dummy() {}
- /*}}}*/
- // PkgIterator::operator ++ - Postfix incr /*{{{*/
- // ---------------------------------------------------------------------
- /* This will advance to the next logical package in the hash table. */
- void pkgCache::PkgIterator::operator ++(int)
- {
- // Follow the current links
- if (Pkg != Owner->PkgP)
- Pkg = Owner->PkgP + Pkg->NextPackage;
-
- // Follow the hash table
- while (Pkg == Owner->PkgP && HashIndex < (signed)_count(Owner->HeaderP->HashTable))
- {
- HashIndex++;
- Pkg = Owner->PkgP + Owner->HeaderP->HashTable[HashIndex];
- }
- };
- /*}}}*/
- // PkgIterator::State - Check the State of the package /*{{{*/
- // ---------------------------------------------------------------------
- /* By this we mean if it is either cleanly installed or cleanly removed. */
- pkgCache::PkgIterator::OkState pkgCache::PkgIterator::State() const
- {
- if (Pkg->InstState == pkgCache::State::ReInstReq ||
- Pkg->InstState == pkgCache::State::HoldReInstReq)
- return NeedsUnpack;
-
- if (Pkg->CurrentState == pkgCache::State::UnPacked ||
- Pkg->CurrentState == pkgCache::State::HalfConfigured)
- return NeedsConfigure;
-
- if (Pkg->CurrentState == pkgCache::State::UnInstalled ||
- Pkg->CurrentState == pkgCache::State::HalfInstalled ||
- Pkg->InstState != pkgCache::State::Ok)
- return NeedsUnpack;
-
- return NeedsNothing;
- }
- /*}}}*/
- // DepIterator::IsCritical - Returns true if the dep is important /*{{{*/
- // ---------------------------------------------------------------------
- /* Currently critical deps are defined as depends, predepends and
- conflicts. */
- bool pkgCache::DepIterator::IsCritical()
- {
- if (Dep->Type == pkgCache::Dep::Conflicts ||
- Dep->Type == pkgCache::Dep::Depends ||
- Dep->Type == pkgCache::Dep::PreDepends)
- return true;
- return false;
- }
- /*}}}*/
- // DepIterator::SmartTargetPkg - Resolve dep target pointers w/provides /*{{{*/
- // ---------------------------------------------------------------------
- /* This intellegently looks at dep target packages and tries to figure
- out which package should be used. This is needed to nicely handle
- provide mapping. If the target package has no other providing packages
- then it returned. Otherwise the providing list is looked at to
- see if there is one one unique providing package if so it is returned.
- Otherwise true is returned and the target package is set. The return
- result indicates whether the node should be expandable */
- bool pkgCache::DepIterator::SmartTargetPkg(PkgIterator &Result)
- {
- Result = TargetPkg();
-
- // No provides at all
- if (Result->ProvidesList == 0)
- return false;
-
- // There is the Base package and the providing ones which is at least 2
- if (Result->VersionList != 0)
- return true;
-
- /* We have to skip over indirect provisions of the package that
- owns the dependency. For instance, if libc5-dev depends on the
- virtual package libc-dev which is provided by libc5-dev and libc6-dev
- we must ignore libc5-dev when considering the provides list. */
- PrvIterator PStart = Result.ProvidesList();
- for (; PStart.end() != true && PStart.OwnerPkg() == ParentPkg(); PStart++);
- // Nothing but indirect self provides
- if (PStart.end() == true)
- return false;
-
- // Check for single packages in the provides list
- PrvIterator P = PStart;
- for (; P.end() != true; P++)
- {
- // Skip over self provides
- if (P.OwnerPkg() == ParentPkg())
- continue;
- if (PStart.OwnerPkg() != P.OwnerPkg())
- break;
- }
-
- // Check for non dups
- if (P.end() != true)
- return true;
- Result = PStart.OwnerPkg();
- return false;
- }
- /*}}}*/
- // DepIterator::AllTargets - Returns the set of all possible targets /*{{{*/
- // ---------------------------------------------------------------------
- /* This is a more usefull version of TargetPkg() that follows versioned
- provides. It includes every possible package-version that could satisfy
- the dependency. The last item in the list has a 0. The resulting pointer
- must be delete [] 'd */
- pkgCache::Version **pkgCache::DepIterator::AllTargets()
- {
- Version **Res = 0;
- unsigned long Size =0;
- while (1)
- {
- Version **End = Res;
- PkgIterator DPkg = TargetPkg();
- // Walk along the actual package providing versions
- for (VerIterator I = DPkg.VersionList(); I.end() == false; I++)
- {
- if (pkgCheckDep(TargetVer(),I.VerStr(),Dep->CompareOp) == false)
- continue;
- if (Dep->Type == pkgCache::Dep::Conflicts &&
- ParentPkg() == I.ParentPkg())
- continue;
-
- Size++;
- if (Res != 0)
- *End++ = I;
- }
-
- // Follow all provides
- for (PrvIterator I = DPkg.ProvidesList(); I.end() == false; I++)
- {
- if (pkgCheckDep(TargetVer(),I.ProvideVersion(),Dep->CompareOp) == false)
- continue;
-
- if (Dep->Type == pkgCache::Dep::Conflicts &&
- ParentPkg() == I.OwnerPkg())
- continue;
-
- Size++;
- if (Res != 0)
- *End++ = I.OwnerVer();
- }
-
- // Do it again and write it into the array
- if (Res == 0)
- {
- Res = new Version *[Size+1];
- Size = 0;
- }
- else
- {
- *End = 0;
- break;
- }
- }
-
- return Res;
- }
- /*}}}*/
- // DepIterator::CompType - Return a string describing the compare type /*{{{*/
- // ---------------------------------------------------------------------
- /* This returns a string representation of the dependency compare
- type */
- const char *pkgCache::DepIterator::CompType()
- {
- const char *Ops[] = {"","<=",">=","<",">","=","!="};
- if ((unsigned)(Dep->CompareOp & 0xF) < 7)
- return Ops[Dep->CompareOp & 0xF];
- return "";
- }
- /*}}}*/
- // DepIterator::DepType - Return a string describing the dep type /*{{{*/
- // ---------------------------------------------------------------------
- /* */
- const char *pkgCache::DepIterator::DepType()
- {
- const char *Types[] = {"","Depends","PreDepends","Suggests",
- "Recommends","Conflicts","Replaces"};
- if (Dep->Type < 7)
- return Types[Dep->Type];
- return "";
- }
- /*}}}*/
- // DepIterator::GlobOr - Compute an OR group /*{{{*/
- // ---------------------------------------------------------------------
- /* This Takes an iterator, iterates past the current dependency grouping
- and returns Start and End so that so End is the final element
- in the group, if End == Start then D is End++ and End is the
- dependency D was pointing to. Use in loops to iterate sensibly. */
- void pkgCache::DepIterator::GlobOr(DepIterator &Start,DepIterator &End)
- {
- // Compute a single dependency element (glob or)
- Start = *this;
- End = *this;
- for (bool LastOR = true; end() == false && LastOR == true; (*this)++)
- {
- LastOR = (Dep->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or;
- if (LastOR == true)
- End = (*this);
- }
- }
- /*}}}*/
- // VerIterator::CompareVer - Fast version compare for same pkgs /*{{{*/
- // ---------------------------------------------------------------------
- /* This just looks over the version list to see if B is listed before A. In
- most cases this will return in under 4 checks, ver lists are short. */
- int pkgCache::VerIterator::CompareVer(const VerIterator &B) const
- {
- // Check if they are equal
- if (*this == B)
- return 0;
- if (end() == true)
- return -1;
- if (B.end() == true)
- return 1;
-
- /* Start at A and look for B. If B is found then A > B otherwise
- B was before A so A < B */
- VerIterator I = *this;
- for (;I.end() == false; I++)
- if (I == B)
- return 1;
- return -1;
- }
- /*}}}*/
- // VerIterator::Downloadable - Checks if the version is downloadable /*{{{*/
- // ---------------------------------------------------------------------
- /* */
- bool pkgCache::VerIterator::Downloadable() const
- {
- VerFileIterator Files = FileList();
- for (; Files.end() == false; Files++)
- if ((Files.File()->Flags & pkgCache::Flag::NotSource) != pkgCache::Flag::NotSource)
- return true;
- return false;
- }
- /*}}}*/
- // VerIterator::PriorityType - Return a string describing the priority /*{{{*/
- // ---------------------------------------------------------------------
- /* */
- const char *pkgCache::VerIterator::PriorityType()
- {
- const char *Types[] = {"","Important","Required","Standard",
- "Optional","Extra"};
- if (Ver->Priority < 6)
- return Types[Ver->Priority];
- return "";
- }
- /*}}}*/
- // PkgFileIterator::IsOk - Checks if the cache is in sync with the file /*{{{*/
- // ---------------------------------------------------------------------
- /* This stats the file and compares its stats with the ones that were
- stored during generation. Date checks should probably also be
- included here. */
- bool pkgCache::PkgFileIterator::IsOk()
- {
- struct stat Buf;
- if (stat(FileName(),&Buf) != 0)
- return false;
- if (Buf.st_size != (signed)File->Size || Buf.st_mtime != File->mtime)
- return false;
- return true;
- }
- /*}}}*/
|