]>
git.saurik.com Git - apt.git/blob - cmdline/apt-cache.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: apt-cache.cc,v 1.32 1999/03/29 19:28:52 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();
187 // Stats - Dump some nice statistics /*{{{*/
188 // ---------------------------------------------------------------------
190 bool Stats(CommandLine
&Cmd
)
192 pkgCache
&Cache
= *GCache
;
193 cout
<< "Total Package Names : " << Cache
.Head().PackageCount
<< " (" <<
194 SizeToStr(Cache
.Head().PackageCount
*Cache
.Head().PackageSz
) << ')' << endl
;
195 pkgCache::PkgIterator I
= Cache
.PkgBegin();
202 for (;I
.end() != true; I
++)
204 if (I
->VersionList
!= 0 && I
->ProvidesList
== 0)
210 if (I
->VersionList
!= 0 && I
->ProvidesList
!= 0)
216 if (I
->VersionList
== 0 && I
->ProvidesList
!= 0)
219 if (I
.ProvidesList()->NextProvides
== 0)
227 if (I
->VersionList
== 0 && I
->ProvidesList
== 0)
233 cout
<< " Normal Packages: " << Normal
<< endl
;
234 cout
<< " Pure Virtual Packages: " << Virtual
<< endl
;
235 cout
<< " Single Virtual Packages: " << DVirt
<< endl
;
236 cout
<< " Mixed Virtual Packages: " << NVirt
<< endl
;
237 cout
<< " Missing: " << Missing
<< endl
;
239 cout
<< "Total Distinct Versions: " << Cache
.Head().VersionCount
<< " (" <<
240 SizeToStr(Cache
.Head().VersionCount
*Cache
.Head().VersionSz
) << ')' << endl
;
241 cout
<< "Total Dependencies: " << Cache
.Head().DependsCount
<< " (" <<
242 SizeToStr(Cache
.Head().DependsCount
*Cache
.Head().DependencySz
) << ')' << endl
;
244 cout
<< "Total Ver/File relations: " << Cache
.Head().VerFileCount
<< " (" <<
245 SizeToStr(Cache
.Head().VerFileCount
*Cache
.Head().VerFileSz
) << ')' << endl
;
246 cout
<< "Total Provides Mappings: " << Cache
.Head().ProvidesCount
<< " (" <<
247 SizeToStr(Cache
.Head().ProvidesCount
*Cache
.Head().ProvidesSz
) << ')' << endl
;
250 unsigned long Size
= 0;
251 unsigned long Count
= 0;
252 for (pkgCache::StringItem
*I
= Cache
.StringItemP
+ Cache
.Head().StringList
;
253 I
!= Cache
.StringItemP
; I
= Cache
.StringItemP
+ I
->NextItem
)
256 Size
+= strlen(Cache
.StrP
+ I
->String
);
258 cout
<< "Total Globbed Strings: " << Count
<< " (" << SizeToStr(Size
) << ')' << endl
;
260 unsigned long Slack
= 0;
261 for (int I
= 0; I
!= 7; I
++)
262 Slack
+= Cache
.Head().Pools
[I
].ItemSize
*Cache
.Head().Pools
[I
].Count
;
263 cout
<< "Total Slack space: " << SizeToStr(Slack
) << endl
;
265 unsigned long Total
= 0;
266 Total
= Slack
+ Size
+ Cache
.Head().DependsCount
*Cache
.Head().DependencySz
+
267 Cache
.Head().VersionCount
*Cache
.Head().VersionSz
+
268 Cache
.Head().PackageCount
*Cache
.Head().PackageSz
+
269 Cache
.Head().VerFileCount
*Cache
.Head().VerFileSz
+
270 Cache
.Head().ProvidesCount
*Cache
.Head().ProvidesSz
;
271 cout
<< "Total Space Accounted for: " << SizeToStr(Total
) << endl
;
276 // Check - Check some things about the cache /*{{{*/
277 // ---------------------------------------------------------------------
278 /* Debug aide mostly */
279 bool Check(CommandLine
&Cmd
)
281 pkgCache
&Cache
= *GCache
;
282 pkgCache::PkgIterator Pkg
= Cache
.PkgBegin();
283 for (;Pkg
.end() != true; Pkg
++)
285 if (Pkg
.Section() == 0 && Pkg
->VersionList
!= 0)
286 cout
<< "Bad section " << Pkg
.Name() << endl
;
288 for (pkgCache::VerIterator Cur
= Pkg
.VersionList();
289 Cur
.end() != true; Cur
++)
291 if (Cur
->Priority
< 1 || Cur
->Priority
> 5)
292 cout
<< "Bad prio " << Pkg
.Name() << ',' << Cur
.VerStr() << " == " << (int)Cur
->Priority
<< endl
;
298 // Dump - show everything /*{{{*/
299 // ---------------------------------------------------------------------
301 bool Dump(CommandLine
&Cmd
)
303 pkgCache
&Cache
= *GCache
;
304 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
306 cout
<< "Package: " << P
.Name() << endl
;
307 for (pkgCache::VerIterator V
= P
.VersionList(); V
.end() == false; V
++)
309 cout
<< " Version: " << V
.VerStr() << endl
;
310 cout
<< " File: " << V
.FileList().File().FileName() << endl
;
311 for (pkgCache::DepIterator D
= V
.DependsList(); D
.end() == false; D
++)
312 cout
<< " Depends: " << D
.TargetPkg().Name() << ' ' << D
.TargetVer() << endl
;
316 for (pkgCache::PkgFileIterator
F(Cache
); F
.end() == false; F
++)
318 cout
<< "File: " << F
.FileName() << endl
;
319 cout
<< " Size: " << F
->Size
<< endl
;
320 cout
<< " ID: " << F
->ID
<< endl
;
321 cout
<< " Flags: " << F
->Flags
<< endl
;
322 cout
<< " Time: " << TimeRFC1123(F
->mtime
) << endl
;
323 cout
<< " Archive: " << F
.Archive() << endl
;
324 cout
<< " Component: " << F
.Component() << endl
;
325 cout
<< " Version: " << F
.Version() << endl
;
326 cout
<< " Origin: " << F
.Origin() << endl
;
327 cout
<< " Label: " << F
.Label() << endl
;
328 cout
<< " Architecture: " << F
.Architecture() << endl
;
334 // GetCandidateVer - Returns the Candidate install version /*{{{*/
335 // ---------------------------------------------------------------------
336 /* This should really use the DepCache or something.. Copied from there.
337 Allow current is required to filter out the status file when emitting
338 an available file. */
339 static pkgCache::VerIterator
GetCandidateVer(pkgCache::PkgIterator Pkg
,
340 bool AllowCurrent
= true)
342 /* Not source/not automatic versions cannot be a candidate version
343 unless they are already installed */
344 for (pkgCache::VerIterator I
= Pkg
.VersionList(); I
.end() == false; I
++)
346 if (Pkg
.CurrentVer() == I
&& AllowCurrent
== true)
349 for (pkgCache::VerFileIterator J
= I
.FileList(); J
.end() == false; J
++)
350 if ((J
.File()->Flags
& pkgCache::Flag::NotSource
) == 0 &&
351 (J
.File()->Flags
& pkgCache::Flag::NotAutomatic
) == 0)
355 return pkgCache::VerIterator(*GCache
,0);
358 // DumpAvail - Print out the available list /*{{{*/
359 // ---------------------------------------------------------------------
360 /* This is needed to make dpkg --merge happy */
361 bool DumpAvail(CommandLine
&Cmd
)
363 pkgCache
&Cache
= *GCache
;
364 unsigned char *Buffer
= new unsigned char[Cache
.HeaderP
->MaxVerFileSize
];
366 for (pkgCache::PkgFileIterator I
= Cache
.FileBegin(); I
.end() == false; I
++)
368 if ((I
->Flags
& pkgCache::Flag::NotSource
) != 0)
371 if (I
.IsOk() == false)
374 return _error
->Error("Package file %s is out of sync.",I
.FileName());
377 FileFd
PkgF(I
.FileName(),FileFd::ReadOnly
);
378 if (_error
->PendingError() == true)
384 /* Write all of the records from this package file, we search the entire
385 structure to find them */
386 for (pkgCache::PkgIterator P
= Cache
.PkgBegin(); P
.end() == false; P
++)
388 // Find the proper version to use. We should probably use the DepCache.
389 pkgCache::VerIterator V
= GetCandidateVer(P
,false);
391 if (V
.end() == true || V
.FileList().File() != I
)
394 // Read the record and then write it out again.
395 if (PkgF
.Seek(V
.FileList()->Offset
) == false ||
396 PkgF
.Read(Buffer
,V
.FileList()->Size
) == false ||
397 write(STDOUT_FILENO
,Buffer
,V
.FileList()->Size
) != V
.FileList()->Size
)
408 // DoAdd - Perform an adding operation /*{{{*/
409 // ---------------------------------------------------------------------
411 bool DoAdd(CommandLine
&CmdL
)
413 // Make sure there is at least one argument
414 if (CmdL
.FileSize() <= 1)
415 return _error
->Error("You must give at least one file name");
418 FileFd
CacheF(_config
->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny
);
419 if (_error
->PendingError() == true)
422 DynamicMMap
Map(CacheF
,MMap::Public
);
423 if (_error
->PendingError() == true)
426 OpTextProgress
Progress(*_config
);
427 pkgCacheGenerator
Gen(Map
,Progress
);
428 if (_error
->PendingError() == true)
431 unsigned long Length
= CmdL
.FileSize() - 1;
432 for (const char **I
= CmdL
.FileList
+ 1; *I
!= 0; I
++)
434 Progress
.OverallProgress(I
- CmdL
.FileList
,Length
,1,"Generating cache");
435 Progress
.SubProgress(Length
);
438 FileFd
TagF(*I
,FileFd::ReadOnly
);
439 debListParser
Parser(TagF
);
440 if (_error
->PendingError() == true)
441 return _error
->Error("Problem opening %s",*I
);
443 if (Gen
.SelectFile(*I
) == false)
444 return _error
->Error("Problem with SelectFile");
446 if (Gen
.MergeList(Parser
) == false)
447 return _error
->Error("Problem with MergeList");
451 GCache
= &Gen
.GetCache();
457 // DisplayRecord - Displays the complete record for the package /*{{{*/
458 // ---------------------------------------------------------------------
459 /* This displays the package record from the proper package index file.
460 It is not used by DumpAvail for performance reasons. */
461 bool DisplayRecord(pkgCache::VerIterator V
)
463 // Find an appropriate file
464 pkgCache::VerFileIterator Vf
= V
.FileList();
465 for (; Vf
.end() == false; Vf
++)
466 if ((Vf
.File()->Flags
& pkgCache::Flag::NotSource
) == 0)
468 if (Vf
.end() == true)
471 // Check and load the package list file
472 pkgCache::PkgFileIterator I
= Vf
.File();
473 if (I
.IsOk() == false)
474 return _error
->Error("Package file %s is out of sync.",I
.FileName());
476 FileFd
PkgF(I
.FileName(),FileFd::ReadOnly
);
477 if (_error
->PendingError() == true)
480 // Read the record and then write it out again.
481 unsigned char *Buffer
= new unsigned char[GCache
->HeaderP
->MaxVerFileSize
];
482 if (PkgF
.Seek(V
.FileList()->Offset
) == false ||
483 PkgF
.Read(Buffer
,V
.FileList()->Size
) == false ||
484 write(STDOUT_FILENO
,Buffer
,V
.FileList()->Size
) != V
.FileList()->Size
)
495 // Search - Perform a search /*{{{*/
496 // ---------------------------------------------------------------------
497 /* This searches the package names and pacakge descriptions for a pattern */
498 bool Search(CommandLine
&CmdL
)
500 pkgCache
&Cache
= *GCache
;
501 bool ShowFull
= _config
->FindB("APT::Cache::ShowFull",false);
502 bool NamesOnly
= _config
->FindB("APT::Cache::NamesOnly",false);
504 // Make sure there is at least one argument
505 if (CmdL
.FileSize() != 2)
506 return _error
->Error("You must give exactly one pattern");
508 // Compile the regex pattern
510 if (regcomp(&Pattern
,CmdL
.FileList
[1],REG_EXTENDED
| REG_ICASE
|
512 return _error
->Error("Regex compilation error");
514 // Create the text record parser
515 pkgRecords
Recs(Cache
);
516 if (_error
->PendingError() == true)
519 // Search package names
520 pkgCache::PkgIterator I
= Cache
.PkgBegin();
521 for (;I
.end() != true; I
++)
523 // We search against the install version as that makes the most sense..
524 pkgCache::VerIterator V
= GetCandidateVer(I
);
528 pkgRecords::Parser
&P
= Recs
.Lookup(V
.FileList());
530 if (regexec(&Pattern
,I
.Name(),0,0,0) == 0 ||
531 (NamesOnly
== false &&
532 regexec(&Pattern
,P
.LongDesc().c_str(),0,0,0) == 0))
534 if (ShowFull
== true)
537 cout
<< I
.Name() << " - " << P
.ShortDesc() << endl
;
545 // ShowPackage - Dump the package record to the screen /*{{{*/
546 // ---------------------------------------------------------------------
548 bool ShowPackage(CommandLine
&CmdL
)
550 pkgCache
&Cache
= *GCache
;
551 for (const char **I
= CmdL
.FileList
+ 1; *I
!= 0; I
++)
553 pkgCache::PkgIterator Pkg
= Cache
.FindPkg(*I
);
554 if (Pkg
.end() == true)
556 _error
->Warning("Unable to locate package %s",*I
);
560 // Find the proper version to use. We should probably use the DepCache.
561 pkgCache::VerIterator V
= GetCandidateVer(Pkg
);
562 if (V
.end() == true || V
.FileList().end() == true)
564 if (DisplayRecord(V
) == false)
570 // GenCaches - Call the main cache generator /*{{{*/
571 // ---------------------------------------------------------------------
573 bool GenCaches(CommandLine
&Cmd
)
575 OpTextProgress
Progress(*_config
);
579 return pkgMakeStatusCache(List
,Progress
);
582 // ShowHelp - Show a help screen /*{{{*/
583 // ---------------------------------------------------------------------
585 bool ShowHelp(CommandLine
&Cmd
)
587 cout
<< PACKAGE
<< ' ' << VERSION
<< " for " << ARCHITECTURE
<<
588 " compiled on " << __DATE__
<< " " << __TIME__
<< endl
;
589 if (_config
->FindB("version") == true)
592 cout
<< "Usage: apt-cache [options] command" << endl
;
593 cout
<< " apt-cache [options] add file1 [file1 ...]" << endl
;
594 cout
<< " apt-cache [options] showpkg pkg1 [pkg2 ...]" << endl
;
596 cout
<< "apt-cache is a low-level tool used to manipulate APT's binary" << endl
;
597 cout
<< "cache files stored in " << _config
->FindFile("Dir::Cache") << endl
;
598 cout
<< "It is not ment for ordinary use only as a debug aide." << endl
;
600 cout
<< "Commands:" << endl
;
601 cout
<< " add - Add an package file to the source cache" << endl
;
602 cout
<< " gencaches - Build both the package and source cache" << endl
;
603 cout
<< " showpkg - Show some general information for a single package" << endl
;
604 cout
<< " stats - Show some basic statistics" << endl
;
605 cout
<< " dump - Show the entire file in a terse form" << endl
;
606 cout
<< " dumpavail - Print an available file to stdout" << endl
;
607 cout
<< " unmet - Show unmet dependencies" << endl
;
608 cout
<< " check - Check the cache a bit" << endl
;
609 cout
<< " search - Search the package list for a regex pattern" << endl
;
610 cout
<< " show - Show a readable record for the package" << endl
;
612 cout
<< "Options:" << endl
;
613 cout
<< " -h This help text." << endl
;
614 cout
<< " -p=? The package cache. [" << _config
->FindFile("Dir::Cache::pkgcache") << ']' << endl
;
615 cout
<< " -s=? The source cache. [" << _config
->FindFile("Dir::Cache::srcpkgcache") << ']' << endl
;
616 cout
<< " -q Disable progress indicator." << endl
;
617 cout
<< " -i Show only important deps for the unmet command." << endl
;
618 cout
<< " -c=? Read this configuration file" << endl
;
619 cout
<< " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl
;
620 cout
<< "See the apt-cache(8) and apt.conf(5) manual pages for more information." << endl
;
624 // CacheInitialize - Initialize things for apt-cache /*{{{*/
625 // ---------------------------------------------------------------------
627 void CacheInitialize()
629 _config
->Set("quiet",0);
630 _config
->Set("help",false);
634 int main(int argc
,const char *argv
[])
636 CommandLine::Args Args
[] = {
637 {'h',"help","help",0},
638 {'v',"version","version",0},
639 {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg
},
640 {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg
},
641 {'q',"quiet","quiet",CommandLine::IntLevel
},
642 {'i',"important","APT::Cache::Important",0},
643 {'f',"full","APT::Cache::ShowFull",0},
644 {0,"names-only","APT::Cache::NamesOnly",0},
645 {'c',"config-file",0,CommandLine::ConfigFile
},
646 {'o',"option",0,CommandLine::ArbItem
},
648 CommandLine::Dispatch CmdsA
[] = {{"help",&ShowHelp
},
650 {"gencaches",&GenCaches
},
652 CommandLine::Dispatch CmdsB
[] = {{"showpkg",&DumpPackage
},
655 {"dumpavail",&DumpAvail
},
659 {"show",&ShowPackage
},
664 // Parse the command line and initialize the package library
665 CommandLine
CmdL(Args
,_config
);
666 if (pkgInitialize(*_config
) == false ||
667 CmdL
.Parse(argc
,argv
) == false)
669 _error
->DumpErrors();
673 // See if the help should be shown
674 if (_config
->FindB("help") == true ||
675 CmdL
.FileSize() == 0)
676 return ShowHelp(CmdL
);
678 // Deal with stdout not being a tty
679 if (ttyname(STDOUT_FILENO
) == 0 && _config
->FindI("quiet",0) < 1)
680 _config
->Set("quiet","1");
682 if (CmdL
.DispatchArg(CmdsA
,false) == false && _error
->PendingError() == false)
684 // Open the cache file
685 FileFd
CacheF(_config
->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly
);
686 MMap
Map(CacheF
,MMap::Public
| MMap::ReadOnly
);
687 if (_error
->PendingError() == false)
691 if (_error
->PendingError() == false)
692 CmdL
.DispatchArg(CmdsB
);
696 // Print any errors or warnings found during parsing
697 if (_error
->empty() == false)
699 bool Errors
= _error
->PendingError();
700 _error
->DumpErrors();
701 return Errors
== true?100:0;