apt-cache.cc 41 KB

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