]> git.saurik.com Git - apt.git/blobdiff - cmdline/apt-cache.cc
Sync
[apt.git] / cmdline / apt-cache.cc
index ce7ccc2c9ef9090fdd261aa7331bb3eea3c429b0..5317445466164f7a1a981bd03583f65dfc0d82c7 100644 (file)
@@ -1,23 +1,14 @@
 // -*- mode: cpp; mode: fold -*-
 // Description                                                         /*{{{*/
-// $Id: apt-cache.cc,v 1.1 1998/07/15 05:56:47 jgg Exp $
+// $Id: apt-cache.cc,v 1.10 1998/10/19 23:45:35 jgg Exp $
 /* ######################################################################
    
-   apt-cache - Manages the cache file.
+   apt-cache - Manages the cache files
    
-   This program should eventually handle both low and high level
-   manipulation of the cache file. Depending how far things go it 
-   might get quite a sophisticated UI.
-   
-   Currently the command line is as follows:
-      apt-cache add cache file1:dist:ver file2:dist:ver ...
-    ie:
-      apt-cache add ./cache Pacakges:hamm:1.0
-
-   A usefull feature is 'upgradable' ie
-      apt-cache upgradable ./cache
-   will list .debs that should be installed to make all packages the latest
-   version.
+   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.
    
    Returns 100 on failure, 0 on success.
    
 #include <apt-pkg/error.h>
 #include <apt-pkg/pkgcachegen.h>
 #include <apt-pkg/deblistparser.h>
+#include <apt-pkg/init.h>
+#include <apt-pkg/progress.h>
+#include <apt-pkg/sourcelist.h>
+#include <apt-pkg/cmndline.h>
 
 #include <iostream.h>
-#include <fstream.h>
-
-                                                                       /*}}}*/
-
-string CacheFile;
-
-// SplitArg - Split the triple                                         /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool SplitArg(const char *Arg,string &File,string &Dist,string Ver)
-{
-   const char *Start = Arg;
-   const char *I = Arg;
-   for (;*I != 0 && *I != ':'; I++);
-   if (*I != ':')
-      return _error->Error("Malformed argument %s, must be in file:dist:rev form",Arg);
-   File = string(Start,I - Start);
-
-   I++;
-   Start = I;
-   for (;*I != 0 && *I != ':'; I++);
-   if (*I != ':')
-      return _error->Error("Malformed argument %s, must be in file:dist:rev form",Arg);
-   Dist = string(Start,I - Start);
-   
-   I++;
-   Start = I;
-   for (;*I != 0 && *I != ':'; I++);
-   if (I == Start)
-      return _error->Error("Malformed argument %s, must be in file:dist:rev form",Arg);
-   Ver = string(Start,I - Start);
-
-   return true;
-}
+#include <config.h>
                                                                        /*}}}*/
-// DoAdd - Perform an adding operation                                 /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-bool DoAdd(int argc,char *argv[])
-{
-   string FileName;
-   string Dist;
-   string Ver;
-   
-   File CacheF(CacheFile,File::WriteEmpty);
-   if (_error->PendingError() == true)
-      return false;
-   
-   DynamicMMap Map(CacheF,MMap::Public);
-   if (_error->PendingError() == true)
-      return false;
-   
-   pkgCacheGenerator Gen(Map);
-   if (_error->PendingError() == true)
-      return false;
 
-   for (int I = 0; I != argc; I++)
-   {
-      if (SplitArg(argv[I],FileName,Dist,Ver) == false)
-        return false;
-      
-      // Do the merge
-      File TagF(FileName.c_str(),File::ReadOnly);
-      debListParser Parser(TagF);
-      if (_error->PendingError() == true)
-        return false;
-      if (Gen.SelectFile(FileName) == false)
-        return false;
-        
-      if (Gen.MergeList(Parser) == false)
-        return false;
-   }
-   
-   return true;
-}
-                                                                       /*}}}*/
 // DumpPackage - Show a dump of a package record                       /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool DumpPackage(int argc,char *argv[])
-{
-   File CacheF(CacheFile,File::ReadOnly);
-   if (_error->PendingError() == true)
-      return false;
-   
-   MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
-   if (_error->PendingError() == true)
-      return false;
-   
-   pkgCache Cache(Map);   
-   if (_error->PendingError() == true)
-      return false;
-   
-   for (int I = 0; I != argc; I++)
+bool DumpPackage(pkgCache &Cache,CommandLine &CmdL)
+{   
+   for (const char **I = CmdL.FileList + 1; *I != 0; I++)
    {
-      pkgCache::PkgIterator Pkg = Cache.FindPkg(argv[I]);
+      pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
       if (Pkg.end() == true)
       {
-        _error->Warning("Unable to locate package %s",argv[0]);
+        _error->Warning("Unable to locate package %s",*I);
         continue;
       }
 
@@ -157,7 +67,11 @@ bool DumpPackage(int argc,char *argv[])
         for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; Prv++)
            cout << Prv.ParentPkg().Name() << " ";
         cout << endl;
-      }      
+      }
+      cout << "Reverse Provides: " << endl;
+      for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++)
+        cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr();
+      cout << endl;
    }
 
    return true;
