apt-cache.cc 60 KB

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