apt-cache.cc 62 KB

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