apt-cache.cc 53 KB

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