]>
git.saurik.com Git - apt.git/blob - cmdline/apt-cache.cc
97211a57a549b81fbfd33a1f43bdb0f23abd5648
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-cache.cc,v 1.5 1998/07/21 05:33:21 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>
31 #include <apt-pkg/progress.h>
40 // SplitArg - Split the triple /*{{{*/
41 // ---------------------------------------------------------------------
43 bool SplitArg(const char *Arg
,string
&File
,string
&Dist
,string Ver
)
45 const char *Start
= Arg
;
47 for (;*I
!= 0 && *I
!= ':'; I
++);
49 return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
);
50 File
= string(Start
,I
- Start
);
54 for (;*I
!= 0 && *I
!= ':'; I
++);
56 return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
);
57 Dist
= string(Start
,I
- Start
);
61 for (;*I
!= 0 && *I
!= ':'; I
++);
63 return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
);
64 Ver
= string(Start
,I
- Start
);
69 // DumpPackage - Show a dump of a package record /*{{{*/
70 // ---------------------------------------------------------------------
72 bool DumpPackage(pkgCache
&Cache
,int argc
,char *argv
[])
74 for (int I
= 0; I
!= argc
; I
++)
76 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(argv
[I
]);
77 if (Pkg
.end() == true)
79 _error
->Warning("Unable to locate package %s",argv
[0]);
83 cout
<< "Package: " << Pkg
.Name() << endl
;
85 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
86 cout
<< Cur
.VerStr() << ',';
89 cout
<< "Reverse Depends: " << endl
;
90 for (pkgCache::DepIterator D
= Pkg
.RevDependsList(); D
.end() != true; D
++)
91 cout
<< " " << D
.ParentPkg().Name() << ',' << D
.TargetPkg().Name() << endl
;
93 cout
<< "Dependencies: " << endl
;
94 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
96 cout
<< Cur
.VerStr() << " - ";
97 for (pkgCache::DepIterator Dep
= Cur
.DependsList(); Dep
.end() != true; Dep
++)
98 cout
<< Dep
.TargetPkg().Name() << " (" << (int)Dep
->CompareOp
<< " " << Dep
.TargetVer() << ") ";
102 cout
<< "Provides: " << endl
;
103 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
105 cout
<< Cur
.VerStr() << " - ";
106 for (pkgCache::PrvIterator Prv
= Cur
.ProvidesList(); Prv
.end() != true; Prv
++)
107 cout
<< Prv
.ParentPkg().Name() << " ";
110 cout
<< "Reverse Provides: " << endl
;
111 for (pkgCache::PrvIterator Prv
= Pkg
.ProvidesList(); Prv
.end() != true; Prv
++)
112 cout
<< Prv
.OwnerPkg().Name() << " " << Prv
.OwnerVer().VerStr();
119 // Stats - Dump some nice statistics /*{{{*/
120 // ---------------------------------------------------------------------
122 bool Stats(pkgCache
&Cache
)
124 cout
<< "Total Package Names : " << Cache
.Head().PackageCount
<< endl
;
125 pkgCache::PkgIterator I
= Cache
.PkgBegin();
132 for (;I
.end() != true; I
++)
134 if (I
->VersionList
!= 0 && I
->ProvidesList
== 0)
140 if (I
->VersionList
!= 0 && I
->ProvidesList
!= 0)
146 if (I
->VersionList
== 0 && I
->ProvidesList
!= 0)
149 if (I
.ProvidesList()->NextProvides
== 0)
157 if (I
->VersionList
== 0 && I
->ProvidesList
== 0)
163 cout
<< " Normal Packages: " << Normal
<< endl
;
164 cout
<< " Pure Virtual Packages: " << Virtual
<< endl
;
165 cout
<< " Single Virtual Packages: " << DVirt
<< endl
;
166 cout
<< " Mixed Virtual Packages: " << NVirt
<< endl
;
167 cout
<< " Missing: " << Missing
<< endl
;
169 cout
<< "Total Distinct Versions: " << Cache
.Head().VersionCount
<< endl
;
170 cout
<< "Total Dependencies: " << Cache
.Head().DependsCount
<< endl
;
174 // Dump - show everything /*{{{*/
175 // ---------------------------------------------------------------------
177 bool Dump(pkgCache
&Cache
)
179 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
181 cout
<< "Package: " << P
.Name() << endl
;
182 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
184 cout
<< " Version: " << V
.VerStr() << endl
;
185 cout
<< " File: " << V
.FileList().File().FileName() << endl
;
186 for (pkgCache::DepIterator D
= V
.DependsList(); D
.end() == false; D
++)
187 cout
<< " Depends: " << D
.TargetPkg().Name() << ' ' << D
.TargetVer() << endl
;
191 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
193 cout
<< "File: " << F
.FileName() << endl
;
194 cout
<< " Size: " << F
->Size
<< endl
;
195 cout
<< " ID: " << F
->ID
<< endl
;
196 cout
<< " Flags: " << F
->Flags
<< endl
;
197 cout
<< " Time: " << ctime(&F
->mtime
) << endl
;
203 // DumpAvail - Print out the available list /*{{{*/
204 // ---------------------------------------------------------------------
205 /* This is needed to make dpkg --merge happy */
206 bool DumpAvail(pkgCache
&Cache
)
208 unsigned char *Buffer
= new unsigned char[Cache
.HeaderP
->MaxVerFileSize
];
210 for (pkgCache::PkgFileIterator I
= Cache
.FileBegin(); I
.end() == false; I
++)
212 if ((I
->Flags
& pkgCache::Flag::NotSource
) != 0)
215 if (I
.IsOk() == false)
218 return _error
->Error("Package file %s is out of sync.",I
.FileName());
221 FileFd
PkgF(I
.FileName(),FileFd::ReadOnly
);
222 if (_error
->PendingError() == true)
228 /* Write all of the records from this package file, we search the entire
229 structure to find them */
230 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
232 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
234 if (V
->FileList
== 0)
236 if (V
.FileList().File() != I
)
239 // Read the record and then write it out again.
240 if (PkgF
.Seek(V
.FileList()->Offset
) == false ||
241 PkgF
.Read(Buffer
,V
.FileList()->Size
) == false ||
242 write(STDOUT_FILENO
,Buffer
,V
.FileList()->Size
) != V
.FileList()->Size
)
254 // DoAdd - Perform an adding operation /*{{{*/
255 // ---------------------------------------------------------------------
257 bool DoAdd(int argc
,char *argv
[])
264 FileFd
CacheF(CacheFile
,FileFd::WriteEmpty
);
265 if (_error
->PendingError() == true)
268 DynamicMMap
Map(CacheF
,MMap::Public
);
269 if (_error
->PendingError() == true)
272 OpTextProgress Progress
;
273 pkgCacheGenerator
Gen(Map
,Progress
);
274 if (_error
->PendingError() == true)
277 for (int I
= 0; I
!= argc
; I
++)
279 Progress
.OverallProgress(I
,argc
,1,"Generating cache");
280 if (SplitArg(argv
[I
],FileName
,Dist
,Ver
) == false)
284 FileFd
TagF(FileName
.c_str(),FileFd::ReadOnly
);
285 debListParser
Parser(TagF
);
286 if (_error
->PendingError() == true)
287 return _error
->Error("Problem opening %s",FileName
.c_str());
289 if (Gen
.SelectFile(FileName
) == false)
290 return _error
->Error("Problem with SelectFile");
292 if (Gen
.MergeList(Parser
) == false)
293 return _error
->Error("Problem with MergeList");
297 Stats(Gen
.GetCache());
303 int main(int argc
, char *argv
[])
308 cerr
<< "Usage is apt-cache add cache file1:dist:ver file2:dist:ver ..." << endl
;
312 pkgInitialize(*_config
);
316 if (strcmp(argv
[1],"add") == 0)
318 DoAdd(argc
- 3,argv
+ 3);
322 // Open the cache file
323 FileFd
CacheF(CacheFile
,FileFd::ReadOnly
);
324 if (_error
->PendingError() == true)
327 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
328 if (_error
->PendingError() == true)
332 if (_error
->PendingError() == true)
335 if (strcmp(argv
[1],"showpkg") == 0)
338 DumpPackage(Cache
,argc
- 3,argv
+ 3);
342 if (strcmp(argv
[1],"stats") == 0)
348 if (strcmp(argv
[1],"dump") == 0)
354 if (strcmp(argv
[1],"dumpavail") == 0)
360 _error
->Error("Invalid operation %s", argv
[1]);
364 // Print any errors or warnings found during parsing
365 if (_error
->empty() == false)
367 _error
->DumpErrors();