@@ -166,20 +80,8 @@ bool DumpPackage(int argc,char *argv[])
 // Stats - Dump some nice statistics                                   /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool Stats(const char *FileName)
+bool Stats(pkgCache &Cache)
 {
-   File CacheF(FileName,File::ReadOnly);
-   if (_error->PendingError() == true)
-      return false;
-   
-   MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
-   if (_error->PendingError() == true)
-      return false;
-   
-   pkgCache Cache(Map);   
-   if (_error->PendingError() == true)
-      return false;
-       
    cout << "Total Package Names : " << Cache.Head().PackageCount << endl;
    pkgCache::PkgIterator I = Cache.PkgBegin();
    
@@ -233,20 +135,8 @@ bool Stats(const char *FileName)
 // Dump - show everything                                              /*{{{*/
 // ---------------------------------------------------------------------
 /* */
-bool Dump()
+bool Dump(pkgCache &Cache)
 {
-   File CacheF(CacheFile,File::ReadOnly);
-   if (_error->PendingError() == true)
-      return false;
-   
-   MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
-   if (_error->PendingError() == true)
-      return false;
-   
-   pkgCache Cache(Map);   
-   if (_error->PendingError() == true)
-      return false;
-
    for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
    {
       cout << "Package: " << P.Name() << endl;
@@ -274,112 +164,246 @@ bool Dump()
 // DumpAvail - Print out the available list                            /*{{{*/
 // ---------------------------------------------------------------------
 /* This is needed to make dpkg --merge happy */
-bool DumpAvail()
+bool DumpAvail(pkgCache &Cache)
 {
-#if 0
-   pkgCache Cache(CacheFile,true,true);
-   if (_error->PendingError() == true)
-      return false;
-
-   pkgControlCache CCache(Cache);
-   if (_error->PendingError() == true)
-      return false;
+   unsigned char *Buffer = new unsigned char[Cache.HeaderP->MaxVerFileSize];
 
-   vector<string> Lines;
-   Lines.reserve(30);
-   
-   pkgCache::PkgIterator I = Cache.PkgBegin();
-   for (;I.end() != true; I++)
+   for (pkgCache::PkgFileIterator I = Cache.FileBegin(); I.end() == false; I++)
    {
-      if (I->VersionList == 0)
+      if ((I->Flags & pkgCache::Flag::NotSource) != 0)
         continue;
       
-      pkgSPkgCtrlInfo Inf = CCache[I.VersionList()];
-      if (Inf.isNull() == true)
-        return _error->Error("Couldn't locate info record");
+      if (I.IsOk() == false)
+      {
+        delete [] Buffer;
+        return _error->Error("Package file %s is out of sync.",I.FileName());
+      }
       
-      // Iterate over each element
-      pkgPkgCtrlInfo::const_iterator Elm = Inf->begin();
-      for (; Elm != Inf->end(); Elm++)
+      FileFd PkgF(I.FileName(),FileFd::ReadOnly);
+      if (_error->PendingError() == true)
       {
-        // Write the tag: value
-        cout << (*Elm)->Tag() << ": " << (*Elm)->Value() << endl;
-        
-        // Write the multiline
-        (*Elm)->GetLines(Lines);
-        for (vector<string>::iterator j = Lines.begin(); j != Lines.end(); j++)
+        delete [] Buffer;
+        return false;
+      }
+
+      /* Write all of the records from this package file, we search the entire
+         structure to find them */
+      for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
+      {
+        for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
         {
-           if ((*j).length() == 0)
-              cout << " ." << endl;
-           else
-              cout << " " << *j << endl;
+           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;
+           }       
         }
-        
-        Lines.erase(Lines.begin(),Lines.end());
       }
+   }
+   
+   return true;
+}
+                                                                       /*}}}*/
+// DoAdd - Perform an adding operation                                 /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool DoAdd(CommandLine &CmdL)
+{
+   // Make sure there is at least one argument
+   if (CmdL.FileSize() <= 1)
+      return _error->Error("You must give at least one file name");
+   
+   // Open the cache
+   FileFd CacheF(_config->FindFile("Dir::Cache::srcpkgcache"),FileFd::WriteAny);
+   if (_error->PendingError() == true)
+      return false;
+   
+   DynamicMMap Map(CacheF,MMap::Public);
+   if (_error->PendingError() == true)
+      return false;
+
+   OpTextProgress Progress(*_config);
+   pkgCacheGenerator Gen(Map,Progress);
+   if (_error->PendingError() == true)
+      return false;
+
+   unsigned long Length = CmdL.FileSize() - 1;
+   for (const char **I = CmdL.FileList + 1; *I != 0; I++)
+   {
+      Progress.OverallProgress(I - CmdL.FileList,Length,1,"Generating cache");
       
-      cout << endl;
+      // Do the merge
+      FileFd TagF(*I,FileFd::ReadOnly);
+      debListParser Parser(TagF);
+      if (_error->PendingError() == true)
+        return _error->Error("Problem opening %s",*I);
+      
+      if (Gen.SelectFile(*I) == false)
+        return _error->Error("Problem with SelectFile");
+        
+      if (Gen.MergeList(Parser) == false)
+        return _error->Error("Problem with MergeList");
    }
-#endif
+
+   Progress.Done();
+   Stats(Gen.GetCache());
+   
    return true;
 }
                                                                        /*}}}*/
+// GenCaches - Call the main cache generator                           /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+bool GenCaches()
+{
+   OpTextProgress Progress(*_config);
+   
+   pkgSourceList List;
+   List.ReadMainList();
+   return pkgMakeStatusCache(List,Progress);
+}
+                                                                       /*}}}*/
+// ShowHelp - Show a help screen                                       /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+int ShowHelp()
+{
+   cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
+       " compiled on " << __DATE__ << "  " << __TIME__ << endl;
+   
+   cout << "Usage: apt-cache [options] command" << endl;
+   cout << "       apt-cache [options] add file1 [file1 ...]" << endl;
+   cout << "       apt-cache [options] showpkg pkg1 [pkg2 ...]" << endl;
+   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 << endl;
+   cout << "Commands:" << endl;
+   cout << "   add - Add an package file to the source cache" << endl;
+   cout << "   gencaches - Build both the package and source cache" << endl;
+   cout << "   showpkg - Show some general information for a single package" << endl;
+   cout << "   stats - Show some basic statistics" << endl;
+   cout << "   dump - Show the entire file in a terse form" << endl;
+   cout << "   dumpavail - Print an available file to stdout" << 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 << "  -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;
+   return 100;
+}
+                                                                       /*}}}*/
+// CacheInitialize - Initialize things for apt-cache                   /*{{{*/
+// ---------------------------------------------------------------------
+/* */
+void CacheInitialize()
+{
+   _config->Set("quiet",0);
+   _config->Set("help",false);
+}
+                                                                       /*}}}*/
 
-int main(int argc, char *argv[])
+int main(int argc,const char *argv[])
 {
-   // Check arguments.
-   if (argc < 3)
+   CommandLine::Args Args[] = {
+      {'h',"help","help",0},
+      {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg},
+      {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg},
+      {'q',"quiet","quiet",CommandLine::IntLevel},
+      {'c',"config-file",0,CommandLine::ConfigFile},
+      {'o',"option",0,CommandLine::ArbItem},
+      {0,0,0,0}};
+
+   CacheInitialize();
+   
+   // Parse the command line and initialize the package library
+   CommandLine CmdL(Args,_config);
+   if (pkgInitialize(*_config) == false ||
+       CmdL.Parse(argc,argv) == false)
    {
-      cerr << "Usage is apt-cache add cache file1:dist:ver file2:dist:ver ..." << endl;
+      _error->DumpErrors();
       return 100;
    }
+
+   // See if the help should be shown
+   if (_config->FindB("help") == true ||
+       CmdL.FileSize() == 0)
+      return ShowHelp();
    
    while (1)
    {
-      if (strcmp(argv[1],"add") == 0)
+      if (strcmp(CmdL.FileList[0],"add") == 0)
+      {
+        DoAdd(CmdL);
+        break;
+      }
+
+      if (strcmp(CmdL.FileList[0],"gencaches") == 0)
       {
-        CacheFile = argv[2];
-        if (DoAdd(argc - 3,argv + 3) == true) 
-           Stats(argv[2]);
+        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(argv[1],"showpkg") == 0)
+      if (strcmp(CmdL.FileList[0],"showpkg") == 0)
       {
-        CacheFile = argv[2];
-        DumpPackage(argc - 3,argv + 3);
+        DumpPackage(Cache,CmdL);
         break;
       }
 
-      if (strcmp(argv[1],"stats") == 0)
+      if (strcmp(CmdL.FileList[0],"stats") == 0)
       {
-        Stats(argv[2]);
+        Stats(Cache);
         break;
       }
       
-      if (strcmp(argv[1],"dump") == 0)
+      if (strcmp(CmdL.FileList[0],"dump") == 0)
       {
-        CacheFile = argv[2];
-        Dump();
+        Dump(Cache);
         break;
       }
       
-      if (strcmp(argv[1],"dumpavail") == 0)
+      if (strcmp(CmdL.FileList[0],"dumpavail") == 0)
       {
-        CacheFile = argv[2];
-        DumpAvail();
+        DumpAvail(Cache);
         break;
       }
-      
-      _error->Error("Invalid operation %s", argv[1]);
+            
+      _error->Error("Invalid operation %s", CmdL.FileList[0]);
       break;
    }
    
    // Print any errors or warnings found during parsing
    if (_error->empty() == false)
    {
+      bool Errors = _error->PendingError();
       _error->DumpErrors();
-      return 100;
+      return Errors == true?100:0;
    }
           
    return 0;