apt-cache.cc 64 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cache.cc,v 1.72 2004/04/30 04:34:03 mdz 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.
  8. Returns 100 on failure, 0 on success.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include<config.h>
  13. #include <apt-pkg/algorithms.h>
  14. #include <apt-pkg/cachefile.h>
  15. #include <apt-pkg/cacheset.h>
  16. #include <apt-pkg/cmndline.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/fileutl.h>
  19. #include <apt-pkg/indexfile.h>
  20. #include <apt-pkg/init.h>
  21. #include <apt-pkg/metaindex.h>
  22. #include <apt-pkg/pkgrecords.h>
  23. #include <apt-pkg/pkgsystem.h>
  24. #include <apt-pkg/policy.h>
  25. #include <apt-pkg/progress.h>
  26. #include <apt-pkg/sourcelist.h>
  27. #include <apt-pkg/sptr.h>
  28. #include <apt-pkg/srcrecords.h>
  29. #include <apt-pkg/strutl.h>
  30. #include <apt-pkg/tagfile.h>
  31. #include <apt-pkg/version.h>
  32. #include <apt-pkg/cacheiterators.h>
  33. #include <apt-pkg/configuration.h>
  34. #include <apt-pkg/depcache.h>
  35. #include <apt-pkg/macros.h>
  36. #include <apt-pkg/mmap.h>
  37. #include <apt-pkg/pkgcache.h>
  38. #include <apt-private/private-cacheset.h>
  39. #include <apt-private/private-cmndline.h>
  40. #include <regex.h>
  41. #include <stddef.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <unistd.h>
  45. #include <algorithm>
  46. #include <cstring>
  47. #include <iomanip>
  48. #include <iostream>
  49. #include <list>
  50. #include <map>
  51. #include <set>
  52. #include <string>
  53. #include <vector>
  54. #include <apti18n.h>
  55. /*}}}*/
  56. using namespace std;
  57. // LocalitySort - Sort a version list by package file locality /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* */
  60. static int LocalityCompare(const void *a, const void *b)
  61. {
  62. pkgCache::VerFile *A = *(pkgCache::VerFile **)a;
  63. pkgCache::VerFile *B = *(pkgCache::VerFile **)b;
  64. if (A == 0 && B == 0)
  65. return 0;
  66. if (A == 0)
  67. return 1;
  68. if (B == 0)
  69. return -1;
  70. if (A->File == B->File)
  71. return A->Offset - B->Offset;
  72. return A->File - B->File;
  73. }
  74. static void LocalitySort(pkgCache::VerFile **begin,
  75. unsigned long Count,size_t Size)
  76. {
  77. qsort(begin,Count,Size,LocalityCompare);
  78. }
  79. static void LocalitySort(pkgCache::DescFile **begin,
  80. unsigned long Count,size_t Size)
  81. {
  82. qsort(begin,Count,Size,LocalityCompare);
  83. }
  84. /*}}}*/
  85. // UnMet - Show unmet dependencies /*{{{*/
  86. // ---------------------------------------------------------------------
  87. /* */
  88. static bool ShowUnMet(pkgCache::VerIterator const &V, bool const Important)
  89. {
  90. bool Header = false;
  91. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false;)
  92. {
  93. // Collect or groups
  94. pkgCache::DepIterator Start;
  95. pkgCache::DepIterator End;
  96. D.GlobOr(Start,End);
  97. // Important deps only
  98. if (Important == true)
  99. if (End->Type != pkgCache::Dep::PreDepends &&
  100. End->Type != pkgCache::Dep::Depends)
  101. continue;
  102. // Skip conflicts and replaces
  103. if (End.IsNegative() == true || End->Type == pkgCache::Dep::Replaces)
  104. continue;
  105. // Verify the or group
  106. bool OK = false;
  107. pkgCache::DepIterator RealStart = Start;
  108. do
  109. {
  110. // See if this dep is Ok
  111. pkgCache::Version **VList = Start.AllTargets();
  112. if (*VList != 0)
  113. {
  114. OK = true;
  115. delete [] VList;
  116. break;
  117. }
  118. delete [] VList;
  119. if (Start == End)
  120. break;
  121. ++Start;
  122. }
  123. while (1);
  124. // The group is OK
  125. if (OK == true)
  126. continue;
  127. // Oops, it failed..
  128. if (Header == false)
  129. ioprintf(cout,_("Package %s version %s has an unmet dep:\n"),
  130. V.ParentPkg().FullName(true).c_str(),V.VerStr());
  131. Header = true;
  132. // Print out the dep type
  133. cout << " " << End.DepType() << ": ";
  134. // Show the group
  135. Start = RealStart;
  136. do
  137. {
  138. cout << Start.TargetPkg().FullName(true);
  139. if (Start.TargetVer() != 0)
  140. cout << " (" << Start.CompType() << " " << Start.TargetVer() <<
  141. ")";
  142. if (Start == End)
  143. break;
  144. cout << " | ";
  145. ++Start;
  146. }
  147. while (1);
  148. cout << endl;
  149. }
  150. return true;
  151. }
  152. static bool UnMet(CommandLine &CmdL)
  153. {
  154. bool const Important = _config->FindB("APT::Cache::Important",false);
  155. pkgCacheFile CacheFile;
  156. if (unlikely(CacheFile.GetPkgCache() == NULL))
  157. return false;
  158. if (CmdL.FileSize() <= 1)
  159. {
  160. for (pkgCache::PkgIterator P = CacheFile.GetPkgCache()->PkgBegin(); P.end() == false; ++P)
  161. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
  162. if (ShowUnMet(V, Important) == false)
  163. return false;
  164. }
  165. else
  166. {
  167. CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
  168. APT::VersionList verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1,
  169. APT::CacheSetHelper::CANDIDATE, helper);
  170. for (APT::VersionList::iterator V = verset.begin(); V != verset.end(); ++V)
  171. if (ShowUnMet(V, Important) == false)
  172. return false;
  173. }
  174. return true;
  175. }
  176. /*}}}*/
  177. // DumpPackage - Show a dump of a package record /*{{{*/
  178. // ---------------------------------------------------------------------
  179. /* */
  180. static bool DumpPackage(CommandLine &CmdL)
  181. {
  182. pkgCacheFile CacheFile;
  183. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  184. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  185. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  186. {
  187. cout << "Package: " << Pkg.FullName(true) << endl;
  188. cout << "Versions: " << endl;
  189. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; ++Cur)
  190. {
  191. cout << Cur.VerStr();
  192. for (pkgCache::VerFileIterator Vf = Cur.FileList(); Vf.end() == false; ++Vf)
  193. cout << " (" << Vf.File().FileName() << ")";
  194. cout << endl;
  195. for (pkgCache::DescIterator D = Cur.DescriptionList(); D.end() == false; ++D)
  196. {
  197. cout << " Description Language: " << D.LanguageCode() << endl
  198. << " File: " << D.FileList().File().FileName() << endl
  199. << " MD5: " << D.md5() << endl;
  200. }
  201. cout << endl;
  202. }
  203. cout << endl;
  204. cout << "Reverse Depends: " << endl;
  205. for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() != true; ++D)
  206. {
  207. cout << " " << D.ParentPkg().FullName(true) << ',' << D.TargetPkg().FullName(true);
  208. if (D->Version != 0)
  209. cout << ' ' << DeNull(D.TargetVer()) << endl;
  210. else
  211. cout << endl;
  212. }
  213. cout << "Dependencies: " << endl;
  214. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; ++Cur)
  215. {
  216. cout << Cur.VerStr() << " - ";
  217. for (pkgCache::DepIterator Dep = Cur.DependsList(); Dep.end() != true; ++Dep)
  218. cout << Dep.TargetPkg().FullName(true) << " (" << (int)Dep->CompareOp << " " << DeNull(Dep.TargetVer()) << ") ";
  219. cout << endl;
  220. }
  221. cout << "Provides: " << endl;
  222. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; ++Cur)
  223. {
  224. cout << Cur.VerStr() << " - ";
  225. for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; ++Prv)
  226. cout << Prv.ParentPkg().FullName(true) << " (= " << (Prv->ProvideVersion == 0 ? "" : Prv.ProvideVersion()) << ") ";
  227. cout << endl;
  228. }
  229. cout << "Reverse Provides: " << endl;
  230. for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; ++Prv)
  231. cout << Prv.OwnerPkg().FullName(true) << " " << Prv.OwnerVer().VerStr() << " (= " << (Prv->ProvideVersion == 0 ? "" : Prv.ProvideVersion()) << ")"<< endl;
  232. }
  233. return true;
  234. }
  235. /*}}}*/
  236. // ShowHashTableStats - Show stats about a hashtable /*{{{*/
  237. // ---------------------------------------------------------------------
  238. /* */
  239. static map_pointer_t PackageNext(pkgCache::Package const * const P) { return P->NextPackage; }
  240. static map_pointer_t GroupNext(pkgCache::Group const * const G) { return G->Next; }
  241. template<class T>
  242. static void ShowHashTableStats(std::string Type,
  243. T *StartP,
  244. map_pointer_t *Hashtable,
  245. unsigned long Size,
  246. map_pointer_t(*Next)(T const * const))
  247. {
  248. // hashtable stats for the HashTable
  249. unsigned long NumBuckets = Size;
  250. unsigned long UsedBuckets = 0;
  251. unsigned long UnusedBuckets = 0;
  252. unsigned long LongestBucket = 0;
  253. unsigned long ShortestBucket = NumBuckets;
  254. unsigned long Entries = 0;
  255. for (unsigned int i=0; i < NumBuckets; ++i)
  256. {
  257. T *P = StartP + Hashtable[i];
  258. if(P == 0 || P == StartP)
  259. {
  260. ++UnusedBuckets;
  261. continue;
  262. }
  263. ++UsedBuckets;
  264. unsigned long ThisBucketSize = 0;
  265. for (; P != StartP; P = StartP + Next(P))
  266. ++ThisBucketSize;
  267. Entries += ThisBucketSize;
  268. LongestBucket = std::max(ThisBucketSize, LongestBucket);
  269. ShortestBucket = std::min(ThisBucketSize, ShortestBucket);
  270. }
  271. cout << "Total buckets in " << Type << ": " << NumBuckets << std::endl;
  272. cout << " Unused: " << UnusedBuckets << std::endl;
  273. cout << " Used: " << UsedBuckets << std::endl;
  274. cout << " Average entries: " << Entries/(double)NumBuckets << std::endl;
  275. cout << " Longest: " << LongestBucket << std::endl;
  276. cout << " Shortest: " << ShortestBucket << std::endl;
  277. }
  278. /*}}}*/
  279. // Stats - Dump some nice statistics /*{{{*/
  280. // ---------------------------------------------------------------------
  281. /* */
  282. static bool Stats(CommandLine &CmdL)
  283. {
  284. pkgCacheFile CacheFile;
  285. pkgCache *Cache = CacheFile.GetPkgCache();
  286. if (CmdL.FileSize() > 1) {
  287. _error->Error(_("apt-cache stats does not take any arguments"));
  288. return false;
  289. }
  290. if (unlikely(Cache == NULL))
  291. return false;
  292. cout << _("Total package names: ") << Cache->Head().GroupCount << " (" <<
  293. SizeToStr(Cache->Head().GroupCount*Cache->Head().GroupSz) << ')' << endl
  294. << _("Total package structures: ") << Cache->Head().PackageCount << " (" <<
  295. SizeToStr(Cache->Head().PackageCount*Cache->Head().PackageSz) << ')' << endl;
  296. int Normal = 0;
  297. int Virtual = 0;
  298. int NVirt = 0;
  299. int DVirt = 0;
  300. int Missing = 0;
  301. pkgCache::PkgIterator I = Cache->PkgBegin();
  302. for (;I.end() != true; ++I)
  303. {
  304. if (I->VersionList != 0 && I->ProvidesList == 0)
  305. {
  306. Normal++;
  307. continue;
  308. }
  309. if (I->VersionList != 0 && I->ProvidesList != 0)
  310. {
  311. NVirt++;
  312. continue;
  313. }
  314. if (I->VersionList == 0 && I->ProvidesList != 0)
  315. {
  316. // Only 1 provides
  317. if (I.ProvidesList()->NextProvides == 0)
  318. {
  319. DVirt++;
  320. }
  321. else
  322. Virtual++;
  323. continue;
  324. }
  325. if (I->VersionList == 0 && I->ProvidesList == 0)
  326. {
  327. Missing++;
  328. continue;
  329. }
  330. }
  331. cout << _(" Normal packages: ") << Normal << endl;
  332. cout << _(" Pure virtual packages: ") << Virtual << endl;
  333. cout << _(" Single virtual packages: ") << DVirt << endl;
  334. cout << _(" Mixed virtual packages: ") << NVirt << endl;
  335. cout << _(" Missing: ") << Missing << endl;
  336. cout << _("Total distinct versions: ") << Cache->Head().VersionCount << " (" <<
  337. SizeToStr(Cache->Head().VersionCount*Cache->Head().VersionSz) << ')' << endl;
  338. cout << _("Total distinct descriptions: ") << Cache->Head().DescriptionCount << " (" <<
  339. SizeToStr(Cache->Head().DescriptionCount*Cache->Head().DescriptionSz) << ')' << endl;
  340. cout << _("Total dependencies: ") << Cache->Head().DependsCount << "/" << Cache->Head().DependsDataCount << " (" <<
  341. SizeToStr((Cache->Head().DependsCount*Cache->Head().DependencySz) +
  342. (Cache->Head().DependsDataCount*Cache->Head().DependencyDataSz)) << ')' << endl;
  343. cout << _("Total ver/file relations: ") << Cache->Head().VerFileCount << " (" <<
  344. SizeToStr(Cache->Head().VerFileCount*Cache->Head().VerFileSz) << ')' << endl;
  345. cout << _("Total Desc/File relations: ") << Cache->Head().DescFileCount << " (" <<
  346. SizeToStr(Cache->Head().DescFileCount*Cache->Head().DescFileSz) << ')' << endl;
  347. cout << _("Total Provides mappings: ") << Cache->Head().ProvidesCount << " (" <<
  348. SizeToStr(Cache->Head().ProvidesCount*Cache->Head().ProvidesSz) << ')' << endl;
  349. // String list stats
  350. std::set<map_stringitem_t> stritems;
  351. for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() == false; ++G)
  352. stritems.insert(G->Name);
  353. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  354. {
  355. stritems.insert(P->Arch);
  356. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
  357. {
  358. if (V->VerStr != 0)
  359. stritems.insert(V->VerStr);
  360. if (V->Section != 0)
  361. stritems.insert(V->Section);
  362. stritems.insert(V->SourcePkgName);
  363. stritems.insert(V->SourceVerStr);
  364. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; ++D)
  365. {
  366. if (D->Version != 0)
  367. stritems.insert(D->Version);
  368. }
  369. for (pkgCache::DescIterator D = V.DescriptionList(); D.end() == false; ++D)
  370. {
  371. stritems.insert(D->md5sum);
  372. stritems.insert(D->language_code);
  373. }
  374. }
  375. for (pkgCache::PrvIterator Prv = P.ProvidesList(); Prv.end() == false; ++Prv)
  376. {
  377. if (Prv->ProvideVersion != 0)
  378. stritems.insert(Prv->ProvideVersion);
  379. }
  380. }
  381. for (pkgCache::RlsFileIterator F = Cache->RlsFileBegin(); F != Cache->RlsFileEnd(); ++F)
  382. {
  383. stritems.insert(F->FileName);
  384. stritems.insert(F->Archive);
  385. stritems.insert(F->Codename);
  386. stritems.insert(F->Version);
  387. stritems.insert(F->Origin);
  388. stritems.insert(F->Label);
  389. stritems.insert(F->Site);
  390. }
  391. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F)
  392. {
  393. stritems.insert(F->FileName);
  394. stritems.insert(F->Architecture);
  395. stritems.insert(F->Component);
  396. stritems.insert(F->IndexType);
  397. }
  398. unsigned long Size = 0;
  399. for (std::set<map_stringitem_t>::const_iterator i = stritems.begin(); i != stritems.end(); ++i)
  400. Size += strlen(Cache->StrP + *i) + 1;
  401. cout << _("Total globbed strings: ") << stritems.size() << " (" << SizeToStr(Size) << ')' << endl;
  402. stritems.clear();
  403. unsigned long Slack = 0;
  404. for (int I = 0; I != 7; I++)
  405. Slack += Cache->Head().Pools[I].ItemSize*Cache->Head().Pools[I].Count;
  406. cout << _("Total slack space: ") << SizeToStr(Slack) << endl;
  407. unsigned long Total = 0;
  408. #define APT_CACHESIZE(X,Y) (Cache->Head().X * Cache->Head().Y)
  409. Total = Slack + Size +
  410. APT_CACHESIZE(GroupCount, GroupSz) +
  411. APT_CACHESIZE(PackageCount, PackageSz) +
  412. APT_CACHESIZE(VersionCount, VersionSz) +
  413. APT_CACHESIZE(DescriptionCount, DescriptionSz) +
  414. APT_CACHESIZE(DependsCount, DependencySz) +
  415. APT_CACHESIZE(DependsDataCount, DependencyDataSz) +
  416. APT_CACHESIZE(ReleaseFileCount, ReleaseFileSz) +
  417. APT_CACHESIZE(PackageFileCount, PackageFileSz) +
  418. APT_CACHESIZE(VerFileCount, VerFileSz) +
  419. APT_CACHESIZE(DescFileCount, DescFileSz) +
  420. APT_CACHESIZE(ProvidesCount, ProvidesSz) +
  421. (2 * Cache->Head().GetHashTableSize() * sizeof(map_id_t));
  422. cout << _("Total space accounted for: ") << SizeToStr(Total) << endl;
  423. #undef APT_CACHESIZE
  424. // hashtable stats
  425. ShowHashTableStats<pkgCache::Package>("PkgHashTable", Cache->PkgP, Cache->Head().PkgHashTableP(), Cache->Head().GetHashTableSize(), PackageNext);
  426. ShowHashTableStats<pkgCache::Group>("GrpHashTable", Cache->GrpP, Cache->Head().GrpHashTableP(), Cache->Head().GetHashTableSize(), GroupNext);
  427. return true;
  428. }
  429. /*}}}*/
  430. // Dump - show everything /*{{{*/
  431. // ---------------------------------------------------------------------
  432. /* This is worthless except fer debugging things */
  433. static bool Dump(CommandLine &)
  434. {
  435. pkgCacheFile CacheFile;
  436. pkgCache *Cache = CacheFile.GetPkgCache();
  437. if (unlikely(Cache == NULL))
  438. return false;
  439. std::cout << "Using Versioning System: " << Cache->VS->Label << std::endl;
  440. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  441. {
  442. std::cout << "Package: " << P.FullName(true) << std::endl;
  443. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
  444. {
  445. std::cout << " Version: " << V.VerStr() << std::endl;
  446. std::cout << " File: " << V.FileList().File().FileName() << std::endl;
  447. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; ++D)
  448. std::cout << " Depends: " << D.TargetPkg().FullName(true) << ' ' <<
  449. DeNull(D.TargetVer()) << std::endl;
  450. for (pkgCache::DescIterator D = V.DescriptionList(); D.end() == false; ++D)
  451. {
  452. std::cout << " Description Language: " << D.LanguageCode() << std::endl
  453. << " File: " << D.FileList().File().FileName() << std::endl
  454. << " MD5: " << D.md5() << std::endl;
  455. }
  456. }
  457. }
  458. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  459. {
  460. std::cout << "File: " << F.FileName() << std::endl;
  461. std::cout << " Type: " << F.IndexType() << std::endl;
  462. std::cout << " Size: " << F->Size << std::endl;
  463. std::cout << " ID: " << F->ID << std::endl;
  464. std::cout << " Flags: " << F->Flags << std::endl;
  465. std::cout << " Time: " << TimeRFC1123(F->mtime) << std::endl;
  466. std::cout << " Archive: " << DeNull(F.Archive()) << std::endl;
  467. std::cout << " Component: " << DeNull(F.Component()) << std::endl;
  468. std::cout << " Version: " << DeNull(F.Version()) << std::endl;
  469. std::cout << " Origin: " << DeNull(F.Origin()) << std::endl;
  470. std::cout << " Site: " << DeNull(F.Site()) << std::endl;
  471. std::cout << " Label: " << DeNull(F.Label()) << std::endl;
  472. std::cout << " Architecture: " << DeNull(F.Architecture()) << std::endl;
  473. }
  474. return true;
  475. }
  476. /*}}}*/
  477. // DumpAvail - Print out the available list /*{{{*/
  478. // ---------------------------------------------------------------------
  479. /* This is needed to make dpkg --merge happy.. I spent a bit of time to
  480. make this run really fast, perhaps I went a little overboard.. */
  481. static bool DumpAvail(CommandLine &)
  482. {
  483. pkgCacheFile CacheFile;
  484. pkgCache *Cache = CacheFile.GetPkgCache();
  485. if (unlikely(Cache == NULL || CacheFile.BuildPolicy() == false))
  486. return false;
  487. unsigned long Count = Cache->HeaderP->PackageCount+1;
  488. pkgCache::VerFile **VFList = new pkgCache::VerFile *[Count];
  489. memset(VFList,0,sizeof(*VFList)*Count);
  490. // Map versions that we want to write out onto the VerList array.
  491. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  492. {
  493. if (P->VersionList == 0)
  494. continue;
  495. /* Find the proper version to use. If the policy says there are no
  496. possible selections we return the installed version, if available..
  497. This prevents dselect from making it obsolete. */
  498. pkgCache::VerIterator V = CacheFile.GetPolicy()->GetCandidateVer(P);
  499. if (V.end() == true)
  500. {
  501. if (P->CurrentVer == 0)
  502. continue;
  503. V = P.CurrentVer();
  504. }
  505. pkgCache::VerFileIterator VF = V.FileList();
  506. for (; VF.end() == false ; ++VF)
  507. if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
  508. break;
  509. /* Okay, here we have a bit of a problem.. The policy has selected the
  510. currently installed package - however it only exists in the
  511. status file.. We need to write out something or dselect will mark
  512. the package as obsolete! Thus we emit the status file entry, but
  513. below we remove the status line to make it valid for the
  514. available file. However! We only do this if their do exist *any*
  515. non-source versions of the package - that way the dselect obsolete
  516. handling works OK. */
  517. if (VF.end() == true)
  518. {
  519. for (pkgCache::VerIterator Cur = P.VersionList(); Cur.end() != true; ++Cur)
  520. {
  521. for (VF = Cur.FileList(); VF.end() == false; ++VF)
  522. {
  523. if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
  524. {
  525. VF = V.FileList();
  526. break;
  527. }
  528. }
  529. if (VF.end() == false)
  530. break;
  531. }
  532. }
  533. VFList[P->ID] = VF;
  534. }
  535. LocalitySort(VFList,Count,sizeof(*VFList));
  536. std::vector<pkgTagSection::Tag> RW;
  537. RW.push_back(pkgTagSection::Tag::Remove("Status"));
  538. RW.push_back(pkgTagSection::Tag::Remove("Config-Version"));
  539. FileFd stdoutfd;
  540. stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
  541. // Iterate over all the package files and write them out.
  542. char *Buffer = new char[Cache->HeaderP->MaxVerFileSize+10];
  543. for (pkgCache::VerFile **J = VFList; *J != 0;)
  544. {
  545. pkgCache::PkgFileIterator File(*Cache,(*J)->File + Cache->PkgFileP);
  546. if (File.IsOk() == false)
  547. {
  548. _error->Error(_("Package file %s is out of sync."),File.FileName());
  549. break;
  550. }
  551. FileFd PkgF(File.FileName(),FileFd::ReadOnly, FileFd::Extension);
  552. if (_error->PendingError() == true)
  553. break;
  554. /* Write all of the records from this package file, since we
  555. already did locality sorting we can now just seek through the
  556. file in read order. We apply 1 more optimization here, since often
  557. there will be < 1 byte gaps between records (for the \n) we read that
  558. into the next buffer and offset a bit.. */
  559. unsigned long Pos = 0;
  560. for (; *J != 0; J++)
  561. {
  562. if ((*J)->File + Cache->PkgFileP != File)
  563. break;
  564. const pkgCache::VerFile &VF = **J;
  565. // Read the record and then write it out again.
  566. unsigned long Jitter = VF.Offset - Pos;
  567. if (Jitter > 8)
  568. {
  569. if (PkgF.Seek(VF.Offset) == false)
  570. break;
  571. Jitter = 0;
  572. }
  573. if (PkgF.Read(Buffer,VF.Size + Jitter) == false)
  574. break;
  575. Buffer[VF.Size + Jitter] = '\n';
  576. // See above..
  577. if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
  578. {
  579. pkgTagSection Tags;
  580. if (Tags.Scan(Buffer+Jitter,VF.Size+1) == false ||
  581. Tags.Write(stdoutfd, NULL, RW) == false ||
  582. stdoutfd.Write("\n", 1) == false)
  583. {
  584. _error->Error("Internal Error, Unable to parse a package record");
  585. break;
  586. }
  587. }
  588. else
  589. {
  590. if (stdoutfd.Write(Buffer + Jitter, VF.Size + 1) == false)
  591. break;
  592. }
  593. Pos = VF.Offset + VF.Size;
  594. }
  595. if (_error->PendingError() == true)
  596. break;
  597. }
  598. delete [] Buffer;
  599. delete [] VFList;
  600. return !_error->PendingError();
  601. }
  602. /*}}}*/
  603. // ShowDepends - Helper for printing out a dependency tree /*{{{*/
  604. static bool ShowDepends(CommandLine &CmdL, bool const RevDepends)
  605. {
  606. pkgCacheFile CacheFile;
  607. pkgCache *Cache = CacheFile.GetPkgCache();
  608. if (unlikely(Cache == NULL))
  609. return false;
  610. CacheSetHelperVirtuals helper(false);
  611. APT::VersionList verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  612. if (verset.empty() == true && helper.virtualPkgs.empty() == true)
  613. return _error->Error(_("No packages found"));
  614. std::vector<bool> Shown(Cache->Head().PackageCount);
  615. bool const Recurse = _config->FindB("APT::Cache::RecurseDepends", false);
  616. bool const Installed = _config->FindB("APT::Cache::Installed", false);
  617. bool const Important = _config->FindB("APT::Cache::Important", false);
  618. bool const ShowDepType = _config->FindB("APT::Cache::ShowDependencyType", RevDepends == false);
  619. bool const ShowVersion = _config->FindB("APT::Cache::ShowVersion", false);
  620. bool const ShowPreDepends = _config->FindB("APT::Cache::ShowPre-Depends", true);
  621. bool const ShowDepends = _config->FindB("APT::Cache::ShowDepends", true);
  622. bool const ShowRecommends = _config->FindB("APT::Cache::ShowRecommends", Important == false);
  623. bool const ShowSuggests = _config->FindB("APT::Cache::ShowSuggests", Important == false);
  624. bool const ShowReplaces = _config->FindB("APT::Cache::ShowReplaces", Important == false);
  625. bool const ShowConflicts = _config->FindB("APT::Cache::ShowConflicts", Important == false);
  626. bool const ShowBreaks = _config->FindB("APT::Cache::ShowBreaks", Important == false);
  627. bool const ShowEnhances = _config->FindB("APT::Cache::ShowEnhances", Important == false);
  628. bool const ShowOnlyFirstOr = _config->FindB("APT::Cache::ShowOnlyFirstOr", false);
  629. bool const ShowImplicit = _config->FindB("APT::Cache::ShowImplicit", false);
  630. while (verset.empty() != true)
  631. {
  632. pkgCache::VerIterator Ver = *verset.begin();
  633. verset.erase(verset.begin());
  634. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  635. Shown[Pkg->ID] = true;
  636. cout << Pkg.FullName(true) << endl;
  637. if (RevDepends == true)
  638. cout << "Reverse Depends:" << endl;
  639. for (pkgCache::DepIterator D = RevDepends ? Pkg.RevDependsList() : Ver.DependsList();
  640. D.end() == false; ++D)
  641. {
  642. switch (D->Type) {
  643. case pkgCache::Dep::PreDepends: if (!ShowPreDepends) continue; break;
  644. case pkgCache::Dep::Depends: if (!ShowDepends) continue; break;
  645. case pkgCache::Dep::Recommends: if (!ShowRecommends) continue; break;
  646. case pkgCache::Dep::Suggests: if (!ShowSuggests) continue; break;
  647. case pkgCache::Dep::Replaces: if (!ShowReplaces) continue; break;
  648. case pkgCache::Dep::Conflicts: if (!ShowConflicts) continue; break;
  649. case pkgCache::Dep::DpkgBreaks: if (!ShowBreaks) continue; break;
  650. case pkgCache::Dep::Enhances: if (!ShowEnhances) continue; break;
  651. }
  652. if (ShowImplicit == false && D.IsImplicit())
  653. continue;
  654. pkgCache::PkgIterator Trg = RevDepends ? D.ParentPkg() : D.TargetPkg();
  655. bool const showNoArch = RevDepends || (D->CompareOp & pkgCache::Dep::ArchSpecific) != pkgCache::Dep::ArchSpecific;
  656. if((Installed && Trg->CurrentVer != 0) || !Installed)
  657. {
  658. if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or && ShowOnlyFirstOr == false)
  659. cout << " |";
  660. else
  661. cout << " ";
  662. // Show the package
  663. if (ShowDepType == true)
  664. cout << D.DepType() << ": ";
  665. if (Trg->VersionList == 0)
  666. cout << "<" << Trg.FullName(showNoArch) << ">";
  667. else
  668. cout << Trg.FullName(showNoArch);
  669. if (ShowVersion == true && D->Version != 0)
  670. cout << " (" << pkgCache::CompTypeDeb(D->CompareOp) << ' ' << D.TargetVer() << ')';
  671. cout << std::endl;
  672. if (Recurse == true && Shown[Trg->ID] == false)
  673. {
  674. Shown[Trg->ID] = true;
  675. verset.insert(APT::VersionSet::FromPackage(CacheFile, Trg, APT::CacheSetHelper::CANDIDATE, helper));
  676. }
  677. }
  678. // Display all solutions
  679. std::unique_ptr<pkgCache::Version *[]> List(D.AllTargets());
  680. pkgPrioSortList(*Cache,List.get());
  681. for (pkgCache::Version **I = List.get(); *I != 0; I++)
  682. {
  683. pkgCache::VerIterator V(*Cache,*I);
  684. if (V != Cache->VerP + V.ParentPkg()->VersionList ||
  685. V->ParentPkg == D->Package)
  686. continue;
  687. cout << " " << V.ParentPkg().FullName(true) << endl;
  688. if (Recurse == true && Shown[V.ParentPkg()->ID] == false)
  689. {
  690. Shown[V.ParentPkg()->ID] = true;
  691. verset.insert(APT::VersionSet::FromPackage(CacheFile, V.ParentPkg(), APT::CacheSetHelper::CANDIDATE, helper));
  692. }
  693. }
  694. if (ShowOnlyFirstOr == true)
  695. while ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or) ++D;
  696. }
  697. }
  698. for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
  699. Pkg != helper.virtualPkgs.end(); ++Pkg)
  700. cout << '<' << Pkg.FullName(true) << '>' << endl;
  701. return true;
  702. }
  703. /*}}}*/
  704. // Depends - Print out a dependency tree /*{{{*/
  705. // ---------------------------------------------------------------------
  706. /* */
  707. static bool Depends(CommandLine &CmdL)
  708. {
  709. return ShowDepends(CmdL, false);
  710. }
  711. /*}}}*/
  712. // RDepends - Print out a reverse dependency tree /*{{{*/
  713. // ---------------------------------------------------------------------
  714. /* */
  715. static bool RDepends(CommandLine &CmdL)
  716. {
  717. return ShowDepends(CmdL, true);
  718. }
  719. /*}}}*/
  720. // xvcg - Generate a graph for xvcg /*{{{*/
  721. // ---------------------------------------------------------------------
  722. // Code contributed from Junichi Uekawa <dancer@debian.org> on 20 June 2002.
  723. static bool XVcg(CommandLine &CmdL)
  724. {
  725. pkgCacheFile CacheFile;
  726. pkgCache *Cache = CacheFile.GetPkgCache();
  727. if (unlikely(Cache == NULL))
  728. return false;
  729. bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
  730. /* Normal packages are boxes
  731. Pure Provides are triangles
  732. Mixed are diamonds
  733. rhomb are missing packages*/
  734. const char *Shapes[] = {"ellipse","triangle","box","rhomb"};
  735. /* Initialize the list of packages to show.
  736. 1 = To Show
  737. 2 = To Show no recurse
  738. 3 = Emitted no recurse
  739. 4 = Emitted
  740. 0 = None */
  741. enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
  742. enum TheFlags {ForceNR=(1<<0)};
  743. unsigned char *Show = new unsigned char[Cache->Head().PackageCount];
  744. unsigned char *Flags = new unsigned char[Cache->Head().PackageCount];
  745. unsigned char *ShapeMap = new unsigned char[Cache->Head().PackageCount];
  746. // Show everything if no arguments given
  747. if (CmdL.FileList[1] == 0)
  748. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  749. Show[I] = ToShow;
  750. else
  751. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  752. Show[I] = None;
  753. memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
  754. // Map the shapes
  755. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  756. {
  757. if (Pkg->VersionList == 0)
  758. {
  759. // Missing
  760. if (Pkg->ProvidesList == 0)
  761. ShapeMap[Pkg->ID] = 0;
  762. else
  763. ShapeMap[Pkg->ID] = 1;
  764. }
  765. else
  766. {
  767. // Normal
  768. if (Pkg->ProvidesList == 0)
  769. ShapeMap[Pkg->ID] = 2;
  770. else
  771. ShapeMap[Pkg->ID] = 3;
  772. }
  773. }
  774. // Load the list of packages from the command line into the show list
  775. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  776. std::list<APT::CacheSetHelper::PkgModifier> mods;
  777. mods.push_back(APT::CacheSetHelper::PkgModifier(0, ",", APT::PackageSet::Modifier::POSTFIX));
  778. mods.push_back(APT::CacheSetHelper::PkgModifier(1, "^", APT::PackageSet::Modifier::POSTFIX));
  779. std::map<unsigned short, APT::PackageSet> pkgsets =
  780. APT::PackageSet::GroupedFromCommandLine(CacheFile, CmdL.FileList + 1, mods, 0, helper);
  781. for (APT::PackageSet::const_iterator Pkg = pkgsets[0].begin();
  782. Pkg != pkgsets[0].end(); ++Pkg)
  783. Show[Pkg->ID] = ToShow;
  784. for (APT::PackageSet::const_iterator Pkg = pkgsets[1].begin();
  785. Pkg != pkgsets[1].end(); ++Pkg)
  786. {
  787. Show[Pkg->ID] = ToShow;
  788. Flags[Pkg->ID] |= ForceNR;
  789. }
  790. // Little header
  791. cout << "graph: { title: \"packages\"" << endl <<
  792. "xmax: 700 ymax: 700 x: 30 y: 30" << endl <<
  793. "layout_downfactor: 8" << endl;
  794. bool Act = true;
  795. while (Act == true)
  796. {
  797. Act = false;
  798. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  799. {
  800. // See we need to show this package
  801. if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
  802. continue;
  803. //printf ("node: { title: \"%s\" label: \"%s\" }\n", Pkg.Name(), Pkg.Name());
  804. // Colour as done
  805. if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
  806. {
  807. // Pure Provides and missing packages have no deps!
  808. if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
  809. Show[Pkg->ID] = Done;
  810. else
  811. Show[Pkg->ID] = DoneNR;
  812. }
  813. else
  814. Show[Pkg->ID] = Done;
  815. Act = true;
  816. // No deps to map out
  817. if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
  818. continue;
  819. pkgCache::VerIterator Ver = Pkg.VersionList();
  820. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; ++D)
  821. {
  822. // See if anything can meet this dep
  823. // Walk along the actual package providing versions
  824. bool Hit = false;
  825. pkgCache::PkgIterator DPkg = D.TargetPkg();
  826. for (pkgCache::VerIterator I = DPkg.VersionList();
  827. I.end() == false && Hit == false; ++I)
  828. {
  829. if (Cache->VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
  830. Hit = true;
  831. }
  832. // Follow all provides
  833. for (pkgCache::PrvIterator I = DPkg.ProvidesList();
  834. I.end() == false && Hit == false; ++I)
  835. {
  836. if (Cache->VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
  837. Hit = true;
  838. }
  839. // Only graph critical deps
  840. if (D.IsCritical() == true)
  841. {
  842. printf ("edge: { sourcename: \"%s\" targetname: \"%s\" class: 2 ",Pkg.FullName(true).c_str(), D.TargetPkg().FullName(true).c_str() );
  843. // Colour the node for recursion
  844. if (Show[D.TargetPkg()->ID] <= DoneNR)
  845. {
  846. /* If a conflicts does not meet anything in the database
  847. then show the relation but do not recurse */
  848. if (Hit == false && D.IsNegative() == true)
  849. {
  850. if (Show[D.TargetPkg()->ID] == None &&
  851. Show[D.TargetPkg()->ID] != ToShow)
  852. Show[D.TargetPkg()->ID] = ToShowNR;
  853. }
  854. else
  855. {
  856. if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
  857. Show[D.TargetPkg()->ID] = ToShowNR;
  858. else
  859. Show[D.TargetPkg()->ID] = ToShow;
  860. }
  861. }
  862. // Edge colour
  863. switch(D->Type)
  864. {
  865. case pkgCache::Dep::Conflicts:
  866. printf("label: \"conflicts\" color: lightgreen }\n");
  867. break;
  868. case pkgCache::Dep::DpkgBreaks:
  869. printf("label: \"breaks\" color: lightgreen }\n");
  870. break;
  871. case pkgCache::Dep::Obsoletes:
  872. printf("label: \"obsoletes\" color: lightgreen }\n");
  873. break;
  874. case pkgCache::Dep::PreDepends:
  875. printf("label: \"predepends\" color: blue }\n");
  876. break;
  877. default:
  878. printf("}\n");
  879. break;
  880. }
  881. }
  882. }
  883. }
  884. }
  885. /* Draw the box colours after the fact since we can not tell what colour
  886. they should be until everything is finished drawing */
  887. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  888. {
  889. if (Show[Pkg->ID] < DoneNR)
  890. continue;
  891. if (Show[Pkg->ID] == DoneNR)
  892. printf("node: { title: \"%s\" label: \"%s\" color: orange shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
  893. Shapes[ShapeMap[Pkg->ID]]);
  894. else
  895. printf("node: { title: \"%s\" label: \"%s\" shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
  896. Shapes[ShapeMap[Pkg->ID]]);
  897. }
  898. delete[] Show;
  899. delete[] Flags;
  900. delete[] ShapeMap;
  901. printf("}\n");
  902. return true;
  903. }
  904. /*}}}*/
  905. // Dotty - Generate a graph for Dotty /*{{{*/
  906. // ---------------------------------------------------------------------
  907. /* Dotty is the graphvis program for generating graphs. It is a fairly
  908. simple queuing algorithm that just writes dependencies and nodes.
  909. http://www.research.att.com/sw/tools/graphviz/ */
  910. static bool Dotty(CommandLine &CmdL)
  911. {
  912. pkgCacheFile CacheFile;
  913. pkgCache *Cache = CacheFile.GetPkgCache();
  914. if (unlikely(Cache == NULL))
  915. return false;
  916. bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
  917. /* Normal packages are boxes
  918. Pure Provides are triangles
  919. Mixed are diamonds
  920. Hexagons are missing packages*/
  921. const char *Shapes[] = {"hexagon","triangle","box","diamond"};
  922. /* Initialize the list of packages to show.
  923. 1 = To Show
  924. 2 = To Show no recurse
  925. 3 = Emitted no recurse
  926. 4 = Emitted
  927. 0 = None */
  928. enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
  929. enum TheFlags {ForceNR=(1<<0)};
  930. unsigned char *Show = new unsigned char[Cache->Head().PackageCount];
  931. unsigned char *Flags = new unsigned char[Cache->Head().PackageCount];
  932. unsigned char *ShapeMap = new unsigned char[Cache->Head().PackageCount];
  933. // Show everything if no arguments given
  934. if (CmdL.FileList[1] == 0)
  935. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  936. Show[I] = ToShow;
  937. else
  938. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  939. Show[I] = None;
  940. memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
  941. // Map the shapes
  942. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  943. {
  944. if (Pkg->VersionList == 0)
  945. {
  946. // Missing
  947. if (Pkg->ProvidesList == 0)
  948. ShapeMap[Pkg->ID] = 0;
  949. else
  950. ShapeMap[Pkg->ID] = 1;
  951. }
  952. else
  953. {
  954. // Normal
  955. if (Pkg->ProvidesList == 0)
  956. ShapeMap[Pkg->ID] = 2;
  957. else
  958. ShapeMap[Pkg->ID] = 3;
  959. }
  960. }
  961. // Load the list of packages from the command line into the show list
  962. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  963. std::list<APT::CacheSetHelper::PkgModifier> mods;
  964. mods.push_back(APT::CacheSetHelper::PkgModifier(0, ",", APT::PackageSet::Modifier::POSTFIX));
  965. mods.push_back(APT::CacheSetHelper::PkgModifier(1, "^", APT::PackageSet::Modifier::POSTFIX));
  966. std::map<unsigned short, APT::PackageSet> pkgsets =
  967. APT::PackageSet::GroupedFromCommandLine(CacheFile, CmdL.FileList + 1, mods, 0, helper);
  968. for (APT::PackageSet::const_iterator Pkg = pkgsets[0].begin();
  969. Pkg != pkgsets[0].end(); ++Pkg)
  970. Show[Pkg->ID] = ToShow;
  971. for (APT::PackageSet::const_iterator Pkg = pkgsets[1].begin();
  972. Pkg != pkgsets[1].end(); ++Pkg)
  973. {
  974. Show[Pkg->ID] = ToShow;
  975. Flags[Pkg->ID] |= ForceNR;
  976. }
  977. // Little header
  978. printf("digraph packages {\n");
  979. printf("concentrate=true;\n");
  980. printf("size=\"30,40\";\n");
  981. bool Act = true;
  982. while (Act == true)
  983. {
  984. Act = false;
  985. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  986. {
  987. // See we need to show this package
  988. if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
  989. continue;
  990. // Colour as done
  991. if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
  992. {
  993. // Pure Provides and missing packages have no deps!
  994. if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
  995. Show[Pkg->ID] = Done;
  996. else
  997. Show[Pkg->ID] = DoneNR;
  998. }
  999. else
  1000. Show[Pkg->ID] = Done;
  1001. Act = true;
  1002. // No deps to map out
  1003. if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
  1004. continue;
  1005. pkgCache::VerIterator Ver = Pkg.VersionList();
  1006. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; ++D)
  1007. {
  1008. // See if anything can meet this dep
  1009. // Walk along the actual package providing versions
  1010. bool Hit = false;
  1011. pkgCache::PkgIterator DPkg = D.TargetPkg();
  1012. for (pkgCache::VerIterator I = DPkg.VersionList();
  1013. I.end() == false && Hit == false; ++I)
  1014. {
  1015. if (Cache->VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
  1016. Hit = true;
  1017. }
  1018. // Follow all provides
  1019. for (pkgCache::PrvIterator I = DPkg.ProvidesList();
  1020. I.end() == false && Hit == false; ++I)
  1021. {
  1022. if (Cache->VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
  1023. Hit = true;
  1024. }
  1025. // Only graph critical deps
  1026. if (D.IsCritical() == true)
  1027. {
  1028. printf("\"%s\" -> \"%s\"",Pkg.FullName(true).c_str(),D.TargetPkg().FullName(true).c_str());
  1029. // Colour the node for recursion
  1030. if (Show[D.TargetPkg()->ID] <= DoneNR)
  1031. {
  1032. /* If a conflicts does not meet anything in the database
  1033. then show the relation but do not recurse */
  1034. if (Hit == false && D.IsNegative() == true)
  1035. {
  1036. if (Show[D.TargetPkg()->ID] == None &&
  1037. Show[D.TargetPkg()->ID] != ToShow)
  1038. Show[D.TargetPkg()->ID] = ToShowNR;
  1039. }
  1040. else
  1041. {
  1042. if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
  1043. Show[D.TargetPkg()->ID] = ToShowNR;
  1044. else
  1045. Show[D.TargetPkg()->ID] = ToShow;
  1046. }
  1047. }
  1048. // Edge colour
  1049. switch(D->Type)
  1050. {
  1051. case pkgCache::Dep::Conflicts:
  1052. case pkgCache::Dep::Obsoletes:
  1053. case pkgCache::Dep::DpkgBreaks:
  1054. printf("[color=springgreen];\n");
  1055. break;
  1056. case pkgCache::Dep::PreDepends:
  1057. printf("[color=blue];\n");
  1058. break;
  1059. default:
  1060. printf(";\n");
  1061. break;
  1062. }
  1063. }
  1064. }
  1065. }
  1066. }
  1067. /* Draw the box colours after the fact since we can not tell what colour
  1068. they should be until everything is finished drawing */
  1069. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  1070. {
  1071. if (Show[Pkg->ID] < DoneNR)
  1072. continue;
  1073. // Orange box for early recursion stoppage
  1074. if (Show[Pkg->ID] == DoneNR)
  1075. printf("\"%s\" [color=orange,shape=%s];\n",Pkg.FullName(true).c_str(),
  1076. Shapes[ShapeMap[Pkg->ID]]);
  1077. else
  1078. printf("\"%s\" [shape=%s];\n",Pkg.FullName(true).c_str(),
  1079. Shapes[ShapeMap[Pkg->ID]]);
  1080. }
  1081. printf("}\n");
  1082. delete[] Show;
  1083. delete[] Flags;
  1084. delete[] ShapeMap;
  1085. return true;
  1086. }
  1087. /*}}}*/
  1088. // DisplayRecord - Displays the complete record for the package /*{{{*/
  1089. // ---------------------------------------------------------------------
  1090. /* This displays the package record from the proper package index file.
  1091. It is not used by DumpAvail for performance reasons. */
  1092. static APT_PURE unsigned char const* skipDescriptionFields(unsigned char const * DescP)
  1093. {
  1094. char const * const TagName = "\nDescription";
  1095. size_t const TagLen = strlen(TagName);
  1096. while ((DescP = (unsigned char*)strchr((char*)DescP, '\n')) != NULL)
  1097. {
  1098. if (DescP[1] == ' ')
  1099. DescP += 2;
  1100. else if (strncmp((char*)DescP, TagName, TagLen) == 0)
  1101. DescP += TagLen;
  1102. else
  1103. break;
  1104. }
  1105. if (DescP != NULL)
  1106. ++DescP;
  1107. return DescP;
  1108. }
  1109. static bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V)
  1110. {
  1111. pkgCache *Cache = CacheFile.GetPkgCache();
  1112. if (unlikely(Cache == NULL))
  1113. return false;
  1114. // Find an appropriate file
  1115. pkgCache::VerFileIterator Vf = V.FileList();
  1116. for (; Vf.end() == false; ++Vf)
  1117. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
  1118. break;
  1119. if (Vf.end() == true)
  1120. Vf = V.FileList();
  1121. // Check and load the package list file
  1122. pkgCache::PkgFileIterator I = Vf.File();
  1123. if (I.IsOk() == false)
  1124. return _error->Error(_("Package file %s is out of sync."),I.FileName());
  1125. FileFd PkgF;
  1126. if (PkgF.Open(I.FileName(), FileFd::ReadOnly, FileFd::Extension) == false)
  1127. return false;
  1128. // Read the record (and ensure that it ends with a newline and NUL)
  1129. unsigned char *Buffer = new unsigned char[Cache->HeaderP->MaxVerFileSize+2];
  1130. Buffer[Vf->Size] = '\n';
  1131. Buffer[Vf->Size+1] = '\0';
  1132. if (PkgF.Seek(Vf->Offset) == false ||
  1133. PkgF.Read(Buffer,Vf->Size) == false)
  1134. {
  1135. delete [] Buffer;
  1136. return false;
  1137. }
  1138. // Get a pointer to start of Description field
  1139. const unsigned char *DescP = (unsigned char*)strstr((char*)Buffer, "\nDescription");
  1140. if (DescP != NULL)
  1141. ++DescP;
  1142. else
  1143. DescP = Buffer + Vf->Size;
  1144. // Write all but Description
  1145. size_t const length = DescP - Buffer;
  1146. if (length != 0 && FileFd::Write(STDOUT_FILENO, Buffer, length) == false)
  1147. {
  1148. delete [] Buffer;
  1149. return false;
  1150. }
  1151. // Show the right description
  1152. pkgRecords Recs(*Cache);
  1153. pkgCache::DescIterator Desc = V.TranslatedDescription();
  1154. if (Desc.end() == false)
  1155. {
  1156. pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
  1157. cout << "Description" << ( (strcmp(Desc.LanguageCode(),"") != 0) ? "-" : "" ) << Desc.LanguageCode() << ": " << P.LongDesc();
  1158. cout << std::endl << "Description-md5: " << Desc.md5() << std::endl;
  1159. // Find the first field after the description (if there is any)
  1160. DescP = skipDescriptionFields(DescP);
  1161. }
  1162. // else we have no translation, so we found a lonely Description-md5 -> don't skip it
  1163. // write the rest of the buffer, but skip mixed in Descriptions* fields
  1164. while (DescP != NULL)
  1165. {
  1166. const unsigned char * const Start = DescP;
  1167. const unsigned char *End = (unsigned char*)strstr((char*)DescP, "\nDescription");
  1168. if (End == NULL)
  1169. {
  1170. End = &Buffer[Vf->Size];
  1171. DescP = NULL;
  1172. }
  1173. else
  1174. {
  1175. ++End; // get the newline into the output
  1176. DescP = skipDescriptionFields(End + strlen("Description"));
  1177. }
  1178. size_t const length = End - Start;
  1179. if (length != 0 && FileFd::Write(STDOUT_FILENO, Start, length) == false)
  1180. {
  1181. delete [] Buffer;
  1182. return false;
  1183. }
  1184. }
  1185. // write a final newline after the last field
  1186. cout<<endl;
  1187. delete [] Buffer;
  1188. return true;
  1189. }
  1190. /*}}}*/
  1191. struct ExDescFile
  1192. {
  1193. pkgCache::DescFile *Df;
  1194. pkgCache::VerIterator V;
  1195. map_id_t ID;
  1196. };
  1197. // Search - Perform a search /*{{{*/
  1198. // ---------------------------------------------------------------------
  1199. /* This searches the package names and package descriptions for a pattern */
  1200. static bool Search(CommandLine &CmdL)
  1201. {
  1202. bool const ShowFull = _config->FindB("APT::Cache::ShowFull",false);
  1203. bool const NamesOnly = _config->FindB("APT::Cache::NamesOnly",false);
  1204. unsigned int const NumPatterns = CmdL.FileSize() -1;
  1205. pkgCacheFile CacheFile;
  1206. pkgCache *Cache = CacheFile.GetPkgCache();
  1207. pkgDepCache::Policy *Plcy = CacheFile.GetPolicy();
  1208. if (unlikely(Cache == NULL || Plcy == NULL))
  1209. return false;
  1210. // Make sure there is at least one argument
  1211. if (NumPatterns < 1)
  1212. return _error->Error(_("You must give at least one search pattern"));
  1213. // Compile the regex pattern
  1214. regex_t *Patterns = new regex_t[NumPatterns];
  1215. memset(Patterns,0,sizeof(*Patterns)*NumPatterns);
  1216. for (unsigned I = 0; I != NumPatterns; I++)
  1217. {
  1218. if (regcomp(&Patterns[I],CmdL.FileList[I+1],REG_EXTENDED | REG_ICASE |
  1219. REG_NOSUB) != 0)
  1220. {
  1221. for (; I != 0; I--)
  1222. regfree(&Patterns[I]);
  1223. return _error->Error("Regex compilation error");
  1224. }
  1225. }
  1226. if (_error->PendingError() == true)
  1227. {
  1228. for (unsigned I = 0; I != NumPatterns; I++)
  1229. regfree(&Patterns[I]);
  1230. return false;
  1231. }
  1232. size_t const descCount = Cache->HeaderP->GroupCount + 1;
  1233. ExDescFile *DFList = new ExDescFile[descCount];
  1234. memset(DFList,0,sizeof(*DFList) * descCount);
  1235. bool *PatternMatch = new bool[descCount * NumPatterns];
  1236. memset(PatternMatch,false,sizeof(*PatternMatch) * descCount * NumPatterns);
  1237. // Map versions that we want to write out onto the VerList array.
  1238. for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() == false; ++G)
  1239. {
  1240. size_t const PatternOffset = G->ID * NumPatterns;
  1241. size_t unmatched = 0, matched = 0;
  1242. for (unsigned I = 0; I < NumPatterns; ++I)
  1243. {
  1244. if (PatternMatch[PatternOffset + I] == true)
  1245. ++matched;
  1246. else if (regexec(&Patterns[I],G.Name(),0,0,0) == 0)
  1247. PatternMatch[PatternOffset + I] = true;
  1248. else
  1249. ++unmatched;
  1250. }
  1251. // already dealt with this package?
  1252. if (matched == NumPatterns)
  1253. continue;
  1254. // Doing names only, drop any that don't match..
  1255. if (NamesOnly == true && unmatched == NumPatterns)
  1256. continue;
  1257. // Find the proper version to use
  1258. pkgCache::PkgIterator P = G.FindPreferredPkg();
  1259. if (P.end() == true)
  1260. continue;
  1261. pkgCache::VerIterator V = Plcy->GetCandidateVer(P);
  1262. if (V.end() == false)
  1263. {
  1264. pkgCache::DescIterator const D = V.TranslatedDescription();
  1265. //FIXME: packages without a description can't be found
  1266. if (D.end() == true)
  1267. continue;
  1268. DFList[G->ID].Df = D.FileList();
  1269. DFList[G->ID].V = V;
  1270. DFList[G->ID].ID = G->ID;
  1271. }
  1272. if (unmatched == NumPatterns)
  1273. continue;
  1274. // Include all the packages that provide matching names too
  1275. for (pkgCache::PrvIterator Prv = P.ProvidesList() ; Prv.end() == false; ++Prv)
  1276. {
  1277. pkgCache::VerIterator V = Plcy->GetCandidateVer(Prv.OwnerPkg());
  1278. if (V.end() == true)
  1279. continue;
  1280. unsigned long id = Prv.OwnerPkg().Group()->ID;
  1281. pkgCache::DescIterator const D = V.TranslatedDescription();
  1282. //FIXME: packages without a description can't be found
  1283. if (D.end() == true)
  1284. continue;
  1285. DFList[id].Df = D.FileList();
  1286. DFList[id].V = V;
  1287. DFList[id].ID = id;
  1288. size_t const PrvPatternOffset = id * NumPatterns;
  1289. for (unsigned I = 0; I < NumPatterns; ++I)
  1290. PatternMatch[PrvPatternOffset + I] |= PatternMatch[PatternOffset + I];
  1291. }
  1292. }
  1293. LocalitySort(&DFList->Df,Cache->HeaderP->GroupCount,sizeof(*DFList));
  1294. // Create the text record parser
  1295. pkgRecords Recs(*Cache);
  1296. // Iterate over all the version records and check them
  1297. for (ExDescFile *J = DFList; J->Df != 0; ++J)
  1298. {
  1299. pkgRecords::Parser &P = Recs.Lookup(pkgCache::DescFileIterator(*Cache,J->Df));
  1300. size_t const PatternOffset = J->ID * NumPatterns;
  1301. if (NamesOnly == false)
  1302. {
  1303. string const LongDesc = P.LongDesc();
  1304. for (unsigned I = 0; I < NumPatterns; ++I)
  1305. {
  1306. if (PatternMatch[PatternOffset + I] == true)
  1307. continue;
  1308. else if (regexec(&Patterns[I],LongDesc.c_str(),0,0,0) == 0)
  1309. PatternMatch[PatternOffset + I] = true;
  1310. }
  1311. }
  1312. bool matchedAll = true;
  1313. for (unsigned I = 0; I < NumPatterns; ++I)
  1314. if (PatternMatch[PatternOffset + I] == false)
  1315. {
  1316. matchedAll = false;
  1317. break;
  1318. }
  1319. if (matchedAll == true)
  1320. {
  1321. if (ShowFull == true)
  1322. DisplayRecord(CacheFile, J->V);
  1323. else
  1324. printf("%s - %s\n",P.Name().c_str(),P.ShortDesc().c_str());
  1325. }
  1326. }
  1327. delete [] DFList;
  1328. delete [] PatternMatch;
  1329. for (unsigned I = 0; I != NumPatterns; I++)
  1330. regfree(&Patterns[I]);
  1331. delete [] Patterns;
  1332. if (ferror(stdout))
  1333. return _error->Error("Write to stdout failed");
  1334. return true;
  1335. }
  1336. /*}}}*/
  1337. /* ShowAuto - show automatically installed packages (sorted) {{{*/
  1338. static bool ShowAuto(CommandLine &)
  1339. {
  1340. pkgCacheFile CacheFile;
  1341. pkgCache *Cache = CacheFile.GetPkgCache();
  1342. pkgDepCache *DepCache = CacheFile.GetDepCache();
  1343. if (unlikely(Cache == NULL || DepCache == NULL))
  1344. return false;
  1345. std::vector<string> packages;
  1346. packages.reserve(Cache->HeaderP->PackageCount / 3);
  1347. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  1348. if ((*DepCache)[P].Flags & pkgCache::Flag::Auto)
  1349. packages.push_back(P.Name());
  1350. std::sort(packages.begin(), packages.end());
  1351. for (vector<string>::iterator I = packages.begin(); I != packages.end(); ++I)
  1352. cout << *I << "\n";
  1353. _error->Notice(_("This command is deprecated. Please use 'apt-mark showauto' instead."));
  1354. return true;
  1355. }
  1356. /*}}}*/
  1357. // ShowPackage - Dump the package record to the screen /*{{{*/
  1358. // ---------------------------------------------------------------------
  1359. /* */
  1360. static bool ShowPackage(CommandLine &CmdL)
  1361. {
  1362. pkgCacheFile CacheFile;
  1363. CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
  1364. APT::CacheSetHelper::VerSelector const select = _config->FindB("APT::Cache::AllVersions", true) ?
  1365. APT::CacheSetHelper::ALL : APT::CacheSetHelper::CANDIDATE;
  1366. APT::VersionList const verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, select, helper);
  1367. for (APT::VersionList::const_iterator Ver = verset.begin(); Ver != verset.end(); ++Ver)
  1368. if (DisplayRecord(CacheFile, Ver) == false)
  1369. return false;
  1370. if (verset.empty() == true)
  1371. {
  1372. if (helper.virtualPkgs.empty() == true)
  1373. return _error->Error(_("No packages found"));
  1374. else
  1375. _error->Notice(_("No packages found"));
  1376. }
  1377. return true;
  1378. }
  1379. /*}}}*/
  1380. // ShowPkgNames - Show package names /*{{{*/
  1381. // ---------------------------------------------------------------------
  1382. /* This does a prefix match on the first argument */
  1383. static bool ShowPkgNames(CommandLine &CmdL)
  1384. {
  1385. pkgCacheFile CacheFile;
  1386. if (unlikely(CacheFile.BuildCaches(NULL, false) == false))
  1387. return false;
  1388. pkgCache::GrpIterator I = CacheFile.GetPkgCache()->GrpBegin();
  1389. bool const All = _config->FindB("APT::Cache::AllNames","false");
  1390. if (CmdL.FileList[1] != 0)
  1391. {
  1392. for (;I.end() != true; ++I)
  1393. {
  1394. if (All == false && I->FirstPackage == 0)
  1395. continue;
  1396. if (I.FindPkg("any")->VersionList == 0)
  1397. continue;
  1398. if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
  1399. cout << I.Name() << endl;
  1400. }
  1401. return true;
  1402. }
  1403. // Show all pkgs
  1404. for (;I.end() != true; ++I)
  1405. {
  1406. if (All == false && I->FirstPackage == 0)
  1407. continue;
  1408. if (I.FindPkg("any")->VersionList == 0)
  1409. continue;
  1410. cout << I.Name() << endl;
  1411. }
  1412. return true;
  1413. }
  1414. /*}}}*/
  1415. // ShowSrcPackage - Show source package records /*{{{*/
  1416. // ---------------------------------------------------------------------
  1417. /* */
  1418. static bool ShowSrcPackage(CommandLine &CmdL)
  1419. {
  1420. pkgCacheFile CacheFile;
  1421. pkgSourceList *List = CacheFile.GetSourceList();
  1422. if (unlikely(List == NULL))
  1423. return false;
  1424. // Create the text record parsers
  1425. pkgSrcRecords SrcRecs(*List);
  1426. if (_error->PendingError() == true)
  1427. return false;
  1428. unsigned found = 0;
  1429. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1430. {
  1431. SrcRecs.Restart();
  1432. pkgSrcRecords::Parser *Parse;
  1433. unsigned found_this = 0;
  1434. while ((Parse = SrcRecs.Find(*I,false)) != 0) {
  1435. cout << Parse->AsStr() << endl;;
  1436. found++;
  1437. found_this++;
  1438. }
  1439. if (found_this == 0) {
  1440. _error->Warning(_("Unable to locate package %s"),*I);
  1441. continue;
  1442. }
  1443. }
  1444. if (found == 0)
  1445. _error->Notice(_("No packages found"));
  1446. return true;
  1447. }
  1448. /*}}}*/
  1449. // Policy - Show the results of the preferences file /*{{{*/
  1450. // ---------------------------------------------------------------------
  1451. /* */
  1452. static bool Policy(CommandLine &CmdL)
  1453. {
  1454. pkgCacheFile CacheFile;
  1455. pkgCache *Cache = CacheFile.GetPkgCache();
  1456. pkgPolicy *Plcy = CacheFile.GetPolicy();
  1457. pkgSourceList *SrcList = CacheFile.GetSourceList();
  1458. if (unlikely(Cache == NULL || Plcy == NULL || SrcList == NULL))
  1459. return false;
  1460. /* Should the MultiArchKiller be run to see which pseudo packages for an
  1461. arch all package are currently installed? Activating it gives a speed
  1462. penality for no real gain beside enhanced debugging, so in general no. */
  1463. if (_config->FindB("APT::Cache::Policy::DepCache", false) == true)
  1464. CacheFile.GetDepCache();
  1465. // Print out all of the package files
  1466. if (CmdL.FileList[1] == 0)
  1467. {
  1468. cout << _("Package files:") << endl;
  1469. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  1470. {
  1471. if (F.Flagged(pkgCache::Flag::NoPackages))
  1472. continue;
  1473. // Locate the associated index files so we can derive a description
  1474. pkgIndexFile *Indx;
  1475. if (SrcList->FindIndex(F,Indx) == false &&
  1476. _system->FindIndex(F,Indx) == false)
  1477. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  1478. printf("%4i %s\n",
  1479. Plcy->GetPriority(F),Indx->Describe(true).c_str());
  1480. // Print the reference information for the package
  1481. string Str = F.RelStr();
  1482. if (Str.empty() == false)
  1483. printf(" release %s\n",F.RelStr().c_str());
  1484. if (F.Site() != 0 && F.Site()[0] != 0)
  1485. printf(" origin %s\n",F.Site());
  1486. }
  1487. // Show any packages have explicit pins
  1488. cout << _("Pinned packages:") << endl;
  1489. pkgCache::PkgIterator I = Cache->PkgBegin();
  1490. for (;I.end() != true; ++I)
  1491. {
  1492. // Old code for debugging
  1493. if (_config->FindI("APT::Policy", 1) < 1) {
  1494. if (Plcy->GetPriority(I) == 0)
  1495. continue;
  1496. // Print the package name and the version we are forcing to
  1497. cout << " " << I.FullName(true) << " -> ";
  1498. pkgCache::VerIterator V = Plcy->GetMatch(I);
  1499. if (V.end() == true)
  1500. cout << _("(not found)") << endl;
  1501. else
  1502. cout << V.VerStr() << endl;
  1503. continue;
  1504. }
  1505. // New code
  1506. for (pkgCache::VerIterator V = I.VersionList(); !V.end(); V++) {
  1507. auto Prio = Plcy->GetPriority(V, false);
  1508. if (Prio == 0)
  1509. continue;
  1510. cout << " ";
  1511. // Print the package name and the version we are forcing to
  1512. ioprintf(cout, _("%s -> %s with priority %d\n"), I.FullName(true).c_str(), V.VerStr(), Prio);
  1513. }
  1514. }
  1515. return true;
  1516. }
  1517. char const * const msgInstalled = _(" Installed: ");
  1518. char const * const msgCandidate = _(" Candidate: ");
  1519. short const InstalledLessCandidate =
  1520. mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
  1521. short const deepInstalled =
  1522. (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
  1523. short const deepCandidate =
  1524. (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
  1525. // Print out detailed information for each package
  1526. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  1527. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  1528. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  1529. {
  1530. cout << Pkg.FullName(true) << ":" << endl;
  1531. // Installed version
  1532. cout << msgInstalled << OutputInDepth(deepInstalled, " ");
  1533. if (Pkg->CurrentVer == 0)
  1534. cout << _("(none)") << endl;
  1535. else
  1536. cout << Pkg.CurrentVer().VerStr() << endl;
  1537. // Candidate Version
  1538. cout << msgCandidate << OutputInDepth(deepCandidate, " ");
  1539. pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
  1540. if (V.end() == true)
  1541. cout << _("(none)") << endl;
  1542. else
  1543. cout << V.VerStr() << endl;
  1544. // Pinned version
  1545. if (_config->FindI("APT::Policy", 1) < 1 && Plcy->GetPriority(Pkg) != 0)
  1546. {
  1547. cout << _(" Package pin: ");
  1548. V = Plcy->GetMatch(Pkg);
  1549. if (V.end() == true)
  1550. cout << _("(not found)") << endl;
  1551. else
  1552. cout << V.VerStr() << endl;
  1553. }
  1554. // Show the priority tables
  1555. cout << _(" Version table:") << endl;
  1556. for (V = Pkg.VersionList(); V.end() == false; ++V)
  1557. {
  1558. if (Pkg.CurrentVer() == V)
  1559. cout << " *** " << V.VerStr();
  1560. else
  1561. cout << " " << V.VerStr();
  1562. if (_config->FindI("APT::Policy", 1) < 1)
  1563. cout << " " << Plcy->GetPriority(Pkg) << endl;
  1564. else
  1565. cout << " " << Plcy->GetPriority(V) << endl;
  1566. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  1567. {
  1568. // Locate the associated index files so we can derive a description
  1569. pkgIndexFile *Indx;
  1570. if (SrcList->FindIndex(VF.File(),Indx) == false &&
  1571. _system->FindIndex(VF.File(),Indx) == false)
  1572. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  1573. printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
  1574. Indx->Describe(true).c_str());
  1575. }
  1576. }
  1577. }
  1578. return true;
  1579. }
  1580. /*}}}*/
  1581. // Madison - Look a bit like katie's madison /*{{{*/
  1582. // ---------------------------------------------------------------------
  1583. /* */
  1584. static bool Madison(CommandLine &CmdL)
  1585. {
  1586. pkgCacheFile CacheFile;
  1587. pkgSourceList *SrcList = CacheFile.GetSourceList();
  1588. if (SrcList == 0)
  1589. return false;
  1590. // Create the src text record parsers and ignore errors about missing
  1591. // deb-src lines that are generated from pkgSrcRecords::pkgSrcRecords
  1592. pkgSrcRecords SrcRecs(*SrcList);
  1593. if (_error->PendingError() == true)
  1594. _error->Discard();
  1595. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  1596. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1597. {
  1598. _error->PushToStack();
  1599. APT::PackageList pkgset = APT::PackageList::FromString(CacheFile, *I, helper);
  1600. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  1601. {
  1602. for (pkgCache::VerIterator V = Pkg.VersionList(); V.end() == false; ++V)
  1603. {
  1604. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  1605. {
  1606. // This might be nice, but wouldn't uniquely identify the source -mdz
  1607. // if (VF.File().Archive() != 0)
  1608. // {
  1609. // cout << setw(10) << Pkg.Name() << " | " << setw(10) << V.VerStr() << " | "
  1610. // << VF.File().Archive() << endl;
  1611. // }
  1612. // Locate the associated index files so we can derive a description
  1613. for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); ++S)
  1614. {
  1615. vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
  1616. for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
  1617. IF != Indexes->end(); ++IF)
  1618. {
  1619. if ((*IF)->FindInCache(*(VF.File().Cache())) == VF.File())
  1620. {
  1621. cout << setw(10) << Pkg.FullName(true) << " | " << setw(10) << V.VerStr() << " | "
  1622. << (*IF)->Describe(true) << endl;
  1623. }
  1624. }
  1625. }
  1626. }
  1627. }
  1628. }
  1629. SrcRecs.Restart();
  1630. pkgSrcRecords::Parser *SrcParser;
  1631. bool foundSomething = false;
  1632. while ((SrcParser = SrcRecs.Find(*I, false)) != 0)
  1633. {
  1634. foundSomething = true;
  1635. // Maybe support Release info here too eventually
  1636. cout << setw(10) << SrcParser->Package() << " | "
  1637. << setw(10) << SrcParser->Version() << " | "
  1638. << SrcParser->Index().Describe(true) << endl;
  1639. }
  1640. if (foundSomething == true)
  1641. _error->RevertToStack();
  1642. else
  1643. _error->MergeWithStack();
  1644. }
  1645. return true;
  1646. }
  1647. /*}}}*/
  1648. // GenCaches - Call the main cache generator /*{{{*/
  1649. // ---------------------------------------------------------------------
  1650. /* */
  1651. static bool GenCaches(CommandLine &)
  1652. {
  1653. OpTextProgress Progress(*_config);
  1654. pkgCacheFile CacheFile;
  1655. return CacheFile.BuildCaches(&Progress, true);
  1656. }
  1657. /*}}}*/
  1658. // ShowHelp - Show a help screen /*{{{*/
  1659. // ---------------------------------------------------------------------
  1660. /* */
  1661. static bool ShowHelp(CommandLine &)
  1662. {
  1663. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  1664. if (_config->FindB("version") == true)
  1665. return true;
  1666. cout <<
  1667. _("Usage: apt-cache [options] command\n"
  1668. " apt-cache [options] showpkg pkg1 [pkg2 ...]\n"
  1669. " apt-cache [options] showsrc pkg1 [pkg2 ...]\n"
  1670. "\n"
  1671. "apt-cache is a low-level tool used to query information\n"
  1672. "from APT's binary cache files\n"
  1673. "\n"
  1674. "Commands:\n"
  1675. " gencaches - Build both the package and source cache\n"
  1676. " showpkg - Show some general information for a single package\n"
  1677. " showsrc - Show source records\n"
  1678. " stats - Show some basic statistics\n"
  1679. " dump - Show the entire file in a terse form\n"
  1680. " dumpavail - Print an available file to stdout\n"
  1681. " unmet - Show unmet dependencies\n"
  1682. " search - Search the package list for a regex pattern\n"
  1683. " show - Show a readable record for the package\n"
  1684. " depends - Show raw dependency information for a package\n"
  1685. " rdepends - Show reverse dependency information for a package\n"
  1686. " pkgnames - List the names of all packages in the system\n"
  1687. " dotty - Generate package graphs for GraphViz\n"
  1688. " xvcg - Generate package graphs for xvcg\n"
  1689. " policy - Show policy settings\n"
  1690. "\n"
  1691. "Options:\n"
  1692. " -h This help text.\n"
  1693. " -p=? The package cache.\n"
  1694. " -s=? The source cache.\n"
  1695. " -q Disable progress indicator.\n"
  1696. " -i Show only important deps for the unmet command.\n"
  1697. " -c=? Read this configuration file\n"
  1698. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  1699. "See the apt-cache(8) and apt.conf(5) manual pages for more information.\n");
  1700. return true;
  1701. }
  1702. /*}}}*/
  1703. int main(int argc,const char *argv[]) /*{{{*/
  1704. {
  1705. CommandLine::Dispatch Cmds[] = {{"help",&ShowHelp},
  1706. {"gencaches",&GenCaches},
  1707. {"showsrc",&ShowSrcPackage},
  1708. {"showpkg",&DumpPackage},
  1709. {"stats",&Stats},
  1710. {"dump",&Dump},
  1711. {"dumpavail",&DumpAvail},
  1712. {"unmet",&UnMet},
  1713. {"search",&Search},
  1714. {"depends",&Depends},
  1715. {"rdepends",&RDepends},
  1716. {"dotty",&Dotty},
  1717. {"xvcg",&XVcg},
  1718. {"show",&ShowPackage},
  1719. {"pkgnames",&ShowPkgNames},
  1720. {"showauto",&ShowAuto},
  1721. {"policy",&Policy},
  1722. {"madison",&Madison},
  1723. {0,0}};
  1724. std::vector<CommandLine::Args> Args = getCommandArgs("apt-cache", CommandLine::GetCommand(Cmds, argc, argv));
  1725. // Set up gettext support
  1726. setlocale(LC_ALL,"");
  1727. textdomain(PACKAGE);
  1728. // Parse the command line and initialize the package library
  1729. CommandLine CmdL;
  1730. ParseCommandLine(CmdL, Cmds, Args.data(), &_config, &_system, argc, argv, ShowHelp);
  1731. InitOutput();
  1732. if (_config->Exists("APT::Cache::Generate") == true)
  1733. _config->Set("pkgCacheFile::Generate", _config->FindB("APT::Cache::Generate", true));
  1734. // Match the operation
  1735. CmdL.DispatchArg(Cmds);
  1736. // Print any errors or warnings found during parsing
  1737. bool const Errors = _error->PendingError();
  1738. if (_config->FindI("quiet",0) > 0)
  1739. _error->DumpErrors();
  1740. else
  1741. _error->DumpErrors(GlobalError::DEBUG);
  1742. return Errors == true ? 100 : 0;
  1743. }
  1744. /*}}}*/