apt-cache.cc 59 KB

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