apt-cache.cc 63 KB

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