apt-cache.cc 41 KB

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