private-show.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. // Includes /*{{{*/
  2. #include <config.h>
  3. #include <apt-pkg/cachefile.h>
  4. #include <apt-pkg/cacheset.h>
  5. #include <apt-pkg/cmndline.h>
  6. #include <apt-pkg/error.h>
  7. #include <apt-pkg/fileutl.h>
  8. #include <apt-pkg/indexfile.h>
  9. #include <apt-pkg/pkgrecords.h>
  10. #include <apt-pkg/pkgsystem.h>
  11. #include <apt-pkg/sourcelist.h>
  12. #include <apt-pkg/strutl.h>
  13. #include <apt-pkg/tagfile.h>
  14. #include <apt-pkg/cacheiterators.h>
  15. #include <apt-pkg/configuration.h>
  16. #include <apt-pkg/depcache.h>
  17. #include <apt-pkg/macros.h>
  18. #include <apt-pkg/pkgcache.h>
  19. #include <apt-pkg/policy.h>
  20. #include <apt-private/private-cacheset.h>
  21. #include <apt-private/private-output.h>
  22. #include <apt-private/private-show.h>
  23. #include <stdio.h>
  24. #include <ostream>
  25. #include <string>
  26. #include <apti18n.h>
  27. /*}}}*/
  28. static bool OpenPackagesFile(pkgCacheFile &CacheFile, pkgCache::VerIterator const &V,/*{{{*/
  29. FileFd &PkgF, pkgCache::VerFileIterator &Vf)
  30. {
  31. pkgCache const * const Cache = CacheFile.GetPkgCache();
  32. if (unlikely(Cache == NULL))
  33. return false;
  34. // Find an appropriate file
  35. Vf = V.FileList();
  36. for (; Vf.end() == false; ++Vf)
  37. if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
  38. break;
  39. if (Vf.end() == true)
  40. Vf = V.FileList();
  41. // Check and load the package list file
  42. pkgCache::PkgFileIterator I = Vf.File();
  43. if (I.IsOk() == false)
  44. return _error->Error(_("Package file %s is out of sync."),I.FileName());
  45. // Read the record
  46. return PkgF.Open(I.FileName(), FileFd::ReadOnly, FileFd::Extension);
  47. }
  48. /*}}}*/
  49. static APT_PURE unsigned char const* skipDescriptionFields(unsigned char const * DescP)/*{{{*/
  50. {
  51. char const * const TagName = "\nDescription";
  52. size_t const TagLen = strlen(TagName);
  53. while ((DescP = (unsigned char*)strchr((char*)DescP, '\n')) != NULL)
  54. {
  55. if (DescP[1] == ' ')
  56. DescP += 2;
  57. else if (strncmp((char*)DescP, TagName, TagLen) == 0)
  58. DescP += TagLen;
  59. else
  60. break;
  61. }
  62. if (DescP != NULL)
  63. ++DescP;
  64. return DescP;
  65. }
  66. /*}}}*/
  67. bool DisplayRecordV1(pkgCacheFile &CacheFile, pkgCache::VerIterator const &V,/*{{{*/
  68. std::ostream &out)
  69. {
  70. FileFd PkgF;
  71. pkgCache::VerFileIterator Vf;
  72. if (OpenPackagesFile(CacheFile, V, PkgF, Vf) == false)
  73. return false;
  74. pkgCache * const Cache = CacheFile.GetPkgCache();
  75. if (unlikely(Cache == NULL))
  76. return false;
  77. // Read the record (and ensure that it ends with a newline and NUL)
  78. unsigned char *Buffer = new unsigned char[Cache->HeaderP->MaxVerFileSize+2];
  79. Buffer[Vf->Size] = '\n';
  80. Buffer[Vf->Size+1] = '\0';
  81. if (PkgF.Seek(Vf->Offset) == false ||
  82. PkgF.Read(Buffer,Vf->Size) == false)
  83. {
  84. delete [] Buffer;
  85. return false;
  86. }
  87. // Get a pointer to start of Description field
  88. const unsigned char *DescP = (unsigned char*)strstr((char*)Buffer, "\nDescription");
  89. if (DescP != NULL)
  90. ++DescP;
  91. else
  92. DescP = Buffer + Vf->Size;
  93. // Write all but Description
  94. size_t const length = DescP - Buffer;
  95. if (length != 0 && FileFd::Write(STDOUT_FILENO, Buffer, length) == false)
  96. {
  97. delete [] Buffer;
  98. return false;
  99. }
  100. // Show the right description
  101. pkgRecords Recs(*Cache);
  102. pkgCache::DescIterator Desc = V.TranslatedDescription();
  103. if (Desc.end() == false)
  104. {
  105. pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
  106. out << "Description" << ( (strcmp(Desc.LanguageCode(),"") != 0) ? "-" : "" ) << Desc.LanguageCode() << ": " << P.LongDesc();
  107. out << std::endl << "Description-md5: " << Desc.md5() << std::endl;
  108. // Find the first field after the description (if there is any)
  109. DescP = skipDescriptionFields(DescP);
  110. }
  111. // else we have no translation, so we found a lonely Description-md5 -> don't skip it
  112. // write the rest of the buffer, but skip mixed in Descriptions* fields
  113. while (DescP != NULL)
  114. {
  115. const unsigned char * const Start = DescP;
  116. const unsigned char *End = (unsigned char*)strstr((char*)DescP, "\nDescription");
  117. if (End == NULL)
  118. {
  119. End = &Buffer[Vf->Size];
  120. DescP = NULL;
  121. }
  122. else
  123. {
  124. ++End; // get the newline into the output
  125. DescP = skipDescriptionFields(End + strlen("Description"));
  126. }
  127. size_t const length = End - Start;
  128. if (length != 0 && FileFd::Write(STDOUT_FILENO, Start, length) == false)
  129. {
  130. delete [] Buffer;
  131. return false;
  132. }
  133. }
  134. // write a final newline after the last field
  135. out << std::endl;
  136. delete [] Buffer;
  137. return true;
  138. }
  139. /*}}}*/
  140. static bool DisplayRecordV2(pkgCacheFile &CacheFile, pkgCache::VerIterator const &V,/*{{{*/
  141. std::ostream &out)
  142. {
  143. FileFd PkgF;
  144. pkgCache::VerFileIterator Vf;
  145. if (OpenPackagesFile(CacheFile, V, PkgF, Vf) == false)
  146. return false;
  147. // Check and load the package list file
  148. pkgCache::PkgFileIterator I = Vf.File();
  149. if (I.IsOk() == false)
  150. return _error->Error(_("Package file %s is out of sync."),I.FileName());
  151. // find matching sources.list metaindex
  152. pkgSourceList *SrcList = CacheFile.GetSourceList();
  153. pkgIndexFile *Index;
  154. if (SrcList->FindIndex(I, Index) == false &&
  155. _system->FindIndex(I, Index) == false)
  156. return _error->Error("Can not find indexfile for Package %s (%s)",
  157. V.ParentPkg().Name(), V.VerStr());
  158. std::string source_index_file = Index->Describe(true);
  159. // Read the record
  160. pkgTagSection Tags;
  161. pkgTagFile TagF(&PkgF);
  162. if (TagF.Jump(Tags, V.FileList()->Offset) == false)
  163. return _error->Error("Internal Error, Unable to parse a package record");
  164. // make size nice
  165. std::string installed_size;
  166. if (Tags.FindI("Installed-Size") > 0)
  167. strprintf(installed_size, "%sB", SizeToStr(Tags.FindI("Installed-Size")*1024).c_str());
  168. else
  169. installed_size = _("unknown");
  170. std::string package_size;
  171. if (Tags.FindI("Size") > 0)
  172. strprintf(package_size, "%sB", SizeToStr(Tags.FindI("Size")).c_str());
  173. else
  174. package_size = _("unknown");
  175. const char *manual_installed = nullptr;
  176. if (V.ParentPkg().CurrentVer() == V)
  177. {
  178. pkgDepCache *depCache = CacheFile.GetDepCache();
  179. if (unlikely(depCache == nullptr))
  180. return false;
  181. pkgDepCache::StateCache &state = (*depCache)[V.ParentPkg()];
  182. manual_installed = !(state.Flags & pkgCache::Flag::Auto) ? "yes" : "no";
  183. }
  184. // FIXME: add verbose that does not do the removal of the tags?
  185. std::vector<pkgTagSection::Tag> RW;
  186. // delete, apt-cache show has this info and most users do not care
  187. RW.push_back(pkgTagSection::Tag::Remove("MD5sum"));
  188. RW.push_back(pkgTagSection::Tag::Remove("SHA1"));
  189. RW.push_back(pkgTagSection::Tag::Remove("SHA256"));
  190. RW.push_back(pkgTagSection::Tag::Remove("SHA512"));
  191. RW.push_back(pkgTagSection::Tag::Remove("Filename"));
  192. RW.push_back(pkgTagSection::Tag::Remove("Multi-Arch"));
  193. RW.push_back(pkgTagSection::Tag::Remove("Architecture"));
  194. RW.push_back(pkgTagSection::Tag::Remove("Conffiles"));
  195. // we use the translated description
  196. RW.push_back(pkgTagSection::Tag::Remove("Description"));
  197. RW.push_back(pkgTagSection::Tag::Remove("Description-md5"));
  198. // improve
  199. RW.push_back(pkgTagSection::Tag::Rewrite("Installed-Size", installed_size));
  200. RW.push_back(pkgTagSection::Tag::Remove("Size"));
  201. RW.push_back(pkgTagSection::Tag::Rewrite("Download-Size", package_size));
  202. // add
  203. if (manual_installed != nullptr)
  204. RW.push_back(pkgTagSection::Tag::Rewrite("APT-Manual-Installed", manual_installed));
  205. RW.push_back(pkgTagSection::Tag::Rewrite("APT-Sources", source_index_file));
  206. FileFd stdoutfd;
  207. if (stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false) == false ||
  208. Tags.Write(stdoutfd, TFRewritePackageOrder, RW) == false || stdoutfd.Close() == false)
  209. return _error->Error("Internal Error, Unable to parse a package record");
  210. // write the description
  211. pkgCache * const Cache = CacheFile.GetPkgCache();
  212. if (unlikely(Cache == NULL))
  213. return false;
  214. pkgRecords Recs(*Cache);
  215. // FIXME: show (optionally) all available translations(?)
  216. pkgCache::DescIterator Desc = V.TranslatedDescription();
  217. if (Desc.end() == false)
  218. {
  219. pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
  220. out << "Description: " << P.LongDesc();
  221. }
  222. // write a final newline (after the description)
  223. out << std::endl << std::endl;
  224. return true;
  225. }
  226. /*}}}*/
  227. bool ShowPackage(CommandLine &CmdL) /*{{{*/
  228. {
  229. pkgCacheFile CacheFile;
  230. CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
  231. APT::CacheSetHelper::VerSelector const select = _config->FindB("APT::Cache::AllVersions", true) ?
  232. APT::CacheSetHelper::ALL : APT::CacheSetHelper::CANDIDATE;
  233. APT::VersionList const verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, select, helper);
  234. int const ShowVersion = _config->FindI("APT::Cache::Show::Version", 1);
  235. for (APT::VersionList::const_iterator Ver = verset.begin(); Ver != verset.end(); ++Ver)
  236. if (ShowVersion <= 1)
  237. {
  238. if (DisplayRecordV1(CacheFile, Ver, std::cout) == false)
  239. return false;
  240. }
  241. else
  242. if (DisplayRecordV2(CacheFile, Ver, c1out) == false)
  243. return false;
  244. if (select == APT::CacheSetHelper::CANDIDATE)
  245. {
  246. APT::VersionList const verset_all = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::ALL, helper);
  247. int const records = verset_all.size() - verset.size();
  248. if (records > 0)
  249. _error->Notice(P_("There is %i additional record. Please use the '-a' switch to see it", "There are %i additional records. Please use the '-a' switch to see them.", records), records);
  250. }
  251. if (_config->FindB("APT::Cache::ShowVirtuals", false) == true)
  252. for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
  253. Pkg != helper.virtualPkgs.end(); ++Pkg)
  254. {
  255. c1out << "Package: " << Pkg.FullName(true) << std::endl;
  256. c1out << "State: " << _("not a real package (virtual)") << std::endl;
  257. // FIXME: show providers, see private-cacheset.h
  258. // CacheSetHelperAPTGet::showVirtualPackageErrors()
  259. }
  260. if (verset.empty() == true)
  261. {
  262. if (helper.virtualPkgs.empty() == true)
  263. return _error->Error(_("No packages found"));
  264. else
  265. _error->Notice(_("No packages found"));
  266. }
  267. return true;
  268. }
  269. /*}}}*/
  270. bool ShowSrcPackage(CommandLine &CmdL) /*{{{*/
  271. {
  272. pkgCacheFile CacheFile;
  273. pkgSourceList *List = CacheFile.GetSourceList();
  274. if (unlikely(List == NULL))
  275. return false;
  276. // Create the text record parsers
  277. pkgSrcRecords SrcRecs(*List);
  278. if (_error->PendingError() == true)
  279. return false;
  280. bool found = false;
  281. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  282. {
  283. SrcRecs.Restart();
  284. pkgSrcRecords::Parser *Parse;
  285. bool found_this = false;
  286. while ((Parse = SrcRecs.Find(*I,false)) != 0) {
  287. // SrcRecs.Find() will find both binary and source names
  288. if (_config->FindB("APT::Cache::Only-Source", false) == true)
  289. if (Parse->Package() != *I)
  290. continue;
  291. std::cout << Parse->AsStr() << std::endl;;
  292. found = true;
  293. found_this = true;
  294. }
  295. if (found_this == false) {
  296. _error->Warning(_("Unable to locate package %s"),*I);
  297. continue;
  298. }
  299. }
  300. if (found == false)
  301. _error->Notice(_("No packages found"));
  302. return true;
  303. }
  304. /*}}}*/
  305. // Policy - Show the results of the preferences file /*{{{*/
  306. bool Policy(CommandLine &CmdL)
  307. {
  308. pkgCacheFile CacheFile;
  309. pkgCache *Cache = CacheFile.GetPkgCache();
  310. pkgPolicy *Plcy = CacheFile.GetPolicy();
  311. pkgSourceList *SrcList = CacheFile.GetSourceList();
  312. if (unlikely(Cache == NULL || Plcy == NULL || SrcList == NULL))
  313. return false;
  314. bool OldPolicy = _config->FindI("APT::Policy", 1) < 1;
  315. // Print out all of the package files
  316. if (CmdL.FileList[1] == 0)
  317. {
  318. std::cout << _("Package files:") << std::endl;
  319. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  320. {
  321. if (F.Flagged(pkgCache::Flag::NoPackages))
  322. continue;
  323. // Locate the associated index files so we can derive a description
  324. pkgIndexFile *Indx;
  325. if (SrcList->FindIndex(F,Indx) == false &&
  326. _system->FindIndex(F,Indx) == false)
  327. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  328. printf("%4i %s\n",
  329. Plcy->GetPriority(F),Indx->Describe(true).c_str());
  330. // Print the reference information for the package
  331. std::string Str = F.RelStr();
  332. if (Str.empty() == false)
  333. printf(" release %s\n",F.RelStr().c_str());
  334. if (F.Site() != 0 && F.Site()[0] != 0)
  335. printf(" origin %s\n",F.Site());
  336. }
  337. // Show any packages have explicit pins
  338. std::cout << _("Pinned packages:") << std::endl;
  339. pkgCache::PkgIterator I = Cache->PkgBegin();
  340. for (;I.end() != true; ++I)
  341. {
  342. // Old code for debugging
  343. if (OldPolicy)
  344. {
  345. if (Plcy->GetPriority(I) == 0)
  346. continue;
  347. // Print the package name and the version we are forcing to
  348. std::cout << " " << I.FullName(true) << " -> ";
  349. pkgCache::VerIterator V = Plcy->GetMatch(I);
  350. if (V.end() == true)
  351. std::cout << _("(not found)") << std::endl;
  352. else
  353. std::cout << V.VerStr() << std::endl;
  354. continue;
  355. }
  356. // New code
  357. for (pkgCache::VerIterator V = I.VersionList(); !V.end(); ++V) {
  358. auto Prio = Plcy->GetPriority(V, false);
  359. if (Prio == 0)
  360. continue;
  361. std::cout << " ";
  362. // Print the package name and the version we are forcing to
  363. ioprintf(std::cout, _("%s -> %s with priority %d\n"), I.FullName(true).c_str(), V.VerStr(), Prio);
  364. }
  365. }
  366. return true;
  367. }
  368. char const * const msgInstalled = _(" Installed: ");
  369. char const * const msgCandidate = _(" Candidate: ");
  370. short const InstalledLessCandidate =
  371. mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
  372. short const deepInstalled =
  373. (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
  374. short const deepCandidate =
  375. (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
  376. // Print out detailed information for each package
  377. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  378. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  379. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  380. {
  381. std::cout << Pkg.FullName(true) << ":" << std::endl;
  382. // Installed version
  383. std::cout << msgInstalled << OutputInDepth(deepInstalled, " ");
  384. if (Pkg->CurrentVer == 0)
  385. std::cout << _("(none)") << std::endl;
  386. else
  387. std::cout << Pkg.CurrentVer().VerStr() << std::endl;
  388. // Candidate Version
  389. std::cout << msgCandidate << OutputInDepth(deepCandidate, " ");
  390. pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
  391. if (V.end() == true)
  392. std::cout << _("(none)") << std::endl;
  393. else
  394. std::cout << V.VerStr() << std::endl;
  395. // Pinned version
  396. if (OldPolicy && Plcy->GetPriority(Pkg) != 0)
  397. {
  398. std::cout << _(" Package pin: ");
  399. V = Plcy->GetMatch(Pkg);
  400. if (V.end() == true)
  401. std::cout << _("(not found)") << std::endl;
  402. else
  403. std::cout << V.VerStr() << std::endl;
  404. }
  405. // Show the priority tables
  406. std::cout << _(" Version table:") << std::endl;
  407. for (V = Pkg.VersionList(); V.end() == false; ++V)
  408. {
  409. if (Pkg.CurrentVer() == V)
  410. std::cout << " *** " << V.VerStr();
  411. else
  412. std::cout << " " << V.VerStr();
  413. if (_config->FindI("APT::Policy", 1) < 1)
  414. std::cout << " " << Plcy->GetPriority(Pkg) << std::endl;
  415. else
  416. std::cout << " " << Plcy->GetPriority(V) << std::endl;
  417. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  418. {
  419. // Locate the associated index files so we can derive a description
  420. pkgIndexFile *Indx;
  421. if (SrcList->FindIndex(VF.File(),Indx) == false &&
  422. _system->FindIndex(VF.File(),Indx) == false)
  423. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  424. printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
  425. Indx->Describe(true).c_str());
  426. }
  427. }
  428. }
  429. return true;
  430. }
  431. /*}}}*/