apt-cache.cc 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cache.cc,v 1.41 1999/10/22 04:05:47 jgg Exp $
  4. /* ######################################################################
  5. apt-cache - Manages the cache files
  6. apt-cache provides some functions fo manipulating the cache files.
  7. It uses the command line interface common to all the APT tools. The
  8. only really usefull function right now is dumpavail which is used
  9. by the dselect method. Everything else is meant as a debug aide.
  10. Returns 100 on failure, 0 on success.
  11. ##################################################################### */
  12. /*}}}*/
  13. // Include Files /*{{{*/
  14. #include <apt-pkg/error.h>
  15. #include <apt-pkg/pkgcachegen.h>
  16. #include <apt-pkg/deblistparser.h>
  17. #include <apt-pkg/init.h>
  18. #include <apt-pkg/progress.h>
  19. #include <apt-pkg/sourcelist.h>
  20. #include <apt-pkg/cmndline.h>
  21. #include <apt-pkg/strutl.h>
  22. #include <apt-pkg/pkgrecords.h>
  23. #include <apt-pkg/srcrecords.h>
  24. #include <config.h>
  25. #include <iostream.h>
  26. #include <unistd.h>
  27. #include <errno.h>
  28. #include <regex.h>
  29. /*}}}*/
  30. pkgCache *GCache = 0;
  31. // UnMet - Show unmet dependencies /*{{{*/
  32. // ---------------------------------------------------------------------
  33. /* */
  34. bool UnMet(CommandLine &CmdL)
  35. {
  36. pkgCache &Cache = *GCache;
  37. bool Important = _config->FindB("APT::Cache::Important",false);
  38. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  39. {
  40. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
  41. {
  42. bool Header = false;
  43. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false;)
  44. {
  45. // Collect or groups
  46. pkgCache::DepIterator Start;
  47. pkgCache::DepIterator End;
  48. D.GlobOr(Start,End);
  49. /* cout << "s: Check " << Start.TargetPkg().Name() << ',' <<
  50. End.TargetPkg().Name() << endl;*/
  51. // Skip conflicts and replaces
  52. if (End->Type != pkgCache::Dep::PreDepends &&
  53. End->Type != pkgCache::Dep::Depends &&
  54. End->Type != pkgCache::Dep::Suggests &&
  55. End->Type != pkgCache::Dep::Recommends)
  56. continue;
  57. // Important deps only
  58. if (Important == true)
  59. if (End->Type != pkgCache::Dep::PreDepends &&
  60. End->Type != pkgCache::Dep::Depends)
  61. continue;
  62. // Verify the or group
  63. bool OK = false;
  64. pkgCache::DepIterator RealStart = Start;
  65. do
  66. {
  67. // See if this dep is Ok
  68. pkgCache::Version **VList = Start.AllTargets();
  69. if (*VList != 0)
  70. {
  71. OK = true;
  72. delete [] VList;
  73. break;
  74. }
  75. delete [] VList;
  76. if (Start == End)
  77. break;
  78. Start++;
  79. }
  80. while (1);
  81. // The group is OK
  82. if (OK == true)
  83. continue;
  84. // Oops, it failed..
  85. if (Header == false)
  86. cout << "Package " << P.Name() << " version " <<
  87. V.VerStr() << " has an unmet dep:" << endl;
  88. Header = true;
  89. // Print out the dep type
  90. cout << " " << End.DepType() << ": ";
  91. // Show the group
  92. Start = RealStart;
  93. do
  94. {
  95. cout << Start.TargetPkg().Name();
  96. if (Start.TargetVer() != 0)
  97. cout << " (" << Start.CompType() << " " << Start.TargetVer() <<
  98. ")";
  99. if (Start == End)
  100. break;
  101. cout << " | ";
  102. Start++;
  103. }
  104. while (1);
  105. cout << endl;
  106. }
  107. }
  108. }
  109. return true;
  110. }
  111. /*}}}*/
  112. // DumpPackage - Show a dump of a package record /*{{{*/
  113. // ---------------------------------------------------------------------
  114. /* */
  115. bool DumpPackage(CommandLine &CmdL)
  116. {
  117. pkgCache &Cache = *GCache;
  118. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  119. {
  120. pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
  121. if (Pkg.end() == true)
  122. {
  123. _error->Warning("Unable to locate package %s",*I);
  124. continue;
  125. }
  126. cout << "Package: " << Pkg.Name() << endl;
  127. cout << "Versions: ";
  128. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  129. {
  130. cout << Cur.VerStr();
  131. for (pkgCache::VerFileIterator Vf = Cur.FileList(); Vf.end() == false; Vf++)
  132. cout << "(" << Vf.File().FileName() << ")";
  133. cout << ',';
  134. }
  135. cout << endl;
  136. cout << "Reverse Depends: " << endl;
  137. for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() != true; D++)
  138. cout << " " << D.ParentPkg().Name() << ',' << D.TargetPkg().Name() << endl;
  139. cout << "Dependencies: " << endl;
  140. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  141. {
  142. cout << Cur.VerStr() << " - ";
  143. for (pkgCache::DepIterator Dep = Cur.DependsList(); Dep.end() != true; Dep++)
  144. cout << Dep.TargetPkg().Name() << " (" << (int)Dep->CompareOp << " " << Dep.TargetVer() << ") ";
  145. cout << endl;
  146. }
  147. cout << "Provides: " << endl;
  148. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  149. {
  150. cout << Cur.VerStr() << " - ";
  151. for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; Prv++)
  152. cout << Prv.ParentPkg().Name() << " ";
  153. cout << endl;
  154. }
  155. cout << "Reverse Provides: " << endl;
  156. for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++)
  157. cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr() << endl;
  158. }
  159. return true;
  160. }
  161. /*}}}*/
  162. // Stats - Dump some nice statistics /*{{{*/
  163. // ---------------------------------------------------------------------
  164. /* */
  165. bool Stats(CommandLine &Cmd)
  166. {
  167. pkgCache &Cache = *GCache;
  168. cout << "Total Package Names : " << Cache.Head().PackageCount << " (" <<
  169. SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl;
  170. pkgCache::PkgIterator I = Cache.PkgBegin();
  171. int Normal = 0;
  172. int Virtual = 0;
  173. int NVirt = 0;
  174. int DVirt = 0;
  175. int Missing = 0;
  176. for (;I.end() != true; I++)
  177. {
  178. if (I->VersionList != 0 && I->ProvidesList == 0)
  179. {
  180. Normal++;
  181. continue;
  182. }
  183. if (I->VersionList != 0 && I->ProvidesList != 0)
  184. {
  185. NVirt++;
  186. continue;
  187. }
  188. if (I->VersionList == 0 && I->ProvidesList != 0)
  189. {
  190. // Only 1 provides
  191. if (I.ProvidesList()->NextProvides == 0)
  192. {
  193. DVirt++;
  194. }
  195. else
  196. Virtual++;
  197. continue;
  198. }
  199. if (I->VersionList == 0 && I->ProvidesList == 0)
  200. {
  201. Missing++;
  202. continue;
  203. }
  204. }
  205. cout << " Normal Packages: " << Normal << endl;
  206. cout << " Pure Virtual Packages: " << Virtual << endl;
  207. cout << " Single Virtual Packages: " << DVirt << endl;
  208. cout << " Mixed Virtual Packages: " << NVirt << endl;
  209. cout << " Missing: " << Missing << endl;
  210. cout << "Total Distinct Versions: " << Cache.Head().VersionCount << " (" <<
  211. SizeToStr(Cache.Head().VersionCount*Cache.Head().VersionSz) << ')' << endl;
  212. cout << "Total Dependencies: " << Cache.Head().DependsCount << " (" <<
  213. SizeToStr(Cache.Head().DependsCount*Cache.Head().DependencySz) << ')' << endl;
  214. cout << "Total Ver/File relations: " << Cache.Head().VerFileCount << " (" <<
  215. SizeToStr(Cache.Head().VerFileCount*Cache.Head().VerFileSz) << ')' << endl;
  216. cout << "Total Provides Mappings: " << Cache.Head().ProvidesCount << " (" <<
  217. SizeToStr(Cache.Head().ProvidesCount*Cache.Head().ProvidesSz) << ')' << endl;
  218. // String list stats
  219. unsigned long Size = 0;
  220. unsigned long Count = 0;
  221. for (pkgCache::StringItem *I = Cache.StringItemP + Cache.Head().StringList;
  222. I!= Cache.StringItemP; I = Cache.StringItemP + I->NextItem)
  223. {
  224. Count++;
  225. Size += strlen(Cache.StrP + I->String);
  226. }
  227. cout << "Total Globbed Strings: " << Count << " (" << SizeToStr(Size) << ')' << endl;
  228. unsigned long Slack = 0;
  229. for (int I = 0; I != 7; I++)
  230. Slack += Cache.Head().Pools[I].ItemSize*Cache.Head().Pools[I].Count;
  231. cout << "Total Slack space: " << SizeToStr(Slack) << endl;
  232. unsigned long Total = 0;
  233. Total = Slack + Size + Cache.Head().DependsCount*Cache.Head().DependencySz +
  234. Cache.Head().VersionCount*Cache.Head().VersionSz +
  235. Cache.Head().PackageCount*Cache.Head().PackageSz +
  236. Cache.Head().VerFileCount*Cache.Head().VerFileSz +
  237. Cache.Head().ProvidesCount*Cache.Head().ProvidesSz;
  238. cout << "Total Space Accounted for: " << SizeToStr(Total) << endl;
  239. return true;
  240. }
  241. /*}}}*/
  242. // Check - Check some things about the cache /*{{{*/
  243. // ---------------------------------------------------------------------
  244. /* Debug aide mostly */
  245. bool Check(CommandLine &Cmd)
  246. {
  247. pkgCache &Cache = *GCache;
  248. pkgCache::PkgIterator Pkg = Cache.PkgBegin();
  249. for (;Pkg.end() != true; Pkg++)
  250. {
  251. if (Pkg.Section() == 0 && Pkg->VersionList != 0)
  252. cout << "Bad section " << Pkg.Name() << endl;
  253. for (pkgCache::VerIterator Cur = Pkg.VersionList();
  254. Cur.end() != true; Cur++)
  255. {
  256. if (Cur->Priority < 1 || Cur->Priority > 5)
  257. cout << "Bad prio " << Pkg.Name() << ',' << Cur.VerStr() << " == " << (int)Cur->Priority << endl;
  258. }
  259. }
  260. return true;
  261. }
  262. /*}}}*/
  263. // Dump - show everything /*{{{*/
  264. // ---------------------------------------------------------------------
  265. /* */
  266. bool Dump(CommandLine &Cmd)
  267. {
  268. pkgCache &Cache = *GCache;
  269. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  270. {
  271. cout << "Package: " << P.Name() << endl;
  272. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
  273. {
  274. cout << " Version: " << V.VerStr() << endl;
  275. cout << " File: " << V.FileList().File().FileName() << endl;
  276. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
  277. cout << " Depends: " << D.TargetPkg().Name() << ' ' << D.TargetVer() << endl;
  278. }
  279. }
  280. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  281. {
  282. cout << "File: " << F.FileName() << endl;
  283. cout << " Size: " << F->Size << endl;
  284. cout << " ID: " << F->ID << endl;
  285. cout << " Flags: " << F->Flags << endl;
  286. cout << " Time: " << TimeRFC1123(F->mtime) << endl;
  287. cout << " Archive: " << F.Archive() << endl;
  288. cout << " Component: " << F.Component() << endl;
  289. cout << " Version: " << F.Version() << endl;
  290. cout << " Origin: " << F.Origin() << endl;
  291. cout << " Label: " << F.Label() << endl;
  292. cout << " Architecture: " << F.Architecture() << endl;
  293. }
  294. return true;
  295. }
  296. /*}}}*/
  297. // DumpAvail - Print out the available list /*{{{*/
  298. // ---------------------------------------------------------------------
  299. /* This is needed to make dpkg --merge happy */
  300. bool DumpAvail(CommandLine &Cmd)
  301. {
  302. pkgCache &Cache = *GCache;
  303. unsigned char *Buffer = new unsigned char[Cache.HeaderP->MaxVerFileSize];
  304. for (pkgCache::PkgFileIterator I = Cache.FileBegin(); I.end() == false; I++)
  305. {
  306. if ((I->Flags & pkgCache::Flag::NotSource) != 0)
  307. continue;
  308. if (I.IsOk() == false)
  309. {
  310. delete [] Buffer;
  311. return _error->Error("Package file %s is out of sync.",I.FileName());
  312. }
  313. FileFd PkgF(I.FileName(),FileFd::ReadOnly);
  314. if (_error->PendingError() == true)
  315. {
  316. delete [] Buffer;
  317. return false;
  318. }
  319. /* Write all of the records from this package file, we search the entire
  320. structure to find them */
  321. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  322. {
  323. // Find the proper version to use. We should probably use the DepCache.
  324. pkgCache::VerIterator V = Cache.GetCandidateVer(P,false);
  325. if (V.end() == true || V.FileList().File() != I)
  326. continue;
  327. // Read the record and then write it out again.
  328. if (PkgF.Seek(V.FileList()->Offset) == false ||
  329. PkgF.Read(Buffer,V.FileList()->Size) == false ||
  330. write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size)
  331. {
  332. delete [] Buffer;
  333. return false;
  334. }
  335. }
  336. }
  337. return true;
  338. }
  339. /*}}}*/
  340. // Depends - Print out a dependency tree /*{{{*/
  341. // ---------------------------------------------------------------------
  342. /* */
  343. bool Depends(CommandLine &CmdL)
  344. {
  345. pkgCache &Cache = *GCache;
  346. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  347. {
  348. pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
  349. if (Pkg.end() == true)
  350. {
  351. _error->Warning("Unable to locate package %s",*I);
  352. continue;
  353. }
  354. pkgCache::VerIterator Ver = Pkg.VersionList();
  355. if (Ver.end() == true)
  356. {
  357. cout << '<' << Pkg.Name() << '>' << endl;
  358. continue;
  359. }
  360. cout << Pkg.Name() << endl;
  361. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  362. {
  363. if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
  364. cout << " |";
  365. else
  366. cout << " ";
  367. // Show the package
  368. pkgCache::PkgIterator Trg = D.TargetPkg();
  369. if (Trg->VersionList == 0)
  370. cout << D.DepType() << ": <" << Trg.Name() << ">" << endl;
  371. else
  372. cout << D.DepType() << ": " << Trg.Name() << endl;
  373. // Display all solutions
  374. pkgCache::Version **List = D.AllTargets();
  375. for (pkgCache::Version **I = List; *I != 0; I++)
  376. {
  377. pkgCache::VerIterator V(Cache,*I);
  378. if (V != Cache.VerP + V.ParentPkg()->VersionList ||
  379. V->ParentPkg == D->Package)
  380. continue;
  381. cout << " " << V.ParentPkg().Name() << endl;
  382. }
  383. delete [] List;
  384. }
  385. }
  386. return true;
  387. }
  388. /*}}}*/
  389. // DoAdd - Perform an adding operation /*{{{*/
  390. // ---------------------------------------------------------------------
  391. /* */
  392. bool DoAdd(CommandLine &CmdL)
  393. {
  394. // Make sure there is at least one argument
  395. if (CmdL.FileSize() <= 1)
  396. return _error->Error("You must give at least one file name");
  397. // Open the cache
  398. FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny);
  399. if (_error->PendingError() == true)
  400. return false;
  401. DynamicMMap Map(CacheF,MMap::Public);
  402. if (_error->PendingError() == true)
  403. return false;
  404. OpTextProgress Progress(*_config);
  405. pkgCacheGenerator Gen(Map,Progress);
  406. if (_error->PendingError() == true)
  407. return false;
  408. unsigned long Length = CmdL.FileSize() - 1;
  409. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  410. {
  411. Progress.OverallProgress(I - CmdL.FileList,Length,1,"Generating cache");
  412. Progress.SubProgress(Length);
  413. // Do the merge
  414. FileFd TagF(*I,FileFd::ReadOnly);
  415. debListParser Parser(TagF);
  416. if (_error->PendingError() == true)
  417. return _error->Error("Problem opening %s",*I);
  418. if (Gen.SelectFile(*I) == false)
  419. return _error->Error("Problem with SelectFile");
  420. if (Gen.MergeList(Parser) == false)
  421. return _error->Error("Problem with MergeList");
  422. }
  423. Progress.Done();
  424. GCache = &Gen.GetCache();
  425. Stats(CmdL);
  426. return true;
  427. }
  428. /*}}}*/
  429. // DisplayRecord - Displays the complete record for the package /*{{{*/
  430. // ---------------------------------------------------------------------
  431. /* This displays the package record from the proper package index file.
  432. It is not used by DumpAvail for performance reasons. */
  433. bool DisplayRecord(pkgCache::VerIterator V)
  434. {
  435. // Find an appropriate file
  436. pkgCache::VerFileIterator Vf = V.FileList();
  437. for (; Vf.end() == false; Vf++)
  438. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
  439. break;
  440. if (Vf.end() == true)
  441. Vf = V.FileList();
  442. // Check and load the package list file
  443. pkgCache::PkgFileIterator I = Vf.File();
  444. if (I.IsOk() == false)
  445. return _error->Error("Package file %s is out of sync.",I.FileName());
  446. FileFd PkgF(I.FileName(),FileFd::ReadOnly);
  447. if (_error->PendingError() == true)
  448. return false;
  449. // Read the record and then write it out again.
  450. unsigned char *Buffer = new unsigned char[GCache->HeaderP->MaxVerFileSize];
  451. if (PkgF.Seek(V.FileList()->Offset) == false ||
  452. PkgF.Read(Buffer,V.FileList()->Size) == false ||
  453. write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size)
  454. {
  455. delete [] Buffer;
  456. return false;
  457. }
  458. delete [] Buffer;
  459. return true;
  460. }
  461. /*}}}*/
  462. // Search - Perform a search /*{{{*/
  463. // ---------------------------------------------------------------------
  464. /* This searches the package names and pacakge descriptions for a pattern */
  465. bool Search(CommandLine &CmdL)
  466. {
  467. pkgCache &Cache = *GCache;
  468. bool ShowFull = _config->FindB("APT::Cache::ShowFull",false);
  469. bool NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
  470. // Make sure there is at least one argument
  471. if (CmdL.FileSize() != 2)
  472. return _error->Error("You must give exactly one pattern");
  473. // Compile the regex pattern
  474. regex_t Pattern;
  475. if (regcomp(&Pattern,CmdL.FileList[1],REG_EXTENDED | REG_ICASE |
  476. REG_NOSUB) != 0)
  477. return _error->Error("Regex compilation error");
  478. // Create the text record parser
  479. pkgRecords Recs(Cache);
  480. if (_error->PendingError() == true)
  481. return false;
  482. // Search package names
  483. pkgCache::PkgIterator I = Cache.PkgBegin();
  484. for (;I.end() != true; I++)
  485. {
  486. // We search against the install version as that makes the most sense..
  487. pkgCache::VerIterator V = Cache.GetCandidateVer(I);
  488. if (V.end() == true)
  489. continue;
  490. pkgRecords::Parser &P = Recs.Lookup(V.FileList());
  491. if (regexec(&Pattern,I.Name(),0,0,0) == 0 ||
  492. (NamesOnly == false &&
  493. regexec(&Pattern,P.LongDesc().c_str(),0,0,0) == 0))
  494. {
  495. if (ShowFull == true)
  496. DisplayRecord(V);
  497. else
  498. cout << I.Name() << " - " << P.ShortDesc() << endl;
  499. }
  500. }
  501. regfree(&Pattern);
  502. return true;
  503. }
  504. /*}}}*/
  505. // ShowPackage - Dump the package record to the screen /*{{{*/
  506. // ---------------------------------------------------------------------
  507. /* */
  508. bool ShowPackage(CommandLine &CmdL)
  509. {
  510. pkgCache &Cache = *GCache;
  511. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  512. {
  513. pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
  514. if (Pkg.end() == true)
  515. {
  516. _error->Warning("Unable to locate package %s",*I);
  517. continue;
  518. }
  519. // Find the proper version to use. We should probably use the DepCache.
  520. if (_config->FindB("APT::Cache::AllVersions","true") == true)
  521. {
  522. pkgCache::VerIterator V;
  523. for (V = Pkg.VersionList(); V.end() == false; V++)
  524. {
  525. if (DisplayRecord(V) == false)
  526. return false;
  527. }
  528. }
  529. else
  530. {
  531. pkgCache::VerIterator V = Cache.GetCandidateVer(Pkg);
  532. if (V.end() == true || V.FileList().end() == true)
  533. continue;
  534. if (DisplayRecord(V) == false)
  535. return false;
  536. }
  537. }
  538. return true;
  539. }
  540. /*}}}*/
  541. // ShowSrcPackage - Show source package records /*{{{*/
  542. // ---------------------------------------------------------------------
  543. /* */
  544. bool ShowSrcPackage(CommandLine &CmdL)
  545. {
  546. pkgSourceList List;
  547. List.ReadMainList();
  548. // Create the text record parsers
  549. pkgSrcRecords SrcRecs(List);
  550. if (_error->PendingError() == true)
  551. return false;
  552. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  553. {
  554. SrcRecs.Restart();
  555. pkgSrcRecords::Parser *Parse;
  556. while ((Parse = SrcRecs.Find(*I,false)) != 0)
  557. cout << Parse->AsStr();
  558. }
  559. return true;
  560. }
  561. /*}}}*/
  562. // GenCaches - Call the main cache generator /*{{{*/
  563. // ---------------------------------------------------------------------
  564. /* */
  565. bool GenCaches(CommandLine &Cmd)
  566. {
  567. OpTextProgress Progress(*_config);
  568. pkgSourceList List;
  569. List.ReadMainList();
  570. return pkgMakeStatusCache(List,Progress);
  571. }
  572. /*}}}*/
  573. // ShowHelp - Show a help screen /*{{{*/
  574. // ---------------------------------------------------------------------
  575. /* */
  576. bool ShowHelp(CommandLine &Cmd)
  577. {
  578. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  579. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  580. if (_config->FindB("version") == true)
  581. return 100;
  582. cout << "Usage: apt-cache [options] command" << endl;
  583. cout << " apt-cache [options] add file1 [file1 ...]" << endl;
  584. cout << " apt-cache [options] showpkg pkg1 [pkg2 ...]" << endl;
  585. cout << endl;
  586. cout << "apt-cache is a low-level tool used to manipulate APT's binary" << endl;
  587. cout << "cache files stored in " << _config->FindFile("Dir::Cache") << endl;
  588. cout << "It is not meant for ordinary use only as a debug aide." << endl;
  589. cout << endl;
  590. cout << "Commands:" << endl;
  591. cout << " add - Add an package file to the source cache" << endl;
  592. cout << " gencaches - Build both the package and source cache" << endl;
  593. cout << " showpkg - Show some general information for a single package" << endl;
  594. cout << " stats - Show some basic statistics" << endl;
  595. cout << " dump - Show the entire file in a terse form" << endl;
  596. cout << " dumpavail - Print an available file to stdout" << endl;
  597. cout << " unmet - Show unmet dependencies" << endl;
  598. cout << " check - Check the cache a bit" << endl;
  599. cout << " search - Search the package list for a regex pattern" << endl;
  600. cout << " show - Show a readable record for the package" << endl;
  601. cout << " depends - Show raw dependency information for a package" << endl;
  602. cout << endl;
  603. cout << "Options:" << endl;
  604. cout << " -h This help text." << endl;
  605. cout << " -p=? The package cache. [" << _config->FindFile("Dir::Cache::pkgcache") << ']' << endl;
  606. cout << " -s=? The source cache. [" << _config->FindFile("Dir::Cache::srcpkgcache") << ']' << endl;
  607. cout << " -q Disable progress indicator." << endl;
  608. cout << " -i Show only important deps for the unmet command." << endl;
  609. cout << " -c=? Read this configuration file" << endl;
  610. cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
  611. cout << "See the apt-cache(8) and apt.conf(5) manual pages for more information." << endl;
  612. return 100;
  613. }
  614. /*}}}*/
  615. // CacheInitialize - Initialize things for apt-cache /*{{{*/
  616. // ---------------------------------------------------------------------
  617. /* */
  618. void CacheInitialize()
  619. {
  620. _config->Set("quiet",0);
  621. _config->Set("help",false);
  622. }
  623. /*}}}*/
  624. int main(int argc,const char *argv[])
  625. {
  626. CommandLine::Args Args[] = {
  627. {'h',"help","help",0},
  628. {'v',"version","version",0},
  629. {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg},
  630. {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg},
  631. {'q',"quiet","quiet",CommandLine::IntLevel},
  632. {'i',"important","APT::Cache::Important",0},
  633. {'f',"full","APT::Cache::ShowFull",0},
  634. {'g',"no-generate","APT::Cache::NoGenerate",0},
  635. {'a',"all-versions","APT::Cache::AllVersions",0},
  636. {0,"names-only","APT::Cache::NamesOnly",0},
  637. {'c',"config-file",0,CommandLine::ConfigFile},
  638. {'o',"option",0,CommandLine::ArbItem},
  639. {0,0,0,0}};
  640. CommandLine::Dispatch CmdsA[] = {{"help",&ShowHelp},
  641. {"add",&DoAdd},
  642. {"gencaches",&GenCaches},
  643. {"showsrc",&ShowSrcPackage},
  644. {0,0}};
  645. CommandLine::Dispatch CmdsB[] = {{"showpkg",&DumpPackage},
  646. {"stats",&Stats},
  647. {"dump",&Dump},
  648. {"dumpavail",&DumpAvail},
  649. {"unmet",&UnMet},
  650. {"check",&Check},
  651. {"search",&Search},
  652. {"depends",&Depends},
  653. {"show",&ShowPackage},
  654. {0,0}};
  655. CacheInitialize();
  656. // Parse the command line and initialize the package library
  657. CommandLine CmdL(Args,_config);
  658. if (pkgInitialize(*_config) == false ||
  659. CmdL.Parse(argc,argv) == false)
  660. {
  661. _error->DumpErrors();
  662. return 100;
  663. }
  664. // See if the help should be shown
  665. if (_config->FindB("help") == true ||
  666. CmdL.FileSize() == 0)
  667. return ShowHelp(CmdL);
  668. // Deal with stdout not being a tty
  669. if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
  670. _config->Set("quiet","1");
  671. if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false)
  672. {
  673. MMap *Map;
  674. if (_config->FindB("APT::Cache::NoGenerate",false) == true)
  675. {
  676. Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
  677. FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
  678. }
  679. else
  680. {
  681. // Open the cache file
  682. pkgSourceList List;
  683. List.ReadMainList();
  684. // Generate it and map it
  685. OpProgress Prog;
  686. Map = pkgMakeStatusCacheMem(List,Prog);
  687. }
  688. if (_error->PendingError() == false)
  689. {
  690. pkgCache Cache(*Map);
  691. GCache = &Cache;
  692. if (_error->PendingError() == false)
  693. CmdL.DispatchArg(CmdsB);
  694. }
  695. delete Map;
  696. }
  697. // Print any errors or warnings found during parsing
  698. if (_error->empty() == false)
  699. {
  700. bool Errors = _error->PendingError();
  701. _error->DumpErrors();
  702. return Errors == true?100:0;
  703. }
  704. return 0;
  705. }