apt-cache.cc 59 KB

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