apt-cache.cc 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cache.cc,v 1.43 2000/05/12 04:00:59 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 <apt-pkg/version.h>
  25. #include <config.h>
  26. #include <iostream.h>
  27. #include <unistd.h>
  28. #include <errno.h>
  29. #include <regex.h>
  30. #include <stdio.h>
  31. /*}}}*/
  32. pkgCache *GCache = 0;
  33. // UnMet - Show unmet dependencies /*{{{*/
  34. // ---------------------------------------------------------------------
  35. /* */
  36. bool UnMet(CommandLine &CmdL)
  37. {
  38. pkgCache &Cache = *GCache;
  39. bool Important = _config->FindB("APT::Cache::Important",false);
  40. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  41. {
  42. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
  43. {
  44. bool Header = false;
  45. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false;)
  46. {
  47. // Collect or groups
  48. pkgCache::DepIterator Start;
  49. pkgCache::DepIterator End;
  50. D.GlobOr(Start,End);
  51. /* cout << "s: Check " << Start.TargetPkg().Name() << ',' <<
  52. End.TargetPkg().Name() << endl;*/
  53. // Skip conflicts and replaces
  54. if (End->Type != pkgCache::Dep::PreDepends &&
  55. End->Type != pkgCache::Dep::Depends &&
  56. End->Type != pkgCache::Dep::Suggests &&
  57. End->Type != pkgCache::Dep::Recommends)
  58. continue;
  59. // Important deps only
  60. if (Important == true)
  61. if (End->Type != pkgCache::Dep::PreDepends &&
  62. End->Type != pkgCache::Dep::Depends)
  63. continue;
  64. // Verify the or group
  65. bool OK = false;
  66. pkgCache::DepIterator RealStart = Start;
  67. do
  68. {
  69. // See if this dep is Ok
  70. pkgCache::Version **VList = Start.AllTargets();
  71. if (*VList != 0)
  72. {
  73. OK = true;
  74. delete [] VList;
  75. break;
  76. }
  77. delete [] VList;
  78. if (Start == End)
  79. break;
  80. Start++;
  81. }
  82. while (1);
  83. // The group is OK
  84. if (OK == true)
  85. continue;
  86. // Oops, it failed..
  87. if (Header == false)
  88. cout << "Package " << P.Name() << " version " <<
  89. V.VerStr() << " has an unmet dep:" << endl;
  90. Header = true;
  91. // Print out the dep type
  92. cout << " " << End.DepType() << ": ";
  93. // Show the group
  94. Start = RealStart;
  95. do
  96. {
  97. cout << Start.TargetPkg().Name();
  98. if (Start.TargetVer() != 0)
  99. cout << " (" << Start.CompType() << " " << Start.TargetVer() <<
  100. ")";
  101. if (Start == End)
  102. break;
  103. cout << " | ";
  104. Start++;
  105. }
  106. while (1);
  107. cout << endl;
  108. }
  109. }
  110. }
  111. return true;
  112. }
  113. /*}}}*/
  114. // DumpPackage - Show a dump of a package record /*{{{*/
  115. // ---------------------------------------------------------------------
  116. /* */
  117. bool DumpPackage(CommandLine &CmdL)
  118. {
  119. pkgCache &Cache = *GCache;
  120. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  121. {
  122. pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
  123. if (Pkg.end() == true)
  124. {
  125. _error->Warning("Unable to locate package %s",*I);
  126. continue;
  127. }
  128. cout << "Package: " << Pkg.Name() << endl;
  129. cout << "Versions: ";
  130. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  131. {
  132. cout << Cur.VerStr();
  133. for (pkgCache::VerFileIterator Vf = Cur.FileList(); Vf.end() == false; Vf++)
  134. cout << "(" << Vf.File().FileName() << ")";
  135. cout << ',';
  136. }
  137. cout << endl;
  138. cout << "Reverse Depends: " << endl;
  139. for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() != true; D++)
  140. cout << " " << D.ParentPkg().Name() << ',' << D.TargetPkg().Name() << endl;
  141. cout << "Dependencies: " << endl;
  142. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  143. {
  144. cout << Cur.VerStr() << " - ";
  145. for (pkgCache::DepIterator Dep = Cur.DependsList(); Dep.end() != true; Dep++)
  146. cout << Dep.TargetPkg().Name() << " (" << (int)Dep->CompareOp << " " << Dep.TargetVer() << ") ";
  147. cout << endl;
  148. }
  149. cout << "Provides: " << endl;
  150. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; Cur++)
  151. {
  152. cout << Cur.VerStr() << " - ";
  153. for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; Prv++)
  154. cout << Prv.ParentPkg().Name() << " ";
  155. cout << endl;
  156. }
  157. cout << "Reverse Provides: " << endl;
  158. for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; Prv++)
  159. cout << Prv.OwnerPkg().Name() << " " << Prv.OwnerVer().VerStr() << endl;
  160. }
  161. return true;
  162. }
  163. /*}}}*/
  164. // Stats - Dump some nice statistics /*{{{*/
  165. // ---------------------------------------------------------------------
  166. /* */
  167. bool Stats(CommandLine &Cmd)
  168. {
  169. pkgCache &Cache = *GCache;
  170. cout << "Total Package Names : " << Cache.Head().PackageCount << " (" <<
  171. SizeToStr(Cache.Head().PackageCount*Cache.Head().PackageSz) << ')' << endl;
  172. pkgCache::PkgIterator I = Cache.PkgBegin();
  173. int Normal = 0;
  174. int Virtual = 0;
  175. int NVirt = 0;
  176. int DVirt = 0;
  177. int Missing = 0;
  178. for (;I.end() != true; I++)
  179. {
  180. if (I->VersionList != 0 && I->ProvidesList == 0)
  181. {
  182. Normal++;
  183. continue;
  184. }
  185. if (I->VersionList != 0 && I->ProvidesList != 0)
  186. {
  187. NVirt++;
  188. continue;
  189. }
  190. if (I->VersionList == 0 && I->ProvidesList != 0)
  191. {
  192. // Only 1 provides
  193. if (I.ProvidesList()->NextProvides == 0)
  194. {
  195. DVirt++;
  196. }
  197. else
  198. Virtual++;
  199. continue;
  200. }
  201. if (I->VersionList == 0 && I->ProvidesList == 0)
  202. {
  203. Missing++;
  204. continue;
  205. }
  206. }
  207. cout << " Normal Packages: " << Normal << endl;
  208. cout << " Pure Virtual Packages: " << Virtual << endl;
  209. cout << " Single Virtual Packages: " << DVirt << endl;
  210. cout << " Mixed Virtual Packages: " << NVirt << endl;
  211. cout << " Missing: " << Missing << endl;
  212. cout << "Total Distinct Versions: " << Cache.Head().VersionCount << " (" <<
  213. SizeToStr(Cache.Head().VersionCount*Cache.Head().VersionSz) << ')' << endl;
  214. cout << "Total Dependencies: " << Cache.Head().DependsCount << " (" <<
  215. SizeToStr(Cache.Head().DependsCount*Cache.Head().DependencySz) << ')' << endl;
  216. cout << "Total Ver/File relations: " << Cache.Head().VerFileCount << " (" <<
  217. SizeToStr(Cache.Head().VerFileCount*Cache.Head().VerFileSz) << ')' << endl;
  218. cout << "Total Provides Mappings: " << Cache.Head().ProvidesCount << " (" <<
  219. SizeToStr(Cache.Head().ProvidesCount*Cache.Head().ProvidesSz) << ')' << endl;
  220. // String list stats
  221. unsigned long Size = 0;
  222. unsigned long Count = 0;
  223. for (pkgCache::StringItem *I = Cache.StringItemP + Cache.Head().StringList;
  224. I!= Cache.StringItemP; I = Cache.StringItemP + I->NextItem)
  225. {
  226. Count++;
  227. Size += strlen(Cache.StrP + I->String);
  228. }
  229. cout << "Total Globbed Strings: " << Count << " (" << SizeToStr(Size) << ')' << endl;
  230. unsigned long Slack = 0;
  231. for (int I = 0; I != 7; I++)
  232. Slack += Cache.Head().Pools[I].ItemSize*Cache.Head().Pools[I].Count;
  233. cout << "Total Slack space: " << SizeToStr(Slack) << endl;
  234. unsigned long Total = 0;
  235. Total = Slack + Size + Cache.Head().DependsCount*Cache.Head().DependencySz +
  236. Cache.Head().VersionCount*Cache.Head().VersionSz +
  237. Cache.Head().PackageCount*Cache.Head().PackageSz +
  238. Cache.Head().VerFileCount*Cache.Head().VerFileSz +
  239. Cache.Head().ProvidesCount*Cache.Head().ProvidesSz;
  240. cout << "Total Space Accounted for: " << SizeToStr(Total) << endl;
  241. return true;
  242. }
  243. /*}}}*/
  244. // Check - Check some things about the cache /*{{{*/
  245. // ---------------------------------------------------------------------
  246. /* Debug aide mostly */
  247. bool Check(CommandLine &Cmd)
  248. {
  249. pkgCache &Cache = *GCache;
  250. pkgCache::PkgIterator Pkg = Cache.PkgBegin();
  251. for (;Pkg.end() != true; Pkg++)
  252. {
  253. if (Pkg.Section() == 0 && Pkg->VersionList != 0)
  254. cout << "Bad section " << Pkg.Name() << endl;
  255. for (pkgCache::VerIterator Cur = Pkg.VersionList();
  256. Cur.end() != true; Cur++)
  257. {
  258. if (Cur->Priority < 1 || Cur->Priority > 5)
  259. cout << "Bad prio " << Pkg.Name() << ',' << Cur.VerStr() << " == " << (int)Cur->Priority << endl;
  260. }
  261. }
  262. return true;
  263. }
  264. /*}}}*/
  265. // Dump - show everything /*{{{*/
  266. // ---------------------------------------------------------------------
  267. /* */
  268. bool Dump(CommandLine &Cmd)
  269. {
  270. pkgCache &Cache = *GCache;
  271. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  272. {
  273. cout << "Package: " << P.Name() << endl;
  274. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; V++)
  275. {
  276. cout << " Version: " << V.VerStr() << endl;
  277. cout << " File: " << V.FileList().File().FileName() << endl;
  278. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; D++)
  279. cout << " Depends: " << D.TargetPkg().Name() << ' ' << D.TargetVer() << endl;
  280. }
  281. }
  282. for (pkgCache::PkgFileIterator F(Cache); F.end() == false; F++)
  283. {
  284. cout << "File: " << F.FileName() << endl;
  285. cout << " Size: " << F->Size << endl;
  286. cout << " ID: " << F->ID << endl;
  287. cout << " Flags: " << F->Flags << endl;
  288. cout << " Time: " << TimeRFC1123(F->mtime) << endl;
  289. cout << " Archive: " << F.Archive() << endl;
  290. cout << " Component: " << F.Component() << endl;
  291. cout << " Version: " << F.Version() << endl;
  292. cout << " Origin: " << F.Origin() << endl;
  293. cout << " Label: " << F.Label() << endl;
  294. cout << " Architecture: " << F.Architecture() << endl;
  295. }
  296. return true;
  297. }
  298. /*}}}*/
  299. // DumpAvail - Print out the available list /*{{{*/
  300. // ---------------------------------------------------------------------
  301. /* This is needed to make dpkg --merge happy */
  302. bool DumpAvail(CommandLine &Cmd)
  303. {
  304. pkgCache &Cache = *GCache;
  305. unsigned char *Buffer = new unsigned char[Cache.HeaderP->MaxVerFileSize];
  306. for (pkgCache::PkgFileIterator I = Cache.FileBegin(); I.end() == false; I++)
  307. {
  308. if ((I->Flags & pkgCache::Flag::NotSource) != 0)
  309. continue;
  310. if (I.IsOk() == false)
  311. {
  312. delete [] Buffer;
  313. return _error->Error("Package file %s is out of sync.",I.FileName());
  314. }
  315. FileFd PkgF(I.FileName(),FileFd::ReadOnly);
  316. if (_error->PendingError() == true)
  317. {
  318. delete [] Buffer;
  319. return false;
  320. }
  321. /* Write all of the records from this package file, we search the entire
  322. structure to find them */
  323. for (pkgCache::PkgIterator P = Cache.PkgBegin(); P.end() == false; P++)
  324. {
  325. // Find the proper version to use. We should probably use the DepCache.
  326. pkgCache::VerIterator V = Cache.GetCandidateVer(P,false);
  327. if (V.end() == true || V.FileList().File() != I)
  328. continue;
  329. // Read the record and then write it out again.
  330. if (PkgF.Seek(V.FileList()->Offset) == false ||
  331. PkgF.Read(Buffer,V.FileList()->Size) == false ||
  332. write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size)
  333. {
  334. delete [] Buffer;
  335. return false;
  336. }
  337. }
  338. }
  339. return true;
  340. }
  341. /*}}}*/
  342. // Depends - Print out a dependency tree /*{{{*/
  343. // ---------------------------------------------------------------------
  344. /* */
  345. bool Depends(CommandLine &CmdL)
  346. {
  347. pkgCache &Cache = *GCache;
  348. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  349. {
  350. pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
  351. if (Pkg.end() == true)
  352. {
  353. _error->Warning("Unable to locate package %s",*I);
  354. continue;
  355. }
  356. pkgCache::VerIterator Ver = Pkg.VersionList();
  357. if (Ver.end() == true)
  358. {
  359. cout << '<' << Pkg.Name() << '>' << endl;
  360. continue;
  361. }
  362. cout << Pkg.Name() << endl;
  363. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  364. {
  365. if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or)
  366. cout << " |";
  367. else
  368. cout << " ";
  369. // Show the package
  370. pkgCache::PkgIterator Trg = D.TargetPkg();
  371. if (Trg->VersionList == 0)
  372. cout << D.DepType() << ": <" << Trg.Name() << ">" << endl;
  373. else
  374. cout << D.DepType() << ": " << Trg.Name() << endl;
  375. // Display all solutions
  376. pkgCache::Version **List = D.AllTargets();
  377. for (pkgCache::Version **I = List; *I != 0; I++)
  378. {
  379. pkgCache::VerIterator V(Cache,*I);
  380. if (V != Cache.VerP + V.ParentPkg()->VersionList ||
  381. V->ParentPkg == D->Package)
  382. continue;
  383. cout << " " << V.ParentPkg().Name() << endl;
  384. }
  385. delete [] List;
  386. }
  387. }
  388. return true;
  389. }
  390. /*}}}*/
  391. // Dotty - Generate a graph for Dotty /*{{{*/
  392. // ---------------------------------------------------------------------
  393. /* Dotty is the graphvis program for generating graphs. It is a fairly
  394. simple queuing algorithm that just writes dependencies and nodes.
  395. http://www.research.att.com/sw/tools/graphviz/ */
  396. bool Dotty(CommandLine &CmdL)
  397. {
  398. pkgCache &Cache = *GCache;
  399. bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
  400. /* Normal packages are boxes
  401. Pure Provides are triangles
  402. Mixed are diamonds
  403. Hexagons are missing packages*/
  404. const char *Shapes[] = {"hexagon","triangle","box","diamond"};
  405. /* Initialize the list of packages to show.
  406. 1 = To Show
  407. 2 = To Show no recurse
  408. 3 = Emitted no recurse
  409. 4 = Emitted
  410. 0 = None */
  411. enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
  412. enum TheFlags {ForceNR=(1<<0)};
  413. unsigned char *Show = new unsigned char[Cache.Head().PackageCount];
  414. unsigned char *Flags = new unsigned char[Cache.Head().PackageCount];
  415. unsigned char *ShapeMap = new unsigned char[Cache.Head().PackageCount];
  416. // Show everything if no arguments given
  417. if (CmdL.FileList[1] == 0)
  418. for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
  419. Show[I] = ToShow;
  420. else
  421. for (unsigned long I = 0; I != Cache.Head().PackageCount; I++)
  422. Show[I] = None;
  423. memset(Flags,0,sizeof(*Flags)*Cache.Head().PackageCount);
  424. // Map the shapes
  425. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
  426. {
  427. if (Pkg->VersionList == 0)
  428. {
  429. // Missing
  430. if (Pkg->ProvidesList == 0)
  431. ShapeMap[Pkg->ID] = 0;
  432. else
  433. ShapeMap[Pkg->ID] = 1;
  434. }
  435. else
  436. {
  437. // Normal
  438. if (Pkg->ProvidesList == 0)
  439. ShapeMap[Pkg->ID] = 2;
  440. else
  441. ShapeMap[Pkg->ID] = 3;
  442. }
  443. }
  444. // Load the list of packages from the command line into the show list
  445. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  446. {
  447. // Process per-package flags
  448. string P = *I;
  449. bool Force = false;
  450. if (P.length() > 3)
  451. {
  452. if (P.end()[-1] == '^')
  453. {
  454. Force = true;
  455. P.erase(P.end()-1);
  456. }
  457. if (P.end()[-1] == ',')
  458. P.erase(P.end()-1);
  459. }
  460. // Locate the package
  461. pkgCache::PkgIterator Pkg = Cache.FindPkg(P);
  462. if (Pkg.end() == true)
  463. {
  464. _error->Warning("Unable to locate package %s",*I);
  465. continue;
  466. }
  467. Show[Pkg->ID] = ToShow;
  468. if (Force == true)
  469. Flags[Pkg->ID] |= ForceNR;
  470. }
  471. // Little header
  472. printf("digraph packages {\n");
  473. printf("concentrate=true;\n");
  474. printf("size=\"30,40\";\n");
  475. bool Act = true;
  476. while (Act == true)
  477. {
  478. Act = false;
  479. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
  480. {
  481. // See we need to show this package
  482. if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
  483. continue;
  484. // Colour as done
  485. if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
  486. {
  487. // Pure Provides and missing packages have no deps!
  488. if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
  489. Show[Pkg->ID] = Done;
  490. else
  491. Show[Pkg->ID] = DoneNR;
  492. }
  493. else
  494. Show[Pkg->ID] = Done;
  495. Act = true;
  496. // No deps to map out
  497. if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
  498. continue;
  499. pkgCache::VerIterator Ver = Pkg.VersionList();
  500. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; D++)
  501. {
  502. // See if anything can meet this dep
  503. // Walk along the actual package providing versions
  504. bool Hit = false;
  505. pkgCache::PkgIterator DPkg = D.TargetPkg();
  506. for (pkgCache::VerIterator I = DPkg.VersionList();
  507. I.end() == false && Hit == false; I++)
  508. {
  509. if (pkgCheckDep(D.TargetVer(),I.VerStr(),D->CompareOp) == true)
  510. Hit = true;
  511. }
  512. // Follow all provides
  513. for (pkgCache::PrvIterator I = DPkg.ProvidesList();
  514. I.end() == false && Hit == false; I++)
  515. {
  516. if (pkgCheckDep(D.TargetVer(),I.ProvideVersion(),D->CompareOp) == false)
  517. Hit = true;
  518. }
  519. // Only graph critical deps
  520. if (D.IsCritical() == true)
  521. {
  522. printf("\"%s\" -> \"%s\"",Pkg.Name(),D.TargetPkg().Name());
  523. // Colour the node for recursion
  524. if (Show[D.TargetPkg()->ID] <= DoneNR)
  525. {
  526. /* If a conflicts does not meet anything in the database
  527. then show the relation but do not recurse */
  528. if (Hit == false && D->Type == pkgCache::Dep::Conflicts)
  529. {
  530. if (Show[D.TargetPkg()->ID] == None &&
  531. Show[D.TargetPkg()->ID] != ToShow)
  532. Show[D.TargetPkg()->ID] = ToShowNR;
  533. }
  534. else
  535. {
  536. if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
  537. Show[D.TargetPkg()->ID] = ToShowNR;
  538. else
  539. Show[D.TargetPkg()->ID] = ToShow;
  540. }
  541. }
  542. // Edge colour
  543. switch(D->Type)
  544. {
  545. case pkgCache::Dep::Conflicts:
  546. printf("[color=springgreen];\n");
  547. break;
  548. case pkgCache::Dep::PreDepends:
  549. printf("[color=blue];\n");
  550. break;
  551. default:
  552. printf(";\n");
  553. break;
  554. }
  555. }
  556. }
  557. }
  558. }
  559. /* Draw the box colours after the fact since we can not tell what colour
  560. they should be until everything is finished drawing */
  561. for (pkgCache::PkgIterator Pkg = Cache.PkgBegin(); Pkg.end() == false; Pkg++)
  562. {
  563. if (Show[Pkg->ID] < DoneNR)
  564. continue;
  565. // Orange box for early recursion stoppage
  566. if (Show[Pkg->ID] == DoneNR)
  567. printf("\"%s\" [color=orange,shape=%s];\n",Pkg.Name(),
  568. Shapes[ShapeMap[Pkg->ID]]);
  569. else
  570. printf("\"%s\" [shape=%s];\n",Pkg.Name(),
  571. Shapes[ShapeMap[Pkg->ID]]);
  572. }
  573. printf("}\n");
  574. return true;
  575. }
  576. /*}}}*/
  577. // DoAdd - Perform an adding operation /*{{{*/
  578. // ---------------------------------------------------------------------
  579. /* */
  580. bool DoAdd(CommandLine &CmdL)
  581. {
  582. // Make sure there is at least one argument
  583. if (CmdL.FileSize() <= 1)
  584. return _error->Error("You must give at least one file name");
  585. // Open the cache
  586. FileFd CacheF(_config->FindFile("Dir::Cache::pkgcache"),FileFd::WriteAny);
  587. if (_error->PendingError() == true)
  588. return false;
  589. DynamicMMap Map(CacheF,MMap::Public);
  590. if (_error->PendingError() == true)
  591. return false;
  592. OpTextProgress Progress(*_config);
  593. pkgCacheGenerator Gen(Map,Progress);
  594. if (_error->PendingError() == true)
  595. return false;
  596. unsigned long Length = CmdL.FileSize() - 1;
  597. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  598. {
  599. Progress.OverallProgress(I - CmdL.FileList,Length,1,"Generating cache");
  600. Progress.SubProgress(Length);
  601. // Do the merge
  602. FileFd TagF(*I,FileFd::ReadOnly);
  603. debListParser Parser(TagF);
  604. if (_error->PendingError() == true)
  605. return _error->Error("Problem opening %s",*I);
  606. if (Gen.SelectFile(*I) == false)
  607. return _error->Error("Problem with SelectFile");
  608. if (Gen.MergeList(Parser) == false)
  609. return _error->Error("Problem with MergeList");
  610. }
  611. Progress.Done();
  612. GCache = &Gen.GetCache();
  613. Stats(CmdL);
  614. return true;
  615. }
  616. /*}}}*/
  617. // DisplayRecord - Displays the complete record for the package /*{{{*/
  618. // ---------------------------------------------------------------------
  619. /* This displays the package record from the proper package index file.
  620. It is not used by DumpAvail for performance reasons. */
  621. bool DisplayRecord(pkgCache::VerIterator V)
  622. {
  623. // Find an appropriate file
  624. pkgCache::VerFileIterator Vf = V.FileList();
  625. for (; Vf.end() == false; Vf++)
  626. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
  627. break;
  628. if (Vf.end() == true)
  629. Vf = V.FileList();
  630. // Check and load the package list file
  631. pkgCache::PkgFileIterator I = Vf.File();
  632. if (I.IsOk() == false)
  633. return _error->Error("Package file %s is out of sync.",I.FileName());
  634. FileFd PkgF(I.FileName(),FileFd::ReadOnly);
  635. if (_error->PendingError() == true)
  636. return false;
  637. // Read the record and then write it out again.
  638. unsigned char *Buffer = new unsigned char[GCache->HeaderP->MaxVerFileSize];
  639. if (PkgF.Seek(V.FileList()->Offset) == false ||
  640. PkgF.Read(Buffer,V.FileList()->Size) == false ||
  641. write(STDOUT_FILENO,Buffer,V.FileList()->Size) != V.FileList()->Size)
  642. {
  643. delete [] Buffer;
  644. return false;
  645. }
  646. delete [] Buffer;
  647. return true;
  648. }
  649. /*}}}*/
  650. // Search - Perform a search /*{{{*/
  651. // ---------------------------------------------------------------------
  652. /* This searches the package names and pacakge descriptions for a pattern */
  653. bool Search(CommandLine &CmdL)
  654. {
  655. pkgCache &Cache = *GCache;
  656. bool ShowFull = _config->FindB("APT::Cache::ShowFull",false);
  657. bool NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
  658. // Make sure there is at least one argument
  659. if (CmdL.FileSize() != 2)
  660. return _error->Error("You must give exactly one pattern");
  661. // Compile the regex pattern
  662. regex_t Pattern;
  663. if (regcomp(&Pattern,CmdL.FileList[1],REG_EXTENDED | REG_ICASE |
  664. REG_NOSUB) != 0)
  665. return _error->Error("Regex compilation error");
  666. // Create the text record parser
  667. pkgRecords Recs(Cache);
  668. if (_error->PendingError() == true)
  669. return false;
  670. // Search package names
  671. pkgCache::PkgIterator I = Cache.PkgBegin();
  672. for (;I.end() != true; I++)
  673. {
  674. // We search against the install version as that makes the most sense..
  675. pkgCache::VerIterator V = Cache.GetCandidateVer(I);
  676. if (V.end() == true)
  677. continue;
  678. pkgRecords::Parser &P = Recs.Lookup(V.FileList());
  679. if (regexec(&Pattern,I.Name(),0,0,0) == 0 ||
  680. (NamesOnly == false &&
  681. regexec(&Pattern,P.LongDesc().c_str(),0,0,0) == 0))
  682. {
  683. if (ShowFull == true)
  684. DisplayRecord(V);
  685. else
  686. cout << I.Name() << " - " << P.ShortDesc() << endl;
  687. }
  688. }
  689. regfree(&Pattern);
  690. return true;
  691. }
  692. /*}}}*/
  693. // ShowPackage - Dump the package record to the screen /*{{{*/
  694. // ---------------------------------------------------------------------
  695. /* */
  696. bool ShowPackage(CommandLine &CmdL)
  697. {
  698. pkgCache &Cache = *GCache;
  699. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  700. {
  701. pkgCache::PkgIterator Pkg = Cache.FindPkg(*I);
  702. if (Pkg.end() == true)
  703. {
  704. _error->Warning("Unable to locate package %s",*I);
  705. continue;
  706. }
  707. // Find the proper version to use. We should probably use the DepCache.
  708. if (_config->FindB("APT::Cache::AllVersions","true") == true)
  709. {
  710. pkgCache::VerIterator V;
  711. for (V = Pkg.VersionList(); V.end() == false; V++)
  712. {
  713. if (DisplayRecord(V) == false)
  714. return false;
  715. }
  716. }
  717. else
  718. {
  719. pkgCache::VerIterator V = Cache.GetCandidateVer(Pkg);
  720. if (V.end() == true || V.FileList().end() == true)
  721. continue;
  722. if (DisplayRecord(V) == false)
  723. return false;
  724. }
  725. }
  726. return true;
  727. }
  728. /*}}}*/
  729. // ShowPkgNames - Show package names /*{{{*/
  730. // ---------------------------------------------------------------------
  731. /* This does a prefix match on the first argument */
  732. bool ShowPkgNames(CommandLine &CmdL)
  733. {
  734. pkgCache &Cache = *GCache;
  735. pkgCache::PkgIterator I = Cache.PkgBegin();
  736. bool All = _config->FindB("APT::Cache::AllNames","false");
  737. if (CmdL.FileList[1] != 0)
  738. {
  739. for (;I.end() != true; I++)
  740. {
  741. if (All == false && I->VersionList == 0)
  742. continue;
  743. if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
  744. cout << I.Name() << endl;
  745. }
  746. return true;
  747. }
  748. // Show all pkgs
  749. for (;I.end() != true; I++)
  750. {
  751. if (All == false && I->VersionList == 0)
  752. continue;
  753. cout << I.Name() << endl;
  754. }
  755. return true;
  756. }
  757. /*}}}*/
  758. // ShowSrcPackage - Show source package records /*{{{*/
  759. // ---------------------------------------------------------------------
  760. /* */
  761. bool ShowSrcPackage(CommandLine &CmdL)
  762. {
  763. pkgSourceList List;
  764. List.ReadMainList();
  765. // Create the text record parsers
  766. pkgSrcRecords SrcRecs(List);
  767. if (_error->PendingError() == true)
  768. return false;
  769. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  770. {
  771. SrcRecs.Restart();
  772. pkgSrcRecords::Parser *Parse;
  773. while ((Parse = SrcRecs.Find(*I,false)) != 0)
  774. cout << Parse->AsStr();
  775. }
  776. return true;
  777. }
  778. /*}}}*/
  779. // GenCaches - Call the main cache generator /*{{{*/
  780. // ---------------------------------------------------------------------
  781. /* */
  782. bool GenCaches(CommandLine &Cmd)
  783. {
  784. OpTextProgress Progress(*_config);
  785. pkgSourceList List;
  786. List.ReadMainList();
  787. return pkgMakeStatusCache(List,Progress);
  788. }
  789. /*}}}*/
  790. // ShowHelp - Show a help screen /*{{{*/
  791. // ---------------------------------------------------------------------
  792. /* */
  793. bool ShowHelp(CommandLine &Cmd)
  794. {
  795. cout << PACKAGE << ' ' << VERSION << " for " << ARCHITECTURE <<
  796. " compiled on " << __DATE__ << " " << __TIME__ << endl;
  797. if (_config->FindB("version") == true)
  798. return 100;
  799. cout << "Usage: apt-cache [options] command" << endl;
  800. cout << " apt-cache [options] add file1 [file1 ...]" << endl;
  801. cout << " apt-cache [options] showpkg pkg1 [pkg2 ...]" << endl;
  802. cout << endl;
  803. cout << "apt-cache is a low-level tool used to manipulate APT's binary" << endl;
  804. cout << "cache files stored in " << _config->FindFile("Dir::Cache") << endl;
  805. cout << "It is not meant for ordinary use only as a debug aide." << endl;
  806. cout << endl;
  807. cout << "Commands:" << endl;
  808. cout << " add - Add an package file to the source cache" << endl;
  809. cout << " gencaches - Build both the package and source cache" << endl;
  810. cout << " showpkg - Show some general information for a single package" << endl;
  811. cout << " stats - Show some basic statistics" << endl;
  812. cout << " dump - Show the entire file in a terse form" << endl;
  813. cout << " dumpavail - Print an available file to stdout" << endl;
  814. cout << " unmet - Show unmet dependencies" << endl;
  815. cout << " check - Check the cache a bit" << endl;
  816. cout << " search - Search the package list for a regex pattern" << endl;
  817. cout << " show - Show a readable record for the package" << endl;
  818. cout << " depends - Show raw dependency information for a package" << endl;
  819. cout << " pkgnames - List the names of all packages" << endl;
  820. cout << " dotty - Generate package graphs for GraphVis" << endl;
  821. cout << endl;
  822. cout << "Options:" << endl;
  823. cout << " -h This help text." << endl;
  824. cout << " -p=? The package cache. [" << _config->FindFile("Dir::Cache::pkgcache") << ']' << endl;
  825. cout << " -s=? The source cache. [" << _config->FindFile("Dir::Cache::srcpkgcache") << ']' << endl;
  826. cout << " -q Disable progress indicator." << endl;
  827. cout << " -i Show only important deps for the unmet command." << endl;
  828. cout << " -c=? Read this configuration file" << endl;
  829. cout << " -o=? Set an arbitary configuration option, eg -o dir::cache=/tmp" << endl;
  830. cout << "See the apt-cache(8) and apt.conf(5) manual pages for more information." << endl;
  831. return 100;
  832. }
  833. /*}}}*/
  834. // CacheInitialize - Initialize things for apt-cache /*{{{*/
  835. // ---------------------------------------------------------------------
  836. /* */
  837. void CacheInitialize()
  838. {
  839. _config->Set("quiet",0);
  840. _config->Set("help",false);
  841. }
  842. /*}}}*/
  843. int main(int argc,const char *argv[])
  844. {
  845. CommandLine::Args Args[] = {
  846. {'h',"help","help",0},
  847. {'v',"version","version",0},
  848. {'p',"pkg-cache","Dir::Cache::pkgcache",CommandLine::HasArg},
  849. {'s',"src-cache","Dir::Cache::srcpkgcache",CommandLine::HasArg},
  850. {'q',"quiet","quiet",CommandLine::IntLevel},
  851. {'i',"important","APT::Cache::Important",0},
  852. {'f',"full","APT::Cache::ShowFull",0},
  853. {'g',"no-generate","APT::Cache::NoGenerate",0},
  854. {'a',"all-versions","APT::Cache::AllVersions",0},
  855. {0,"names-only","APT::Cache::NamesOnly",0},
  856. {0,"all-names","APT::Cache::AllNames",0},
  857. {'c',"config-file",0,CommandLine::ConfigFile},
  858. {'o',"option",0,CommandLine::ArbItem},
  859. {0,0,0,0}};
  860. CommandLine::Dispatch CmdsA[] = {{"help",&ShowHelp},
  861. {"add",&DoAdd},
  862. {"gencaches",&GenCaches},
  863. {"showsrc",&ShowSrcPackage},
  864. {0,0}};
  865. CommandLine::Dispatch CmdsB[] = {{"showpkg",&DumpPackage},
  866. {"stats",&Stats},
  867. {"dump",&Dump},
  868. {"dumpavail",&DumpAvail},
  869. {"unmet",&UnMet},
  870. {"check",&Check},
  871. {"search",&Search},
  872. {"depends",&Depends},
  873. {"dotty",&Dotty},
  874. {"show",&ShowPackage},
  875. {"pkgnames",&ShowPkgNames},
  876. {0,0}};
  877. CacheInitialize();
  878. // Parse the command line and initialize the package library
  879. CommandLine CmdL(Args,_config);
  880. if (pkgInitialize(*_config) == false ||
  881. CmdL.Parse(argc,argv) == false)
  882. {
  883. _error->DumpErrors();
  884. return 100;
  885. }
  886. // See if the help should be shown
  887. if (_config->FindB("help") == true ||
  888. CmdL.FileSize() == 0)
  889. return ShowHelp(CmdL);
  890. // Deal with stdout not being a tty
  891. if (ttyname(STDOUT_FILENO) == 0 && _config->FindI("quiet",0) < 1)
  892. _config->Set("quiet","1");
  893. if (CmdL.DispatchArg(CmdsA,false) == false && _error->PendingError() == false)
  894. {
  895. MMap *Map;
  896. if (_config->FindB("APT::Cache::NoGenerate",false) == true)
  897. {
  898. Map = new MMap(*new FileFd(_config->FindFile("Dir::Cache::pkgcache"),
  899. FileFd::ReadOnly),MMap::Public|MMap::ReadOnly);
  900. }
  901. else
  902. {
  903. // Open the cache file
  904. pkgSourceList List;
  905. List.ReadMainList();
  906. // Generate it and map it
  907. OpProgress Prog;
  908. Map = pkgMakeStatusCacheMem(List,Prog);
  909. }
  910. if (_error->PendingError() == false)
  911. {
  912. pkgCache Cache(*Map);
  913. GCache = &Cache;
  914. if (_error->PendingError() == false)
  915. CmdL.DispatchArg(CmdsB);
  916. }
  917. delete Map;
  918. }
  919. // Print any errors or warnings found during parsing
  920. if (_error->empty() == false)
  921. {
  922. bool Errors = _error->PendingError();
  923. _error->DumpErrors();
  924. return Errors == true?100:0;
  925. }
  926. return 0;
  927. }