apt-cache.cc 63 KB

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