apt-cache.cc 56 KB

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