apt-cache.cc 62 KB

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