Show "Manual-Installed: {yes|no}" in apt show
[ntk/apt.git] / apt-private / private-show.cc
1 // Includes /*{{{*/
2 #include <apt-pkg/error.h>
3 #include <apt-pkg/cachefile.h>
4 #include <apt-pkg/cachefilter.h>
5 #include <apt-pkg/cacheset.h>
6 #include <apt-pkg/init.h>
7 #include <apt-pkg/progress.h>
8 #include <apt-pkg/sourcelist.h>
9 #include <apt-pkg/cmndline.h>
10 #include <apt-pkg/strutl.h>
11 #include <apt-pkg/fileutl.h>
12 #include <apt-pkg/pkgrecords.h>
13 #include <apt-pkg/srcrecords.h>
14 #include <apt-pkg/version.h>
15 #include <apt-pkg/policy.h>
16 #include <apt-pkg/tagfile.h>
17 #include <apt-pkg/algorithms.h>
18 #include <apt-pkg/sptr.h>
19 #include <apt-pkg/pkgsystem.h>
20 #include <apt-pkg/indexfile.h>
21 #include <apt-pkg/metaindex.h>
22
23 #include <apti18n.h>
24
25 #include "private-output.h"
26 #include "private-cacheset.h"
27 /*}}}*/
28
29 namespace APT {
30 namespace Cmd {
31
32 // DisplayRecord - Displays the complete record for the package /*{{{*/
33 // ---------------------------------------------------------------------
34 bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V,
35 ostream &out)
36 {
37 pkgCache *Cache = CacheFile.GetPkgCache();
38 if (unlikely(Cache == NULL))
39 return false;
40 pkgDepCache *depCache = CacheFile.GetDepCache();
41 if (unlikely(depCache == NULL))
42 return false;
43
44 // Find an appropriate file
45 pkgCache::VerFileIterator Vf = V.FileList();
46 for (; Vf.end() == false; ++Vf)
47 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
48 break;
49 if (Vf.end() == true)
50 Vf = V.FileList();
51
52 // Check and load the package list file
53 pkgCache::PkgFileIterator I = Vf.File();
54 if (I.IsOk() == false)
55 return _error->Error(_("Package file %s is out of sync."),I.FileName());
56
57 // Read the record
58 FileFd PkgF;
59 if (PkgF.Open(I.FileName(), FileFd::ReadOnly, FileFd::Extension) == false)
60 return false;
61 pkgTagSection Tags;
62 pkgTagFile TagF(&PkgF);
63
64 if (TagF.Jump(Tags, V.FileList()->Offset) == false)
65 return _error->Error("Internal Error, Unable to parse a package record");
66
67 // make size nice
68 std::string installed_size;
69 if (Tags.FindI("Installed-Size") > 0)
70 installed_size = SizeToStr(Tags.FindI("Installed-Size")*1024);
71 else
72 installed_size = _("unknown");
73 std::string package_size;
74 if (Tags.FindI("Size") > 0)
75 package_size = SizeToStr(Tags.FindI("Size"));
76 else
77 package_size = _("unknown");
78
79 pkgDepCache::StateCache &state = (*depCache)[V.ParentPkg()];
80 bool is_installed = V.ParentPkg().CurrentVer() == V;
81 const char *manual_installed;
82 if (is_installed)
83 manual_installed = !(state.Flags & pkgCache::Flag::Auto) ? "yes" : "no";
84 else
85 manual_installed = 0;
86 std::string suite = GetArchiveSuite(CacheFile, V);
87 TFRewriteData RW[] = {
88 {"Conffiles",0},
89 {"Description",0},
90 {"Description-md5",0},
91 {"Installed-Size", installed_size.c_str(), 0},
92 {"Size", package_size.c_str(), "Download-Size"},
93 {"Archive-Origin", suite.c_str(), 0},
94 {"Manual-Installed", manual_installed, 0},
95 {}
96 };
97
98 if(TFRewrite(stdout, Tags, NULL, RW) == false)
99 return _error->Error("Internal Error, Unable to parse a package record");
100
101 // write the description
102 pkgRecords Recs(*Cache);
103 // FIXME: show (optionally) all available translations(?)
104 pkgCache::DescIterator Desc = V.TranslatedDescription();
105 if (Desc.end() == false)
106 {
107 pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
108 out << "Description: " << P.LongDesc();
109 }
110
111 // write a final newline (after the description)
112 out << std::endl << std::endl;
113
114 return true;
115 }
116 /*}}}*/
117 bool ShowPackage(CommandLine &CmdL) /*{{{*/
118 {
119 pkgCacheFile CacheFile;
120 CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
121 APT::VersionList::Version const select = _config->FindB("APT::Cache::AllVersions", false) ?
122 APT::VersionList::ALL : APT::VersionList::CANDIDATE;
123 APT::VersionList const verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, select, helper);
124 for (APT::VersionList::const_iterator Ver = verset.begin(); Ver != verset.end(); ++Ver)
125 if (DisplayRecord(CacheFile, Ver, c1out) == false)
126 return false;
127
128 if (select == APT::VersionList::CANDIDATE)
129 {
130 APT::VersionList const verset_all = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::VersionList::ALL, helper);
131 if (verset_all.size() > verset.size())
132 _error->Notice(ngettext("There is %lu additional record. Please use the '-a' switch to see it", "There are %lu additional records. Please use the '-a' switch to see them.", verset_all.size() - verset.size()), verset_all.size() - verset.size());
133 }
134
135 for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
136 Pkg != helper.virtualPkgs.end(); ++Pkg)
137 {
138 c1out << "Package: " << Pkg.FullName(true) << std::endl;
139 c1out << "State: " << _("not a real package (virtual)") << std::endl;
140 // FIXME: show providers, see private-cacheset.h
141 // CacheSetHelperAPTGet::showVirtualPackageErrors()
142 }
143
144 if (verset.empty() == true)
145 {
146 if (helper.virtualPkgs.empty() == true)
147 return _error->Error(_("No packages found"));
148 else
149 _error->Notice(_("No packages found"));
150 }
151
152 return true;
153 }
154 /*}}}*/
155 } // namespace Cmd
156 } // namespace APT