apt-cache.cc 63 KB

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