private-show.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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. static std::string Sha1FromString(std::string const &input) /*{{{*/
  272. {
  273. // XXX: move to hashes.h: HashString::FromString() ?
  274. SHA1Summation sha1;
  275. sha1.Add(input.c_str(), input.length());
  276. return sha1.Result().Value();
  277. }
  278. /*}}}*/
  279. bool ShowSrcPackage(CommandLine &CmdL) /*{{{*/
  280. {
  281. pkgCacheFile CacheFile;
  282. pkgSourceList *List = CacheFile.GetSourceList();
  283. if (unlikely(List == NULL))
  284. return false;
  285. // Create the text record parsers
  286. pkgSrcRecords SrcRecs(*List);
  287. if (_error->PendingError() == true)
  288. return false;
  289. bool found = false;
  290. // avoid showing identical records
  291. std::set<std::string> seen;
  292. for (const char **I = CmdL.FileList + 1; *I != 0; I++)
  293. {
  294. SrcRecs.Restart();
  295. pkgSrcRecords::Parser *Parse;
  296. bool found_this = false;
  297. while ((Parse = SrcRecs.Find(*I,false)) != 0) {
  298. // SrcRecs.Find() will find both binary and source names
  299. if (_config->FindB("APT::Cache::Only-Source", false) == true)
  300. if (Parse->Package() != *I)
  301. continue;
  302. std::string sha1str = Sha1FromString(Parse->AsStr());
  303. if (std::find(seen.begin(), seen.end(), sha1str) == seen.end())
  304. {
  305. std::cout << Parse->AsStr() << std::endl;;
  306. found = true;
  307. found_this = true;
  308. seen.insert(sha1str);
  309. }
  310. }
  311. if (found_this == false) {
  312. _error->Warning(_("Unable to locate package %s"),*I);
  313. continue;
  314. }
  315. }
  316. if (found == false)
  317. _error->Notice(_("No packages found"));
  318. return true;
  319. }
  320. /*}}}*/
  321. // Policy - Show the results of the preferences file /*{{{*/
  322. bool Policy(CommandLine &CmdL)
  323. {
  324. pkgCacheFile CacheFile;
  325. pkgCache *Cache = CacheFile.GetPkgCache();
  326. pkgPolicy *Plcy = CacheFile.GetPolicy();
  327. pkgSourceList *SrcList = CacheFile.GetSourceList();
  328. if (unlikely(Cache == NULL || Plcy == NULL || SrcList == NULL))
  329. return false;
  330. bool OldPolicy = _config->FindI("APT::Policy", 1) < 1;
  331. // Print out all of the package files
  332. if (CmdL.FileList[1] == 0)
  333. {
  334. std::cout << _("Package files:") << std::endl;
  335. for (pkgCache::PkgFileIterator F = Cache->FileBegin(); F.end() == false; ++F)
  336. {
  337. if (F.Flagged(pkgCache::Flag::NoPackages))
  338. continue;
  339. // Locate the associated index files so we can derive a description
  340. pkgIndexFile *Indx;
  341. if (SrcList->FindIndex(F,Indx) == false &&
  342. _system->FindIndex(F,Indx) == false)
  343. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  344. printf("%4i %s\n",
  345. Plcy->GetPriority(F),Indx->Describe(true).c_str());
  346. // Print the reference information for the package
  347. std::string Str = F.RelStr();
  348. if (Str.empty() == false)
  349. printf(" release %s\n",F.RelStr().c_str());
  350. if (F.Site() != 0 && F.Site()[0] != 0)
  351. printf(" origin %s\n",F.Site());
  352. }
  353. // Show any packages have explicit pins
  354. std::cout << _("Pinned packages:") << std::endl;
  355. pkgCache::PkgIterator I = Cache->PkgBegin();
  356. for (;I.end() != true; ++I)
  357. {
  358. // Old code for debugging
  359. if (OldPolicy)
  360. {
  361. if (Plcy->GetPriority(I) == 0)
  362. continue;
  363. // Print the package name and the version we are forcing to
  364. std::cout << " " << I.FullName(true) << " -> ";
  365. pkgCache::VerIterator V = Plcy->GetMatch(I);
  366. if (V.end() == true)
  367. std::cout << _("(not found)") << std::endl;
  368. else
  369. std::cout << V.VerStr() << std::endl;
  370. continue;
  371. }
  372. // New code
  373. for (pkgCache::VerIterator V = I.VersionList(); !V.end(); ++V) {
  374. auto Prio = Plcy->GetPriority(V, false);
  375. if (Prio == 0)
  376. continue;
  377. std::cout << " ";
  378. // Print the package name and the version we are forcing to
  379. ioprintf(std::cout, _("%s -> %s with priority %d\n"), I.FullName(true).c_str(), V.VerStr(), Prio);
  380. }
  381. }
  382. return true;
  383. }
  384. char const * const msgInstalled = _(" Installed: ");
  385. char const * const msgCandidate = _(" Candidate: ");
  386. short const InstalledLessCandidate =
  387. mbstowcs(NULL, msgInstalled, 0) - mbstowcs(NULL, msgCandidate, 0);
  388. short const deepInstalled =
  389. (InstalledLessCandidate < 0 ? (InstalledLessCandidate*-1) : 0) - 1;
  390. short const deepCandidate =
  391. (InstalledLessCandidate > 0 ? (InstalledLessCandidate) : 0) - 1;
  392. // Print out detailed information for each package
  393. APT::CacheSetHelper helper(true, GlobalError::NOTICE);
  394. APT::PackageList pkgset = APT::PackageList::FromCommandLine(CacheFile, CmdL.FileList + 1, helper);
  395. for (APT::PackageList::const_iterator Pkg = pkgset.begin(); Pkg != pkgset.end(); ++Pkg)
  396. {
  397. std::cout << Pkg.FullName(true) << ":" << std::endl;
  398. // Installed version
  399. std::cout << msgInstalled << OutputInDepth(deepInstalled, " ");
  400. if (Pkg->CurrentVer == 0)
  401. std::cout << _("(none)") << std::endl;
  402. else
  403. std::cout << Pkg.CurrentVer().VerStr() << std::endl;
  404. // Candidate Version
  405. std::cout << msgCandidate << OutputInDepth(deepCandidate, " ");
  406. pkgCache::VerIterator V = Plcy->GetCandidateVer(Pkg);
  407. if (V.end() == true)
  408. std::cout << _("(none)") << std::endl;
  409. else
  410. std::cout << V.VerStr() << std::endl;
  411. // Pinned version
  412. if (OldPolicy && Plcy->GetPriority(Pkg) != 0)
  413. {
  414. std::cout << _(" Package pin: ");
  415. V = Plcy->GetMatch(Pkg);
  416. if (V.end() == true)
  417. std::cout << _("(not found)") << std::endl;
  418. else
  419. std::cout << V.VerStr() << std::endl;
  420. }
  421. // Show the priority tables
  422. std::cout << _(" Version table:") << std::endl;
  423. for (V = Pkg.VersionList(); V.end() == false; ++V)
  424. {
  425. if (Pkg.CurrentVer() == V)
  426. std::cout << " *** " << V.VerStr();
  427. else
  428. std::cout << " " << V.VerStr();
  429. if (_config->FindI("APT::Policy", 1) < 1)
  430. std::cout << " " << Plcy->GetPriority(Pkg) << std::endl;
  431. else
  432. std::cout << " " << Plcy->GetPriority(V) << std::endl;
  433. for (pkgCache::VerFileIterator VF = V.FileList(); VF.end() == false; ++VF)
  434. {
  435. // Locate the associated index files so we can derive a description
  436. pkgIndexFile *Indx;
  437. if (SrcList->FindIndex(VF.File(),Indx) == false &&
  438. _system->FindIndex(VF.File(),Indx) == false)
  439. return _error->Error(_("Cache is out of sync, can't x-ref a package file"));
  440. printf(" %4i %s\n",Plcy->GetPriority(VF.File()),
  441. Indx->Describe(true).c_str());
  442. }
  443. }
  444. }
  445. return true;
  446. }
  447. /*}}}*/