]> git.saurik.com Git - apt.git/blob - apt-private/private-show.cc
94f944af19bf245cb3831a5b19c075d06f4b0941
[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 #include "private-show.h"
28 /*}}}*/
29
30 namespace APT {
31 namespace Cmd {
32
33 // DisplayRecord - Displays the complete record for the package /*{{{*/
34 // ---------------------------------------------------------------------
35 static bool DisplayRecord(pkgCacheFile &CacheFile, pkgCache::VerIterator V,
36 ostream &out)
37 {
38 pkgCache *Cache = CacheFile.GetPkgCache();
39 if (unlikely(Cache == NULL))
40 return false;
41 pkgDepCache *depCache = CacheFile.GetDepCache();
42 if (unlikely(depCache == NULL))
43 return false;
44
45 // Find an appropriate file
46 pkgCache::VerFileIterator Vf = V.FileList();
47 for (; Vf.end() == false; ++Vf)
48 if ((Vf.File()->Flags & pkgCache::Flag::NotSource) == 0)
49 break;
50 if (Vf.end() == true)
51 Vf = V.FileList();
52
53 // Check and load the package list file
54 pkgCache::PkgFileIterator I = Vf.File();
55 if (I.IsOk() == false)
56 return _error->Error(_("Package file %s is out of sync."),I.FileName());
57
58 // find matching sources.list metaindex
59 pkgSourceList *SrcList = CacheFile.GetSourceList();
60 pkgIndexFile *Index;
61 if (SrcList->FindIndex(I, Index) == false &&
62 _system->FindIndex(I, Index) == false)
63 return _error->Error("Can not find indexfile for Package %s (%s)",
64 V.ParentPkg().Name(), V.VerStr());
65 std::string source_index_file = Index->Describe(true);
66
67 // Read the record
68 FileFd PkgF;
69 if (PkgF.Open(I.FileName(), FileFd::ReadOnly, FileFd::Extension) == false)
70 return false;
71 pkgTagSection Tags;
72 pkgTagFile TagF(&PkgF);
73
74 if (TagF.Jump(Tags, V.FileList()->Offset) == false)
75 return _error->Error("Internal Error, Unable to parse a package record");
76
77 // make size nice
78 std::string installed_size;
79 if (Tags.FindI("Installed-Size") > 0)
80 strprintf(installed_size, "%sB", SizeToStr(Tags.FindI("Installed-Size")*1024).c_str());
81 else
82 installed_size = _("unknown");
83 std::string package_size;
84 if (Tags.FindI("Size") > 0)
85 strprintf(package_size, "%sB", SizeToStr(Tags.FindI("Size")).c_str());
86 else
87 package_size = _("unknown");
88
89 pkgDepCache::StateCache &state = (*depCache)[V.ParentPkg()];
90 bool is_installed = V.ParentPkg().CurrentVer() == V;
91 const char *manual_installed;
92 if (is_installed)
93 manual_installed = !(state.Flags & pkgCache::Flag::Auto) ? "yes" : "no";
94 else
95 manual_installed = 0;
96
97 // FIXME: add verbose that does not do the removal of the tags?
98 TFRewriteData RW[] = {
99 // delete, apt-cache show has this info and most users do not care
100 {"MD5sum", NULL, NULL},
101 {"SHA1", NULL, NULL},
102 {"SHA256", NULL, NULL},
103 {"Filename", NULL, NULL},
104 {"Multi-Arch", NULL, NULL},
105 {"Architecture", NULL, NULL},
106 {"Conffiles", NULL, NULL},
107 // we use the translated description
108 {"Description", NULL, NULL},
109 {"Description-md5", NULL, NULL},
110 // improve
111 {"Installed-Size", installed_size.c_str(), NULL},
112 {"Size", package_size.c_str(), "Download-Size"},
113 // add
114 {"APT-Manual-Installed", manual_installed, NULL},
115 {"APT-Sources", source_index_file.c_str(), NULL},
116 {NULL, NULL, NULL}
117 };
118
119 if(TFRewrite(stdout, Tags, NULL, RW) == false)
120 return _error->Error("Internal Error, Unable to parse a package record");
121
122 // write the description
123 pkgRecords Recs(*Cache);
124 // FIXME: show (optionally) all available translations(?)
125 pkgCache::DescIterator Desc = V.TranslatedDescription();
126 if (Desc.end() == false)
127 {
128 pkgRecords::Parser &P = Recs.Lookup(Desc.FileList());
129 out << "Description: " << P.LongDesc();
130 }
131
132 // write a final newline (after the description)
133 out << std::endl << std::endl;
134
135 return true;
136 }
137 /*}}}*/
138 bool ShowPackage(CommandLine &CmdL) /*{{{*/
139 {
140 pkgCacheFile CacheFile;
141 CacheSetHelperVirtuals helper(true, GlobalError::NOTICE);
142 APT::VersionList::Version const select = _config->FindB("APT::Cache::AllVersions", false) ?
143 APT::VersionList::ALL : APT::VersionList::CANDIDATE;
144 APT::VersionList const verset = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, select, helper);
145 for (APT::VersionList::const_iterator Ver = verset.begin(); Ver != verset.end(); ++Ver)
146 if (DisplayRecord(CacheFile, Ver, c1out) == false)
147 return false;
148
149 if (select == APT::VersionList::CANDIDATE)
150 {
151 APT::VersionList const verset_all = APT::VersionList::FromCommandLine(CacheFile, CmdL.FileList + 1, APT::VersionList::ALL, helper);
152 int const records = verset_all.size() - verset.size();
153 if (records > 0)
154 _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);
155 }
156
157 for (APT::PackageSet::const_iterator Pkg = helper.virtualPkgs.begin();
158 Pkg != helper.virtualPkgs.end(); ++Pkg)
159 {
160 c1out << "Package: " << Pkg.FullName(true) << std::endl;
161 c1out << "State: " << _("not a real package (virtual)") << std::endl;
162 // FIXME: show providers, see private-cacheset.h
163 // CacheSetHelperAPTGet::showVirtualPackageErrors()
164 }
165
166 if (verset.empty() == true)
167 {
168 if (helper.virtualPkgs.empty() == true)
169 return _error->Error(_("No packages found"));
170 else
171 _error->Notice(_("No packages found"));
172 }
173
174 return true;
175 }
176 /*}}}*/
177 } // namespace Cmd
178 } // namespace APT