apt-cache.cc 50 KB

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