apt-cache.cc 60 KB

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