]>
git.saurik.com Git - apt.git/blob - cmdline/apt-cache.cc
ac5dc17ca19cff93ec1b0e925b1688c3781ede6c
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-cache.cc,v 1.2 1998/07/16 06:08:43 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 // DoAdd - Perform an adding operation /*{{{*/
69 // ---------------------------------------------------------------------
71 bool DoAdd(int argc
,char *argv
[])
77 File
CacheF(CacheFile
,File::WriteEmpty
);
78 if (_error
->PendingError() == true)
81 DynamicMMap
Map(CacheF
,MMap::Public
);
82 if (_error
->PendingError() == true)
85 pkgCacheGenerator
Gen(Map
);
86 if (_error
->PendingError() == true)
89 for (int I
= 0; I
!= argc
; I
++)
91 if (SplitArg(argv
[I
],FileName
,Dist
,Ver
) == false)
93 cout
<< FileName
<< endl
;
96 File
TagF(FileName
.c_str(),File::ReadOnly
);
97 debListParser
Parser(TagF
);
98 if (_error
->PendingError() == true)
99 return _error
->Error("Problem opening %s",FileName
.c_str());
101 if (Gen
.SelectFile(FileName
) == false)
102 return _error
->Error("Problem with SelectFile");
104 if (Gen
.MergeList(Parser
) == false)
105 return _error
->Error("Problem with MergeList");
111 // DumpPackage - Show a dump of a package record /*{{{*/
112 // ---------------------------------------------------------------------
114 bool DumpPackage(int argc
,char *argv
[])
116 File
CacheF(CacheFile
,File::ReadOnly
);
117 if (_error
->PendingError() == true)
120 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
121 if (_error
->PendingError() == true)
125 if (_error
->PendingError() == true)
128 for (int I
= 0; I
!= argc
; I
++)
130 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(argv
[I
]);
131 if (Pkg
.end() == true)
133 _error
->Warning("Unable to locate package %s",argv
[0]);
137 cout
<< "Package: " << Pkg
.Name() << endl
;
138 cout
<< "Versions: ";
139 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
140 cout
<< Cur
.VerStr() << ',';
143 cout
<< "Reverse Depends: " << endl
;
144 for (pkgCache::DepIterator D
= Pkg
.RevDependsList(); D
.end() != true; D
++)
145 cout
<< " " << D
.ParentPkg().Name() << ',' << D
.TargetPkg().Name() << endl
;
147 cout
<< "Dependencies: " << endl
;
148 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
150 cout
<< Cur
.VerStr() << " - ";
151 for (pkgCache::DepIterator Dep
= Cur
.DependsList(); Dep
.end() != true; Dep
++)
152 cout
<< Dep
.TargetPkg().Name() << " (" << (int)Dep
->CompareOp
<< " " << Dep
.TargetVer() << ") ";
156 cout
<< "Provides: " << endl
;
157 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
159 cout
<< Cur
.VerStr() << " - ";
160 for (pkgCache::PrvIterator Prv
= Cur
.ProvidesList(); Prv
.end() != true; Prv
++)
161 cout
<< Prv
.ParentPkg().Name() << " ";
164 cout
<< "Reverse Provides: " << endl
;
165 for (pkgCache::PrvIterator Prv
= Pkg
.ProvidesList(); Prv
.end() != true; Prv
++)
166 cout
<< Prv
.OwnerPkg().Name() << " " << Prv
.OwnerVer().VerStr();
173 // Stats - Dump some nice statistics /*{{{*/
174 // ---------------------------------------------------------------------
176 bool Stats(const char *FileName
)
178 File
CacheF(FileName
,File::ReadOnly
);
179 if (_error
->PendingError() == true)
182 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
183 if (_error
->PendingError() == true)
187 if (_error
->PendingError() == true)
190 cout
<< "Total Package Names : " << Cache
.Head().PackageCount
<< endl
;
191 pkgCache::PkgIterator I
= Cache
.PkgBegin();
198 for (;I
.end() != true; I
++)
200 if (I
->VersionList
!= 0 && I
->ProvidesList
== 0)
206 if (I
->VersionList
!= 0 && I
->ProvidesList
!= 0)
212 if (I
->VersionList
== 0 && I
->ProvidesList
!= 0)
215 if (I
.ProvidesList()->NextProvides
== 0)
223 if (I
->VersionList
== 0 && I
->ProvidesList
== 0)
229 cout
<< " Normal Packages: " << Normal
<< endl
;
230 cout
<< " Pure Virtual Packages: " << Virtual
<< endl
;
231 cout
<< " Single Virtual Packages: " << DVirt
<< endl
;
232 cout
<< " Mixed Virtual Packages: " << NVirt
<< endl
;
233 cout
<< " Missing: " << Missing
<< endl
;
235 cout
<< "Total Distinct Versions: " << Cache
.Head().VersionCount
<< endl
;
236 cout
<< "Total Dependencies: " << Cache
.Head().DependsCount
<< endl
;
240 // Dump - show everything /*{{{*/
241 // ---------------------------------------------------------------------
245 File
CacheF(CacheFile
,File::ReadOnly
);
246 if (_error
->PendingError() == true)
249 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
250 if (_error
->PendingError() == true)
254 if (_error
->PendingError() == true)
257 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
259 cout
<< "Package: " << P
.Name() << endl
;
260 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
262 cout
<< " Version: " << V
.VerStr() << endl
;
263 cout
<< " File: " << V
.FileList().File().FileName() << endl
;
264 for (pkgCache::DepIterator D
= V
.DependsList(); D
.end() == false; D
++)
265 cout
<< " Depends: " << D
.TargetPkg().Name() << ' ' << D
.TargetVer() << endl
;
269 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
271 cout
<< "File: " << F
.FileName() << endl
;
272 cout
<< " Size: " << F
->Size
<< endl
;
273 cout
<< " ID: " << F
->ID
<< endl
;
274 cout
<< " Flags: " << F
->Flags
<< endl
;
275 cout
<< " Time: " << ctime(&F
->mtime
) << endl
;
281 // DumpAvail - Print out the available list /*{{{*/
282 // ---------------------------------------------------------------------
283 /* This is needed to make dpkg --merge happy */
287 pkgCache
Cache(CacheFile
,true,true);
288 if (_error
->PendingError() == true)
291 pkgControlCache
CCache(Cache
);
292 if (_error
->PendingError() == true)
295 vector
<string
> Lines
;
298 pkgCache::PkgIterator I
= Cache
.PkgBegin();
299 for (;I
.end() != true; I
++)
301 if (I
->VersionList
== 0)
304 pkgSPkgCtrlInfo Inf
= CCache
[I
.VersionList()];
305 if (Inf
.isNull() == true)
306 return _error
->Error("Couldn't locate info record");
308 // Iterate over each element
309 pkgPkgCtrlInfo::const_iterator Elm
= Inf
->begin();
310 for (; Elm
!= Inf
->end(); Elm
++)
312 // Write the tag: value
313 cout
<< (*Elm
)->Tag() << ": " << (*Elm
)->Value() << endl
;
315 // Write the multiline
316 (*Elm
)->GetLines(Lines
);
317 for (vector
<string
>::iterator j
= Lines
.begin(); j
!= Lines
.end(); j
++)
319 if ((*j
).length() == 0)
320 cout
<< " ." << endl
;
322 cout
<< " " << *j
<< endl
;
325 Lines
.erase(Lines
.begin(),Lines
.end());
335 int main(int argc
, char *argv
[])
340 cerr
<< "Usage is apt-cache add cache file1:dist:ver file2:dist:ver ..." << endl
;
344 pkgInitialize(*_config
);
347 if (strcmp(argv
[1],"add") == 0)
350 if (DoAdd(argc
- 3,argv
+ 3) == true)
355 if (strcmp(argv
[1],"showpkg") == 0)
358 DumpPackage(argc
- 3,argv
+ 3);
362 if (strcmp(argv
[1],"stats") == 0)
368 if (strcmp(argv
[1],"dump") == 0)
375 if (strcmp(argv
[1],"dumpavail") == 0)
382 _error
->Error("Invalid operation %s", argv
[1]);
386 // Print any errors or warnings found during parsing
387 if (_error
->empty() == false)
389 _error
->DumpErrors();