apt-cache.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cache.cc,v 1.18 1998/12/10 05:39:53 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 ment 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 <strutl.h>
  22. #include <iostream.h>
  23. #include <config.h>
  24. /*}}}*/
  25. // UnMet - Show unmet dependencies /*{{{*/
  26. // ---------------------------------------------------------------------
  27. /* */
  28. bool UnMet(pkgCache &Cache)
  29. {
  30. bool Important = _config->FindB("APT::Cache::Important",false);
  31. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  32. {
  33. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
  34. {
  35. bool Header = false;
  36. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false;)
  37. {
  38. // Collect or groups
  39. pkgCache::DepIterator Start;
  40. pkgCache::DepIterator End;
  41. D.GlobOr(Start,End);
  42. /* cout << "s: Check " << Start.TargetPkg().Name() << ',' <<
  43. End.TargetPkg().Name() << endl;*/
  44. // Skip conflicts and replaces
  45. if (End->Type != pkgCache::Dep::PreDepends &&
  46. End->Type != pkgCache::Dep::Depends &&
  47. End->Type != pkgCache::Dep::Suggests &&
  48. End->Type != pkgCache::Dep::Recommends)
  49. continue;
  50. // Important deps only
  51. if (Important == true)
  52. if (End->Type != pkgCache::Dep::PreDepends &&
  53. End->Type != pkgCache::Dep::Depends)
  54. continue;
  55. // Verify the or group
  56. bool OK = false;
  57. pkgCache::DepIterator RealStart = Start;
  58. do
  59. {
  60. // See if this dep is Ok
  61. pkgCache::Version **VList = Start.AllTargets();
  62. if (*VList != 0)
  63. {
  64. OK = true;
  65. delete [] VList;
  66. break;
  67. }
  68. delete [] VList;
  69. if (Start == End)
  70. break;
  71. Start++;
  72. }
  73. while (1);
  74. // The group is OK
  75. if (OK == true)
  76. continue;
  77. // Oops, it failed..
  78. if (Header == false)
  79. cout << "Package " << P.Name() << " version " <<
  80. V.VerStr() << " has an unmet dep:" << endl;
  81. Header = true;
  82. // Print out the dep type
  83. cout << " " << End.DepType() << ": ";
  84. // Show the group
  85. Start = RealStart;
  86. do
  87. {
  88. cout << Start.TargetPkg().Name();
  89. if (Start.TargetVer() != 0)
  90. cout << " (" << Start.CompType() << " " << Start.TargetVer() <<
  91. ")";
  92. if (Start == End)
  93. break;
  94. cout << " | ";
  95. Start++;
  96. }
  97. while (1);
  98. cout << endl;
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104. /*}}}*/
  105. // DumpPackage - Show a dump of a package record /*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* */
  108. bool DumpPackage(pkgCache &Cache,CommandLine &CmdL)
  109. {
  110. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  111. {
  112. pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
  113. if (Pkg.end() == true)
  114. {
  115. _error->Warning("Unable to locate package %s",*I);
  116. continue;
  117. }
  118. cout << "Package: " << Pkg.Name() << endl;
  119. cout << "Versions: ";
  120. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  121. {
  122. cout << Cur.VerStr();
  123. for (pkgCache::VerFileIterator Vf = Cur.FileList(); Vf.end() == false; Vf++)
  124. cout << "(" << Vf.File().FileName() << ")";
  125. cout << ',';
  126. }
  127. cout << endl;
  128. cout << "Reverse Depends: " << endl;
  129. for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() != true; D++)
  130. cout << " " << D.ParentPkg().Name() << ',' << D.TargetPkg().Name() << endl;
  131. cout << "Dependencies: " << endl;
  132. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  133. {
  134. cout << Cur.VerStr() << " - ";
  135. for (pkgCache::DepIterator Dep = Cur.DependsList(); Dep.end() != true; Dep++)
  136. cout << Dep.TargetPkg().Name() << " (" << (int)Dep->CompareOp << " " << Dep.TargetVer() << ") ";
  137. cout << endl;
  138. }
  139. cout << "Provides: " << endl;
  140. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  141. {
  142. cout << Cur.VerStr() << " - ";
  143. for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; Prv++)
  144. cout << Prv.ParentPkg().Name() << " ";
  145. cout << endl;
  146. }
  147. cout << "Reverse Provides: " << endl;
  148. for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++)
  149. cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr();
  150. cout << endl;
  151. }
  152. return true;
  153. }
  154. /*}}}*/
  155. // Stats - Dump some nice statistics /*{{{*/
  156. // ---------------------------------------------------------------------
  157. /* */
  158. bool Stats(pkgCache &Cache)
  159. {
  160. cout << "Total Package Names : " << Cache.Head().PackageCount << " (" <<
  161. SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl;
  162. pkgCache::PkgIterator I = Cache.PkgBegin();
  163. int Normal = 0;
  164. int Virtual = 0;
  165. int NVirt = 0;
  166. int DVirt = 0;
  167. int Missing = 0;
  168. for (;I.end() != true; I++)
  169. {
  170. if (I->VersionList != 0 && I->ProvidesList == 0)
  171. {
  172. Normal++;
  173. continue;
  174. }
  175. if (I->VersionList != 0 && I->ProvidesList != 0)
  176. {
  177. NVirt++;
  178. continue;
  179. }
  180. if (I->VersionList == 0 && I->ProvidesList != 0)
  181. {
  182. // Only 1 provides
  183. if (I.ProvidesList()->NextProvides == 0)
  184. {
  185. DVirt++;
  186. }
  187. else
  188. Virtual++;
  189. continue;
  190. }
  191. if (I->VersionList == 0 && I->ProvidesList == 0)
  192. {
  193. Missing++;
  194. continue;
  195. }
  196. }
  197. cout << " Normal Packages: " << Normal << endl;
  198. cout << " Pure Virtual Packages: " << Virtual << endl;
  199. cout << " Single Virtual Packages: " << DVirt << endl;
  200. cout << " Mixed Virtual Packages: " << NVirt << endl;
  201. cout << " Missing: " << Missing << endl;
  202. cout << "Total Distinct Versions: " << Cache.Head().VersionCount << " (" <<
  203. SizeToStr(Cache.Head().VersionCount*Cache.Head().VersionSz) << ')' << endl;
  204. cout << "Total Dependencies: " << Cache.Head().DependsCount << " (" <<
  205. SizeToStr(Cache.Head().DependsCount*Cache.Head().DependencySz) << ')' << endl;
  206. cout << "Total Ver/File relations: " << Cache.Head().PackageCount << " (" <<
  207. SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl;
  208. // String list stats
  209. unsigned long Size = 0;
  210. unsigned long Count = 0;
  211. for (pkgCache::StringItem *I = Cache.StringItemP + Cache.Head().StringList;
  212. I!= Cache.StringItemP; I = Cache.StringItemP + I->NextItem)
  213. {
  214. Count++;
  215. Size += strlen(Cache.StrP + I->String);
  216. }
  217. cout << "Total Globbed Strings: " << Count << " (" << SizeToStr(Size) << ')' << endl;
  218. unsigned long Slack = 0;
  219. for (int I = 0; I != 7; I++)
  220. Slack += Cache.Head().Pools[I].ItemSize*Cache.Head().Pools[I].Count;
  221. cout << "Total Slack space: " << SizeToStr(Slack) << endl;
  222. unsigned long Total = 0;
  223. Total = Slack + Size + Cache.Head().DependsCount*Cache.Head().DependencySz +
  224. Cache.Head().VersionCount*Cache.Head().VersionSz +
  225. Cache.Head().PackageCount*Cache.Head().PackageSz;
  226. cout << "Total Space Accounted for: " << SizeToStr(Total) << endl;
  227. return true;
  228. }
  229. /*}}}*/
  230. // Check - Check some things about the cache /*{{{*/
  231. // ---------------------------------------------------------------------
  232. /* Debug aide mostly */
  233. bool Check(pkgCache &Cache)
  234. {
  235. pkgCache::PkgIterator Pkg = Cache.PkgBegin();
  236. for (;Pkg.end() != true; Pkg++)
  237. {
  238. if (Pkg.Section() == 0 && Pkg->VersionList != 0)
  239. cout << "Bad section " << Pkg.Name() << endl;
  240. for (pkgCache::VerIterator Cur = Pkg.VersionList();
  241. Cur.end() != true; Cur++)
  242. {
  243. if (Cur->Priority < 1 || Cur->Priority > 5)
  244. cout << "Bad prio " << Pkg.Name() << ',' << Cur.VerStr() << " == " << (int)Cur->Priority << endl;
  245. }
  246. }
  247. return true;
  248. }
  249. /*}}}*/
  250. // Dump - show everything /*{{{*/
  251. // ---------------------------------------------------------------------
  252. /* */
  253. bool Dump(pkgCache &Cache)
  254. {
  255. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  256. {
  257. cout << "Package: " << P.Name() << endl;
  258. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
  259. {
  260. cout << " Version: " << V.VerStr() << endl;
  261. cout << " File: " << V.FileList().File().FileName() << endl;
  262. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
  263. cout << " Depends: " << D.TargetPkg().Name() << ' ' << D.TargetVer() << endl;
  264. }
  265. }
  266. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  267. {
  268. cout << "File: " << F.FileName() << endl;
  269. cout << " Size: " << F->Size << endl;
  270. cout << " ID: " << F->ID << endl;
  271. cout << " Flags: " << F->Flags << endl;
  272. cout << " Time: " << ctime(&F->mtime) << endl;
  273. }
  274. return true;
  275. }
  276. /*}}}*/
  277. // DumpAvail - Print out the available list /*{{{*/
  278. // ---------------------------------------------------------------------
  279. /* This is needed to make dpkg --merge happy */
  280. bool DumpAvail(pkgCache &Cache)
  281. {
  282. unsigned char *Buffer = new unsigned char[Cache.HeaderP->MaxVerFileSize];
  283. for (pkgCache::PkgFileIterator I = Cache.FileBegin(); I.end() == false; I++)
  284. {
  285. if ((I->Flags & pkgCache::Flag::NotSource) != 0)
  286. continue;
  287. if (I.IsOk() == false)
  288. {
  289. delete [] Buffer;
  290. return _error->Error("Package file %s is out of sync.",I.FileName());
  291. }
  292. FileFd PkgF(I.FileName(),FileFd::ReadOnly);
  293. if (_error->PendingError() == true)
  294. {
  295. delete [] Buffer;
  296. return false;
  297. }
  298. /* Write all of the records from this package file, we search the entire
  299. structure to find them */
  300. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  301. {
  302. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
  303. {
  304. if (V->FileList == 0)
  305. continue;
  306. if (V.FileList().File() != I)
  307. continue;
  308. // Read the record and then write it out again.
  309. if (PkgF.Seek(V.FileList()->Offset) == false ||
  310. PkgF.Read(Buffer,V.FileList()->Size) == false ||
  311. write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size)
  312. {
  313. delete [] Buffer;
  314. return false;
  315. }
  316. }
  317. }
  318. }
  319. return true;
  320. }
  321. /*}}}*/
  322. // DoAdd - Perform an adding operation /*{{{*/
  323. // ---------------------------------------------------------------------
  324. /* */
  325. bool DoAdd(CommandLine &CmdL)
  326. {
  327. // Make sure there is at least one argument
  328. if (CmdL.FileSize() <= 1)
  329. return _error->Error("You must give at least one file name");
  330. // Open the cache
  331. FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny);
  332. if (_error->PendingError() == true)
  333. return false;
  334. DynamicMMap Map(CacheF,MMap::Public);
  335. if (_error->PendingError() == true)
  336. return false;
  337. OpTextProgress Progress(*_config);
  338. pkgCacheGenerator Gen(Map,Progress);
  339. if (_error->PendingError() == true)
  340. return false;
  341. unsigned long Length = CmdL.FileSize() - 1;
  342. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  343. {
  344. Progress.OverallProgress(I - CmdL.FileList,Length,1,"Generating cache");
  345. Progress.SubProgress(Length);
  346. // Do the merge
  347. FileFd TagF(*I,FileFd::ReadOnly);
  348. debListParser Parser(TagF);
  349. if (_error->PendingError() == true)
  350. return _error->Error("Problem opening %s",*I);
  351. if (Gen.SelectFile(*I) == false)
  352. return _error->Error("Problem with SelectFile");
  353. if (Gen.MergeList(Parser) == false)
  354. return _error->Error("Problem with MergeList");
  355. }
  356. Progress.Done();
  357. Stats(Gen.GetCache());
  358. return true;
  359. }
  360. /*}}}*/
  361. // GenCaches - Call the main cache generator /*{{{*/
  362. // ---------------------------------------------------------------------
  363. /* */
  364. bool GenCaches()
  365. {
  366. OpTextProgress Progress(*_config);
  367. pkgSourceList List;
  368. List.ReadMainList();
  369. return pkgMakeStatusCache(List,Progress);
  370. }
  371. /*}}}*/
  372. // ShowHelp - Show a help screen /*{{{*/
  373. // ---------------------------------------------------------------------
  374. /* */
  375. int ShowHelp()
  376. {
  377. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  378. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  379. cout << "Usage: apt-cache [options] command" << endl;
  380. cout << " apt-cache [options] add file1 [file1 ...]" << endl;
  381. cout << " apt-cache [options] showpkg pkg1 [pkg2 ...]" << endl;
  382. cout << endl;
  383. cout << "apt-cache is a low-level tool used to manipulate APT's binary" << endl;
  384. cout << "cache files stored in " << _config->FindFile("Dir::Cache") << endl;
  385. cout << "It is not ment for ordinary use only as a debug aide." << endl;
  386. cout << endl;
  387. cout << "Commands:" << endl;
  388. cout << " add - Add an package file to the source cache" << endl;
  389. cout << " gencaches - Build both the package and source cache" << endl;
  390. cout << " showpkg - Show some general information for a single package" << endl;
  391. cout << " stats - Show some basic statistics" << endl;
  392. cout << " dump - Show the entire file in a terse form" << endl;
  393. cout << " dumpavail - Print an available file to stdout" << endl;
  394. cout << " unmet - Show unmet dependencies" << endl;
  395. cout << " check - Check the cache a bit" << endl;
  396. cout << endl;
  397. cout << "Options:" << endl;
  398. cout << " -h This help text." << endl;
  399. cout << " -p=? The package cache. [" << _config->FindFile("Dir::Cache::pkgcache") << ']' << endl;
  400. cout << " -s=? The source cache. [" << _config->FindFile("Dir::Cache::srcpkgcache") << ']' << endl;
  401. cout << " -q Disable progress indicator." << endl;
  402. cout << " -i Show only important deps for the unmet command." << endl;
  403. cout << " -c=? Read this configuration file" << endl;
  404. cout << " -o=? Set an arbitary configuration option, ie -o dir::cache=/tmp" << endl;
  405. cout << "See the apt-cache(8) and apt.conf(8) manual pages for more information." << endl;
  406. return 100;
  407. }
  408. /*}}}*/
  409. // CacheInitialize - Initialize things for apt-cache /*{{{*/
  410. // ---------------------------------------------------------------------
  411. /* */
  412. void CacheInitialize()
  413. {
  414. _config->Set("quiet",0);
  415. _config->Set("help",false);
  416. }
  417. /*}}}*/
  418. int main(int argc,const char *argv[])
  419. {
  420. CommandLine::Args Args[] = {
  421. {'h',"help","help",0},
  422. {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg},
  423. {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg},
  424. {'q',"quiet","quiet",CommandLine::IntLevel},
  425. {'i',"important","APT::Cache::Important",0},
  426. {'c',"config-file",0,CommandLine::ConfigFile},
  427. {'o',"option",0,CommandLine::ArbItem},
  428. {0,0,0,0}};
  429. CacheInitialize();
  430. // Parse the command line and initialize the package library
  431. CommandLine CmdL(Args,_config);
  432. if (pkgInitialize(*_config) == false ||
  433. CmdL.Parse(argc,argv) == false)
  434. {
  435. _error->DumpErrors();
  436. return 100;
  437. }
  438. // See if the help should be shown
  439. if (_config->FindB("help") == true ||
  440. CmdL.FileSize() == 0)
  441. return ShowHelp();
  442. while (1)
  443. {
  444. if (strcmp(CmdL.FileList[0],"add") == 0)
  445. {
  446. ShowHelp();
  447. break;
  448. }
  449. if (strcmp(CmdL.FileList[0],"add") == 0)
  450. {
  451. DoAdd(CmdL);
  452. break;
  453. }
  454. if (strcmp(CmdL.FileList[0],"gencaches") == 0)
  455. {
  456. GenCaches();
  457. break;
  458. }
  459. // Open the cache file
  460. FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::ReadOnly);
  461. if (_error->PendingError() == true)
  462. break;
  463. MMap Map(CacheF,MMap::Public | MMap::ReadOnly);
  464. if (_error->PendingError() == true)
  465. break;
  466. pkgCache Cache(Map);
  467. if (_error->PendingError() == true)
  468. break;
  469. if (strcmp(CmdL.FileList[0],"showpkg") == 0)
  470. {
  471. DumpPackage(Cache,CmdL);
  472. break;
  473. }
  474. if (strcmp(CmdL.FileList[0],"stats") == 0)
  475. {
  476. Stats(Cache);
  477. break;
  478. }
  479. if (strcmp(CmdL.FileList[0],"dump") == 0)
  480. {
  481. Dump(Cache);
  482. break;
  483. }
  484. if (strcmp(CmdL.FileList[0],"dumpavail") == 0)
  485. {
  486. DumpAvail(Cache);
  487. break;
  488. }
  489. if (strcmp(CmdL.FileList[0],"unmet") == 0)
  490. {
  491. UnMet(Cache);
  492. break;
  493. }
  494. if (strcmp(CmdL.FileList[0],"check") == 0)
  495. {
  496. Check(Cache);
  497. break;
  498. }
  499. _error->Error("Invalid operation %s", CmdL.FileList[0]);
  500. break;
  501. }
  502. // Print any errors or warnings found during parsing
  503. if (_error->empty() == false)
  504. {
  505. bool Errors = _error->PendingError();
  506. _error->DumpErrors();
  507. return Errors == true?100:0;
  508. }
  509. return 0;
  510. }