apt-cache.cc 14 KB

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