]>
git.saurik.com Git - apt.git/blob - cmdline/apt-cache.cc
e36bff1984d013080df00baca1e6966f6a09279f
   1 // -*- mode: cpp; mode: fold -*- 
   3 // $Id: apt-cache.cc,v 1.3 1998/07/19 04:22:10 jgg Exp $ 
   4 /* ###################################################################### 
   6    apt-cache - Manages the cache file. 
   8    This program should eventually handle both low and high level 
   9    manipulation of the cache file. Depending how far things go it  
  10    might get quite a sophisticated UI. 
  12    Currently the command line is as follows: 
  13       apt-cache add cache file1:dist:ver file2:dist:ver ... 
  15       apt-cache add ./cache Pacakges:hamm:1.0 
  17    A usefull feature is 'upgradable' ie 
  18       apt-cache upgradable ./cache 
  19    will list .debs that should be installed to make all packages the latest 
  22    Returns 100 on failure, 0 on success. 
  24    ##################################################################### */ 
  26 // Include Files                                                        /*{{{*/ 
  27 #include <apt-pkg/error.h> 
  28 #include <apt-pkg/pkgcachegen.h> 
  29 #include <apt-pkg/deblistparser.h> 
  30 #include <apt-pkg/init.h> 
  39 // SplitArg - Split the triple                                          /*{{{*/ 
  40 // --------------------------------------------------------------------- 
  42 bool SplitArg(const char *Arg
,string 
&File
,string 
&Dist
,string Ver
) 
  44    const char *Start 
= Arg
; 
  46    for (;*I 
!= 0 && *I 
!= ':'; I
++); 
  48       return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
); 
  49    File 
= string(Start
,I 
- Start
); 
  53    for (;*I 
!= 0 && *I 
!= ':'; I
++); 
  55       return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
); 
  56    Dist 
= string(Start
,I 
- Start
); 
  60    for (;*I 
!= 0 && *I 
!= ':'; I
++); 
  62       return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
); 
  63    Ver 
= string(Start
,I 
- Start
); 
  68 // DumpPackage - Show a dump of a package record                        /*{{{*/ 
  69 // --------------------------------------------------------------------- 
  71 bool DumpPackage(pkgCache 
&Cache
,int argc
,char *argv
[]) 
  73    for (int I 
= 0; I 
!= argc
; I
++) 
  75       pkgCache::PkgIterator Pkg 
= Cache
.FindPkg(argv
[I
]); 
  76       if (Pkg
.end() == true) 
  78          _error
->Warning("Unable to locate package %s",argv
[0]); 
  82       cout 
<< "Package: " << Pkg
.Name() << endl
; 
  84       for (pkgCache::VerIterator Cur 
= Pkg
.VersionList(); Cur
.end() != true; Cur
++) 
  85          cout 
<< Cur
.VerStr() << ','; 
  88       cout 
<< "Reverse Depends: " << endl
; 
  89       for (pkgCache::DepIterator D 
= Pkg
.RevDependsList(); D
.end() != true; D
++) 
  90          cout 
<< "  " << D
.ParentPkg().Name() << ',' << D
.TargetPkg().Name() << endl
; 
  92       cout 
<< "Dependencies: " << endl
; 
  93       for (pkgCache::VerIterator Cur 
= Pkg
.VersionList(); Cur
.end() != true; Cur
++) 
  95          cout 
<< Cur
.VerStr() << " - "; 
  96          for (pkgCache::DepIterator Dep 
= Cur
.DependsList(); Dep
.end() != true; Dep
++) 
  97             cout 
<< Dep
.TargetPkg().Name() << " (" << (int)Dep
->CompareOp 
<< " " << Dep
.TargetVer() << ") "; 
 101       cout 
<< "Provides: " << endl
; 
 102       for (pkgCache::VerIterator Cur 
= Pkg
.VersionList(); Cur
.end() != true; Cur
++) 
 104          cout 
<< Cur
.VerStr() << " - "; 
 105          for (pkgCache::PrvIterator Prv 
= Cur
.ProvidesList(); Prv
.end() != true; Prv
++) 
 106             cout 
<< Prv
.ParentPkg().Name() << " "; 
 109       cout 
<< "Reverse Provides: " << endl
; 
 110       for (pkgCache::PrvIterator Prv 
= Pkg
.ProvidesList(); Prv
.end() != true; Prv
++) 
 111          cout 
<< Prv
.OwnerPkg().Name() << " " << Prv
.OwnerVer().VerStr(); 
 118 // Stats - Dump some nice statistics                                    /*{{{*/ 
 119 // --------------------------------------------------------------------- 
 121 bool Stats(pkgCache 
&Cache
) 
 123    cout 
<< "Total Package Names : " << Cache
.Head().PackageCount 
<< endl
; 
 124    pkgCache::PkgIterator I 
= Cache
.PkgBegin(); 
 131    for (;I
.end() != true; I
++) 
 133       if (I
->VersionList 
!= 0 && I
->ProvidesList 
== 0) 
 139       if (I
->VersionList 
!= 0 && I
->ProvidesList 
!= 0) 
 145       if (I
->VersionList 
== 0 && I
->ProvidesList 
!= 0) 
 148          if (I
.ProvidesList()->NextProvides 
== 0) 
 156       if (I
->VersionList 
== 0 && I
->ProvidesList 
== 0) 
 162    cout 
<< "  Normal Packages: " << Normal 
<< endl
; 
 163    cout 
<< "  Pure Virtual Packages: " << Virtual 
<< endl
; 
 164    cout 
<< "  Single Virtual Packages: " << DVirt 
<< endl
; 
 165    cout 
<< "  Mixed Virtual Packages: " << NVirt 
<< endl
; 
 166    cout 
<< "  Missing: " << Missing 
<< endl
; 
 168    cout 
