apt-cache.cc 63 KB

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