apt-cache.cc 61 KB

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