X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/cdb970c74216a19194380ad657f44332c4aae5a3..b3e53cecf4e761ff9d9b0a139ca71f0d67848041:/cmdline/apt-cache.cc?ds=sidebyside diff --git a/cmdline/apt-cache.cc b/cmdline/apt-cache.cc index 502c29378..de5fc615b 100644 --- a/cmdline/apt-cache.cc +++ b/cmdline/apt-cache.cc @@ -1,6 +1,6 @@ // -*- mode: cpp; mode: fold -*- // Description /*{{{*/ -// $Id: apt-cache.cc,v 1.25 1999/02/11 00:03:07 jgg Exp $ +// $Id: apt-cache.cc,v 1.36 1999/05/14 02:51:36 jgg Exp $ /* ###################################################################### apt-cache - Manages the cache files @@ -8,7 +8,7 @@ apt-cache provides some functions fo manipulating the cache files. It uses the command line interface common to all the APT tools. The only really usefull function right now is dumpavail which is used - by the dselect method. Everything else is ment as a debug aide. + by the dselect method. Everything else is meant as a debug aide. Returns 100 on failure, 0 on success. @@ -23,11 +23,13 @@ #include #include #include +#include #include #include #include #include +#include /*}}}*/ pkgCache *GCache = 0; @@ -174,9 +176,7 @@ bool DumpPackage(CommandLine &CmdL) } cout << "Reverse Provides: " << endl; for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++) - cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr(); - cout << endl; - + cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr() << endl; } return true; @@ -359,22 +359,20 @@ bool DumpAvail(CommandLine &Cmd) structure to find them */ for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++) { - for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++) + // Find the proper version to use. We should probably use the DepCache. + pkgCache::VerIterator V = Cache.GetCandidateVer(P,false); + + if (V.end() == true || V.FileList().File() != I) + continue; + + // Read the record and then write it out again. + if (PkgF.Seek(V.FileList()->Offset) == false || + PkgF.Read(Buffer,V.FileList()->Size) == false || + write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size) { - if (V->FileList == 0) - continue; - if (V.FileList().File() != I) - continue; - - // Read the record and then write it out again. - if (PkgF.Seek(V.FileList()->Offset) == false || - PkgF.Read(Buffer,V.FileList()->Size) == false || - write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size) - { - delete [] Buffer; - return false; - } - } + delete [] Buffer; + return false; + } } } @@ -427,6 +425,119 @@ bool DoAdd(CommandLine &CmdL) GCache = &Gen.GetCache(); Stats(CmdL); + return true; +} + /*}}}*/ +// DisplayRecord - Displays the complete record for the package /*{{{*/ +// --------------------------------------------------------------------- +/* This displays the package record from the proper package index file. + It is not used by DumpAvail for performance reasons. */ +bool DisplayRecord(pkgCache::VerIterator V) +{ + // Find an appropriate file + pkgCache::VerFileIterator Vf = V.FileList(); + for (; Vf.end() == false; Vf++) + if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0) + break; + if (Vf.end() == true) + Vf = V.FileList(); + + // Check and load the package list file + pkgCache::PkgFileIterator I = Vf.File(); + if (I.IsOk() == false) + return _error->Error("Package file %s is out of sync.",I.FileName()); + + FileFd PkgF(I.FileName(),FileFd::ReadOnly); + if (_error->PendingError() == true) + return false; + + // Read the record and then write it out again. + unsigned char *Buffer = new unsigned char[GCache->HeaderP->MaxVerFileSize]; + if (PkgF.Seek(V.FileList()->Offset) == false || + PkgF.Read(Buffer,V.FileList()->Size) == false || + write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size) + { + delete [] Buffer; + return false; + } + + delete [] Buffer; + + return true; +} + /*}}}*/ +// Search - Perform a search /*{{{*/ +// --------------------------------------------------------------------- +/* This searches the package names and pacakge descriptions for a pattern */ +bool Search(CommandLine &CmdL) +{ + pkgCache &Cache = *GCache; + bool ShowFull = _config->FindB("APT::Cache::ShowFull",false); + bool NamesOnly = _config->FindB("APT::Cache::NamesOnly",false); + + // Make sure there is at least one argument + if (CmdL.FileSize() != 2) + return _error->Error("You must give exactly one pattern"); + + // Compile the regex pattern + regex_t Pattern; + if (regcomp(&Pattern,CmdL.FileList[1],REG_EXTENDED | REG_ICASE | + REG_NOSUB) != 0) + return _error->Error("Regex compilation error"); + + // Create the text record parser + pkgRecords Recs(Cache); + if (_error->PendingError() == true) + return false; + + // Search package names + pkgCache::PkgIterator I = Cache.PkgBegin(); + for (;I.end() != true; I++) + { + // We search against the install version as that makes the most sense.. + pkgCache::VerIterator V = Cache.GetCandidateVer(I); + if (V.end() == true) + continue; + + pkgRecords::Parser &P = Recs.Lookup(V.FileList()); + + if (regexec(&Pattern,I.Name(),0,0,0) == 0 || + (NamesOnly == false && + regexec(&Pattern,P.LongDesc().c_str(),0,0,0) == 0)) + { + if (ShowFull == true) + DisplayRecord(V); + else + cout << I.Name() << " - " << P.ShortDesc() << endl; + } + } + + regfree(&Pattern); + return true; +} + /*}}}*/ +// ShowPackage - Dump the package record to the screen /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool ShowPackage(CommandLine &CmdL) +{ + pkgCache &Cache = *GCache; + for (const char **I = CmdL.FileList + 1; *I != 0; I++) + { + pkgCache::PkgIterator Pkg = Cache.FindPkg(*I); + if (Pkg.end() == true) + { + _error->Warning("Unable to locate package %s",*I); + continue; + } + + // Find the proper version to use. We should probably use the DepCache. + pkgCache::VerIterator V = Cache.GetCandidateVer(Pkg); + if (V.end() == true || V.FileList().end() == true) + continue; + if (DisplayRecord(V) == false) + return false; + } return true; } /*}}}*/ @@ -458,7 +569,7 @@ bool ShowHelp(CommandLine &Cmd) cout << endl; cout << "apt-cache is a low-level tool used to manipulate APT's binary" << endl; cout << "cache files stored in " << _config->FindFile("Dir::Cache") << endl; - cout << "It is not ment for ordinary use only as a debug aide." << endl; + cout << "It is not meant for ordinary use only as a debug aide." << endl; cout << endl; cout << "Commands:" << endl; cout << " add - Add an package file to the source cache" << endl; @@ -469,6 +580,8 @@ bool ShowHelp(CommandLine &Cmd) cout << " dumpavail - Print an available file to stdout" << endl; cout << " unmet - Show unmet dependencies" << endl; cout << " check - Check the cache a bit" << endl; + cout << " search - Search the package list for a regex pattern" << endl; + cout << " show - Show a readable record for the package" << endl; cout << endl; cout << "Options:" << endl; cout << " -h This help text." << endl; @@ -477,7 +590,7 @@ bool ShowHelp(CommandLine &Cmd) cout << " -q Disable progress indicator." << endl; cout << " -i Show only important deps for the unmet command." << endl; cout << " -c=? Read this configuration file" << endl; - cout << " -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl; + cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl; cout << "See the apt-cache(8) and apt.conf(5) manual pages for more information." << endl; return 100; } @@ -501,6 +614,8 @@ int main(int argc,const char *argv[]) {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg}, {'q',"quiet","quiet",CommandLine::IntLevel}, {'i',"important","APT::Cache::Important",0}, + {'f',"full","APT::Cache::ShowFull",0}, + {0,"names-only","APT::Cache::NamesOnly",0}, {'c',"config-file",0,CommandLine::ConfigFile}, {'o',"option",0,CommandLine::ArbItem}, {0,0,0,0}}; @@ -514,6 +629,8 @@ int main(int argc,const char *argv[]) {"dumpavail",&DumpAvail}, {"unmet",&UnMet}, {"check",&Check}, + {"search",&Search}, + {"show",&ShowPackage}, {0,0}}; CacheInitialize(); @@ -532,18 +649,27 @@ int main(int argc,const char *argv[]) CmdL.FileSize() == 0) return ShowHelp(CmdL); + // Deal with stdout not being a tty + if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1) + _config->Set("quiet","1"); + if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false) { // Open the cache file - FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly); - MMap Map(CacheF,MMap::Public | MMap::ReadOnly); + pkgSourceList List; + List.ReadMainList(); + + // Generate it and map it + OpProgress Prog; + MMap *Map = pkgMakeStatusCacheMem(List,Prog); if (_error->PendingError() == false) { - pkgCache Cache(Map); + pkgCache Cache(*Map); GCache = &Cache; if (_error->PendingError() == false) CmdL.DispatchArg(CmdsB); - } + } + delete Map; } // Print any errors or warnings found during parsing