]>
git.saurik.com Git - apt.git/blob - cmdline/apt-cache.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-cache.cc,v 1.35 1999/05/11 05:45:10 jgg Exp $
4 /* ######################################################################
6 apt-cache - Manages the cache files
8 apt-cache provides some functions fo manipulating the cache files.
9 It uses the command line interface common to all the APT tools. The
10 only really usefull function right now is dumpavail which is used
11 by the dselect method. Everything else is ment as a debug aide.
13 Returns 100 on failure, 0 on success.
15 ##################################################################### */
17 // Include Files /*{{{*/
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/pkgcachegen.h>
20 #include <apt-pkg/deblistparser.h>
21 #include <apt-pkg/init.h>
22 #include <apt-pkg/progress.h>
23 #include <apt-pkg/sourcelist.h>
24 #include <apt-pkg/cmndline.h>
25 #include <apt-pkg/strutl.h>
26 #include <apt-pkg/pkgrecords.h>
37 // UnMet - Show unmet dependencies /*{{{*/
38 // ---------------------------------------------------------------------
40 bool UnMet(CommandLine
&CmdL
)
42 pkgCache
&Cache
= *GCache
;
43 bool Important
= _config
->FindB("APT::Cache::Important",false);
45 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
47 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
50 for (pkgCache::DepIterator D
= V
.DependsList(); D
.end() == false;)
53 pkgCache::DepIterator Start
;
54 pkgCache::DepIterator End
;
57 /* cout << "s: Check " << Start.TargetPkg().Name() << ',' <<
58 End.TargetPkg().Name() << endl;*/
60 // Skip conflicts and replaces
61 if (End
->Type
!= pkgCache::Dep::PreDepends
&&
62 End
->Type
!= pkgCache::Dep::Depends
&&
63 End
->Type
!= pkgCache::Dep::Suggests
&&
64 End
->Type
!= pkgCache::Dep::Recommends
)
67 // Important deps only
68 if (Important
== true)
69 if (End
->Type
!= pkgCache::Dep::PreDepends
&&
70 End
->Type
!= pkgCache::Dep::Depends
)
73 // Verify the or group
75 pkgCache::DepIterator RealStart
= Start
;
78 // See if this dep is Ok
79 pkgCache::Version
**VList
= Start
.AllTargets();
100 cout
<< "Package " << P
.Name() << " version " <<
101 V
.VerStr() << " has an unmet dep:" << endl
;
104 // Print out the dep type
105 cout
<< " " << End
.DepType() << ": ";
111 cout
<< Start
.TargetPkg().Name();
112 if (Start
.TargetVer() != 0)
113 cout
<< " (" << Start
.CompType() << " " << Start
.TargetVer() <<
129 // DumpPackage - Show a dump of a package record /*{{{*/
130 // ---------------------------------------------------------------------
132 bool DumpPackage(CommandLine
&CmdL
)
134 pkgCache
&Cache
= *GCache
;
135 for (const char **I
= CmdL
.FileList
+ 1; *I
!= 0; I
++)
137 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(*I
);
138 if (Pkg
.end() == true)
140 _error
->Warning("Unable to locate package %s",*I
);
144 cout
<< "Package: " << Pkg
.Name() << endl
;
145 cout
<< "Versions: ";
146 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
148 cout
<< Cur
.VerStr();
149 for (pkgCache::VerFileIterator Vf
= Cur
.FileList(); Vf
.end() == false; Vf
++)
150 cout
<< "(" << Vf
.File().FileName() << ")";
156 cout
<< "Reverse Depends: " << endl
;
157 for (pkgCache::DepIterator D
= Pkg
.RevDependsList(); D
.end() != true; D
++)
158 cout
<< " " << D
.ParentPkg().Name() << ',' << D
.TargetPkg().Name() << endl
;
160 cout
<< "Dependencies: " << endl
;
161 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
163 cout
<< Cur
.VerStr() << " - ";
164 for (pkgCache::DepIterator Dep
= Cur
.DependsList(); Dep
.end() != true; Dep
++)
165 cout
<< Dep
.TargetPkg().Name() << " (" << (int)Dep
->CompareOp
<< " " << Dep
.TargetVer() << ") ";
169 cout
<< "Provides: " << endl
;
170 for (pkgCache::VerIterator Cur
= Pkg
.VersionList(); Cur
.end() != true; Cur
++)
172 cout
<< Cur
.VerStr() << " - ";
173 for (pkgCache::PrvIterator Prv
= Cur
.ProvidesList(); Prv
.end() != true; Prv
++)
174 cout
<< Prv
.ParentPkg().Name() << " ";
177 cout
<< "Reverse Provides: " << endl
;
178 for (pkgCache::PrvIterator Prv
= Pkg
.ProvidesList(); Prv
.end() != true; Prv
++)
179 cout
<< Prv
.OwnerPkg().Name() << " " << Prv
.OwnerVer().VerStr() << endl
;
185 // Stats - Dump some nice statistics /*{{{*/
186 // ---------------------------------------------------------------------
188 bool Stats(CommandLine
&Cmd
)
190 pkgCache
&Cache
= *GCache
;
191 cout
<< "Total Package Names : " << Cache
.Head().PackageCount
<< " (" <<
192 SizeToStr(Cache
.Head().PackageCount
*Cache
.Head().PackageSz
) << ')' << endl
;
193 pkgCache::PkgIterator I
= Cache
.PkgBegin();
200 for (;I
.end() != true; I
++)
202 if (I
->VersionList
!= 0 && I
->ProvidesList
== 0)
208 if (I
->VersionList
!= 0 && I
->ProvidesList
!= 0)
214 if (I
->VersionList
== 0 && I
->ProvidesList
!= 0)
217 if (I
.ProvidesList()->NextProvides
== 0)
225 if (I
->VersionList
== 0 && I
->ProvidesList
== 0)
231 cout
<< " Normal Packages: " << Normal
<< endl
;
232 cout
<< " Pure Virtual Packages: " << Virtual
<< endl
;
233 cout
<< " Single Virtual Packages: " << DVirt
<< endl
;
234 cout
<< " Mixed Virtual Packages: " << NVirt
<< endl
;
235 cout
<< " Missing: " << Missing
<< endl
;
237 cout
<< "Total Distinct Versions: " << Cache
.Head().VersionCount
<< " (" <<
238 SizeToStr(Cache
.Head().VersionCount
*Cache
.Head().VersionSz
) << ')' << endl
;
239 cout
<< "Total Dependencies: " << Cache
.Head().DependsCount
<< " (" <<
240 SizeToStr(Cache
.Head().DependsCount
*Cache
.Head().DependencySz
) << ')' << endl
;
242 cout
<< "Total Ver/File relations: " << Cache
.Head().VerFileCount
<< " (" <<
243 SizeToStr(Cache
.Head().VerFileCount
*Cache
.Head().VerFileSz
) << ')' << endl
;
244 cout
<< "Total Provides Mappings: " << Cache
.Head().ProvidesCount
<< " (" <<
245 SizeToStr(Cache
.Head().ProvidesCount
*Cache
.Head().ProvidesSz
) << ')' << endl
;
248 unsigned long Size
= 0;
249 unsigned long Count
= 0;
250 for (pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.Head().StringList
;
251 I
!= Cache
.StringItemP
; I
= Cache
.StringItemP
+ I
->NextItem
)
254 Size
+= strlen(Cache
.StrP
+ I
->String
);
256 cout
<< "Total Globbed Strings: " << Count
<< " (" << SizeToStr(Size
) << ')' << endl
;
258 unsigned long Slack
= 0;
259 for (int I
= 0; I
!= 7; I
++)
260 Slack
+= Cache
.Head().Pools
[I
].ItemSize
*Cache
.Head().Pools
[I
].Count
;
261 cout
<< "Total Slack space: " << SizeToStr(Slack
) << endl
;
263 unsigned long Total
= 0;
264 Total
= Slack
+ Size
+ Cache
.Head().DependsCount
*Cache
.Head().DependencySz
+
265 Cache
.Head().VersionCount
*Cache
.Head().VersionSz
+
266 Cache
.Head().PackageCount
*Cache
.Head().PackageSz
+
267 Cache
.Head().VerFileCount
*Cache
.Head().VerFileSz
+
268 Cache
.Head().ProvidesCount
*Cache
.Head().ProvidesSz
;
269 cout
<< "Total Space Accounted for: " << SizeToStr(Total
) << endl
;
274 // Check - Check some things about the cache /*{{{*/
275 // ---------------------------------------------------------------------
276 /* Debug aide mostly */
277 bool Check(CommandLine
&Cmd
)
279 pkgCache
&Cache
= *GCache
;
280 pkgCache::PkgIterator Pkg
= Cache
.PkgBegin();
281 for (;Pkg
.end() != true; Pkg
++)
283 if (Pkg
.Section() == 0 && Pkg
->VersionList
!= 0)
284 cout
<< "Bad section " << Pkg
.Name() << endl
;
286 for (pkgCache::VerIterator Cur
= Pkg
.VersionList();
287 Cur
.end() != true; Cur
++)
289 if (Cur
->Priority
< 1 || Cur
->Priority
> 5)
290 cout
<< "Bad prio " << Pkg
.Name() << ',' << Cur
.VerStr() << " == " << (int)Cur
->Priority
<< endl
;
296 // Dump - show everything /*{{{*/
297 // ---------------------------------------------------------------------
299 bool Dump(CommandLine
&Cmd
)
301 pkgCache
&Cache
= *GCache
;
302 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
304 cout
<< "Package: " << P
.Name() << endl
;
305 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
307 cout
<< " Version: " << V
.VerStr() << endl
;
308 cout
<< " File: " << V
.FileList().File().FileName() << endl
;
309 for (pkgCache::DepIterator D
= V
.DependsList(); D
.end() == false; D
++)
310 cout
<< " Depends: " << D
.TargetPkg().Name() << ' ' << D
.TargetVer() << endl
;
314 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
316 cout
<< "File: " << F
.FileName() << endl
;
317 cout
<< " Size: " << F
->Size
<< endl
;
318 cout
<< " ID: " << F
->ID
<< endl
;
319 cout
<< " Flags: " << F
->Flags
<< endl
;
320 cout
<< " Time: " << TimeRFC1123(F
->mtime
) << endl
;
321 cout
<< " Archive: " << F
.Archive() << endl
;
322 cout
<< " Component: " << F
.Component() << endl
;
323 cout
<< " Version: " << F
.Version() << endl
;
324 cout
<< " Origin: " << F
.Origin() << endl
;
325 cout
<< " Label: " << F
.Label() << endl
;
326 cout
<< " Architecture: " << F
.Architecture() << endl
;
332 // DumpAvail - Print out the available list /*{{{*/
333 // ---------------------------------------------------------------------
334 /* This is needed to make dpkg --merge happy */
335 bool DumpAvail(CommandLine
&Cmd
)
337 pkgCache
&Cache
= *GCache
;
338 unsigned char *Buffer
= new unsigned char[Cache
.HeaderP
->MaxVerFileSize
];
340 for (pkgCache::PkgFileIterator I
= Cache
.FileBegin(); I
.end() == false; I
++)
342 if ((I
->Flags
& pkgCache::Flag::NotSource
) != 0)
345 if (I
.IsOk() == false)
348 return _error
->Error("Package file %s is out of sync.",I
.FileName());
351 FileFd
PkgF(I
.FileName(),FileFd::ReadOnly
);
352 if (_error
->PendingError() == true)
358 /* Write all of the records from this package file, we search the entire
359 structure to find them */
360 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
362 // Find the proper version to use. We should probably use the DepCache.
363 pkgCache::VerIterator V
= Cache
.GetCandidateVer(P
,false);
365 if (V
.end() == true || V
.FileList().File() != I
)
368 // Read the record and then write it out again.
369 if (PkgF
.Seek(V
.FileList()->Offset
) == false ||
370 PkgF
.Read(Buffer
,V
.FileList()->Size
) == false ||
371 write(STDOUT_FILENO
,Buffer
,V
.FileList()->Size
) != V
.FileList()->Size
)
382 // DoAdd - Perform an adding operation /*{{{*/
383 // ---------------------------------------------------------------------
385 bool DoAdd(CommandLine
&CmdL
)
387 // Make sure there is at least one argument
388 if (CmdL
.FileSize() <= 1)
389 return _error
->Error("You must give at least one file name");
392 FileFd
CacheF(_config
->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny
);
393 if (_error
->PendingError() == true)
396 DynamicMMap
Map(CacheF
,MMap::Public
);
397 if (_error
->PendingError() == true)
400 OpTextProgress
Progress(*_config
);
401 pkgCacheGenerator
Gen(Map
,Progress
);
402 if (_error
->PendingError() == true)
405 unsigned long Length
= CmdL
.FileSize() - 1;
406 for (const char **I
= CmdL
.FileList
+ 1; *I
!= 0; I
++)
408 Progress
.OverallProgress(I
- CmdL
.FileList
,Length
,1,"Generating cache");
409 Progress
.SubProgress(Length
);
412 FileFd
TagF(*I
,FileFd::ReadOnly
);
413 debListParser
Parser(TagF
);
414 if (_error
->PendingError() == true)
415 return _error
->Error("Problem opening %s",*I
);
417 if (Gen
.SelectFile(*I
) == false)
418 return _error
->Error("Problem with SelectFile");
420 if (Gen
.MergeList(Parser
) == false)
421 return _error
->Error("Problem with MergeList");
425 GCache
= &Gen
.GetCache();
431 // DisplayRecord - Displays the complete record for the package /*{{{*/
432 // ---------------------------------------------------------------------
433 /* This displays the package record from the proper package index file.
434 It is not used by DumpAvail for performance reasons. */
435 bool DisplayRecord(pkgCache::VerIterator V
)
437 // Find an appropriate file
438 pkgCache::VerFileIterator Vf
= V
.FileList();
439 for (; Vf
.end() == false; Vf
++)
440 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) == 0)
442 if (Vf
.end() == true)
445 // Check and load the package list file
446 pkgCache::PkgFileIterator I
= Vf
.File();
447 if (I
.IsOk() == false)
448 return _error
->Error("Package file %s is out of sync.",I
.FileName());
450 FileFd
PkgF(I
.FileName(),FileFd::ReadOnly
);
451 if (_error
->PendingError() == true)
454 // Read the record and then write it out again.
455 unsigned char *Buffer
= new unsigned char[GCache
->HeaderP
->MaxVerFileSize
];
456 if (PkgF
.Seek(V
.FileList()->Offset
) == false ||
457 PkgF
.Read(Buffer
,V
.FileList()->Size
) == false ||
458 write(STDOUT_FILENO
,Buffer
,V
.FileList()->Size
) != V
.FileList()->Size
)
469 // Search - Perform a search /*{{{*/
470 // ---------------------------------------------------------------------
471 /* This searches the package names and pacakge descriptions for a pattern */
472 bool Search(CommandLine
&CmdL
)
474 pkgCache
&Cache
= *GCache
;
475 bool ShowFull
= _config
->FindB("APT::Cache::ShowFull",false);
476 bool NamesOnly
= _config
->FindB("APT::Cache::NamesOnly",false);
478 // Make sure there is at least one argument
479 if (CmdL
.FileSize() != 2)
480 return _error
->Error("You must give exactly one pattern");
482 // Compile the regex pattern
484 if (regcomp(&Pattern
,CmdL
.FileList
[1],REG_EXTENDED
| REG_ICASE
|
486 return _error
->Error("Regex compilation error");
488 // Create the text record parser
489 pkgRecords
Recs(Cache
);
490 if (_error
->PendingError() == true)
493 // Search package names
494 pkgCache::PkgIterator I
= Cache
.PkgBegin();
495 for (;I
.end() != true; I
++)
497 // We search against the install version as that makes the most sense..
498 pkgCache::VerIterator V
= Cache
.GetCandidateVer(I
);
502 pkgRecords::Parser
&P
= Recs
.Lookup(V
.FileList());
504 if (regexec(&Pattern
,I
.Name(),0,0,0) == 0 ||
505 (NamesOnly
== false &&
506 regexec(&Pattern
,P
.LongDesc().c_str(),0,0,0) == 0))
508 if (ShowFull
== true)
511 cout
<< I
.Name() << " - " << P
.ShortDesc() << endl
;
519 // ShowPackage - Dump the package record to the screen /*{{{*/
520 // ---------------------------------------------------------------------
522 bool ShowPackage(CommandLine
&CmdL
)
524 pkgCache
&Cache
= *GCache
;
525 for (const char **I
= CmdL
.FileList
+ 1; *I
!= 0; I
++)
527 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(*I
);
528 if (Pkg
.end() == true)
530 _error
->Warning("Unable to locate package %s",*I
);
534 // Find the proper version to use. We should probably use the DepCache.
535 pkgCache::VerIterator V
= Cache
.GetCandidateVer(Pkg
);
536 if (V
.end() == true || V
.FileList().end() == true)
538 if (DisplayRecord(V
) == false)
544 // GenCaches - Call the main cache generator /*{{{*/
545 // ---------------------------------------------------------------------
547 bool GenCaches(CommandLine
&Cmd
)
549 OpTextProgress
Progress(*_config
);
553 return pkgMakeStatusCache(List
,Progress
);
556 // ShowHelp - Show a help screen /*{{{*/
557 // ---------------------------------------------------------------------
559 bool ShowHelp(CommandLine
&Cmd
)
561 cout
<< PACKAGE
<< ' ' << VERSION
<< " for " << ARCHITECTURE
<<
562 " compiled on " << __DATE__
<< " " << __TIME__
<< endl
;
563 if (_config
->FindB("version") == true)
566 cout
<< "Usage: apt-cache [options] command" << endl
;
567 cout
<< " apt-cache [options] add file1 [file1 ...]" << endl
;
568 cout
<< " apt-cache [options] showpkg pkg1 [pkg2 ...]" << endl
;
570 cout
<< "apt-cache is a low-level tool used to manipulate APT's binary" << endl
;
571 cout
<< "cache files stored in " << _config
->FindFile("Dir::Cache") << endl
;
572 cout
<< "It is not ment for ordinary use only as a debug aide." << endl
;
574 cout
<< "Commands:" << endl
;
575 cout
<< " add - Add an package file to the source cache" << endl
;
576 cout
<< " gencaches - Build both the package and source cache" << endl
;
577 cout
<< " showpkg - Show some general information for a single package" << endl
;
578 cout
<< " stats - Show some basic statistics" << endl
;
579 cout
<< " dump - Show the entire file in a terse form" << endl
;
580 cout
<< " dumpavail - Print an available file to stdout" << endl
;
581 cout
<< " unmet - Show unmet dependencies" << endl
;
582 cout
<< " check - Check the cache a bit" << endl
;
583 cout
<< " search - Search the package list for a regex pattern" << endl
;
584 cout
<< " show - Show a readable record for the package" << endl
;
586 cout
<< "Options:" << endl
;
587 cout
<< " -h This help text." << endl
;
588 cout
<< " -p=? The package cache. [" << _config
->FindFile("Dir::Cache::pkgcache") << ']' << endl
;
589 cout
<< " -s=? The source cache. [" << _config
->FindFile("Dir::Cache::srcpkgcache") << ']' << endl
;
590 cout
<< " -q Disable progress indicator." << endl
;
591 cout
<< " -i Show only important deps for the unmet command." << endl
;
592 cout
<< " -c=? Read this configuration file" << endl
;
593 cout
<< " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl
;
594 cout
<< "See the apt-cache(8) and apt.conf(5) manual pages for more information." << endl
;
598 // CacheInitialize - Initialize things for apt-cache /*{{{*/
599 // ---------------------------------------------------------------------
601 void CacheInitialize()
603 _config
->Set("quiet",0);
604 _config
->Set("help",false);
608 int main(int argc
,const char *argv
[])
610 CommandLine::Args Args
[] = {
611 {'h',"help","help",0},
612 {'v',"version","version",0},
613 {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg
},
614 {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg
},
615 {'q',"quiet","quiet",CommandLine::IntLevel
},
616 {'i',"important","APT::Cache::Important",0},
617 {'f',"full","APT::Cache::ShowFull",0},
618 {0,"names-only","APT::Cache::NamesOnly",0},
619 {'c',"config-file",0,CommandLine::ConfigFile
},
620 {'o',"option",0,CommandLine::ArbItem
},
622 CommandLine::Dispatch CmdsA
[] = {{"help",&ShowHelp
},
624 {"gencaches",&GenCaches
},
626 CommandLine::Dispatch CmdsB
[] = {{"showpkg",&DumpPackage
},
629 {"dumpavail",&DumpAvail
},
633 {"show",&ShowPackage
},
638 // Parse the command line and initialize the package library
639 CommandLine
CmdL(Args
,_config
);
640 if (pkgInitialize(*_config
) == false ||
641 CmdL
.Parse(argc
,argv
) == false)
643 _error
->DumpErrors();
647 // See if the help should be shown
648 if (_config
->FindB("help") == true ||
649 CmdL
.FileSize() == 0)
650 return ShowHelp(CmdL
);
652 // Deal with stdout not being a tty
653 if (ttyname(STDOUT_FILENO
) == 0 && _config
->FindI("quiet",0) < 1)
654 _config
->Set("quiet","1");
656 if (CmdL
.DispatchArg(CmdsA
,false) == false && _error
->PendingError() == false)
658 // Open the cache file
662 // Generate it and map it
664 MMap
*Map
= pkgMakeStatusCacheMem(List
,Prog
);
665 if (_error
->PendingError() == false)
667 pkgCache
Cache(*Map
);
669 if (_error
->PendingError() == false)
670 CmdL
.DispatchArg(CmdsB
);
675 // Print any errors or warnings found during parsing
676 if (_error
->empty() == false)
678 bool Errors
= _error
->PendingError();
679 _error
->DumpErrors();
680 return Errors
== true?100:0;