private-show.cc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. if (select == APT::CacheSetHelper::CANDIDATE && CacheFile.GetDepCache() == nullptr)
  235. return false;
  236. APT::VersionList const verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, select, helper);
  237. int const ShowVersion = _config->FindI("APT::Cache::Show::Version", 1);
  238. for (APT::VersionList::const_iterator Ver = verset.begin(); Ver != verset.end(); ++Ver)
  239. if (ShowVersion <= 1)
  240. {
  241. if (DisplayRecordV1(CacheFile, Ver, std::cout) == false)
  242. return false;
  243. }
  244. else
  245. if (DisplayRecordV2(CacheFile, Ver, c1out) == false)
  246. return false;
  247. if (select == APT::CacheSetHelper::CANDIDATE)
  248. {
  249. APT::VersionList const verset_all = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::CacheSetHelper::ALL, helper);
  250. int const records = verset_all.size() - verset.size();
  251. if (records > 0)
  252. _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);
  253. }
  254. if (_config->FindB("APT::Cache::ShowVirtuals", false) == true)
  255. for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
  256. Pkg != helper.virtualPkgs.end(); ++Pkg)
  257. {
  258. c1out << "Package: " << Pkg.FullName(true) << std::endl;
  259. c1out << "State: " << _("not a real package (virtual)") << std::endl;
  260. // FIXME: show providers, see private-cacheset.h
  261. // CacheSetHelperAPTGet::showVirtualPackageErrors()
  262. }
  263. if (verset.empty() == true)
  264. {
  265. if (helper.virtualPkgs.empty() == true)
  266. return _error->Error(_("No packages found"));
  267. else
  268. _error->Notice(_("No packages found"));
  269. }
  270. return true;
  271. }
  272. /*}}}*/
  273. static std::string Sha1FromString(std::string const &input) /*{{{*/
  274. {
  275. // XXX: move to hashes.h: HashString::FromString() ?
  276. SHA1Summation sha1;
  277. sha1.Add(input.c_str(), input.length());
  278. return sha1.Result().Value();
  279. }
  280. /*}}}*/
  281. bool ShowSrcPackage(CommandLine &CmdL) /*{{{*/
  282. {
  283. pkgCacheFile CacheFile;
  284. pkgSourceList *List = CacheFile.GetSourceList();
  285. if (unlikely(List == NULL))
  286. return false;
  287. // Create the text record parsers
  288. pkgSrcRecords SrcRecs(*List);
  289. if (_error->PendingError() == true)
  290. return false;
  291. bool found = false;
  292. // avoid showing identical records
  293. std::set<std::string> seen;
  294. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  295. {
  296. SrcRecs.Restart();
  297. pkgSrcRecords::Parser *Parse;
  298. bool found_this = false;
  299. while ((Parse = SrcRecs.Find(*I,false)) != 0) {
  300. // SrcRecs.Find() will find both binary and source names
  301. if (_config->FindB("APT::Cache::Only-Source", false) == true)
  302. if (Parse->Package() != *I)
  303. continue;
  304. std::string sha1str = Sha1FromString(Parse->AsStr());
  305. if (std::find(seen.begin(), seen.end(), sha1str) == seen.end())
  306. {
  307. std::cout << Parse->AsStr() << std::endl;;
  308. found = true;
  309. found_this = true;
  310. seen.insert(sha1str);
  311. }
  312. }
  313. if (found_this == false) {
  314. _error->Warning(_("Unable to locate package %s"),*I);
  315. continue;
  316. }
  317. }
  318. if (found == false)
  319. _error->Notice(_("No packages found"));
  320. return true;
  321. }
  322. /*}}}*/
  323. // Policy - Show the results of the preferences file /*{{{*/
  324. bool Policy(CommandLine &CmdL)
  325. {
  326. pkgCacheFile CacheFile;
  327. pkgSourceList const * const SrcList = CacheFile.GetSourceList();
  328. if (unlikely(SrcList == nullptr))
  329. return false;
  330. pkgCache * const Cache = CacheFile.GetPkgCache();
  331. if (unlikely(Cache == nullptr))
  332. return false;
  333. pkgPolicy * const Plcy = CacheFile.GetPolicy();
  334. if (unlikely(Plcy == nullptr))
  335. return false;
  336. // Print out all of the package files
  337. if (CmdL.FileList[1] == 0)
  338. {
  339. std::cout << _("Package files:") << std::endl;
  340. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  341. {
  342. if (F.Flagged(pkgCache::Flag::NoPackages))
  343. continue;
  344. // Locate the associated index files so we can derive a description
  345. pkgIndexFile *Indx;
  346. if (SrcList->FindIndex(F,Indx) == false &&
  347. _system->FindIndex(F,Indx) == false)
  348. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  349. printf("%4i %s\n",
  350. Plcy->GetPriority(F),Indx->Describe(true).c_str());
  351. // Print the reference information for the package
  352. std::string Str = F.RelStr();
  353. if (Str.empty() == false)
  354. printf(" release %s\n",F.RelStr().c_str());
  355. if (F.Site() != 0 && F.Site()[0] != 0)
  356. printf(" origin %s\n",F.Site());
  357. }
  358. // Show any packages have explicit pins
  359. std::cout << _("Pinned packages:") << std::endl;
  360. pkgCache::PkgIterator I = Cache->PkgBegin();
  361. for (;I.end() != true; ++I)
  362. {
  363. for (pkgCache::VerIterator V = I.VersionList(); !V.end(); ++V) {
  364. auto Prio = Plcy->GetPriority(V, false);
  365. if (Prio == 0)
  366. continue;
  367. std::cout << " ";
  368. // Print the package name and the version we are forcing to
  369. ioprintf(std::cout, _("%s -> %s with priority %d\n"), I.FullName(true).c_str(), V.VerStr(), Prio);
  370. }
  371. }
  372. return true;
  373. }
  374. char const * const msgInstalled = _(" Installed: ");
  375. char const * const msgCandidate = _(" Candidate: ");
  376. short const InstalledLessCandidate =
  377. mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
  378. short const deepInstalled =
  379. (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
  380. short const deepCandidate =
  381. (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
  382. // Print out detailed information for each package
  383. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  384. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  385. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  386. {
  387. std::cout << Pkg.FullName(true) << ":" << std::endl;
  388. // Installed version
  389. std::cout << msgInstalled << OutputInDepth(deepInstalled, " ");
  390. if (Pkg->CurrentVer == 0)
  391. std::cout << _("(none)") << std::endl;
  392. else
  393. std::cout << Pkg.CurrentVer().VerStr() << std::endl;
  394. // Candidate Version
  395. std::cout << msgCandidate << OutputInDepth(deepCandidate, " ");
  396. pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
  397. if (V.end() == true)
  398. std::cout << _("(none)") << std::endl;
  399. else
  400. std::cout << V.VerStr() << std::endl;
  401. // Show the priority tables
  402. std::cout << _(" Version table:") << std::endl;
  403. for (V = Pkg.VersionList(); V.end() == false; ++V)
  404. {
  405. if (Pkg.CurrentVer() == V)
  406. std::cout << " *** " << V.VerStr();
  407. else
  408. std::cout << " " << V.VerStr();
  409. std::cout << " " << Plcy->GetPriority(V) << std::endl;
  410. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  411. {
  412. // Locate the associated index files so we can derive a description
  413. pkgIndexFile *Indx;
  414. if (SrcList->FindIndex(VF.File(),Indx) == false &&
  415. _system->FindIndex(VF.File(),Indx) == false)
  416. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  417. printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
  418. Indx->Describe(true).c_str());
  419. }
  420. }
  421. }
  422. return true;
  423. }
  424. /*}}}*/