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