apt-cache.cc 41 KB

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