fix and non-silent fail dpkg-overwrite error test
[ntk/apt.git] / apt-private / private-list.cc
CommitLineData
b9179170
MV
1// Include Files /*{{{*/
2#include <config.h>
3
b9179170
MV
4#include <apt-pkg/cachefile.h>
5#include <apt-pkg/cachefilter.h>
6#include <apt-pkg/cacheset.h>
b9179170 7#include <apt-pkg/cmndline.h>
b9179170 8#include <apt-pkg/pkgrecords.h>
453b82a3
DK
9#include <apt-pkg/progress.h>
10#include <apt-pkg/strutl.h>
11#include <apt-pkg/configuration.h>
12#include <apt-pkg/macros.h>
13#include <apt-pkg/pkgcache.h>
14#include <apt-pkg/cacheiterators.h>
15
16#include <apt-private/private-cacheset.h>
17#include <apt-private/private-list.h>
18#include <apt-private/private-output.h>
b9179170 19
453b82a3 20#include <iostream>
b9179170 21#include <sstream>
453b82a3
DK
22#include <map>
23#include <string>
b9179170 24#include <utility>
453b82a3 25#include <vector>
b9179170
MV
26
27#include <apti18n.h>
28 /*}}}*/
29
ee0167c4 30struct PackageSortAlphabetic /*{{{*/
b9179170
MV
31{
32 bool operator () (const pkgCache::PkgIterator &p_lhs,
33 const pkgCache::PkgIterator &p_rhs)
34 {
35 const std::string &l_name = p_lhs.FullName(true);
36 const std::string &r_name = p_rhs.FullName(true);
37 return (l_name < r_name);
38 }
39};
ee0167c4
DK
40 /*}}}*/
41class PackageNameMatcher : public Matcher /*{{{*/
42{
dd4d9729
MV
43#ifdef PACKAGE_MATCHER_ABI_COMPAT
44#define PackageMatcher PackageNameMatchesFnmatch
45#endif
b9179170
MV
46 public:
47 PackageNameMatcher(const char **patterns)
48 {
9ce3cfc9 49 for(int i=0; patterns[i] != NULL; ++i)
b9179170
MV
50 {
51 std::string pattern = patterns[i];
dd4d9729
MV
52#ifdef PACKAGE_MATCHER_ABI_COMPAT
53 APT::CacheFilter::PackageNameMatchesFnmatch *cachefilter = NULL;
54 cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
55#else
b9179170 56 APT::CacheFilter::PackageMatcher *cachefilter = NULL;
c1a61d1c 57 if(_config->FindB("APT::Cmd::Use-Regexp", false) == true)
b9179170
MV
58 cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern);
59 else
60 cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern);
dd4d9729 61#endif
b9179170
MV
62 filters.push_back(cachefilter);
63 }
64 }
65 virtual ~PackageNameMatcher()
66 {
9ce3cfc9 67 for(J=filters.begin(); J != filters.end(); ++J)
b9179170
MV
68 delete *J;
69 }
70 virtual bool operator () (const pkgCache::PkgIterator &P)
71 {
9ce3cfc9 72 for(J=filters.begin(); J != filters.end(); ++J)
b9179170
MV
73 {
74 APT::CacheFilter::PackageMatcher *cachefilter = *J;
75 if((*cachefilter)(P))
76 return true;
77 }
78 return false;
79 }
80
81private:
82 std::vector<APT::CacheFilter::PackageMatcher*> filters;
83 std::vector<APT::CacheFilter::PackageMatcher*>::const_iterator J;
dd4d9729 84 #undef PackageMatcher
b9179170 85};
ee0167c4 86 /*}}}*/
c3ccac92 87static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/
1a68655d
DK
88 pkgCache::PkgIterator const &P, std::ostream &outs,
89 std::string const &format)
b9179170
MV
90{
91 for (pkgCache::VerIterator Ver = P.VersionList();
796673c3 92 Ver.end() == false; ++Ver)
14109555 93 {
1a68655d
DK
94 ListSingleVersion(CacheFile, records, Ver, outs, format);
95 outs << std::endl;
14109555 96 }
b9179170 97}
ee0167c4 98 /*}}}*/
b9179170
MV
99// list - list package based on criteria /*{{{*/
100// ---------------------------------------------------------------------
ec4371ac 101bool DoList(CommandLine &Cmd)
b9179170
MV
102{
103 pkgCacheFile CacheFile;
104 pkgCache *Cache = CacheFile.GetPkgCache();
b9179170
MV
105 if (unlikely(Cache == NULL))
106 return false;
ec4371ac 107 pkgRecords records(CacheFile);
b9179170
MV
108
109 const char **patterns;
110 const char *all_pattern[] = { "*", NULL};
111
112 if (strv_length(Cmd.FileList + 1) == 0)
113 {
114 patterns = all_pattern;
115 } else {
116 patterns = Cmd.FileList + 1;
117 }
118
1a68655d
DK
119 std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}";
120 if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true)
121 format += "\n ${Description}\n";
e1dc051a 122
b9179170
MV
123 PackageNameMatcher matcher(patterns);
124 LocalitySortedVersionSet bag;
14109555 125 OpTextProgress progress(*_config);
b9179170
MV
126 progress.OverallProgress(0,
127 Cache->Head().PackageCount,
128 Cache->Head().PackageCount,
129 _("Listing"));
25594bb5 130 GetLocalitySortedVersionSet(CacheFile, &bag, matcher, &progress);
1a68655d
DK
131 bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false);
132 std::map<std::string, std::string> output_map;
9ce3cfc9 133 for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V)
b9179170
MV
134 {
135 std::stringstream outs;
d6570f85 136 if(ShowAllVersions == true)
1a68655d
DK
137 ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format);
138 else
139 ListSingleVersion(CacheFile, records, V, outs, format);
140 output_map.insert(std::make_pair<std::string, std::string>(
141 V.ParentPkg().Name(), outs.str()));
b9179170
MV
142 }
143
144 // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status)
145 // output the sorted map
1a68655d 146 std::map<std::string, std::string>::const_iterator K;
9ce3cfc9 147 for (K = output_map.begin(); K != output_map.end(); ++K)
6763aaec 148 std::cout << (*K).second << std::endl;
b9179170 149
d6570f85
MV
150 // be nice and tell the user if there is more to see
151 if (bag.size() == 1 && ShowAllVersions == false)
152 {
153 // start with -1 as we already displayed one version
154 int versions = -1;
155 pkgCache::VerIterator Ver = *bag.begin();
1a68655d
DK
156 for ( ; Ver.end() == false; ++Ver)
157 ++versions;
d6570f85
MV
158 if (versions > 0)
159 _error->Notice(P_("There is %i additional version. Please use the '-a' switch to see it", "There are %i additional versions. Please use the '-a' switch to see them.", versions), versions);
160 }
161
b9179170
MV
162 return true;
163}
164