X-Git-Url: https://git.saurik.com/apt.git/blobdiff_plain/f826cfaa1c3f9fcf492fb65b22a2d964e78ae7ec..a3eaf9544039a9adfef159e8185f376f19b15c68:/cmdline/apt-cache.cc diff --git a/cmdline/apt-cache.cc b/cmdline/apt-cache.cc index b20227153..80c15a749 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.16 1998/12/07 00:34:22 jgg Exp $ +// $Id: apt-cache.cc,v 1.41 1999/10/22 04:05:47 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. @@ -22,17 +22,25 @@ #include #include #include -#include +#include +#include +#include +#include #include -#include +#include +#include +#include /*}}}*/ +pkgCache *GCache = 0; + // UnMet - Show unmet dependencies /*{{{*/ // --------------------------------------------------------------------- /* */ -bool UnMet(pkgCache &Cache) +bool UnMet(CommandLine &CmdL) { + pkgCache &Cache = *GCache; bool Important = _config->FindB("APT::Cache::Important",false); for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++) @@ -122,8 +130,9 @@ bool UnMet(pkgCache &Cache) // DumpPackage - Show a dump of a package record /*{{{*/ // --------------------------------------------------------------------- /* */ -bool DumpPackage(pkgCache &Cache,CommandLine &CmdL) +bool DumpPackage(CommandLine &CmdL) { + pkgCache &Cache = *GCache; for (const char **I = CmdL.FileList + 1; *I != 0; I++) { pkgCache::PkgIterator Pkg = Cache.FindPkg(*I); @@ -168,9 +177,7 @@ bool DumpPackage(pkgCache &Cache,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; @@ -179,8 +186,9 @@ bool DumpPackage(pkgCache &Cache,CommandLine &CmdL) // Stats - Dump some nice statistics /*{{{*/ // --------------------------------------------------------------------- /* */ -bool Stats(pkgCache &Cache) +bool Stats(CommandLine &Cmd) { + pkgCache &Cache = *GCache; cout << "Total Package Names : " << Cache.Head().PackageCount << " (" << SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl; pkgCache::PkgIterator I = Cache.PkgBegin(); @@ -232,8 +240,10 @@ bool Stats(pkgCache &Cache) cout << "Total Dependencies: " << Cache.Head().DependsCount << " (" << SizeToStr(Cache.Head().DependsCount*Cache.Head().DependencySz) << ')' << endl; - cout << "Total Ver/File relations: " << Cache.Head().PackageCount << " (" << - SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl; + cout << "Total Ver/File relations: " << Cache.Head().VerFileCount << " (" << + SizeToStr(Cache.Head().VerFileCount*Cache.Head().VerFileSz) << ')' << endl; + cout << "Total Provides Mappings: " << Cache.Head().ProvidesCount << " (" << + SizeToStr(Cache.Head().ProvidesCount*Cache.Head().ProvidesSz) << ')' << endl; // String list stats unsigned long Size = 0; @@ -254,7 +264,9 @@ bool Stats(pkgCache &Cache) unsigned long Total = 0; Total = Slack + Size + Cache.Head().DependsCount*Cache.Head().DependencySz + Cache.Head().VersionCount*Cache.Head().VersionSz + - Cache.Head().PackageCount*Cache.Head().PackageSz; + Cache.Head().PackageCount*Cache.Head().PackageSz + + Cache.Head().VerFileCount*Cache.Head().VerFileSz + + Cache.Head().ProvidesCount*Cache.Head().ProvidesSz; cout << "Total Space Accounted for: " << SizeToStr(Total) << endl; return true; @@ -263,8 +275,9 @@ bool Stats(pkgCache &Cache) // Check - Check some things about the cache /*{{{*/ // --------------------------------------------------------------------- /* Debug aide mostly */ -bool Check(pkgCache &Cache) +bool Check(CommandLine &Cmd) { + pkgCache &Cache = *GCache; pkgCache::PkgIterator Pkg = Cache.PkgBegin(); for (;Pkg.end() != true; Pkg++) { @@ -284,8 +297,9 @@ bool Check(pkgCache &Cache) // Dump - show everything /*{{{*/ // --------------------------------------------------------------------- /* */ -bool Dump(pkgCache &Cache) +bool Dump(CommandLine &Cmd) { + pkgCache &Cache = *GCache; for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++) { cout << "Package: " << P.Name() << endl; @@ -304,7 +318,13 @@ bool Dump(pkgCache &Cache) cout << " Size: " << F->Size << endl; cout << " ID: " << F->ID << endl; cout << " Flags: " << F->Flags << endl; - cout << " Time: " << ctime(&F->mtime) << endl; + cout << " Time: " << TimeRFC1123(F->mtime) << endl; + cout << " Archive: " << F.Archive() << endl; + cout << " Component: " << F.Component() << endl; + cout << " Version: " << F.Version() << endl; + cout << " Origin: " << F.Origin() << endl; + cout << " Label: " << F.Label() << endl; + cout << " Architecture: " << F.Architecture() << endl; } return true; @@ -313,8 +333,9 @@ bool Dump(pkgCache &Cache) // DumpAvail - Print out the available list /*{{{*/ // --------------------------------------------------------------------- /* This is needed to make dpkg --merge happy */ -bool DumpAvail(pkgCache &Cache) +bool DumpAvail(CommandLine &Cmd) { + pkgCache &Cache = *GCache; unsigned char *Buffer = new unsigned char[Cache.HeaderP->MaxVerFileSize]; for (pkgCache::PkgFileIterator I = Cache.FileBegin(); I.end() == false; I++) @@ -339,24 +360,78 @@ bool DumpAvail(pkgCache &Cache) 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; + delete [] Buffer; + return false; + } + } + } + + return true; +} + /*}}}*/ +// Depends - Print out a dependency tree /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool Depends(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; + } + + pkgCache::VerIterator Ver = Pkg.VersionList(); + if (Ver.end() == true) + { + cout << '<' << Pkg.Name() << '>' << endl; + continue; + } + + cout << Pkg.Name() << endl; + + for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++) + { + if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or) + cout << " |"; + else + cout << " "; + + // Show the package + pkgCache::PkgIterator Trg = D.TargetPkg(); + if (Trg->VersionList == 0) + cout << D.DepType() << ": <" << Trg.Name() << ">" << endl; + else + cout << D.DepType() << ": " << Trg.Name() << endl; - // 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; - } + // Display all solutions + pkgCache::Version **List = D.AllTargets(); + for (pkgCache::Version **I = List; *I != 0; I++) + { + pkgCache::VerIterator V(Cache,*I); + if (V != Cache.VerP + V.ParentPkg()->VersionList || + V->ParentPkg == D->Package) + continue; + cout << " " << V.ParentPkg().Name() << endl; } + delete [] List; } - } + } return true; } @@ -404,15 +479,165 @@ bool DoAdd(CommandLine &CmdL) } Progress.Done(); - Stats(Gen.GetCache()); + 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. + if (_config->FindB("APT::Cache::AllVersions","true") == true) + { + pkgCache::VerIterator V; + for (V = Pkg.VersionList(); V.end() == false; V++) + { + if (DisplayRecord(V) == false) + return false; + } + } + else + { + pkgCache::VerIterator V = Cache.GetCandidateVer(Pkg); + if (V.end() == true || V.FileList().end() == true) + continue; + if (DisplayRecord(V) == false) + return false; + } + } + return true; +} + /*}}}*/ +// ShowSrcPackage - Show source package records /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool ShowSrcPackage(CommandLine &CmdL) +{ + pkgSourceList List; + List.ReadMainList(); + + // Create the text record parsers + pkgSrcRecords SrcRecs(List); + if (_error->PendingError() == true) + return false; + + for (const char **I = CmdL.FileList + 1; *I != 0; I++) + { + SrcRecs.Restart(); + + pkgSrcRecords::Parser *Parse; + while ((Parse = SrcRecs.Find(*I,false)) != 0) + cout << Parse->AsStr(); + } return true; } /*}}}*/ // GenCaches - Call the main cache generator /*{{{*/ // --------------------------------------------------------------------- /* */ -bool GenCaches() +bool GenCaches(CommandLine &Cmd) { OpTextProgress Progress(*_config); @@ -424,10 +649,12 @@ bool GenCaches() // ShowHelp - Show a help screen /*{{{*/ // --------------------------------------------------------------------- /* */ -int ShowHelp() +bool ShowHelp(CommandLine &Cmd) { cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE << " compiled on " << __DATE__ << " " << __TIME__ << endl; + if (_config->FindB("version") == true) + return 100; cout << "Usage: apt-cache [options] command" << endl; cout << " apt-cache [options] add file1 [file1 ...]" << endl; @@ -435,7 +662,7 @@ int ShowHelp() 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; @@ -446,15 +673,19 @@ int ShowHelp() 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 << " depends - Show raw dependency information for a package" << endl; cout << endl; cout << "Options:" << endl; cout << " -h This help text." << endl; cout << " -p=? The package cache. [" << _config->FindFile("Dir::Cache::pkgcache") << ']' << endl; cout << " -s=? The source cache. [" << _config->FindFile("Dir::Cache::srcpkgcache") << ']' << endl; - cout << " -q Disable progress indicator. " << endl; + 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 << "See the apt-cache(8) and apt.conf(8) manual pages for more information." << 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; } /*}}}*/ @@ -472,13 +703,33 @@ int main(int argc,const char *argv[]) { CommandLine::Args Args[] = { {'h',"help","help",0}, + {'v',"version","version",0}, {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg}, {'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}, + {'g',"no-generate","APT::Cache::NoGenerate",0}, + {'a',"all-versions","APT::Cache::AllVersions",0}, + {0,"names-only","APT::Cache::NamesOnly",0}, {'c',"config-file",0,CommandLine::ConfigFile}, {'o',"option",0,CommandLine::ArbItem}, {0,0,0,0}}; + CommandLine::Dispatch CmdsA[] = {{"help",&ShowHelp}, + {"add",&DoAdd}, + {"gencaches",&GenCaches}, + {"showsrc",&ShowSrcPackage}, + {0,0}}; + CommandLine::Dispatch CmdsB[] = {{"showpkg",&DumpPackage}, + {"stats",&Stats}, + {"dump",&Dump}, + {"dumpavail",&DumpAvail}, + {"unmet",&UnMet}, + {"check",&Check}, + {"search",&Search}, + {"depends",&Depends}, + {"show",&ShowPackage}, + {0,0}}; CacheInitialize(); @@ -494,73 +745,39 @@ int main(int argc,const char *argv[]) // See if the help should be shown if (_config->FindB("help") == true || CmdL.FileSize() == 0) - return ShowHelp(); - - while (1) - { - if (strcmp(CmdL.FileList[0],"add") == 0) - { - DoAdd(CmdL); - break; - } + return ShowHelp(CmdL); - if (strcmp(CmdL.FileList[0],"gencaches") == 0) - { - GenCaches(); - break; - } - - // Open the cache file - FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly); - if (_error->PendingError() == true) - break; - - MMap Map(CacheF,MMap::Public | MMap::ReadOnly); - if (_error->PendingError() == true) - break; - - pkgCache Cache(Map); - if (_error->PendingError() == true) - break; - - if (strcmp(CmdL.FileList[0],"showpkg") == 0) - { - DumpPackage(Cache,CmdL); - break; - } + // Deal with stdout not being a tty + if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1) + _config->Set("quiet","1"); - if (strcmp(CmdL.FileList[0],"stats") == 0) + if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false) + { + MMap *Map; + if (_config->FindB("APT::Cache::NoGenerate",false) == true) { - Stats(Cache); - break; + Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"), + FileFd::ReadOnly),MMap::Public|MMap::ReadOnly); } - - if (strcmp(CmdL.FileList[0],"dump") == 0) + else { - Dump(Cache); - break; - } - - if (strcmp(CmdL.FileList[0],"dumpavail") == 0) - { - DumpAvail(Cache); - break; + // Open the cache file + pkgSourceList List; + List.ReadMainList(); + + // Generate it and map it + OpProgress Prog; + Map = pkgMakeStatusCacheMem(List,Prog); } - if (strcmp(CmdL.FileList[0],"unmet") == 0) + if (_error->PendingError() == false) { - UnMet(Cache); - break; - } - - if (strcmp(CmdL.FileList[0],"check") == 0) - { - Check(Cache); - break; + pkgCache Cache(*Map); + GCache = &Cache; + if (_error->PendingError() == false) + CmdL.DispatchArg(CmdsB); } - - _error->Error("Invalid operation %s", CmdL.FileList[0]); - break; + delete Map; } // Print any errors or warnings found during parsing