apt-cache.cc 58 KB

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