1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-cache.cc,v 1.6 1998/07/26 23:11:56 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>
32 #include <apt-pkg/sourcelist.h>
41 // SplitArg - Split the triple /*{{{*/
42 // ---------------------------------------------------------------------
44 bool SplitArg(const char *Arg
,string
&File
,string
&Dist
,string Ver
)
46 const char *Start
= Arg
;
48 for (;*I
!= 0 && *I
!= ':'; I
++);
50 return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
);
51 File
= string(Start
,I
- Start
);
55 for (;*I
!= 0 && *I
!= ':'; I
++);
57 return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
);
58 Dist
= string(Start
,I
- Start
);
62 for (;*I
!= 0 && *I
!= ':'; I
++);
64 return _error
->Error("Malformed argument %s, must be in file:dist:rev form",Arg
);
65 Ver
= string(Start
,I
- Start
);
70 // DumpPackage - Show a dump of a package record /*{{{*/
71 // ---------------------------------------------------------------------
73 bool DumpPackage(pkgCache
&Cache
,int argc
,char *argv
[])
75 for (int I
= 0; I
!= argc
; I
++)
77 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(argv
[I
]);
78 if (Pkg
.end() == true)
80 _error
->Warning("Unable to locate package %s",argv
[0]);
84 cout
<< "Package: " << Pkg
.Name() << endl
;
86 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
87 cout
<< Cur
.VerStr() << ',';
90 cout
<< "Reverse Depends: " << endl
;
91 for (pkgCache::DepIterator D
= Pkg
.RevDependsList(); D
.end() != true; D
++)
92 cout
<< " " << D
.ParentPkg().Name() << ',' << D
.TargetPkg().Name() << endl
;
94 cout
<< "Dependencies: " << endl
;
95 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
97 cout
<< Cur
.VerStr() << " - ";
98 for (pkgCache::DepIterator Dep
= Cur
.DependsList(); Dep
.end() != true; Dep
++)
99 cout
<< Dep
.TargetPkg().Name() << " (" << (int)Dep
->CompareOp
<< " " << Dep
.TargetVer() << ") ";
103 cout
<< "Provides: " << endl
;
104 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
106 cout
<< Cur
.VerStr() << " - ";
107 for (pkgCache::PrvIterator Prv
= Cur
.ProvidesList(); Prv
.end() != true; Prv
++)
108 cout
<< Prv
.ParentPkg().Name() << " ";
111 cout
<< "Reverse Provides: " << endl
;
112 for (pkgCache::PrvIterator Prv
= Pkg
.ProvidesList(); Prv
.end() != true; Prv
++)
113 cout
<< Prv
.OwnerPkg().Name() << " " << Prv
.OwnerVer().VerStr();
120 // Stats - Dump some nice statistics /*{{{*/
121 // ---------------------------------------------------------------------
123 bool Stats(pkgCache
&Cache
)
125 cout
<< "Total Package Names : " << Cache
.Head().PackageCount
<< endl
;
126 pkgCache::PkgIterator I
= Cache
.PkgBegin();
133 for (;I
.end() != true; I
++)
135 if (I
->VersionList
!= 0 && I
->ProvidesList
== 0)
141 if (I
->VersionList
!= 0 && I
->ProvidesList
!= 0)
147 if (I
->VersionList
== 0 && I
->ProvidesList
!= 0)
150 if (I
.ProvidesList()->NextProvides
== 0)
158 if (I
->VersionList
== 0 && I
->ProvidesList
== 0)
164 cout
<< " Normal Packages: " << Normal
<< endl
;
165 cout
<< " Pure Virtual Packages: " << Virtual
<< endl
;
166 cout
<< " Single Virtual Packages: " << DVirt
<< endl
;
167 cout
<< " Mixed Virtual Packages: " << NVirt
<< endl
;
168 cout
<< " Missing: " << Missing
<< endl
;
170 cout
<< "Total Distinct Versions: " << Cache
.Head().VersionCount
<< endl
;
171 cout
<< "Total Dependencies: " << Cache
.Head().DependsCount
<< endl
;
175 // Dump - show everything /*{{{*/
176 // ---------------------------------------------------------------------
178 bool Dump(pkgCache
&Cache
)
180 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
182 cout
<< "Package: " << P
.Name() << endl
;
183 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
185 cout
<< " Version: " << V
.VerStr() << endl
;
186 cout
<< " File: " << V
.FileList().File().FileName() << endl
;
187 for (pkgCache::DepIterator D
= V
.DependsList(); D
.end() == false; D
++)
188 cout
<< " Depends: " << D
.TargetPkg().Name() << ' ' << D
.TargetVer() << endl
;
192 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
194 cout
<< "File: " << F
.FileName() << endl
;
195 cout
<< " Size: " << F
->Size
<< endl
;
196 cout
<< " ID: " << F
->ID
<< endl
;
197 cout
<< " Flags: " << F
->Flags
<< endl
;
198 cout
<< " Time: " << ctime(&F
->mtime
) << endl
;
204 // DumpAvail - Print out the available list /*{{{*/
205 // ---------------------------------------------------------------------
206 /* This is needed to make dpkg --merge happy */
207 bool DumpAvail(pkgCache
&Cache
)
209 unsigned char *Buffer
= new unsigned char[Cache
.HeaderP
->MaxVerFileSize
];
211 for (pkgCache::PkgFileIterator I
= Cache
.FileBegin(); I
.end() == false; I
++)
213 if ((I
->Flags
& pkgCache::Flag::NotSource
) != 0)
216 if (I
.IsOk() == false)
219 return _error
->Error("Package file %s is out of sync.",I
.FileName());
222 FileFd
PkgF(I
.FileName(),FileFd::ReadOnly
);
223 if (_error
->PendingError() == true)
229 /* Write all of the records from this package file, we search the entire
230 structure to find them */
231 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
233 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
235 if (V
->FileList
== 0)
237 if (V
.FileList().File() != I
)
240 // Read the record and then write it out again.
241 if (PkgF
.Seek(V
.FileList()->Offset
) == false ||
242 PkgF
.Read(Buffer
,V
.FileList()->Size
) == false ||
243 write(STDOUT_FILENO
,Buffer
,V
.FileList()->Size
) != V
.FileList()->Size
)
255 // DoAdd - Perform an adding operation /*{{{*/
256 // ---------------------------------------------------------------------
258 bool DoAdd(int argc
,char *argv
[])
265 FileFd
CacheF(CacheFile
,FileFd::WriteEmpty
);
266 if (_error
->PendingError() == true)
269 DynamicMMap
Map(CacheF
,MMap::Public
);
270 if (_error
->PendingError() == true)
273 OpTextProgress Progress
;
274 pkgCacheGenerator
Gen(Map
,Progress
);
275 if (_error
->PendingError() == true)
278 for (int I
= 0; I
!= argc
; I
++)
280 Progress
.OverallProgress(I
,argc
,1,"Generating cache");
281 if (SplitArg(argv
[I
],FileName
,Dist
,Ver
) == false)
285 FileFd
TagF(FileName
.c_str(),FileFd::ReadOnly
);
286 debListParser
Parser(TagF
);
287 if (_error
->PendingError() == true)
288 return _error
->Error("Problem opening %s",FileName
.c_str());
290 if (Gen
.SelectFile(FileName
) == false)
291 return _error
->Error("Problem with SelectFile");
293 if (Gen
.MergeList(Parser
) == false)
294 return _error
->Error("Problem with MergeList");
298 Stats(Gen
.GetCache());
303 // GenCaches - Call the main cache generator /*{{{*/
304 // ---------------------------------------------------------------------
308 OpTextProgress Progress
;
311 return pkgMakeStatusCache(List
,Progress
);
315 int main(int argc
, char *argv
[])
320 cerr
<< "Usage is apt-cache add cache file1:dist:ver file2:dist:ver ..." << endl
;
324 pkgInitialize(*_config
);
328 if (strcmp(argv
[1],"add") == 0)
330 DoAdd(argc
- 3,argv
+ 3);
334 if (strcmp(argv
[1],"gencaches") == 0)
340 // Open the cache file
341 FileFd
CacheF(CacheFile
,FileFd::ReadOnly
);
342 if (_error
->PendingError() == true)
345 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
346 if (_error
->PendingError() == true)
350 if (_error
->PendingError() == true)
353 if (strcmp(argv
[1],"showpkg") == 0)
356 DumpPackage(Cache
,argc
- 3,argv
+ 3);
360 if (strcmp(argv
[1],"stats") == 0)
366 if (strcmp(argv
[1],"dump") == 0)
372 if (strcmp(argv
[1],"dumpavail") == 0)
378 _error
->Error("Invalid operation %s", argv
[1]);
382 // Print any errors or warnings found during parsing
383 if (_error
->empty() == false)
385 _error
->DumpErrors();