<< "Total Distinct Versions: " << Cache
.Head().VersionCount 
<< endl
; 
 169    cout 
<< "Total Dependencies: " << Cache
.Head().DependsCount 
<< endl
; 
 173 // Dump - show everything                                               /*{{{*/ 
 174 // --------------------------------------------------------------------- 
 176 bool Dump(pkgCache 
&Cache
) 
 178    for (pkgCache::PkgIterator P 
= Cache
.PkgBegin(); P
.end() == false; P
++) 
 180       cout 
<< "Package: " << P
.Name() << endl
; 
 181       for (pkgCache::VerIterator V 
= P
.VersionList(); V
.end() == false; V
++) 
 183          cout 
<< " Version: " << V
.VerStr() << endl
; 
 184          cout 
<< "     File: " << V
.FileList().File().FileName() << endl
; 
 185          for (pkgCache::DepIterator D 
= V
.DependsList(); D
.end() == false; D
++) 
 186             cout 
<< "  Depends: " << D
.TargetPkg().Name() << ' ' << D
.TargetVer() << endl
; 
 190    for (pkgCache::PkgFileIterator 
F(Cache
); F
.end() == false; F
++) 
 192       cout 
<< "File: " << F
.FileName() << endl
; 
 193       cout 
<< " Size: " << F
->Size 
<< endl
; 
 194       cout 
<< " ID: " << F
->ID 
<< endl
; 
 195       cout 
<< " Flags: " << F
->Flags 
<< endl
; 
 196       cout 
<< " Time: " << ctime(&F
->mtime
) << endl
; 
 202 // DumpAvail - Print out the available list                             /*{{{*/ 
 203 // --------------------------------------------------------------------- 
 204 /* This is needed to make dpkg --merge happy */ 
 205 bool DumpAvail(pkgCache 
&Cache
) 
 207    unsigned char *Buffer 
= new unsigned char[Cache
.HeaderP
->MaxVerFileSize
]; 
 209    for (pkgCache::PkgFileIterator I 
= Cache
.FileBegin(); I
.end() == false; I
++) 
 211       if ((I
->Flags 
& pkgCache::Flag::NotSource
) != 0) 
 214       if (I
.IsOk() == false) 
 217          return _error
->Error("Package file %s is out of sync.",I
.FileName()); 
 220       File 
PkgF(I
.FileName(),File::ReadOnly
); 
 221       if (_error
->PendingError() == true) 
 227       /* Write all of the records from this package file, we search the entire 
 228          structure to find them */ 
 229       for (pkgCache::PkgIterator P 
= Cache
.PkgBegin(); P
.end() == false; P
++) 
 231          for (pkgCache::VerIterator V 
= P
.VersionList(); V
.end() == false; V
++) 
 233             if (V
->FileList 
== 0) 
 235             if (V
.FileList().File() != I
) 
 238             // Read the record and then write it out again. 
 239             if (PkgF
.Seek(V
.FileList()->Offset
) == false || 
 240                 PkgF
.Read(Buffer
,V
.FileList()->Size
) == false || 
 241                 write(STDOUT_FILENO
,Buffer
,V
.FileList()->Size
) != V
.FileList()->Size
) 
 253 // DoAdd - Perform an adding operation                                  /*{{{*/ 
 254 // --------------------------------------------------------------------- 
 256 bool DoAdd(int argc
,char *argv
[]) 
 263    File 
CacheF(CacheFile
,File::WriteEmpty
); 
 264    if (_error
->PendingError() == true) 
 267    DynamicMMap 
Map(CacheF
,MMap::Public
); 
 268    if (_error
->PendingError() == true) 
 271    pkgCacheGenerator 
Gen(Map
); 
 272    if (_error
->PendingError() == true) 
 275    for (int I 
= 0; I 
!= argc
; I
++) 
 277       if (SplitArg(argv
[I
],FileName
,Dist
,Ver
) == false) 
 279       cout 
<< FileName 
<< endl
; 
 282       File 
TagF(FileName
.c_str(),File::ReadOnly
); 
 283       debListParser 
Parser(TagF
); 
 284       if (_error
->PendingError() == true) 
 285          return _error
->Error("Problem opening %s",FileName
.c_str()); 
 287       if (Gen
.SelectFile(FileName
) == false) 
 288          return _error
->Error("Problem with SelectFile"); 
 290       if (Gen
.MergeList(Parser
) == false) 
 291          return _error
->Error("Problem with MergeList"); 
 294    Stats(Gen
.GetCache()); 
 300 int main(int argc
, char *argv
[]) 
 305       cerr 
<< "Usage is apt-cache add cache file1:dist:ver file2:dist:ver ..." << endl
; 
 309    pkgInitialize(*_config
); 
 313       if (strcmp(argv
[1],"add") == 0) 
 315          DoAdd(argc 
- 3,argv 
+ 3); 
 319       // Open the cache file 
 320       File 
CacheF(CacheFile
,File::ReadOnly
); 
 321       if (_error
->PendingError() == true) 
 324       MMap 
Map(CacheF
,MMap::Public 
| MMap::ReadOnly
); 
 325       if (_error
->PendingError() == true) 
 329       if (_error
->PendingError() == true) 
 332       if (strcmp(argv
[1],"showpkg") == 0) 
 335          DumpPackage(Cache
,argc 
- 3,argv 
+ 3); 
 339       if (strcmp(argv
[1],"stats") == 0) 
 345       if (strcmp(argv
[1],"dump") == 0) 
 351       if (strcmp(argv
[1],"dumpavail") == 0) 
 357       _error
->Error("Invalid operation %s", argv
[1]); 
 361    // Print any errors or warnings found during parsing 
 362    if (_error
->empty() == false) 
 364       _error
->DumpErrors();