apt-cache.cc 59 KB

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