From 549a391d6438e828001eeeaf235b080c054a7bf3 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 08:14:41 +0200 Subject: Adding upstream version 2.2.4. Signed-off-by: Daniel Baumann --- apt-private/private-list.cc | 165 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 165 insertions(+) create mode 100644 apt-private/private-list.cc (limited to 'apt-private/private-list.cc') diff --git a/apt-private/private-list.cc b/apt-private/private-list.cc new file mode 100644 index 0000000..eee657c --- /dev/null +++ b/apt-private/private-list.cc @@ -0,0 +1,165 @@ +// Include Files /*{{{*/ +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include + +#include +#include +#include +#include +#include +#include + +#include + /*}}}*/ + +struct PackageSortAlphabetic /*{{{*/ +{ + bool operator () (const pkgCache::PkgIterator &p_lhs, + const pkgCache::PkgIterator &p_rhs) + { + const std::string &l_name = p_lhs.FullName(true); + const std::string &r_name = p_rhs.FullName(true); + return (l_name < r_name); + } +}; + +class PackageNameMatcher : public Matcher +{ + static constexpr const char *const isfnmatch_strict = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+-.:*"; + pkgCacheFile &cacheFile; + public: + explicit PackageNameMatcher(pkgCacheFile &cacheFile, const char **patterns) + : cacheFile(cacheFile) + { + for(int i=0; patterns[i] != NULL; ++i) + { + std::string pattern = patterns[i]; + APT::CacheFilter::Matcher *cachefilter = NULL; + if(_config->FindB("APT::Cmd::Use-Regexp", false) == true) + cachefilter = new APT::CacheFilter::PackageNameMatchesRegEx(pattern); + else if (pattern.find_first_not_of(isfnmatch_strict) == std::string::npos) + cachefilter = new APT::CacheFilter::PackageNameMatchesFnmatch(pattern); + else + cachefilter = APT::CacheFilter::ParsePattern(pattern, &cacheFile).release(); + + if (cachefilter == nullptr) { + return; + filters.clear(); + } + filters.push_back(cachefilter); + } + } + virtual ~PackageNameMatcher() + { + for(J=filters.begin(); J != filters.end(); ++J) + delete *J; + } + virtual bool operator () (const pkgCache::PkgIterator &P) APT_OVERRIDE + { + for(J=filters.begin(); J != filters.end(); ++J) + { + APT::CacheFilter::Matcher *cachefilter = *J; + if((*cachefilter)(P)) + return true; + } + return false; + } + +private: + std::vector filters; + std::vector::const_iterator J; + #undef PackageMatcher +}; + /*}}}*/ +static void ListAllVersions(pkgCacheFile &CacheFile, pkgRecords &records,/*{{{*/ + pkgCache::PkgIterator const &P, std::ostream &outs, + std::string const &format) +{ + for (pkgCache::VerIterator Ver = P.VersionList(); + Ver.end() == false; ++Ver) + { + ListSingleVersion(CacheFile, records, Ver, outs, format); + outs << std::endl; + } +} + /*}}}*/ +// list - list package based on criteria /*{{{*/ +// --------------------------------------------------------------------- +bool DoList(CommandLine &Cmd) +{ + pkgCacheFile CacheFile; + pkgCache * const Cache = CacheFile.GetPkgCache(); + if (unlikely(Cache == nullptr || CacheFile.GetDepCache() == nullptr)) + return false; + pkgRecords records(CacheFile); + + const char **patterns; + const char *all_pattern[] = { "*", NULL}; + + if (strv_length(Cmd.FileList + 1) == 0) + { + patterns = all_pattern; + } else { + patterns = Cmd.FileList + 1; + } + + std::string format = "${color:highlight}${Package}${color:neutral}/${Origin} ${Version} ${Architecture}${ }${apt:Status}"; + if (_config->FindB("APT::Cmd::List-Include-Summary", false) == true) + format += "\n ${Description}\n"; + + PackageNameMatcher matcher(CacheFile, patterns); + LocalitySortedVersionSet bag; + OpTextProgress progress(*_config); + progress.OverallProgress(0, + Cache->Head().PackageCount, + Cache->Head().PackageCount, + _("Listing")); + GetLocalitySortedVersionSet(CacheFile, &bag, matcher, &progress); + bool const ShowAllVersions = _config->FindB("APT::Cmd::All-Versions", false); + std::map output_map; + for (LocalitySortedVersionSet::iterator V = bag.begin(); V != bag.end(); ++V) + { + std::stringstream outs; + if(ShowAllVersions == true) + ListAllVersions(CacheFile, records, V.ParentPkg(), outs, format); + else + ListSingleVersion(CacheFile, records, V, outs, format); + output_map.insert(std::make_pair( + V.ParentPkg().FullName(), outs.str())); + } + + // FIXME: SORT! and make sorting flexible (alphabetic, by pkg status) + // output the sorted map + std::map::const_iterator K; + for (K = output_map.begin(); K != output_map.end(); ++K) + std::cout << (*K).second << std::endl; + + // be nice and tell the user if there is more to see + if (bag.size() == 1 && ShowAllVersions == false) + { + // start with -1 as we already displayed one version + int versions = -1; + pkgCache::VerIterator Ver = *bag.begin(); + for ( ; Ver.end() == false; ++Ver) + ++versions; + if (versions > 0) + _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); + } + + return true; +} + -- cgit v1.2.3