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