private-show.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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("Package", V.ParentPkg().FullName(true)));
  200. RW.push_back(pkgTagSection::Tag::Rewrite("Installed-Size", installed_size));
  201. RW.push_back(pkgTagSection::Tag::Remove("Size"));
  202. RW.push_back(pkgTagSection::Tag::Rewrite("Download-Size", package_size));
  203. // add
  204. if (manual_installed != nullptr)
  205. RW.push_back(pkgTagSection::Tag::Rewrite("APT-Manual-Installed", manual_installed));
  206. RW.push_back(pkgTagSection::Tag::Rewrite("APT-Sources", source_index_file));
  207. FileFd stdoutfd;
  208. if (stdoutfd.OpenDescriptor(STDOUT_FILENO, FileFd::WriteOnly, false) == false ||
  209. Tags.Write(stdoutfd, TFRewritePackageOrder, RW) == false || stdoutfd.Close() == false)
  210. return _error->Error("Internal Error, Unable to parse a package record");
  211. // write the description
  212. pkgCache * const Cache = CacheFile.GetPkgCache();
  213. if (unlikely(Cache == NULL))
  214. return false;
  215. pkgRecords Recs(*Cache);
  216. // FIXME: show (optionally) all available translations(?)
  217. pkgCache::DescIterator Desc = V.TranslatedDescription();
  218. if (Desc.end() == false)
  219. {
  220. pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
  221. out << "Description: " << P.LongDesc();
  222. }
  223. // write a final newline (after the description)
  224. out << std::endl << std::endl;
  225. return true;
  226. }
  227. /*}}}*/
  228. bool ShowPackage(CommandLine &CmdL) /*{{{*/
  229. {
  230. pkgCacheFile CacheFile;
  231. CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
  232. APT::CacheSetHelper::VerSelector const select = _config->FindB("APT::Cache::AllVersions", true) ?
  233. APT::CacheSetHelper::ALL : APT::CacheSetHelper::CANDIDATE;
  234. APT::VersionList const verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, select, helper);
  235. int const ShowVersion = _config->FindI("APT::Cache::Show::Version", 1);
  236. for (APT::VersionList::const_iterator Ver = verset.begin(); Ver != verset.end(); ++Ver)
  237. if (ShowVersion <= 1)
  238. {
  239. if (DisplayRecordV1(CacheFile, Ver, std::cout) == false)
  240. return false;
  241. }
  242. else
  243. if (DisplayRecordV2(CacheFile, Ver, c1out) == false)
  244. return false;
  245. if (select == APT::CacheSetHelper::CANDIDATE)
  246. {
  247. APT::VersionList const verset_all = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::ALL, helper);
  248. int const records = verset_all.size() - verset.size();
  249. if (records > 0)
  250. _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);
  251. }
  252. if (_config->FindB("APT::Cache::ShowVirtuals", false) == true)
  253. for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
  254. Pkg != helper.virtualPkgs.end(); ++Pkg)
  255. {
  256. c1out << "Package: " << Pkg.FullName(true) << std::endl;
  257. c1out << "State: " << _("not a real package (virtual)") << std::endl;
  258. // FIXME: show providers, see private-cacheset.h
  259. // CacheSetHelperAPTGet::showVirtualPackageErrors()
  260. }
  261. if (verset.empty() == true)
  262. {
  263. if (helper.virtualPkgs.empty() == true)
  264. return _error->Error(_("No packages found"));
  265. else
  266. _error->Notice(_("No packages found"));
  267. }
  268. return true;
  269. }
  270. /*}}}*/
  271. bool ShowSrcPackage(CommandLine &CmdL) /*{{{*/
  272. {
  273. pkgCacheFile CacheFile;
  274. pkgSourceList *List = CacheFile.GetSourceList();
  275. if (unlikely(List == NULL))
  276. return false;
  277. // Create the text record parsers
  278. pkgSrcRecords SrcRecs(*List);
  279. if (_error->PendingError() == true)
  280. return false;
  281. bool found = false;
  282. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  283. {
  284. SrcRecs.Restart();
  285. pkgSrcRecords::Parser *Parse;
  286. bool found_this = false;
  287. while ((Parse = SrcRecs.Find(*I,false)) != 0) {
  288. // SrcRecs.Find() will find both binary and source names
  289. if (_config->FindB("APT::Cache::Only-Source", false) == true)
  290. if (Parse->Package() != *I)
  291. continue;
  292. std::cout << Parse->AsStr() << std::endl;;
  293. found = true;
  294. found_this = true;
  295. }
  296. if (found_this == false) {
  297. _error->Warning(_("Unable to locate package %s"),*I);
  298. continue;
  299. }
  300. }
  301. if (found == false)
  302. _error->Notice(_("No packages found"));
  303. return true;
  304. }
  305. /*}}}*/
  306. // Policy - Show the results of the preferences file /*{{{*/
  307. bool Policy(CommandLine &CmdL)
  308. {
  309. pkgCacheFile CacheFile;
  310. pkgCache *Cache = CacheFile.GetPkgCache();
  311. pkgPolicy *Plcy = CacheFile.GetPolicy();
  312. pkgSourceList *SrcList = CacheFile.GetSourceList();
  313. if (unlikely(Cache == NULL || Plcy == NULL || SrcList == NULL))
  314. return false;
  315. bool OldPolicy = _config->FindI("APT::Policy", 1) < 1;
  316. // Print out all of the package files
  317. if (CmdL.FileList[1] == 0)
  318. {
  319. std::cout << _("Package files:") << std::endl;
  320. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  321. {
  322. if (F.Flagged(pkgCache::Flag::NoPackages))
  323. continue;
  324. // Locate the associated index files so we can derive a description
  325. pkgIndexFile *Indx;
  326. if (SrcList->FindIndex(F,Indx) == false &&
  327. _system->FindIndex(F,Indx) == false)
  328. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  329. printf("%4i %s\n",
  330. Plcy->GetPriority(F),Indx->Describe(true).c_str());
  331. // Print the reference information for the package
  332. std::string Str = F.RelStr();
  333. if (Str.empty() == false)
  334. printf(" release %s\n",F.RelStr().c_str());
  335. if (F.Site() != 0 && F.Site()[0] != 0)
  336. printf(" origin %s\n",F.Site());
  337. }
  338. // Show any packages have explicit pins
  339. std::cout << _("Pinned packages:") << std::endl;
  340. pkgCache::PkgIterator I = Cache->PkgBegin();
  341. for (;I.end() != true; ++I)
  342. {
  343. // Old code for debugging
  344. if (OldPolicy)
  345. {
  346. if (Plcy->GetPriority(I) == 0)
  347. continue;
  348. // Print the package name and the version we are forcing to
  349. std::cout << " " << I.FullName(true) << " -> ";
  350. pkgCache::VerIterator V = Plcy->GetMatch(I);
  351. if (V.end() == true)
  352. std::cout << _("(not found)") << std::endl;
  353. else
  354. std::cout << V.VerStr() << std::endl;
  355. continue;
  356. }
  357. // New code
  358. for (pkgCache::VerIterator V = I.VersionList(); !V.end(); ++V) {
  359. auto Prio = Plcy->GetPriority(V, false);
  360. if (Prio == 0)
  361. continue;
  362. std::cout << " ";
  363. // Print the package name and the version we are forcing to
  364. ioprintf(std::cout, _("%s -> %s with priority %d\n"), I.FullName(true).c_str(), V.VerStr(), Prio);
  365. }
  366. }
  367. return true;
  368. }
  369. char const * const msgInstalled = _(" Installed: ");
  370. char const * const msgCandidate = _(" Candidate: ");
  371. short const InstalledLessCandidate =
  372. mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
  373. short const deepInstalled =
  374. (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
  375. short const deepCandidate =
  376. (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
  377. // Print out detailed information for each package
  378. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  379. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  380. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  381. {
  382. std::cout << Pkg.FullName(true) << ":" << std::endl;
  383. // Installed version
  384. std::cout << msgInstalled << OutputInDepth(deepInstalled, " ");
  385. if (Pkg->CurrentVer == 0)
  386. std::cout << _("(none)") << std::endl;
  387. else
  388. std::cout << Pkg.CurrentVer().VerStr() << std::endl;
  389. // Candidate Version
  390. std::cout << msgCandidate << OutputInDepth(deepCandidate, " ");
  391. pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
  392. if (V.end() == true)
  393. std::cout << _("(none)") << std::endl;
  394. else
  395. std::cout << V.VerStr() << std::endl;
  396. // Pinned version
  397. if (OldPolicy && Plcy->GetPriority(Pkg) != 0)
  398. {
  399. std::cout << _(" Package pin: ");
  400. V = Plcy->GetMatch(Pkg);
  401. if (V.end() == true)
  402. std::cout << _("(not found)") << std::endl;
  403. else
  404. std::cout << V.VerStr() << std::endl;
  405. }
  406. // Show the priority tables
  407. std::cout << _(" Version table:") << std::endl;
  408. for (V = Pkg.VersionList(); V.end() == false; ++V)
  409. {
  410. if (Pkg.CurrentVer() == V)
  411. std::cout << " *** " << V.VerStr();
  412. else
  413. std::cout << " " << V.VerStr();
  414. if (_config->FindI("APT::Policy", 1) < 1)
  415. std::cout << " " << Plcy->GetPriority(Pkg) << std::endl;
  416. else
  417. std::cout << " " << Plcy->GetPriority(V) << std::endl;
  418. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  419. {
  420. // Locate the associated index files so we can derive a description
  421. pkgIndexFile *Indx;
  422. if (SrcList->FindIndex(VF.File(),Indx) == false &&
  423. _system->FindIndex(VF.File(),Indx) == false)
  424. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  425. printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
  426. Indx->Describe(true).c_str());
  427. }
  428. }
  429. }
  430. return true;
  431. }
  432. /*}}}*/