// -*- mode: cpp; mode: fold -*-
// Description /*{{{*/
-// $Id: apt-cache.cc,v 1.24 1999/02/08 07:54:58 jgg Exp $
+// $Id: apt-cache.cc,v 1.29 1999/03/17 03:25:25 jgg Exp $
/* ######################################################################
apt-cache - Manages the cache files
#include <apt-pkg/sourcelist.h>
#include <apt-pkg/cmndline.h>
#include <apt-pkg/strutl.h>
+#include <apt-pkg/pkgrecords.h>
#include <config.h>
#include <iostream.h>
+#include <unistd.h>
#include <errno.h>
+#include <regex.h>
/*}}}*/
pkgCache *GCache = 0;
return true;
}
/*}}}*/
+// GetCandidateVer - Returns the Candidate install version /*{{{*/
+// ---------------------------------------------------------------------
+/* This should really use the DepCache or something.. Copied from there. */
+static pkgCache::VerIterator GetCandidateVer(pkgCache::PkgIterator Pkg)
+{
+ /* Not source/not automatic versions cannot be a candidate version
+ unless they are already installed */
+ for (pkgCache::VerIterator I = Pkg.VersionList(); I.end() == false; I++)
+ {
+ if (Pkg.CurrentVer() == I)
+ return I;
+ for (pkgCache::VerFileIterator J = I.FileList(); J.end() == false; J++)
+ if ((J.File()->Flags & pkgCache::Flag::NotSource) == 0 &&
+ (J.File()->Flags & pkgCache::Flag::NotAutomatic) == 0)
+ return I;
+ }
+
+ return pkgCache::VerIterator(*GCache,0);
+}
+ /*}}}*/
// DumpAvail - Print out the available list /*{{{*/
// ---------------------------------------------------------------------
/* This is needed to make dpkg --merge happy */
for (pkgCache::PkgFileIterator I = Cache.FileBegin(); I.end() == false; I++)
{
- if ((I->Flags & pkgCache::Flag::NotSource) != 0)
- continue;
-
if (I.IsOk() == false)
{
delete [] Buffer;
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 = GetCandidateVer(P);
+
+ 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;
+ }
}
}
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 = 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 = GetCandidateVer(Pkg);
+ if (V.end() == true || V.FileList().end() == true)
+ continue;
+ if (DisplayRecord(V) == false)
+ return false;
+ }
return true;
}
/*}}}*/
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;
{'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}};
{"dumpavail",&DumpAvail},
{"unmet",&UnMet},
{"check",&Check},
+ {"search",&Search},
+ {"show",&ShowPackage},
{0,0}};
CacheInitialize();