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.
 
 
 
 
 
 

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