apt-cache.cc 58 KB

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