You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

1601 lines
53 KiB

  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: apt-cache.cc,v 1.72 2004/04/30 04:34:03 mdz Exp $
  4. /* ######################################################################
  5. apt-cache - Manages the cache files
  6. apt-cache provides some functions fo manipulating the cache files.
  7. It uses the command line interface common to all the APT tools.
  8. Returns 100 on failure, 0 on success.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include<config.h>
  13. #include <apt-pkg/algorithms.h>
  14. #include <apt-pkg/cachefile.h>
  15. #include <apt-pkg/cacheset.h>
  16. #include <apt-pkg/cmndline.h>
  17. #include <apt-pkg/error.h>
  18. #include <apt-pkg/fileutl.h>
  19. #include <apt-pkg/indexfile.h>
  20. #include <apt-pkg/init.h>
  21. #include <apt-pkg/metaindex.h>
  22. #include <apt-pkg/pkgrecords.h>
  23. #include <apt-pkg/pkgsystem.h>
  24. #include <apt-pkg/policy.h>
  25. #include <apt-pkg/progress.h>
  26. #include <apt-pkg/sourcelist.h>
  27. #include <apt-pkg/sptr.h>
  28. #include <apt-pkg/srcrecords.h>
  29. #include <apt-pkg/strutl.h>
  30. #include <apt-pkg/tagfile.h>
  31. #include <apt-pkg/version.h>
  32. #include <apt-pkg/cacheiterators.h>
  33. #include <apt-pkg/configuration.h>
  34. #include <apt-pkg/depcache.h>
  35. #include <apt-pkg/macros.h>
  36. #include <apt-pkg/mmap.h>
  37. #include <apt-pkg/pkgcache.h>
  38. #include <apt-private/private-cacheset.h>
  39. #include <apt-private/private-cmndline.h>
  40. #include <apt-private/private-show.h>
  41. #include <apt-private/private-search.h>
  42. #include <apt-private/private-main.h>
  43. #include <regex.h>
  44. #include <stddef.h>
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <unistd.h>
  48. #include <algorithm>
  49. #include <cstring>
  50. #include <iomanip>
  51. #include <iostream>
  52. #include <list>
  53. #include <map>
  54. #include <set>
  55. #include <string>
  56. #include <vector>
  57. #include <apti18n.h>
  58. /*}}}*/
  59. using namespace std;
  60. // UnMet - Show unmet dependencies /*{{{*/
  61. // ---------------------------------------------------------------------
  62. /* */
  63. static bool ShowUnMet(pkgCache::VerIterator const &V, bool const Important)
  64. {
  65. bool Header = false;
  66. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false;)
  67. {
  68. // Collect or groups
  69. pkgCache::DepIterator Start;
  70. pkgCache::DepIterator End;
  71. D.GlobOr(Start,End);
  72. // Important deps only
  73. if (Important == true)
  74. if (End->Type != pkgCache::Dep::PreDepends &&
  75. End->Type != pkgCache::Dep::Depends)
  76. continue;
  77. // Skip conflicts and replaces
  78. if (End.IsNegative() == true || End->Type == pkgCache::Dep::Replaces)
  79. continue;
  80. // Verify the or group
  81. bool OK = false;
  82. pkgCache::DepIterator RealStart = Start;
  83. do
  84. {
  85. // See if this dep is Ok
  86. pkgCache::Version **VList = Start.AllTargets();
  87. if (*VList != 0)
  88. {
  89. OK = true;
  90. delete [] VList;
  91. break;
  92. }
  93. delete [] VList;
  94. if (Start == End)
  95. break;
  96. ++Start;
  97. }
  98. while (1);
  99. // The group is OK
  100. if (OK == true)
  101. continue;
  102. // Oops, it failed..
  103. if (Header == false)
  104. ioprintf(cout,_("Package %s version %s has an unmet dep:\n"),
  105. V.ParentPkg().FullName(true).c_str(),V.VerStr());
  106. Header = true;
  107. // Print out the dep type
  108. cout << " " << End.DepType() << ": ";
  109. // Show the group
  110. Start = RealStart;
  111. do
  112. {
  113. cout << Start.TargetPkg().FullName(true);
  114. if (Start.TargetVer() != 0)
  115. cout << " (" << Start.CompType() << " " << Start.TargetVer() <<
  116. ")";
  117. if (Start == End)
  118. break;
  119. cout << " | ";
  120. ++Start;
  121. }
  122. while (1);
  123. cout << endl;
  124. }
  125. return true;
  126. }
  127. static bool UnMet(CommandLine &CmdL)
  128. {
  129. bool const Important = _config->FindB("APT::Cache::Important",false);
  130. pkgCacheFile CacheFile;
  131. if (unlikely(CacheFile.GetPkgCache() == NULL))
  132. return false;
  133. if (CmdL.FileSize() <= 1)
  134. {
  135. for (pkgCache::PkgIterator P = CacheFile.GetPkgCache()->PkgBegin(); P.end() == false; ++P)
  136. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
  137. if (ShowUnMet(V, Important) == false)
  138. return false;
  139. }
  140. else
  141. {
  142. CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
  143. APT::VersionList verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1,
  144. APT::CacheSetHelper::CANDIDATE, helper);
  145. for (APT::VersionList::iterator V = verset.begin(); V != verset.end(); ++V)
  146. if (ShowUnMet(V, Important) == false)
  147. return false;
  148. }
  149. return true;
  150. }
  151. /*}}}*/
  152. // DumpPackage - Show a dump of a package record /*{{{*/
  153. // ---------------------------------------------------------------------
  154. /* */
  155. static bool DumpPackage(CommandLine &CmdL)
  156. {
  157. pkgCacheFile CacheFile;
  158. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  159. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  160. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  161. {
  162. cout << "Package: " << Pkg.FullName(true) << endl;
  163. cout << "Versions: " << endl;
  164. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; ++Cur)
  165. {
  166. cout << Cur.VerStr();
  167. for (pkgCache::VerFileIterator Vf = Cur.FileList(); Vf.end() == false; ++Vf)
  168. cout << " (" << Vf.File().FileName() << ")";
  169. cout << endl;
  170. for (pkgCache::DescIterator D = Cur.DescriptionList(); D.end() == false; ++D)
  171. {
  172. cout << " Description Language: " << D.LanguageCode() << endl
  173. << " File: " << D.FileList().File().FileName() << endl
  174. << " MD5: " << D.md5() << endl;
  175. }
  176. cout << endl;
  177. }
  178. cout << endl;
  179. cout << "Reverse Depends: " << endl;
  180. for (pkgCache::DepIterator D = Pkg.RevDependsList(); D.end() != true; ++D)
  181. {
  182. cout << " " << D.ParentPkg().FullName(true) << ',' << D.TargetPkg().FullName(true);
  183. if (D->Version != 0)
  184. cout << ' ' << DeNull(D.TargetVer()) << endl;
  185. else
  186. cout << endl;
  187. }
  188. cout << "Dependencies: " << endl;
  189. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; ++Cur)
  190. {
  191. cout << Cur.VerStr() << " - ";
  192. for (pkgCache::DepIterator Dep = Cur.DependsList(); Dep.end() != true; ++Dep)
  193. cout << Dep.TargetPkg().FullName(true) << " (" << (int)Dep->CompareOp << " " << DeNull(Dep.TargetVer()) << ") ";
  194. cout << endl;
  195. }
  196. cout << "Provides: " << endl;
  197. for (pkgCache::VerIterator Cur = Pkg.VersionList(); Cur.end() != true; ++Cur)
  198. {
  199. cout << Cur.VerStr() << " - ";
  200. for (pkgCache::PrvIterator Prv = Cur.ProvidesList(); Prv.end() != true; ++Prv)
  201. cout << Prv.ParentPkg().FullName(true) << " (= " << (Prv->ProvideVersion == 0 ? "" : Prv.ProvideVersion()) << ") ";
  202. cout << endl;
  203. }
  204. cout << "Reverse Provides: " << endl;
  205. for (pkgCache::PrvIterator Prv = Pkg.ProvidesList(); Prv.end() != true; ++Prv)
  206. cout << Prv.OwnerPkg().FullName(true) << " " << Prv.OwnerVer().VerStr() << " (= " << (Prv->ProvideVersion == 0 ? "" : Prv.ProvideVersion()) << ")"<< endl;
  207. }
  208. return true;
  209. }
  210. /*}}}*/
  211. // ShowHashTableStats - Show stats about a hashtable /*{{{*/
  212. // ---------------------------------------------------------------------
  213. /* */
  214. static map_pointer_t PackageNext(pkgCache::Package const * const P) { return P->NextPackage; }
  215. static map_pointer_t GroupNext(pkgCache::Group const * const G) { return G->Next; }
  216. template<class T>
  217. static void ShowHashTableStats(std::string Type,
  218. T *StartP,
  219. map_pointer_t *Hashtable,
  220. unsigned long Size,
  221. map_pointer_t(*Next)(T const * const))
  222. {
  223. // hashtable stats for the HashTable
  224. unsigned long NumBuckets = Size;
  225. unsigned long UsedBuckets = 0;
  226. unsigned long UnusedBuckets = 0;
  227. unsigned long LongestBucket = 0;
  228. unsigned long ShortestBucket = NumBuckets;
  229. unsigned long Entries = 0;
  230. for (unsigned int i=0; i < NumBuckets; ++i)
  231. {
  232. T *P = StartP + Hashtable[i];
  233. if(P == 0 || P == StartP)
  234. {
  235. ++UnusedBuckets;
  236. continue;
  237. }
  238. ++UsedBuckets;
  239. unsigned long ThisBucketSize = 0;
  240. for (; P != StartP; P = StartP + Next(P))
  241. ++ThisBucketSize;
  242. Entries += ThisBucketSize;
  243. LongestBucket = std::max(ThisBucketSize, LongestBucket);
  244. ShortestBucket = std::min(ThisBucketSize, ShortestBucket);
  245. }
  246. cout << "Total buckets in " << Type << ": " << NumBuckets << std::endl;
  247. cout << " Unused: " << UnusedBuckets << std::endl;
  248. cout << " Used: " << UsedBuckets << std::endl;
  249. cout << " Average entries: " << Entries/(double)NumBuckets << std::endl;
  250. cout << " Longest: " << LongestBucket << std::endl;
  251. cout << " Shortest: " << ShortestBucket << std::endl;
  252. }
  253. /*}}}*/
  254. // Stats - Dump some nice statistics /*{{{*/
  255. // ---------------------------------------------------------------------
  256. /* */
  257. static bool Stats(CommandLine &CmdL)
  258. {
  259. if (CmdL.FileSize() > 1) {
  260. _error->Error(_("apt-cache stats does not take any arguments"));
  261. return false;
  262. }
  263. pkgCacheFile CacheFile;
  264. pkgCache *Cache = CacheFile.GetPkgCache();
  265. if (unlikely(Cache == NULL))
  266. return false;
  267. cout << _("Total package names: ") << Cache->Head().GroupCount << " (" <<
  268. SizeToStr(Cache->Head().GroupCount*Cache->Head().GroupSz) << ')' << endl
  269. << _("Total package structures: ") << Cache->Head().PackageCount << " (" <<
  270. SizeToStr(Cache->Head().PackageCount*Cache->Head().PackageSz) << ')' << endl;
  271. int Normal = 0;
  272. int Virtual = 0;
  273. int NVirt = 0;
  274. int DVirt = 0;
  275. int Missing = 0;
  276. pkgCache::PkgIterator I = Cache->PkgBegin();
  277. for (;I.end() != true; ++I)
  278. {
  279. if (I->VersionList != 0 && I->ProvidesList == 0)
  280. {
  281. Normal++;
  282. continue;
  283. }
  284. if (I->VersionList != 0 && I->ProvidesList != 0)
  285. {
  286. NVirt++;
  287. continue;
  288. }
  289. if (I->VersionList == 0 && I->ProvidesList != 0)
  290. {
  291. // Only 1 provides
  292. if (I.ProvidesList()->NextProvides == 0)
  293. {
  294. DVirt++;
  295. }
  296. else
  297. Virtual++;
  298. continue;
  299. }
  300. if (I->VersionList == 0 && I->ProvidesList == 0)
  301. {
  302. Missing++;
  303. continue;
  304. }
  305. }
  306. cout << _(" Normal packages: ") << Normal << endl;
  307. cout << _(" Pure virtual packages: ") << Virtual << endl;
  308. cout << _(" Single virtual packages: ") << DVirt << endl;
  309. cout << _(" Mixed virtual packages: ") << NVirt << endl;
  310. cout << _(" Missing: ") << Missing << endl;
  311. cout << _("Total distinct versions: ") << Cache->Head().VersionCount << " (" <<
  312. SizeToStr(Cache->Head().VersionCount*Cache->Head().VersionSz) << ')' << endl;
  313. cout << _("Total distinct descriptions: ") << Cache->Head().DescriptionCount << " (" <<
  314. SizeToStr(Cache->Head().DescriptionCount*Cache->Head().DescriptionSz) << ')' << endl;
  315. cout << _("Total dependencies: ") << Cache->Head().DependsCount << "/" << Cache->Head().DependsDataCount << " (" <<
  316. SizeToStr((Cache->Head().DependsCount*Cache->Head().DependencySz) +
  317. (Cache->Head().DependsDataCount*Cache->Head().DependencyDataSz)) << ')' << endl;
  318. cout << _("Total ver/file relations: ") << Cache->Head().VerFileCount << " (" <<
  319. SizeToStr(Cache->Head().VerFileCount*Cache->Head().VerFileSz) << ')' << endl;
  320. cout << _("Total Desc/File relations: ") << Cache->Head().DescFileCount << " (" <<
  321. SizeToStr(Cache->Head().DescFileCount*Cache->Head().DescFileSz) << ')' << endl;
  322. cout << _("Total Provides mappings: ") << Cache->Head().ProvidesCount << " (" <<
  323. SizeToStr(Cache->Head().ProvidesCount*Cache->Head().ProvidesSz) << ')' << endl;
  324. // String list stats
  325. std::set<map_stringitem_t> stritems;
  326. for (pkgCache::GrpIterator G = Cache->GrpBegin(); G.end() == false; ++G)
  327. stritems.insert(G->Name);
  328. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  329. {
  330. stritems.insert(P->Arch);
  331. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
  332. {
  333. if (V->VerStr != 0)
  334. stritems.insert(V->VerStr);
  335. if (V->Section != 0)
  336. stritems.insert(V->Section);
  337. stritems.insert(V->SourcePkgName);
  338. stritems.insert(V->SourceVerStr);
  339. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; ++D)
  340. {
  341. if (D->Version != 0)
  342. stritems.insert(D->Version);
  343. }
  344. for (pkgCache::DescIterator D = V.DescriptionList(); D.end() == false; ++D)
  345. {
  346. stritems.insert(D->md5sum);
  347. stritems.insert(D->language_code);
  348. }
  349. }
  350. for (pkgCache::PrvIterator Prv = P.ProvidesList(); Prv.end() == false; ++Prv)
  351. {
  352. if (Prv->ProvideVersion != 0)
  353. stritems.insert(Prv->ProvideVersion);
  354. }
  355. }
  356. for (pkgCache::RlsFileIterator F = Cache->RlsFileBegin(); F != Cache->RlsFileEnd(); ++F)
  357. {
  358. stritems.insert(F->FileName);
  359. stritems.insert(F->Archive);
  360. stritems.insert(F->Codename);
  361. stritems.insert(F->Version);
  362. stritems.insert(F->Origin);
  363. stritems.insert(F->Label);
  364. stritems.insert(F->Site);
  365. }
  366. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F != Cache->FileEnd(); ++F)
  367. {
  368. stritems.insert(F->FileName);
  369. stritems.insert(F->Architecture);
  370. stritems.insert(F->Component);
  371. stritems.insert(F->IndexType);
  372. }
  373. unsigned long Size = 0;
  374. for (std::set<map_stringitem_t>::const_iterator i = stritems.begin(); i != stritems.end(); ++i)
  375. Size += strlen(Cache->StrP + *i) + 1;
  376. cout << _("Total globbed strings: ") << stritems.size() << " (" << SizeToStr(Size) << ')' << endl;
  377. stritems.clear();
  378. unsigned long Slack = 0;
  379. for (int I = 0; I != 7; I++)
  380. Slack += Cache->Head().Pools[I].ItemSize*Cache->Head().Pools[I].Count;
  381. cout << _("Total slack space: ") << SizeToStr(Slack) << endl;
  382. unsigned long Total = 0;
  383. #define APT_CACHESIZE(X,Y) (Cache->Head().X * Cache->Head().Y)
  384. Total = Slack + Size +
  385. APT_CACHESIZE(GroupCount, GroupSz) +
  386. APT_CACHESIZE(PackageCount, PackageSz) +
  387. APT_CACHESIZE(VersionCount, VersionSz) +
  388. APT_CACHESIZE(DescriptionCount, DescriptionSz) +
  389. APT_CACHESIZE(DependsCount, DependencySz) +
  390. APT_CACHESIZE(DependsDataCount, DependencyDataSz) +
  391. APT_CACHESIZE(ReleaseFileCount, ReleaseFileSz) +
  392. APT_CACHESIZE(PackageFileCount, PackageFileSz) +
  393. APT_CACHESIZE(VerFileCount, VerFileSz) +
  394. APT_CACHESIZE(DescFileCount, DescFileSz) +
  395. APT_CACHESIZE(ProvidesCount, ProvidesSz) +
  396. (2 * Cache->Head().GetHashTableSize() * sizeof(map_id_t));
  397. cout << _("Total space accounted for: ") << SizeToStr(Total) << endl;
  398. #undef APT_CACHESIZE
  399. // hashtable stats
  400. ShowHashTableStats<pkgCache::Package>("PkgHashTable", Cache->PkgP, Cache->Head().PkgHashTableP(), Cache->Head().GetHashTableSize(), PackageNext);
  401. ShowHashTableStats<pkgCache::Group>("GrpHashTable", Cache->GrpP, Cache->Head().GrpHashTableP(), Cache->Head().GetHashTableSize(), GroupNext);
  402. return true;
  403. }
  404. /*}}}*/
  405. // Dump - show everything /*{{{*/
  406. // ---------------------------------------------------------------------
  407. /* This is worthless except fer debugging things */
  408. static bool Dump(CommandLine &)
  409. {
  410. pkgCacheFile CacheFile;
  411. pkgCache *Cache = CacheFile.GetPkgCache();
  412. if (unlikely(Cache == NULL))
  413. return false;
  414. std::cout << "Using Versioning System: " << Cache->VS->Label << std::endl;
  415. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  416. {
  417. std::cout << "Package: " << P.FullName(true) << std::endl;
  418. for (pkgCache::VerIterator V = P.VersionList(); V.end() == false; ++V)
  419. {
  420. std::cout << " Version: " << V.VerStr() << std::endl;
  421. std::cout << " File: " << V.FileList().File().FileName() << std::endl;
  422. for (pkgCache::DepIterator D = V.DependsList(); D.end() == false; ++D)
  423. std::cout << " Depends: " << D.TargetPkg().FullName(true) << ' ' <<
  424. DeNull(D.TargetVer()) << std::endl;
  425. for (pkgCache::DescIterator D = V.DescriptionList(); D.end() == false; ++D)
  426. {
  427. std::cout << " Description Language: " << D.LanguageCode() << std::endl
  428. << " File: " << D.FileList().File().FileName() << std::endl
  429. << " MD5: " << D.md5() << std::endl;
  430. }
  431. }
  432. }
  433. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  434. {
  435. std::cout << "File: " << F.FileName() << std::endl;
  436. std::cout << " Type: " << F.IndexType() << std::endl;
  437. std::cout << " Size: " << F->Size << std::endl;
  438. std::cout << " ID: " << F->ID << std::endl;
  439. std::cout << " Flags: " << F->Flags << std::endl;
  440. std::cout << " Time: " << TimeRFC1123(F->mtime) << std::endl;
  441. std::cout << " Archive: " << DeNull(F.Archive()) << std::endl;
  442. std::cout << " Component: " << DeNull(F.Component()) << std::endl;
  443. std::cout << " Version: " << DeNull(F.Version()) << std::endl;
  444. std::cout << " Origin: " << DeNull(F.Origin()) << std::endl;
  445. std::cout << " Site: " << DeNull(F.Site()) << std::endl;
  446. std::cout << " Label: " << DeNull(F.Label()) << std::endl;
  447. std::cout << " Architecture: " << DeNull(F.Architecture()) << std::endl;
  448. }
  449. return true;
  450. }
  451. /*}}}*/
  452. // DumpAvail - Print out the available list /*{{{*/
  453. // ---------------------------------------------------------------------
  454. /* This is needed to make dpkg --merge happy.. I spent a bit of time to
  455. make this run really fast, perhaps I went a little overboard.. */
  456. static bool DumpAvail(CommandLine &)
  457. {
  458. pkgCacheFile CacheFile;
  459. pkgCache *Cache = CacheFile.GetPkgCache();
  460. if (unlikely(Cache == NULL || CacheFile.BuildPolicy() == false))
  461. return false;
  462. unsigned long Count = Cache->HeaderP->PackageCount+1;
  463. pkgCache::VerFile **VFList = new pkgCache::VerFile *[Count];
  464. memset(VFList,0,sizeof(*VFList)*Count);
  465. // Map versions that we want to write out onto the VerList array.
  466. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  467. {
  468. if (P->VersionList == 0)
  469. continue;
  470. /* Find the proper version to use. If the policy says there are no
  471. possible selections we return the installed version, if available..
  472. This prevents dselect from making it obsolete. */
  473. pkgCache::VerIterator V = CacheFile.GetPolicy()->GetCandidateVer(P);
  474. if (V.end() == true)
  475. {
  476. if (P->CurrentVer == 0)
  477. continue;
  478. V = P.CurrentVer();
  479. }
  480. pkgCache::VerFileIterator VF = V.FileList();
  481. for (; VF.end() == false ; ++VF)
  482. if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
  483. break;
  484. /* Okay, here we have a bit of a problem.. The policy has selected the
  485. currently installed package - however it only exists in the
  486. status file.. We need to write out something or dselect will mark
  487. the package as obsolete! Thus we emit the status file entry, but
  488. below we remove the status line to make it valid for the
  489. available file. However! We only do this if their do exist *any*
  490. non-source versions of the package - that way the dselect obsolete
  491. handling works OK. */
  492. if (VF.end() == true)
  493. {
  494. for (pkgCache::VerIterator Cur = P.VersionList(); Cur.end() != true; ++Cur)
  495. {
  496. for (VF = Cur.FileList(); VF.end() == false; ++VF)
  497. {
  498. if ((VF.File()->Flags & pkgCache::Flag::NotSource) == 0)
  499. {
  500. VF = V.FileList();
  501. break;
  502. }
  503. }
  504. if (VF.end() == false)
  505. break;
  506. }
  507. }
  508. VFList[P->ID] = VF;
  509. }
  510. LocalitySort(VFList,Count,sizeof(*VFList));
  511. std::vector<pkgTagSection::Tag> RW;
  512. RW.push_back(pkgTagSection::Tag::Remove("Status"));
  513. RW.push_back(pkgTagSection::Tag::Remove("Config-Version"));
  514. FileFd stdoutfd;
  515. stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false);
  516. // Iterate over all the package files and write them out.
  517. char *Buffer = new char[Cache->HeaderP->MaxVerFileSize+10];
  518. for (pkgCache::VerFile **J = VFList; *J != 0;)
  519. {
  520. pkgCache::PkgFileIterator File(*Cache,(*J)->File + Cache->PkgFileP);
  521. if (File.IsOk() == false)
  522. {
  523. _error->Error(_("Package file %s is out of sync."),File.FileName());
  524. break;
  525. }
  526. FileFd PkgF(File.FileName(),FileFd::ReadOnly, FileFd::Extension);
  527. if (_error->PendingError() == true)
  528. break;
  529. /* Write all of the records from this package file, since we
  530. already did locality sorting we can now just seek through the
  531. file in read order. We apply 1 more optimization here, since often
  532. there will be < 1 byte gaps between records (for the \n) we read that
  533. into the next buffer and offset a bit.. */
  534. unsigned long Pos = 0;
  535. for (; *J != 0; J++)
  536. {
  537. if ((*J)->File + Cache->PkgFileP != File)
  538. break;
  539. const pkgCache::VerFile &VF = **J;
  540. // Read the record and then write it out again.
  541. unsigned long Jitter = VF.Offset - Pos;
  542. if (Jitter > 8)
  543. {
  544. if (PkgF.Seek(VF.Offset) == false)
  545. break;
  546. Jitter = 0;
  547. }
  548. if (PkgF.Read(Buffer,VF.Size + Jitter) == false)
  549. break;
  550. Buffer[VF.Size + Jitter] = '\n';
  551. // See above..
  552. if ((File->Flags & pkgCache::Flag::NotSource) == pkgCache::Flag::NotSource)
  553. {
  554. pkgTagSection Tags;
  555. if (Tags.Scan(Buffer+Jitter,VF.Size+1) == false ||
  556. Tags.Write(stdoutfd, NULL, RW) == false ||
  557. stdoutfd.Write("\n", 1) == false)
  558. {
  559. _error->Error("Internal Error, Unable to parse a package record");
  560. break;
  561. }
  562. }
  563. else
  564. {
  565. if (stdoutfd.Write(Buffer + Jitter, VF.Size + 1) == false)
  566. break;
  567. }
  568. Pos = VF.Offset + VF.Size;
  569. }
  570. if (_error->PendingError() == true)
  571. break;
  572. }
  573. delete [] Buffer;
  574. delete [] VFList;
  575. return !_error->PendingError();
  576. }
  577. /*}}}*/
  578. // ShowDepends - Helper for printing out a dependency tree /*{{{*/
  579. static bool ShowDepends(CommandLine &CmdL, bool const RevDepends)
  580. {
  581. pkgCacheFile CacheFile;
  582. pkgCache *Cache = CacheFile.GetPkgCache();
  583. if (unlikely(Cache == NULL))
  584. return false;
  585. CacheSetHelperVirtuals helper(false);
  586. APT::VersionList verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::CANDIDATE, helper);
  587. if (verset.empty() == true && helper.virtualPkgs.empty() == true)
  588. return _error->Error(_("No packages found"));
  589. std::vector<bool> Shown(Cache->Head().PackageCount);
  590. bool const Recurse = _config->FindB("APT::Cache::RecurseDepends", false);
  591. bool const Installed = _config->FindB("APT::Cache::Installed", false);
  592. bool const Important = _config->FindB("APT::Cache::Important", false);
  593. bool const ShowDepType = _config->FindB("APT::Cache::ShowDependencyType", RevDepends == false);
  594. bool const ShowVersion = _config->FindB("APT::Cache::ShowVersion", false);
  595. bool const ShowPreDepends = _config->FindB("APT::Cache::ShowPre-Depends", true);
  596. bool const ShowDepends = _config->FindB("APT::Cache::ShowDepends", true);
  597. bool const ShowRecommends = _config->FindB("APT::Cache::ShowRecommends", Important == false);
  598. bool const ShowSuggests = _config->FindB("APT::Cache::ShowSuggests", Important == false);
  599. bool const ShowReplaces = _config->FindB("APT::Cache::ShowReplaces", Important == false);
  600. bool const ShowConflicts = _config->FindB("APT::Cache::ShowConflicts", Important == false);
  601. bool const ShowBreaks = _config->FindB("APT::Cache::ShowBreaks", Important == false);
  602. bool const ShowEnhances = _config->FindB("APT::Cache::ShowEnhances", Important == false);
  603. bool const ShowOnlyFirstOr = _config->FindB("APT::Cache::ShowOnlyFirstOr", false);
  604. bool const ShowImplicit = _config->FindB("APT::Cache::ShowImplicit", false);
  605. while (verset.empty() != true)
  606. {
  607. pkgCache::VerIterator Ver = *verset.begin();
  608. verset.erase(verset.begin());
  609. pkgCache::PkgIterator Pkg = Ver.ParentPkg();
  610. Shown[Pkg->ID] = true;
  611. cout << Pkg.FullName(true) << endl;
  612. if (RevDepends == true)
  613. cout << "Reverse Depends:" << endl;
  614. for (pkgCache::DepIterator D = RevDepends ? Pkg.RevDependsList() : Ver.DependsList();
  615. D.end() == false; ++D)
  616. {
  617. switch (D->Type) {
  618. case pkgCache::Dep::PreDepends: if (!ShowPreDepends) continue; break;
  619. case pkgCache::Dep::Depends: if (!ShowDepends) continue; break;
  620. case pkgCache::Dep::Recommends: if (!ShowRecommends) continue; break;
  621. case pkgCache::Dep::Suggests: if (!ShowSuggests) continue; break;
  622. case pkgCache::Dep::Replaces: if (!ShowReplaces) continue; break;
  623. case pkgCache::Dep::Conflicts: if (!ShowConflicts) continue; break;
  624. case pkgCache::Dep::DpkgBreaks: if (!ShowBreaks) continue; break;
  625. case pkgCache::Dep::Enhances: if (!ShowEnhances) continue; break;
  626. }
  627. if (ShowImplicit == false && D.IsImplicit())
  628. continue;
  629. pkgCache::PkgIterator Trg = RevDepends ? D.ParentPkg() : D.TargetPkg();
  630. if((Installed && Trg->CurrentVer != 0) || !Installed)
  631. {
  632. if ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or && ShowOnlyFirstOr == false)
  633. cout << " |";
  634. else
  635. cout << " ";
  636. // Show the package
  637. if (ShowDepType == true)
  638. cout << D.DepType() << ": ";
  639. if (Trg->VersionList == 0)
  640. cout << "<" << Trg.FullName(true) << ">";
  641. else
  642. cout << Trg.FullName(true);
  643. if (ShowVersion == true && D->Version != 0)
  644. cout << " (" << pkgCache::CompTypeDeb(D->CompareOp) << ' ' << D.TargetVer() << ')';
  645. cout << std::endl;
  646. if (Recurse == true && Shown[Trg->ID] == false)
  647. {
  648. Shown[Trg->ID] = true;
  649. verset.insert(APT::VersionSet::FromPackage(CacheFile, Trg, APT::CacheSetHelper::CANDIDATE, helper));
  650. }
  651. }
  652. // Display all solutions
  653. std::unique_ptr<pkgCache::Version *[]> List(D.AllTargets());
  654. pkgPrioSortList(*Cache,List.get());
  655. for (pkgCache::Version **I = List.get(); *I != 0; I++)
  656. {
  657. pkgCache::VerIterator V(*Cache,*I);
  658. if (V != Cache->VerP + V.ParentPkg()->VersionList ||
  659. V->ParentPkg == D->Package)
  660. continue;
  661. cout << " " << V.ParentPkg().FullName(true) << endl;
  662. if (Recurse == true && Shown[V.ParentPkg()->ID] == false)
  663. {
  664. Shown[V.ParentPkg()->ID] = true;
  665. verset.insert(APT::VersionSet::FromPackage(CacheFile, V.ParentPkg(), APT::CacheSetHelper::CANDIDATE, helper));
  666. }
  667. }
  668. if (ShowOnlyFirstOr == true)
  669. while ((D->CompareOp & pkgCache::Dep::Or) == pkgCache::Dep::Or) ++D;
  670. }
  671. }
  672. for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
  673. Pkg != helper.virtualPkgs.end(); ++Pkg)
  674. cout << '<' << Pkg.FullName(true) << '>' << endl;
  675. return true;
  676. }
  677. /*}}}*/
  678. // Depends - Print out a dependency tree /*{{{*/
  679. // ---------------------------------------------------------------------
  680. /* */
  681. static bool Depends(CommandLine &CmdL)
  682. {
  683. return ShowDepends(CmdL, false);
  684. }
  685. /*}}}*/
  686. // RDepends - Print out a reverse dependency tree /*{{{*/
  687. // ---------------------------------------------------------------------
  688. /* */
  689. static bool RDepends(CommandLine &CmdL)
  690. {
  691. return ShowDepends(CmdL, true);
  692. }
  693. /*}}}*/
  694. // xvcg - Generate a graph for xvcg /*{{{*/
  695. // ---------------------------------------------------------------------
  696. // Code contributed from Junichi Uekawa <dancer@debian.org> on 20 June 2002.
  697. static bool XVcg(CommandLine &CmdL)
  698. {
  699. pkgCacheFile CacheFile;
  700. pkgCache *Cache = CacheFile.GetPkgCache();
  701. if (unlikely(Cache == NULL))
  702. return false;
  703. bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
  704. /* Normal packages are boxes
  705. Pure Provides are triangles
  706. Mixed are diamonds
  707. rhomb are missing packages*/
  708. const char *Shapes[] = {"ellipse","triangle","box","rhomb"};
  709. /* Initialize the list of packages to show.
  710. 1 = To Show
  711. 2 = To Show no recurse
  712. 3 = Emitted no recurse
  713. 4 = Emitted
  714. 0 = None */
  715. enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
  716. enum TheFlags {ForceNR=(1<<0)};
  717. unsigned char *Show = new unsigned char[Cache->Head().PackageCount];
  718. unsigned char *Flags = new unsigned char[Cache->Head().PackageCount];
  719. unsigned char *ShapeMap = new unsigned char[Cache->Head().PackageCount];
  720. // Show everything if no arguments given
  721. if (CmdL.FileList[1] == 0)
  722. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  723. Show[I] = ToShow;
  724. else
  725. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  726. Show[I] = None;
  727. memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
  728. // Map the shapes
  729. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  730. {
  731. if (Pkg->VersionList == 0)
  732. {
  733. // Missing
  734. if (Pkg->ProvidesList == 0)
  735. ShapeMap[Pkg->ID] = 0;
  736. else
  737. ShapeMap[Pkg->ID] = 1;
  738. }
  739. else
  740. {
  741. // Normal
  742. if (Pkg->ProvidesList == 0)
  743. ShapeMap[Pkg->ID] = 2;
  744. else
  745. ShapeMap[Pkg->ID] = 3;
  746. }
  747. }
  748. // Load the list of packages from the command line into the show list
  749. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  750. std::list<APT::CacheSetHelper::PkgModifier> mods;
  751. mods.push_back(APT::CacheSetHelper::PkgModifier(0, ",", APT::PackageSet::Modifier::POSTFIX));
  752. mods.push_back(APT::CacheSetHelper::PkgModifier(1, "^", APT::PackageSet::Modifier::POSTFIX));
  753. std::map<unsigned short, APT::PackageSet> pkgsets =
  754. APT::PackageSet::GroupedFromCommandLine(CacheFile, CmdL.FileList + 1, mods, 0, helper);
  755. for (APT::PackageSet::const_iterator Pkg = pkgsets[0].begin();
  756. Pkg != pkgsets[0].end(); ++Pkg)
  757. Show[Pkg->ID] = ToShow;
  758. for (APT::PackageSet::const_iterator Pkg = pkgsets[1].begin();
  759. Pkg != pkgsets[1].end(); ++Pkg)
  760. {
  761. Show[Pkg->ID] = ToShow;
  762. Flags[Pkg->ID] |= ForceNR;
  763. }
  764. // Little header
  765. cout << "graph: { title: \"packages\"" << endl <<
  766. "xmax: 700 ymax: 700 x: 30 y: 30" << endl <<
  767. "layout_downfactor: 8" << endl;
  768. bool Act = true;
  769. while (Act == true)
  770. {
  771. Act = false;
  772. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  773. {
  774. // See we need to show this package
  775. if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
  776. continue;
  777. //printf ("node: { title: \"%s\" label: \"%s\" }\n", Pkg.Name(), Pkg.Name());
  778. // Colour as done
  779. if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
  780. {
  781. // Pure Provides and missing packages have no deps!
  782. if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
  783. Show[Pkg->ID] = Done;
  784. else
  785. Show[Pkg->ID] = DoneNR;
  786. }
  787. else
  788. Show[Pkg->ID] = Done;
  789. Act = true;
  790. // No deps to map out
  791. if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
  792. continue;
  793. pkgCache::VerIterator Ver = Pkg.VersionList();
  794. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; ++D)
  795. {
  796. // See if anything can meet this dep
  797. // Walk along the actual package providing versions
  798. bool Hit = false;
  799. pkgCache::PkgIterator DPkg = D.TargetPkg();
  800. for (pkgCache::VerIterator I = DPkg.VersionList();
  801. I.end() == false && Hit == false; ++I)
  802. {
  803. if (Cache->VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
  804. Hit = true;
  805. }
  806. // Follow all provides
  807. for (pkgCache::PrvIterator I = DPkg.ProvidesList();
  808. I.end() == false && Hit == false; ++I)
  809. {
  810. if (Cache->VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
  811. Hit = true;
  812. }
  813. // Only graph critical deps
  814. if (D.IsCritical() == true)
  815. {
  816. printf ("edge: { sourcename: \"%s\" targetname: \"%s\" class: 2 ",Pkg.FullName(true).c_str(), D.TargetPkg().FullName(true).c_str() );
  817. // Colour the node for recursion
  818. if (Show[D.TargetPkg()->ID] <= DoneNR)
  819. {
  820. /* If a conflicts does not meet anything in the database
  821. then show the relation but do not recurse */
  822. if (Hit == false && D.IsNegative() == true)
  823. {
  824. if (Show[D.TargetPkg()->ID] == None &&
  825. Show[D.TargetPkg()->ID] != ToShow)
  826. Show[D.TargetPkg()->ID] = ToShowNR;
  827. }
  828. else
  829. {
  830. if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
  831. Show[D.TargetPkg()->ID] = ToShowNR;
  832. else
  833. Show[D.TargetPkg()->ID] = ToShow;
  834. }
  835. }
  836. // Edge colour
  837. switch(D->Type)
  838. {
  839. case pkgCache::Dep::Conflicts:
  840. printf("label: \"conflicts\" color: lightgreen }\n");
  841. break;
  842. case pkgCache::Dep::DpkgBreaks:
  843. printf("label: \"breaks\" color: lightgreen }\n");
  844. break;
  845. case pkgCache::Dep::Obsoletes:
  846. printf("label: \"obsoletes\" color: lightgreen }\n");
  847. break;
  848. case pkgCache::Dep::PreDepends:
  849. printf("label: \"predepends\" color: blue }\n");
  850. break;
  851. default:
  852. printf("}\n");
  853. break;
  854. }
  855. }
  856. }
  857. }
  858. }
  859. /* Draw the box colours after the fact since we can not tell what colour
  860. they should be until everything is finished drawing */
  861. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  862. {
  863. if (Show[Pkg->ID] < DoneNR)
  864. continue;
  865. if (Show[Pkg->ID] == DoneNR)
  866. printf("node: { title: \"%s\" label: \"%s\" color: orange shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
  867. Shapes[ShapeMap[Pkg->ID]]);
  868. else
  869. printf("node: { title: \"%s\" label: \"%s\" shape: %s }\n", Pkg.FullName(true).c_str(), Pkg.FullName(true).c_str(),
  870. Shapes[ShapeMap[Pkg->ID]]);
  871. }
  872. delete[] Show;
  873. delete[] Flags;
  874. delete[] ShapeMap;
  875. printf("}\n");
  876. return true;
  877. }
  878. /*}}}*/
  879. // Dotty - Generate a graph for Dotty /*{{{*/
  880. // ---------------------------------------------------------------------
  881. /* Dotty is the graphvis program for generating graphs. It is a fairly
  882. simple queuing algorithm that just writes dependencies and nodes.
  883. http://www.research.att.com/sw/tools/graphviz/ */
  884. static bool Dotty(CommandLine &CmdL)
  885. {
  886. pkgCacheFile CacheFile;
  887. pkgCache *Cache = CacheFile.GetPkgCache();
  888. if (unlikely(Cache == NULL))
  889. return false;
  890. bool GivenOnly = _config->FindB("APT::Cache::GivenOnly",false);
  891. /* Normal packages are boxes
  892. Pure Provides are triangles
  893. Mixed are diamonds
  894. Hexagons are missing packages*/
  895. const char *Shapes[] = {"hexagon","triangle","box","diamond"};
  896. /* Initialize the list of packages to show.
  897. 1 = To Show
  898. 2 = To Show no recurse
  899. 3 = Emitted no recurse
  900. 4 = Emitted
  901. 0 = None */
  902. enum States {None=0, ToShow, ToShowNR, DoneNR, Done};
  903. enum TheFlags {ForceNR=(1<<0)};
  904. unsigned char *Show = new unsigned char[Cache->Head().PackageCount];
  905. unsigned char *Flags = new unsigned char[Cache->Head().PackageCount];
  906. unsigned char *ShapeMap = new unsigned char[Cache->Head().PackageCount];
  907. // Show everything if no arguments given
  908. if (CmdL.FileList[1] == 0)
  909. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  910. Show[I] = ToShow;
  911. else
  912. for (unsigned long I = 0; I != Cache->Head().PackageCount; I++)
  913. Show[I] = None;
  914. memset(Flags,0,sizeof(*Flags)*Cache->Head().PackageCount);
  915. // Map the shapes
  916. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  917. {
  918. if (Pkg->VersionList == 0)
  919. {
  920. // Missing
  921. if (Pkg->ProvidesList == 0)
  922. ShapeMap[Pkg->ID] = 0;
  923. else
  924. ShapeMap[Pkg->ID] = 1;
  925. }
  926. else
  927. {
  928. // Normal
  929. if (Pkg->ProvidesList == 0)
  930. ShapeMap[Pkg->ID] = 2;
  931. else
  932. ShapeMap[Pkg->ID] = 3;
  933. }
  934. }
  935. // Load the list of packages from the command line into the show list
  936. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  937. std::list<APT::CacheSetHelper::PkgModifier> mods;
  938. mods.push_back(APT::CacheSetHelper::PkgModifier(0, ",", APT::PackageSet::Modifier::POSTFIX));
  939. mods.push_back(APT::CacheSetHelper::PkgModifier(1, "^", APT::PackageSet::Modifier::POSTFIX));
  940. std::map<unsigned short, APT::PackageSet> pkgsets =
  941. APT::PackageSet::GroupedFromCommandLine(CacheFile, CmdL.FileList + 1, mods, 0, helper);
  942. for (APT::PackageSet::const_iterator Pkg = pkgsets[0].begin();
  943. Pkg != pkgsets[0].end(); ++Pkg)
  944. Show[Pkg->ID] = ToShow;
  945. for (APT::PackageSet::const_iterator Pkg = pkgsets[1].begin();
  946. Pkg != pkgsets[1].end(); ++Pkg)
  947. {
  948. Show[Pkg->ID] = ToShow;
  949. Flags[Pkg->ID] |= ForceNR;
  950. }
  951. // Little header
  952. printf("digraph packages {\n");
  953. printf("concentrate=true;\n");
  954. printf("size=\"30,40\";\n");
  955. bool Act = true;
  956. while (Act == true)
  957. {
  958. Act = false;
  959. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  960. {
  961. // See we need to show this package
  962. if (Show[Pkg->ID] == None || Show[Pkg->ID] >= DoneNR)
  963. continue;
  964. // Colour as done
  965. if (Show[Pkg->ID] == ToShowNR || (Flags[Pkg->ID] & ForceNR) == ForceNR)
  966. {
  967. // Pure Provides and missing packages have no deps!
  968. if (ShapeMap[Pkg->ID] == 0 || ShapeMap[Pkg->ID] == 1)
  969. Show[Pkg->ID] = Done;
  970. else
  971. Show[Pkg->ID] = DoneNR;
  972. }
  973. else
  974. Show[Pkg->ID] = Done;
  975. Act = true;
  976. // No deps to map out
  977. if (Pkg->VersionList == 0 || Show[Pkg->ID] == DoneNR)
  978. continue;
  979. pkgCache::VerIterator Ver = Pkg.VersionList();
  980. for (pkgCache::DepIterator D = Ver.DependsList(); D.end() == false; ++D)
  981. {
  982. // See if anything can meet this dep
  983. // Walk along the actual package providing versions
  984. bool Hit = false;
  985. pkgCache::PkgIterator DPkg = D.TargetPkg();
  986. for (pkgCache::VerIterator I = DPkg.VersionList();
  987. I.end() == false && Hit == false; ++I)
  988. {
  989. if (Cache->VS->CheckDep(I.VerStr(),D->CompareOp,D.TargetVer()) == true)
  990. Hit = true;
  991. }
  992. // Follow all provides
  993. for (pkgCache::PrvIterator I = DPkg.ProvidesList();
  994. I.end() == false && Hit == false; ++I)
  995. {
  996. if (Cache->VS->CheckDep(I.ProvideVersion(),D->CompareOp,D.TargetVer()) == false)
  997. Hit = true;
  998. }
  999. // Only graph critical deps
  1000. if (D.IsCritical() == true)
  1001. {
  1002. printf("\"%s\" -> \"%s\"",Pkg.FullName(true).c_str(),D.TargetPkg().FullName(true).c_str());
  1003. // Colour the node for recursion
  1004. if (Show[D.TargetPkg()->ID] <= DoneNR)
  1005. {
  1006. /* If a conflicts does not meet anything in the database
  1007. then show the relation but do not recurse */
  1008. if (Hit == false && D.IsNegative() == true)
  1009. {
  1010. if (Show[D.TargetPkg()->ID] == None &&
  1011. Show[D.TargetPkg()->ID] != ToShow)
  1012. Show[D.TargetPkg()->ID] = ToShowNR;
  1013. }
  1014. else
  1015. {
  1016. if (GivenOnly == true && Show[D.TargetPkg()->ID] != ToShow)
  1017. Show[D.TargetPkg()->ID] = ToShowNR;
  1018. else
  1019. Show[D.TargetPkg()->ID] = ToShow;
  1020. }
  1021. }
  1022. // Edge colour
  1023. switch(D->Type)
  1024. {
  1025. case pkgCache::Dep::Conflicts:
  1026. case pkgCache::Dep::Obsoletes:
  1027. case pkgCache::Dep::DpkgBreaks:
  1028. printf("[color=springgreen];\n");
  1029. break;
  1030. case pkgCache::Dep::PreDepends:
  1031. printf("[color=blue];\n");
  1032. break;
  1033. default:
  1034. printf(";\n");
  1035. break;
  1036. }
  1037. }
  1038. }
  1039. }
  1040. }
  1041. /* Draw the box colours after the fact since we can not tell what colour
  1042. they should be until everything is finished drawing */
  1043. for (pkgCache::PkgIterator Pkg = Cache->PkgBegin(); Pkg.end() == false; ++Pkg)
  1044. {
  1045. if (Show[Pkg->ID] < DoneNR)
  1046. continue;
  1047. // Orange box for early recursion stoppage
  1048. if (Show[Pkg->ID] == DoneNR)
  1049. printf("\"%s\" [color=orange,shape=%s];\n",Pkg.FullName(true).c_str(),
  1050. Shapes[ShapeMap[Pkg->ID]]);
  1051. else
  1052. printf("\"%s\" [shape=%s];\n",Pkg.FullName(true).c_str(),
  1053. Shapes[ShapeMap[Pkg->ID]]);
  1054. }
  1055. printf("}\n");
  1056. delete[] Show;
  1057. delete[] Flags;
  1058. delete[] ShapeMap;
  1059. return true;
  1060. }
  1061. /*}}}*/
  1062. /* ShowAuto - show automatically installed packages (sorted) {{{*/
  1063. static bool ShowAuto(CommandLine &)
  1064. {
  1065. pkgCacheFile CacheFile;
  1066. pkgCache *Cache = CacheFile.GetPkgCache();
  1067. pkgDepCache *DepCache = CacheFile.GetDepCache();
  1068. if (unlikely(Cache == NULL || DepCache == NULL))
  1069. return false;
  1070. std::vector<string> packages;
  1071. packages.reserve(Cache->HeaderP->PackageCount / 3);
  1072. for (pkgCache::PkgIterator P = Cache->PkgBegin(); P.end() == false; ++P)
  1073. if ((*DepCache)[P].Flags & pkgCache::Flag::Auto)
  1074. packages.push_back(P.Name());
  1075. std::sort(packages.begin(), packages.end());
  1076. for (vector<string>::iterator I = packages.begin(); I != packages.end(); ++I)
  1077. cout << *I << "\n";
  1078. _error->Notice(_("This command is deprecated. Please use 'apt-mark showauto' instead."));
  1079. return true;
  1080. }
  1081. /*}}}*/
  1082. // ShowPkgNames - Show package names /*{{{*/
  1083. // ---------------------------------------------------------------------
  1084. /* This does a prefix match on the first argument */
  1085. static bool ShowPkgNames(CommandLine &CmdL)
  1086. {
  1087. pkgCacheFile CacheFile;
  1088. if (unlikely(CacheFile.BuildCaches(NULL, false) == false))
  1089. return false;
  1090. pkgCache::GrpIterator I = CacheFile.GetPkgCache()->GrpBegin();
  1091. bool const All = _config->FindB("APT::Cache::AllNames","false");
  1092. if (CmdL.FileList[1] != 0)
  1093. {
  1094. for (;I.end() != true; ++I)
  1095. {
  1096. if (All == false && I->FirstPackage == 0)
  1097. continue;
  1098. if (I.FindPkg("any")->VersionList == 0)
  1099. continue;
  1100. if (strncmp(I.Name(),CmdL.FileList[1],strlen(CmdL.FileList[1])) == 0)
  1101. cout << I.Name() << endl;
  1102. }
  1103. return true;
  1104. }
  1105. // Show all pkgs
  1106. for (;I.end() != true; ++I)
  1107. {
  1108. if (All == false && I->FirstPackage == 0)
  1109. continue;
  1110. if (I.FindPkg("any")->VersionList == 0)
  1111. continue;
  1112. cout << I.Name() << endl;
  1113. }
  1114. return true;
  1115. }
  1116. /*}}}*/
  1117. // ShowSrcPackage - Show source package records /*{{{*/
  1118. // ---------------------------------------------------------------------
  1119. /* */
  1120. static bool ShowSrcPackage(CommandLine &CmdL)
  1121. {
  1122. pkgCacheFile CacheFile;
  1123. pkgSourceList *List = CacheFile.GetSourceList();
  1124. if (unlikely(List == NULL))
  1125. return false;
  1126. // Create the text record parsers
  1127. pkgSrcRecords SrcRecs(*List);
  1128. if (_error->PendingError() == true)
  1129. return false;
  1130. unsigned found = 0;
  1131. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1132. {
  1133. SrcRecs.Restart();
  1134. pkgSrcRecords::Parser *Parse;
  1135. unsigned found_this = 0;
  1136. while ((Parse = SrcRecs.Find(*I,false)) != 0) {
  1137. // SrcRecs.Find() will find both binary and source names
  1138. if (_config->FindB("APT::Cache::Only-Source", false) == true)
  1139. if (Parse->Package() != *I)
  1140. continue;
  1141. cout << Parse->AsStr() << endl;;
  1142. found++;
  1143. found_this++;
  1144. }
  1145. if (found_this == 0) {
  1146. _error->Warning(_("Unable to locate package %s"),*I);
  1147. continue;
  1148. }
  1149. }
  1150. if (found == 0)
  1151. _error->Notice(_("No packages found"));
  1152. return true;
  1153. }
  1154. /*}}}*/
  1155. // Policy - Show the results of the preferences file /*{{{*/
  1156. // ---------------------------------------------------------------------
  1157. /* */
  1158. static bool Policy(CommandLine &CmdL)
  1159. {
  1160. pkgCacheFile CacheFile;
  1161. pkgCache *Cache = CacheFile.GetPkgCache();
  1162. pkgPolicy *Plcy = CacheFile.GetPolicy();
  1163. pkgSourceList *SrcList = CacheFile.GetSourceList();
  1164. if (unlikely(Cache == NULL || Plcy == NULL || SrcList == NULL))
  1165. return false;
  1166. /* Should the MultiArchKiller be run to see which pseudo packages for an
  1167. arch all package are currently installed? Activating it gives a speed
  1168. penality for no real gain beside enhanced debugging, so in general no. */
  1169. if (_config->FindB("APT::Cache::Policy::DepCache", false) == true)
  1170. CacheFile.GetDepCache();
  1171. // Print out all of the package files
  1172. if (CmdL.FileList[1] == 0)
  1173. {
  1174. cout << _("Package files:") << endl;
  1175. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  1176. {
  1177. if (F.Flagged(pkgCache::Flag::NoPackages))
  1178. continue;
  1179. // Locate the associated index files so we can derive a description
  1180. pkgIndexFile *Indx;
  1181. if (SrcList->FindIndex(F,Indx) == false &&
  1182. _system->FindIndex(F,Indx) == false)
  1183. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  1184. printf("%4i %s\n",
  1185. Plcy->GetPriority(F),Indx->Describe(true).c_str());
  1186. // Print the reference information for the package
  1187. string Str = F.RelStr();
  1188. if (Str.empty() == false)
  1189. printf(" release %s\n",F.RelStr().c_str());
  1190. if (F.Site() != 0 && F.Site()[0] != 0)
  1191. printf(" origin %s\n",F.Site());
  1192. }
  1193. // Show any packages have explicit pins
  1194. cout << _("Pinned packages:") << endl;
  1195. pkgCache::PkgIterator I = Cache->PkgBegin();
  1196. for (;I.end() != true; ++I)
  1197. {
  1198. // Old code for debugging
  1199. if (_config->FindI("APT::Policy", 1) < 1) {
  1200. if (Plcy->GetPriority(I) == 0)
  1201. continue;
  1202. // Print the package name and the version we are forcing to
  1203. cout << " " << I.FullName(true) << " -> ";
  1204. pkgCache::VerIterator V = Plcy->GetMatch(I);
  1205. if (V.end() == true)
  1206. cout << _("(not found)") << endl;
  1207. else
  1208. cout << V.VerStr() << endl;
  1209. continue;
  1210. }
  1211. // New code
  1212. for (pkgCache::VerIterator V = I.VersionList(); !V.end(); V++) {
  1213. auto Prio = Plcy->GetPriority(V, false);
  1214. if (Prio == 0)
  1215. continue;
  1216. cout << " ";
  1217. // Print the package name and the version we are forcing to
  1218. ioprintf(cout, _("%s -> %s with priority %d\n"), I.FullName(true).c_str(), V.VerStr(), Prio);
  1219. }
  1220. }
  1221. return true;
  1222. }
  1223. char const * const msgInstalled = _(" Installed: ");
  1224. char const * const msgCandidate = _(" Candidate: ");
  1225. short const InstalledLessCandidate =
  1226. mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
  1227. short const deepInstalled =
  1228. (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
  1229. short const deepCandidate =
  1230. (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
  1231. // Print out detailed information for each package
  1232. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  1233. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  1234. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  1235. {
  1236. cout << Pkg.FullName(true) << ":" << endl;
  1237. // Installed version
  1238. cout << msgInstalled << OutputInDepth(deepInstalled, " ");
  1239. if (Pkg->CurrentVer == 0)
  1240. cout << _("(none)") << endl;
  1241. else
  1242. cout << Pkg.CurrentVer().VerStr() << endl;
  1243. // Candidate Version
  1244. cout << msgCandidate << OutputInDepth(deepCandidate, " ");
  1245. pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
  1246. if (V.end() == true)
  1247. cout << _("(none)") << endl;
  1248. else
  1249. cout << V.VerStr() << endl;
  1250. // Pinned version
  1251. if (_config->FindI("APT::Policy", 1) < 1 && Plcy->GetPriority(Pkg) != 0)
  1252. {
  1253. cout << _(" Package pin: ");
  1254. V = Plcy->GetMatch(Pkg);
  1255. if (V.end() == true)
  1256. cout << _("(not found)") << endl;
  1257. else
  1258. cout << V.VerStr() << endl;
  1259. }
  1260. // Show the priority tables
  1261. cout << _(" Version table:") << endl;
  1262. for (V = Pkg.VersionList(); V.end() == false; ++V)
  1263. {
  1264. if (Pkg.CurrentVer() == V)
  1265. cout << " *** " << V.VerStr();
  1266. else
  1267. cout << " " << V.VerStr();
  1268. if (_config->FindI("APT::Policy", 1) < 1)
  1269. cout << " " << Plcy->GetPriority(Pkg) << endl;
  1270. else
  1271. cout << " " << Plcy->GetPriority(V) << endl;
  1272. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  1273. {
  1274. // Locate the associated index files so we can derive a description
  1275. pkgIndexFile *Indx;
  1276. if (SrcList->FindIndex(VF.File(),Indx) == false &&
  1277. _system->FindIndex(VF.File(),Indx) == false)
  1278. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  1279. printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
  1280. Indx->Describe(true).c_str());
  1281. }
  1282. }
  1283. }
  1284. return true;
  1285. }
  1286. /*}}}*/
  1287. // Madison - Look a bit like katie's madison /*{{{*/
  1288. // ---------------------------------------------------------------------
  1289. /* */
  1290. static bool Madison(CommandLine &CmdL)
  1291. {
  1292. pkgCacheFile CacheFile;
  1293. pkgSourceList *SrcList = CacheFile.GetSourceList();
  1294. if (SrcList == 0)
  1295. return false;
  1296. // Create the src text record parsers and ignore errors about missing
  1297. // deb-src lines that are generated from pkgSrcRecords::pkgSrcRecords
  1298. pkgSrcRecords SrcRecs(*SrcList);
  1299. if (_error->PendingError() == true)
  1300. _error->Discard();
  1301. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  1302. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  1303. {
  1304. _error->PushToStack();
  1305. APT::PackageList pkgset = APT::PackageList::FromString(CacheFile, *I, helper);
  1306. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  1307. {
  1308. for (pkgCache::VerIterator V = Pkg.VersionList(); V.end() == false; ++V)
  1309. {
  1310. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  1311. {
  1312. // This might be nice, but wouldn't uniquely identify the source -mdz
  1313. // if (VF.File().Archive() != 0)
  1314. // {
  1315. // cout << setw(10) << Pkg.Name() << " | " << setw(10) << V.VerStr() << " | "
  1316. // << VF.File().Archive() << endl;
  1317. // }
  1318. // Locate the associated index files so we can derive a description
  1319. for (pkgSourceList::const_iterator S = SrcList->begin(); S != SrcList->end(); ++S)
  1320. {
  1321. vector<pkgIndexFile *> *Indexes = (*S)->GetIndexFiles();
  1322. for (vector<pkgIndexFile *>::const_iterator IF = Indexes->begin();
  1323. IF != Indexes->end(); ++IF)
  1324. {
  1325. if ((*IF)->FindInCache(*(VF.File().Cache())) == VF.File())
  1326. {
  1327. cout << setw(10) << Pkg.FullName(true) << " | " << setw(10) << V.VerStr() << " | "
  1328. << (*IF)->Describe(true) << endl;
  1329. }
  1330. }
  1331. }
  1332. }
  1333. }
  1334. }
  1335. SrcRecs.Restart();
  1336. pkgSrcRecords::Parser *SrcParser;
  1337. bool foundSomething = false;
  1338. while ((SrcParser = SrcRecs.Find(*I, false)) != 0)
  1339. {
  1340. foundSomething = true;
  1341. // Maybe support Release info here too eventually
  1342. cout << setw(10) << SrcParser->Package() << " | "
  1343. << setw(10) << SrcParser->Version() << " | "
  1344. << SrcParser->Index().Describe(true) << endl;
  1345. }
  1346. if (foundSomething == true)
  1347. _error->RevertToStack();
  1348. else
  1349. _error->MergeWithStack();
  1350. }
  1351. return true;
  1352. }
  1353. /*}}}*/
  1354. // GenCaches - Call the main cache generator /*{{{*/
  1355. // ---------------------------------------------------------------------
  1356. /* */
  1357. static bool GenCaches(CommandLine &)
  1358. {
  1359. OpTextProgress Progress(*_config);
  1360. pkgCacheFile CacheFile;
  1361. return CacheFile.BuildCaches(&Progress, true);
  1362. }
  1363. /*}}}*/
  1364. // ShowHelp - Show a help screen /*{{{*/
  1365. bool ShowHelp(CommandLine &, CommandLine::DispatchWithHelp const * Cmds)
  1366. {
  1367. ioprintf(cout, "%s %s (%s)\n", PACKAGE, PACKAGE_VERSION, COMMON_ARCH);
  1368. if (_config->FindB("version") == true)
  1369. return true;
  1370. std::cout <<
  1371. _("Usage: apt-cache [options] command\n"
  1372. " apt-cache [options] show pkg1 [pkg2 ...]\n"
  1373. "\n"
  1374. "apt-cache is a low-level tool used to query information\n"
  1375. "from APT's binary cache files\n")
  1376. << std::endl
  1377. << _("Commands:") << std::endl;
  1378. for (; Cmds->Handler != nullptr; ++Cmds)
  1379. {
  1380. if (Cmds->Help == nullptr)
  1381. continue;
  1382. std::cout << " " << Cmds->Match << " - " << Cmds->Help << std::endl;
  1383. }
  1384. std::cout << std::endl
  1385. << _("Options:\n"
  1386. " -h This help text.\n"
  1387. " -p=? The package cache.\n"
  1388. " -s=? The source cache.\n"
  1389. " -q Disable progress indicator.\n"
  1390. " -i Show only important deps for the unmet command.\n"
  1391. " -c=? Read this configuration file\n"
  1392. " -o=? Set an arbitrary configuration option, eg -o dir::cache=/tmp\n"
  1393. "See the apt-cache(8) and apt.conf(5) manual pages for more information.\n");
  1394. return true;
  1395. }
  1396. /*}}}*/
  1397. std::vector<CommandLine::DispatchWithHelp> GetCommands() /*{{{*/
  1398. {
  1399. return {
  1400. {"gencaches",&GenCaches, nullptr},
  1401. {"showsrc",&ShowSrcPackage, _("Show source records")},
  1402. {"showpkg",&DumpPackage, nullptr},
  1403. {"stats",&Stats, nullptr},
  1404. {"dump",&Dump, nullptr},
  1405. {"dumpavail",&DumpAvail, nullptr},
  1406. {"unmet",&UnMet, nullptr},
  1407. {"search",&DoSearch, _("Search the package list for a regex pattern")},
  1408. {"depends",&Depends, _("Show raw dependency information for a package")},
  1409. {"rdepends",&RDepends, _("Show reverse dependency information for a package")},
  1410. {"dotty",&Dotty, nullptr},
  1411. {"xvcg",&XVcg, nullptr},
  1412. {"show",&ShowPackage, _("Show a readable record for the package")},
  1413. {"pkgnames",&ShowPkgNames, _("List the names of all packages in the system")},
  1414. {"showauto",&ShowAuto, nullptr},
  1415. {"policy",&Policy, _("Show policy settings")},
  1416. {"madison",&Madison, nullptr},
  1417. {nullptr, nullptr, nullptr}
  1418. };
  1419. }
  1420. /*}}}*/
  1421. int main(int argc,const char *argv[]) /*{{{*/
  1422. {
  1423. InitLocale();
  1424. // Parse the command line and initialize the package library
  1425. CommandLine CmdL;
  1426. auto const Cmds = ParseCommandLine(CmdL, APT_CMD::APT_CACHE, &_config, &_system, argc, argv);
  1427. InitOutput();
  1428. if (_config->Exists("APT::Cache::Generate") == true)
  1429. _config->Set("pkgCacheFile::Generate", _config->FindB("APT::Cache::Generate", true));
  1430. return DispatchCommandLine(CmdL, Cmds);
  1431. }
  1432. /*}}}*/