apt-cache.cc 62 KB

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