private-show.cc 16 KB

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