From 7959c5eda83bd6d69876942566cf47d74fc76530 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 31 May 2010 21:00:09 +0200 Subject: rename packageset into cacheset while it is not too late --- apt-pkg/cacheset.cc | 103 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 apt-pkg/cacheset.cc (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc new file mode 100644 index 000000000..b49b36539 --- /dev/null +++ b/apt-pkg/cacheset.cc @@ -0,0 +1,103 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +/* ###################################################################### + + Simple wrapper around a std::set to provide a similar interface to + a set of cache structures as to the complete set of all structures + in the pkgCache. Currently only Package is supported. + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ +#include +#include +#include +#include + +#include + +#include + +#include + /*}}}*/ +namespace APT { +// FromRegEx - Return all packages in the cache matching a pattern /*{{{*/ +PackageSet PackageSet::FromRegEx(pkgCache &Cache, std::string pattern, std::ostream &out) { + PackageSet pkgset; + std::string arch = "native"; + static const char * const isregex = ".?+*|[^$"; + + if (pattern.find_first_of(isregex) == std::string::npos) + return pkgset; + + size_t archfound = pattern.find_last_of(':'); + if (archfound != std::string::npos) { + arch = pattern.substr(archfound+1); + if (arch.find_first_of(isregex) == std::string::npos) + pattern.erase(archfound); + else + arch = "native"; + } + + regex_t Pattern; + int Res; + if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) { + char Error[300]; + regerror(Res, &Pattern, Error, sizeof(Error)); + _error->Error(_("Regex compilation error - %s"), Error); + return pkgset; + } + + for (pkgCache::GrpIterator Grp = Cache.GrpBegin(); Grp.end() == false; ++Grp) + { + if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0) + continue; + pkgCache::PkgIterator Pkg = Grp.FindPkg(arch); + if (Pkg.end() == true) { + if (archfound == std::string::npos) { + std::vector archs = APT::Configuration::getArchitectures(); + for (std::vector::const_iterator a = archs.begin(); + a != archs.end() && Pkg.end() != true; ++a) + Pkg = Grp.FindPkg(*a); + } + if (Pkg.end() == true) + continue; + } + + ioprintf(out, _("Note, selecting %s for regex '%s'\n"), + Pkg.FullName(true).c_str(), pattern.c_str()); + + pkgset.insert(Pkg); + } + + regfree(&Pattern); + + return pkgset; +} + /*}}}*/ +// FromCommandLine - Return all packages specified on commandline /*{{{*/ +PackageSet PackageSet::FromCommandLine(pkgCache &Cache, const char **cmdline, std::ostream &out) { + PackageSet pkgset; + for (const char **I = cmdline + 1; *I != 0; I++) { + pkgCache::PkgIterator Pkg = Cache.FindPkg(*I); + if (Pkg.end() == true) { + std::vector archs = APT::Configuration::getArchitectures(); + for (std::vector::const_iterator a = archs.begin(); + a != archs.end() || Pkg.end() != true; ++a) { + Pkg = Cache.FindPkg(*I, *a); + } + if (Pkg.end() == true) { + PackageSet regex = FromRegEx(Cache, *I, out); + if (regex.empty() == true) + _error->Warning(_("Unable to locate package %s"),*I); + else + pkgset.insert(regex.begin(), regex.end()); + continue; + } + } + pkgset.insert(Pkg); + } + return pkgset; +} + /*}}}*/ +} -- cgit v1.2.3 From 856d3b06a6eb6a7f455294c69fc13604e099fd56 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 5 Jun 2010 09:20:29 +0200 Subject: Add a method to get a VersionSet from the Commandline and refactor the existing methods a bit to reuse them easier intern --- apt-pkg/cacheset.cc | 140 +++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 121 insertions(+), 19 deletions(-) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index b49b36539..7372c909e 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -13,6 +13,7 @@ #include #include #include +#include #include @@ -22,7 +23,7 @@ /*}}}*/ namespace APT { // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/ -PackageSet PackageSet::FromRegEx(pkgCache &Cache, std::string pattern, std::ostream &out) { +PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out) { PackageSet pkgset; std::string arch = "native"; static const char * const isregex = ".?+*|[^$"; @@ -48,7 +49,7 @@ PackageSet PackageSet::FromRegEx(pkgCache &Cache, std::string pattern, std::ostr return pkgset; } - for (pkgCache::GrpIterator Grp = Cache.GrpBegin(); Grp.end() == false; ++Grp) + for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) { if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0) continue; @@ -76,28 +77,129 @@ PackageSet PackageSet::FromRegEx(pkgCache &Cache, std::string pattern, std::ostr } /*}}}*/ // FromCommandLine - Return all packages specified on commandline /*{{{*/ -PackageSet PackageSet::FromCommandLine(pkgCache &Cache, const char **cmdline, std::ostream &out) { +PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) { PackageSet pkgset; for (const char **I = cmdline + 1; *I != 0; I++) { - pkgCache::PkgIterator Pkg = Cache.FindPkg(*I); - if (Pkg.end() == true) { - std::vector archs = APT::Configuration::getArchitectures(); - for (std::vector::const_iterator a = archs.begin(); - a != archs.end() || Pkg.end() != true; ++a) { - Pkg = Cache.FindPkg(*I, *a); - } - if (Pkg.end() == true) { - PackageSet regex = FromRegEx(Cache, *I, out); - if (regex.empty() == true) - _error->Warning(_("Unable to locate package %s"),*I); - else - pkgset.insert(regex.begin(), regex.end()); - continue; + PackageSet pset = FromString(Cache, *I, out); + pkgset.insert(pset.begin(), pset.end()); + } + return pkgset; +} + /*}}}*/ +// FromString - Return all packages matching a specific string /*{{{*/ +PackageSet PackageSet::FromString(pkgCacheFile &Cache, const char * const str, std::ostream &out) { + pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(str); + if (Grp.end() == false) { + pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg(); + PackageSet pkgset; + pkgset.insert(Pkg); + return pkgset; + } + PackageSet regex = FromRegEx(Cache, str, out); + if (regex.empty() == true) + _error->Warning(_("Unable to locate package %s"), str); + return regex; +} + /*}}}*/ +// FromCommandLine - Return all versions specified on commandline /*{{{*/ +APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, + APT::VersionSet::Version const &fallback, std::ostream &out) { + VersionSet verset; + for (const char **I = cmdline + 1; *I != 0; I++) { + std::string pkg = *I; + std::string ver; + bool verIsRel = false; + size_t const vertag = pkg.find_last_of("/="); + if (vertag != string::npos) { + ver = pkg.substr(vertag+1); + verIsRel = (pkg[vertag] == '/'); + pkg.erase(vertag); + } + PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out); + for (PackageSet::const_iterator P = pkgset.begin(); + P != pkgset.end(); ++P) { + if (vertag != string::npos) { + pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release : + pkgVersionMatch::Version)); + pkgCache::VerIterator V = Match.Find(P); + if (V.end() == true) { + if (verIsRel == true) + _error->Error(_("Release '%s' for '%s' was not found"), + ver.c_str(), P.FullName(true).c_str()); + else + _error->Error(_("Version '%s' for '%s' was not found"), + ver.c_str(), P.FullName(true).c_str()); + continue; + } + if (strcmp(ver.c_str(), V.VerStr()) != 0) + ioprintf(out, _("Selected version %s (%s) for %s\n"), + V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str()); + verset.insert(V); + } else { + pkgCache::VerIterator V; + switch(fallback) { + case VersionSet::ALL: + for (V = P.VersionList(); V.end() != true; ++V) + verset.insert(V); + break; + case VersionSet::CANDANDINST: + verset.insert(getInstalledVer(Cache, P)); + verset.insert(getCandidateVer(Cache, P)); + break; + case VersionSet::CANDIDATE: + verset.insert(getCandidateVer(Cache, P)); + break; + case VersionSet::INSTALLED: + verset.insert(getInstalledVer(Cache, P)); + break; + case VersionSet::CANDINST: + V = getCandidateVer(Cache, P, true); + if (V.end() == true) + V = getInstalledVer(Cache, P, true); + if (V.end() == false) + verset.insert(V); + else + _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str()); + break; + case VersionSet::INSTCAND: + V = getInstalledVer(Cache, P, true); + if (V.end() == true) + V = getCandidateVer(Cache, P, true); + if (V.end() == false) + verset.insert(V); + else + _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str()); + break; + case VersionSet::NEWEST: + if (P->VersionList != 0) + verset.insert(P.VersionList()); + else + _error->Error(_("Can't select newest version from package %s as it is purely virtual"), P.FullName(true).c_str()); + break; + } } } - pkgset.insert(Pkg); } - return pkgset; + return verset; +} + /*}}}*/ +// getCandidateVer - Returns the candidate version of the given package /*{{{*/ +pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg, bool const &AllowError) { + if (unlikely(Cache.BuildDepCache() == false)) + return pkgCache::VerIterator(*Cache); + pkgCache::VerIterator Cand = Cache[Pkg].InstVerIter(Cache); + if (AllowError == false && Cand.end() == true) + _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str()); + return Cand; +} + /*}}}*/ +// getInstalledVer - Returns the installed version of the given package /*{{{*/ +pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg, bool const &AllowError) { + if (AllowError == false && Pkg->CurrentVer == 0) + _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str()); + return Pkg.CurrentVer(); } /*}}}*/ } -- cgit v1.2.3 From 9f1f17ccd4f84c23410ae62911c85f5836c3b503 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 5 Jun 2010 13:09:18 +0200 Subject: Don't increase the commandline parameter in the library but in the application to be really generic. --- apt-pkg/cacheset.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index 7372c909e..5dbd1a4df 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -79,7 +79,7 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std:: // FromCommandLine - Return all packages specified on commandline /*{{{*/ PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) { PackageSet pkgset; - for (const char **I = cmdline + 1; *I != 0; I++) { + for (const char **I = cmdline; *I != 0; ++I) { PackageSet pset = FromString(Cache, *I, out); pkgset.insert(pset.begin(), pset.end()); } @@ -105,7 +105,7 @@ PackageSet PackageSet::FromString(pkgCacheFile &Cache, const char * const str, s APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, APT::VersionSet::Version const &fallback, std::ostream &out) { VersionSet verset; - for (const char **I = cmdline + 1; *I != 0; I++) { + for (const char **I = cmdline; *I != 0; ++I) { std::string pkg = *I; std::string ver; bool verIsRel = false; -- cgit v1.2.3 From fe870febbfc1145d4a2a6b86985b0253419d52a8 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 5 Jun 2010 15:47:16 +0200 Subject: handle :arch modifier in PackageSet::FromString correctly --- apt-pkg/cacheset.cc | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index 5dbd1a4df..8fcffaf9a 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -87,17 +87,31 @@ PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline } /*}}}*/ // FromString - Return all packages matching a specific string /*{{{*/ -PackageSet PackageSet::FromString(pkgCacheFile &Cache, const char * const str, std::ostream &out) { - pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(str); - if (Grp.end() == false) { - pkgCache::PkgIterator Pkg = Grp.FindPreferredPkg(); +PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, std::ostream &out) { + std::string pkg = str; + size_t archfound = pkg.find_last_of(':'); + std::string arch; + if (archfound != std::string::npos) { + arch = pkg.substr(archfound+1); + pkg.erase(archfound); + } + + pkgCache::PkgIterator Pkg; + if (arch.empty() == true) { + pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg); + if (Grp.end() == false) + Pkg = Grp.FindPreferredPkg(); + } else + Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch); + + if (Pkg.end() == false) { PackageSet pkgset; pkgset.insert(Pkg); return pkgset; } PackageSet regex = FromRegEx(Cache, str, out); if (regex.empty() == true) - _error->Warning(_("Unable to locate package %s"), str); + _error->Warning(_("Unable to locate package %s"), str.c_str()); return regex; } /*}}}*/ @@ -188,7 +202,7 @@ pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg, bool const &AllowError) { if (unlikely(Cache.BuildDepCache() == false)) return pkgCache::VerIterator(*Cache); - pkgCache::VerIterator Cand = Cache[Pkg].InstVerIter(Cache); + pkgCache::VerIterator Cand = Cache[Pkg].CandidateVerIter(Cache); if (AllowError == false && Cand.end() == true) _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str()); return Cand; -- cgit v1.2.3 From 84910ad5090b8cffc81d7942497ca09cc5d93244 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 5 Jun 2010 18:00:00 +0200 Subject: support special release-modifier 'installed' and 'candidate' --- apt-pkg/cacheset.cc | 126 ++++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 49 deletions(-) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index 8fcffaf9a..c0b06ba32 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -132,10 +132,19 @@ APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cm PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out); for (PackageSet::const_iterator P = pkgset.begin(); P != pkgset.end(); ++P) { - if (vertag != string::npos) { + if (vertag == string::npos) { + AddSelectedVersion(Cache, verset, P, fallback); + continue; + } + pkgCache::VerIterator V; + if (ver == "installed") + V = getInstalledVer(Cache, P); + else if (ver == "candidate") + V = getCandidateVer(Cache, P); + else { pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release : - pkgVersionMatch::Version)); - pkgCache::VerIterator V = Match.Find(P); + pkgVersionMatch::Version)); + V = Match.Find(P); if (V.end() == true) { if (verIsRel == true) _error->Error(_("Release '%s' for '%s' was not found"), @@ -145,58 +154,77 @@ APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cm ver.c_str(), P.FullName(true).c_str()); continue; } - if (strcmp(ver.c_str(), V.VerStr()) != 0) - ioprintf(out, _("Selected version %s (%s) for %s\n"), - V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str()); - verset.insert(V); - } else { - pkgCache::VerIterator V; - switch(fallback) { - case VersionSet::ALL: - for (V = P.VersionList(); V.end() != true; ++V) - verset.insert(V); - break; - case VersionSet::CANDANDINST: - verset.insert(getInstalledVer(Cache, P)); - verset.insert(getCandidateVer(Cache, P)); - break; - case VersionSet::CANDIDATE: - verset.insert(getCandidateVer(Cache, P)); - break; - case VersionSet::INSTALLED: - verset.insert(getInstalledVer(Cache, P)); - break; - case VersionSet::CANDINST: - V = getCandidateVer(Cache, P, true); - if (V.end() == true) - V = getInstalledVer(Cache, P, true); - if (V.end() == false) - verset.insert(V); - else - _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str()); - break; - case VersionSet::INSTCAND: - V = getInstalledVer(Cache, P, true); - if (V.end() == true) - V = getCandidateVer(Cache, P, true); - if (V.end() == false) - verset.insert(V); - else - _error->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P.FullName(true).c_str()); - break; - case VersionSet::NEWEST: - if (P->VersionList != 0) - verset.insert(P.VersionList()); - else - _error->Error(_("Can't select newest version from package %s as it is purely virtual"), P.FullName(true).c_str()); - break; - } } + if (V.end() == true) + continue; + if (ver == V.VerStr()) + ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"), + V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str()); + verset.insert(V); } } return verset; } /*}}}*/ +// AddSelectedVersion - add version from package based on fallback /*{{{*/ +bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset, + pkgCache::PkgIterator const &P, VersionSet::Version const &fallback, + bool const &AllowError) { + pkgCache::VerIterator V; + switch(fallback) { + case VersionSet::ALL: + if (P->VersionList != 0) + for (V = P.VersionList(); V.end() != true; ++V) + verset.insert(V); + else if (AllowError == false) + return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str()); + else + return false; + break; + case VersionSet::CANDANDINST: + verset.insert(getInstalledVer(Cache, P, AllowError)); + verset.insert(getCandidateVer(Cache, P, AllowError)); + break; + case VersionSet::CANDIDATE: + verset.insert(getCandidateVer(Cache, P, AllowError)); + break; + case VersionSet::INSTALLED: + verset.insert(getInstalledVer(Cache, P, AllowError)); + break; + case VersionSet::CANDINST: + V = getCandidateVer(Cache, P, true); + if (V.end() == true) + V = getInstalledVer(Cache, P, true); + if (V.end() == false) + verset.insert(V); + else if (AllowError == false) + return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str()); + else + return false; + break; + case VersionSet::INSTCAND: + V = getInstalledVer(Cache, P, true); + if (V.end() == true) + V = getCandidateVer(Cache, P, true); + if (V.end() == false) + verset.insert(V); + else if (AllowError == false) + return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str()); + else + return false; + break; + case VersionSet::NEWEST: + if (P->VersionList != 0) + verset.insert(P.VersionList()); + else if (AllowError == false) + return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str()); + else + return false; + break; + } + return true; +} + /*}}}*/ // getCandidateVer - Returns the candidate version of the given package /*{{{*/ pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg, bool const &AllowError) { -- cgit v1.2.3 From 9cc83a6fbc089ccf9cb8d52ee9f380cf5df62d5c Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 6 Jun 2010 21:41:27 +0200 Subject: add a GroupedFromCommandLine method to the PackageSet to split the packages on the commandline into groups based on modifiers --- apt-pkg/cacheset.cc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index c0b06ba32..43ade4b4e 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -76,6 +76,39 @@ PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std:: return pkgset; } /*}}}*/ +// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/ +std::map PackageSet::GroupedFromCommandLine( + pkgCacheFile &Cache, const char **cmdline, + std::list const &mods, + unsigned short const &fallback, std::ostream &out) { + std::map pkgsets; + for (const char **I = cmdline; *I != 0; ++I) { + unsigned short modID = fallback; + std::string str = *I; + for (std::list::const_iterator mod = mods.begin(); + mod != mods.end(); ++mod) { + size_t const alength = strlen(mod->Alias); + switch(mod->Pos) { + case PackageSet::Modifier::POSTFIX: + if (str.compare(str.length() - alength, alength, + mod->Alias, 0, alength) != 0) + continue; + str.erase(str.length() - alength); + modID = mod->ID; + break; + case PackageSet::Modifier::PREFIX: + continue; + case PackageSet::Modifier::NONE: + continue; + } + break; + } + PackageSet pset = PackageSet::FromString(Cache, str, out); + pkgsets[modID].insert(pset.begin(), pset.end()); + } + return pkgsets; +} + /*}}}*/ // FromCommandLine - Return all packages specified on commandline /*{{{*/ PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) { PackageSet pkgset; -- cgit v1.2.3 From a8ef7efd6df81d4fb9e52419695f10df9fe76cda Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 10 Jun 2010 13:09:23 +0200 Subject: * apt-pkg/cacheset.cc: - get the candidate either from an already built depcache or use the policy which is a bit faster than depcache generation --- apt-pkg/cacheset.cc | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index 43ade4b4e..e91b56997 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -261,9 +261,14 @@ bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset, // getCandidateVer - Returns the candidate version of the given package /*{{{*/ pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache, pkgCache::PkgIterator const &Pkg, bool const &AllowError) { - if (unlikely(Cache.BuildDepCache() == false)) - return pkgCache::VerIterator(*Cache); - pkgCache::VerIterator Cand = Cache[Pkg].CandidateVerIter(Cache); + pkgCache::VerIterator Cand; + if (Cache.IsDepCacheBuilt() == true) + Cand = Cache[Pkg].CandidateVerIter(Cache); + else { + if (unlikely(Cache.BuildPolicy() == false)) + return pkgCache::VerIterator(*Cache); + Cand = Cache.GetPolicy()->GetCandidateVer(Pkg); + } if (AllowError == false && Cand.end() == true) _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str()); return Cand; -- cgit v1.2.3 From 55c59998a046e8d60b5bf51772bd7db5cd43ca7c Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 15 Jun 2010 20:29:50 +0200 Subject: Add a GroupedFromCommandLine for the VersionSet similar to the one for PackageSet and refactor the existing VersionSet methods to simplify that. --- apt-pkg/cacheset.cc | 123 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 84 insertions(+), 39 deletions(-) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index e91b56997..fde52168a 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -91,7 +91,7 @@ std::map PackageSet::GroupedFromCommandLine( switch(mod->Pos) { case PackageSet::Modifier::POSTFIX: if (str.compare(str.length() - alength, alength, - mod->Alias, 0, alength) != 0) + mod->Alias, 0, alength) != 0) continue; str.erase(str.length() - alength); modID = mod->ID; @@ -148,53 +148,98 @@ PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, s return regex; } /*}}}*/ +// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/ +std::map VersionSet::GroupedFromCommandLine( + pkgCacheFile &Cache, const char **cmdline, + std::list const &mods, + unsigned short const &fallback, std::ostream &out) { + std::map versets; + for (const char **I = cmdline; *I != 0; ++I) { + unsigned short modID = fallback; + VersionSet::Version select = VersionSet::NEWEST; + std::string str = *I; + for (std::list::const_iterator mod = mods.begin(); + mod != mods.end(); ++mod) { + if (modID == fallback && mod->ID == fallback) + select = mod->SelectVersion; + size_t const alength = strlen(mod->Alias); + switch(mod->Pos) { + case VersionSet::Modifier::POSTFIX: + if (str.compare(str.length() - alength, alength, + mod->Alias, 0, alength) != 0) + continue; + str.erase(str.length() - alength); + modID = mod->ID; + select = mod->SelectVersion; + break; + case VersionSet::Modifier::PREFIX: + continue; + case VersionSet::Modifier::NONE: + continue; + } + break; + } + VersionSet vset = VersionSet::FromString(Cache, str, select , out); + versets[modID].insert(vset.begin(), vset.end()); + } + return versets; +} + /*}}}*/ // FromCommandLine - Return all versions specified on commandline /*{{{*/ APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, APT::VersionSet::Version const &fallback, std::ostream &out) { VersionSet verset; for (const char **I = cmdline; *I != 0; ++I) { - std::string pkg = *I; - std::string ver; - bool verIsRel = false; - size_t const vertag = pkg.find_last_of("/="); - if (vertag != string::npos) { - ver = pkg.substr(vertag+1); - verIsRel = (pkg[vertag] == '/'); - pkg.erase(vertag); + VersionSet vset = VersionSet::FromString(Cache, *I, fallback, out); + verset.insert(vset.begin(), vset.end()); + } + return verset; +} + /*}}}*/ +// FromString - Returns all versions spedcified by a string /*{{{*/ +APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg, + APT::VersionSet::Version const &fallback, std::ostream &out) { + std::string ver; + bool verIsRel = false; + size_t const vertag = pkg.find_last_of("/="); + if (vertag != string::npos) { + ver = pkg.substr(vertag+1); + verIsRel = (pkg[vertag] == '/'); + pkg.erase(vertag); + } + PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out); + VersionSet verset; + for (PackageSet::const_iterator P = pkgset.begin(); + P != pkgset.end(); ++P) { + if (vertag == string::npos) { + AddSelectedVersion(Cache, verset, P, fallback); + continue; } - PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out); - for (PackageSet::const_iterator P = pkgset.begin(); - P != pkgset.end(); ++P) { - if (vertag == string::npos) { - AddSelectedVersion(Cache, verset, P, fallback); + pkgCache::VerIterator V; + if (ver == "installed") + V = getInstalledVer(Cache, P); + else if (ver == "candidate") + V = getCandidateVer(Cache, P); + else { + pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release : + pkgVersionMatch::Version)); + V = Match.Find(P); + if (V.end() == true) { + if (verIsRel == true) + _error->Error(_("Release '%s' for '%s' was not found"), + ver.c_str(), P.FullName(true).c_str()); + else + _error->Error(_("Version '%s' for '%s' was not found"), + ver.c_str(), P.FullName(true).c_str()); continue; } - pkgCache::VerIterator V; - if (ver == "installed") - V = getInstalledVer(Cache, P); - else if (ver == "candidate") - V = getCandidateVer(Cache, P); - else { - pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release : - pkgVersionMatch::Version)); - V = Match.Find(P); - if (V.end() == true) { - if (verIsRel == true) - _error->Error(_("Release '%s' for '%s' was not found"), - ver.c_str(), P.FullName(true).c_str()); - else - _error->Error(_("Version '%s' for '%s' was not found"), - ver.c_str(), P.FullName(true).c_str()); - continue; - } - } - if (V.end() == true) - continue; - if (ver == V.VerStr()) - ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"), - V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str()); - verset.insert(V); } + if (V.end() == true) + continue; + if (ver == V.VerStr()) + ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"), + V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str()); + verset.insert(V); } return verset; } -- cgit v1.2.3 From d8276801a1c84582a85ed9ea1f2eb4e66e052e6b Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 15 Jun 2010 20:46:09 +0200 Subject: * cmdline/cacheset.cc: - doesn't include it in the library for now as it is too volatile --- apt-pkg/cacheset.cc | 330 ---------------------------------------------------- 1 file changed, 330 deletions(-) delete mode 100644 apt-pkg/cacheset.cc (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc deleted file mode 100644 index fde52168a..000000000 --- a/apt-pkg/cacheset.cc +++ /dev/null @@ -1,330 +0,0 @@ -// -*- mode: cpp; mode: fold -*- -// Description /*{{{*/ -/* ###################################################################### - - Simple wrapper around a std::set to provide a similar interface to - a set of cache structures as to the complete set of all structures - in the pkgCache. Currently only Package is supported. - - ##################################################################### */ - /*}}}*/ -// Include Files /*{{{*/ -#include -#include -#include -#include -#include - -#include - -#include - -#include - /*}}}*/ -namespace APT { -// FromRegEx - Return all packages in the cache matching a pattern /*{{{*/ -PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, std::ostream &out) { - PackageSet pkgset; - std::string arch = "native"; - static const char * const isregex = ".?+*|[^$"; - - if (pattern.find_first_of(isregex) == std::string::npos) - return pkgset; - - size_t archfound = pattern.find_last_of(':'); - if (archfound != std::string::npos) { - arch = pattern.substr(archfound+1); - if (arch.find_first_of(isregex) == std::string::npos) - pattern.erase(archfound); - else - arch = "native"; - } - - regex_t Pattern; - int Res; - if ((Res = regcomp(&Pattern, pattern.c_str() , REG_EXTENDED | REG_ICASE | REG_NOSUB)) != 0) { - char Error[300]; - regerror(Res, &Pattern, Error, sizeof(Error)); - _error->Error(_("Regex compilation error - %s"), Error); - return pkgset; - } - - for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) - { - if (regexec(&Pattern, Grp.Name(), 0, 0, 0) != 0) - continue; - pkgCache::PkgIterator Pkg = Grp.FindPkg(arch); - if (Pkg.end() == true) { - if (archfound == std::string::npos) { - std::vector archs = APT::Configuration::getArchitectures(); - for (std::vector::const_iterator a = archs.begin(); - a != archs.end() && Pkg.end() != true; ++a) - Pkg = Grp.FindPkg(*a); - } - if (Pkg.end() == true) - continue; - } - - ioprintf(out, _("Note, selecting %s for regex '%s'\n"), - Pkg.FullName(true).c_str(), pattern.c_str()); - - pkgset.insert(Pkg); - } - - regfree(&Pattern); - - return pkgset; -} - /*}}}*/ -// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/ -std::map PackageSet::GroupedFromCommandLine( - pkgCacheFile &Cache, const char **cmdline, - std::list const &mods, - unsigned short const &fallback, std::ostream &out) { - std::map pkgsets; - for (const char **I = cmdline; *I != 0; ++I) { - unsigned short modID = fallback; - std::string str = *I; - for (std::list::const_iterator mod = mods.begin(); - mod != mods.end(); ++mod) { - size_t const alength = strlen(mod->Alias); - switch(mod->Pos) { - case PackageSet::Modifier::POSTFIX: - if (str.compare(str.length() - alength, alength, - mod->Alias, 0, alength) != 0) - continue; - str.erase(str.length() - alength); - modID = mod->ID; - break; - case PackageSet::Modifier::PREFIX: - continue; - case PackageSet::Modifier::NONE: - continue; - } - break; - } - PackageSet pset = PackageSet::FromString(Cache, str, out); - pkgsets[modID].insert(pset.begin(), pset.end()); - } - return pkgsets; -} - /*}}}*/ -// FromCommandLine - Return all packages specified on commandline /*{{{*/ -PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, std::ostream &out) { - PackageSet pkgset; - for (const char **I = cmdline; *I != 0; ++I) { - PackageSet pset = FromString(Cache, *I, out); - pkgset.insert(pset.begin(), pset.end()); - } - return pkgset; -} - /*}}}*/ -// FromString - Return all packages matching a specific string /*{{{*/ -PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, std::ostream &out) { - std::string pkg = str; - size_t archfound = pkg.find_last_of(':'); - std::string arch; - if (archfound != std::string::npos) { - arch = pkg.substr(archfound+1); - pkg.erase(archfound); - } - - pkgCache::PkgIterator Pkg; - if (arch.empty() == true) { - pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg); - if (Grp.end() == false) - Pkg = Grp.FindPreferredPkg(); - } else - Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch); - - if (Pkg.end() == false) { - PackageSet pkgset; - pkgset.insert(Pkg); - return pkgset; - } - PackageSet regex = FromRegEx(Cache, str, out); - if (regex.empty() == true) - _error->Warning(_("Unable to locate package %s"), str.c_str()); - return regex; -} - /*}}}*/ -// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/ -std::map VersionSet::GroupedFromCommandLine( - pkgCacheFile &Cache, const char **cmdline, - std::list const &mods, - unsigned short const &fallback, std::ostream &out) { - std::map versets; - for (const char **I = cmdline; *I != 0; ++I) { - unsigned short modID = fallback; - VersionSet::Version select = VersionSet::NEWEST; - std::string str = *I; - for (std::list::const_iterator mod = mods.begin(); - mod != mods.end(); ++mod) { - if (modID == fallback && mod->ID == fallback) - select = mod->SelectVersion; - size_t const alength = strlen(mod->Alias); - switch(mod->Pos) { - case VersionSet::Modifier::POSTFIX: - if (str.compare(str.length() - alength, alength, - mod->Alias, 0, alength) != 0) - continue; - str.erase(str.length() - alength); - modID = mod->ID; - select = mod->SelectVersion; - break; - case VersionSet::Modifier::PREFIX: - continue; - case VersionSet::Modifier::NONE: - continue; - } - break; - } - VersionSet vset = VersionSet::FromString(Cache, str, select , out); - versets[modID].insert(vset.begin(), vset.end()); - } - return versets; -} - /*}}}*/ -// FromCommandLine - Return all versions specified on commandline /*{{{*/ -APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, - APT::VersionSet::Version const &fallback, std::ostream &out) { - VersionSet verset; - for (const char **I = cmdline; *I != 0; ++I) { - VersionSet vset = VersionSet::FromString(Cache, *I, fallback, out); - verset.insert(vset.begin(), vset.end()); - } - return verset; -} - /*}}}*/ -// FromString - Returns all versions spedcified by a string /*{{{*/ -APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg, - APT::VersionSet::Version const &fallback, std::ostream &out) { - std::string ver; - bool verIsRel = false; - size_t const vertag = pkg.find_last_of("/="); - if (vertag != string::npos) { - ver = pkg.substr(vertag+1); - verIsRel = (pkg[vertag] == '/'); - pkg.erase(vertag); - } - PackageSet pkgset = PackageSet::FromString(Cache, pkg.c_str(), out); - VersionSet verset; - for (PackageSet::const_iterator P = pkgset.begin(); - P != pkgset.end(); ++P) { - if (vertag == string::npos) { - AddSelectedVersion(Cache, verset, P, fallback); - continue; - } - pkgCache::VerIterator V; - if (ver == "installed") - V = getInstalledVer(Cache, P); - else if (ver == "candidate") - V = getCandidateVer(Cache, P); - else { - pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release : - pkgVersionMatch::Version)); - V = Match.Find(P); - if (V.end() == true) { - if (verIsRel == true) - _error->Error(_("Release '%s' for '%s' was not found"), - ver.c_str(), P.FullName(true).c_str()); - else - _error->Error(_("Version '%s' for '%s' was not found"), - ver.c_str(), P.FullName(true).c_str()); - continue; - } - } - if (V.end() == true) - continue; - if (ver == V.VerStr()) - ioprintf(out, _("Selected version '%s' (%s) for '%s'\n"), - V.VerStr(), V.RelStr().c_str(), P.FullName(true).c_str()); - verset.insert(V); - } - return verset; -} - /*}}}*/ -// AddSelectedVersion - add version from package based on fallback /*{{{*/ -bool VersionSet::AddSelectedVersion(pkgCacheFile &Cache, VersionSet &verset, - pkgCache::PkgIterator const &P, VersionSet::Version const &fallback, - bool const &AllowError) { - pkgCache::VerIterator V; - switch(fallback) { - case VersionSet::ALL: - if (P->VersionList != 0) - for (V = P.VersionList(); V.end() != true; ++V) - verset.insert(V); - else if (AllowError == false) - return _error->Error(_("Can't select versions from package '%s' as it purely virtual"), P.FullName(true).c_str()); - else - return false; - break; - case VersionSet::CANDANDINST: - verset.insert(getInstalledVer(Cache, P, AllowError)); - verset.insert(getCandidateVer(Cache, P, AllowError)); - break; - case VersionSet::CANDIDATE: - verset.insert(getCandidateVer(Cache, P, AllowError)); - break; - case VersionSet::INSTALLED: - verset.insert(getInstalledVer(Cache, P, AllowError)); - break; - case VersionSet::CANDINST: - V = getCandidateVer(Cache, P, true); - if (V.end() == true) - V = getInstalledVer(Cache, P, true); - if (V.end() == false) - verset.insert(V); - else if (AllowError == false) - return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str()); - else - return false; - break; - case VersionSet::INSTCAND: - V = getInstalledVer(Cache, P, true); - if (V.end() == true) - V = getCandidateVer(Cache, P, true); - if (V.end() == false) - verset.insert(V); - else if (AllowError == false) - return _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), P.FullName(true).c_str()); - else - return false; - break; - case VersionSet::NEWEST: - if (P->VersionList != 0) - verset.insert(P.VersionList()); - else if (AllowError == false) - return _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), P.FullName(true).c_str()); - else - return false; - break; - } - return true; -} - /*}}}*/ -// getCandidateVer - Returns the candidate version of the given package /*{{{*/ -pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache, - pkgCache::PkgIterator const &Pkg, bool const &AllowError) { - pkgCache::VerIterator Cand; - if (Cache.IsDepCacheBuilt() == true) - Cand = Cache[Pkg].CandidateVerIter(Cache); - else { - if (unlikely(Cache.BuildPolicy() == false)) - return pkgCache::VerIterator(*Cache); - Cand = Cache.GetPolicy()->GetCandidateVer(Pkg); - } - if (AllowError == false && Cand.end() == true) - _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str()); - return Cand; -} - /*}}}*/ -// getInstalledVer - Returns the installed version of the given package /*{{{*/ -pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache, - pkgCache::PkgIterator const &Pkg, bool const &AllowError) { - if (AllowError == false && Pkg->CurrentVer == 0) - _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str()); - return Pkg.CurrentVer(); -} - /*}}}*/ -} -- cgit v1.2.3 From 8fde723961709118837153cdf94f150c680e10e9 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 17 Jul 2010 20:04:44 +0200 Subject: * apt-pkg/cacheset.cc: - move them back to the library as they look stable now --- apt-pkg/cacheset.cc | 506 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 506 insertions(+) create mode 100644 apt-pkg/cacheset.cc (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc new file mode 100644 index 000000000..df7e99fd0 --- /dev/null +++ b/apt-pkg/cacheset.cc @@ -0,0 +1,506 @@ +// -*- mode: cpp; mode: fold -*- +// Description /*{{{*/ +/* ###################################################################### + + Simple wrapper around a std::set to provide a similar interface to + a set of cache structures as to the complete set of all structures + in the pkgCache. Currently only Package is supported. + + ##################################################################### */ + /*}}}*/ +// Include Files /*{{{*/ +#include +#include +#include +#include +#include +#include + +#include + +#include + +#include + /*}}}*/ +namespace APT { +// FromTask - Return all packages in the cache from a specific task /*{{{*/ +PackageSet PackageSet::FromTask(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { + size_t const archfound = pattern.find_last_of(':'); + std::string arch = "native"; + if (archfound != std::string::npos) { + arch = pattern.substr(archfound+1); + pattern.erase(archfound); + } + + if (pattern[pattern.length() -1] != '^') + return APT::PackageSet(TASK); + pattern.erase(pattern.length()-1); + + if (unlikely(Cache.GetPkgCache() == 0 || Cache.GetDepCache() == 0)) + return APT::PackageSet(TASK); + + PackageSet pkgset(TASK); + // get the records + pkgRecords Recs(Cache); + + // build regexp for the task + regex_t Pattern; + char S[300]; + snprintf(S, sizeof(S), "^Task:.*[, ]%s([, ]|$)", pattern.c_str()); + if(regcomp(&Pattern,S, REG_EXTENDED | REG_NOSUB | REG_NEWLINE) != 0) { + _error->Error("Failed to compile task regexp"); + return pkgset; + } + + for (pkgCache::GrpIterator Grp = Cache->GrpBegin(); Grp.end() == false; ++Grp) { + pkgCache::PkgIterator Pkg = Grp.FindPkg(arch); + if (Pkg.end() == true) + continue; + pkgCache::VerIterator ver = Cache[Pkg].CandidateVerIter(Cache); + if(ver.end() == true) + continue; + + pkgRecords::Parser &parser = Recs.Lookup(ver.FileList()); + const char *start, *end; + parser.GetRec(start,end); + unsigned int const length = end - start; + char buf[length]; + strncpy(buf, start, length); + buf[length-1] = '\0'; + if (regexec(&Pattern, buf, 0, 0, 0) != 0) + continue; + + pkgset.insert(Pkg); + } + regfree(&Pattern); + + if (pkgset.empty() == true) + return helper.canNotFindTask(Cache, pattern); + + helper.showTaskSelection(pkgset, pattern); + return pkgset; +} + /*}}}*/ +// FromRegEx - Return all packages in the cache matching a pattern /*{{{*/ +PackageSet PackageSet::FromRegEx(pkgCacheFile &Cache, std::string pattern, CacheSetHelper &helper) { + static const char * const isregex = ".?+*|[^$"; + if (pattern.find_first_of(isregex) == std::string::npos) + return PackageSet(REGEX); + + size_t archfound = pattern.find_last_of(':'); + std::string arch = "native"; + if (archfound != std::string::npos) { + arch = pattern.substr(archfound+1); + if (arch.find_first_of(isregex) == std::string::npos) + pattern.erase(archfound); + else + arch = "native"; + } + + if (unlikely(Cache.GetPkgCache() == 0)) + return PackageSet(REGEX); + + APT::CacheFilter::PackageNameMatchesRegEx regexfilter(pattern); + + PackageSet pkgset(REGEX); + for (pkgCache::GrpIterator Grp = Cache.GetPkgCache()->GrpBegin(); Grp.end() == false; ++Grp) { + if (regexfilter(Grp) == false) + continue; + pkgCache::PkgIterator Pkg = Grp.FindPkg(arch); + if (Pkg.end() == true) { + if (archfound == std::string::npos) { + std::vector archs = APT::Configuration::getArchitectures(); + for (std::vector::const_iterator a = archs.begin(); + a != archs.end() && Pkg.end() != true; ++a) + Pkg = Grp.FindPkg(*a); + } + if (Pkg.end() == true) + continue; + } + + pkgset.insert(Pkg); + } + + if (pkgset.empty() == true) + return helper.canNotFindRegEx(Cache, pattern); + + helper.showRegExSelection(pkgset, pattern); + return pkgset; +} + /*}}}*/ +// FromName - Returns the package defined by this string /*{{{*/ +pkgCache::PkgIterator PackageSet::FromName(pkgCacheFile &Cache, + std::string const &str, CacheSetHelper &helper) { + std::string pkg = str; + size_t archfound = pkg.find_last_of(':'); + std::string arch; + if (archfound != std::string::npos) { + arch = pkg.substr(archfound+1); + pkg.erase(archfound); + } + + if (Cache.GetPkgCache() == 0) + return pkgCache::PkgIterator(Cache, 0); + + pkgCache::PkgIterator Pkg(Cache, 0); + if (arch.empty() == true) { + pkgCache::GrpIterator Grp = Cache.GetPkgCache()->FindGrp(pkg); + if (Grp.end() == false) + Pkg = Grp.FindPreferredPkg(); + } else + Pkg = Cache.GetPkgCache()->FindPkg(pkg, arch); + + if (Pkg.end() == true) + return helper.canNotFindPkgName(Cache, str); + return Pkg; +} + /*}}}*/ +// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/ +std::map PackageSet::GroupedFromCommandLine( + pkgCacheFile &Cache, const char **cmdline, + std::list const &mods, + unsigned short const &fallback, CacheSetHelper &helper) { + std::map pkgsets; + for (const char **I = cmdline; *I != 0; ++I) { + unsigned short modID = fallback; + std::string str = *I; + bool modifierPresent = false; + for (std::list::const_iterator mod = mods.begin(); + mod != mods.end(); ++mod) { + size_t const alength = strlen(mod->Alias); + switch(mod->Pos) { + case PackageSet::Modifier::POSTFIX: + if (str.compare(str.length() - alength, alength, + mod->Alias, 0, alength) != 0) + continue; + str.erase(str.length() - alength); + modID = mod->ID; + break; + case PackageSet::Modifier::PREFIX: + continue; + case PackageSet::Modifier::NONE: + continue; + } + modifierPresent = true; + break; + } + if (modifierPresent == true) { + bool const errors = helper.showErrors(false); + pkgCache::PkgIterator Pkg = FromName(Cache, *I, helper); + helper.showErrors(errors); + if (Pkg.end() == false) { + pkgsets[fallback].insert(Pkg); + continue; + } + } + pkgsets[modID].insert(PackageSet::FromString(Cache, str, helper)); + } + return pkgsets; +} + /*}}}*/ +// FromCommandLine - Return all packages specified on commandline /*{{{*/ +PackageSet PackageSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, CacheSetHelper &helper) { + PackageSet pkgset; + for (const char **I = cmdline; *I != 0; ++I) { + PackageSet pset = FromString(Cache, *I, helper); + pkgset.insert(pset.begin(), pset.end()); + } + return pkgset; +} + /*}}}*/ +// FromString - Return all packages matching a specific string /*{{{*/ +PackageSet PackageSet::FromString(pkgCacheFile &Cache, std::string const &str, CacheSetHelper &helper) { + _error->PushToStack(); + + PackageSet pkgset; + pkgCache::PkgIterator Pkg = FromName(Cache, str, helper); + if (Pkg.end() == false) + pkgset.insert(Pkg); + else { + pkgset = FromTask(Cache, str, helper); + if (pkgset.empty() == true) { + pkgset = FromRegEx(Cache, str, helper); + if (pkgset.empty() == true) + pkgset = helper.canNotFindPackage(Cache, str); + } + } + + if (pkgset.empty() == false) + _error->RevertToStack(); + else + _error->MergeWithStack(); + return pkgset; +} + /*}}}*/ +// GroupedFromCommandLine - Return all versions specified on commandline/*{{{*/ +std::map VersionSet::GroupedFromCommandLine( + pkgCacheFile &Cache, const char **cmdline, + std::list const &mods, + unsigned short const &fallback, CacheSetHelper &helper) { + std::map versets; + for (const char **I = cmdline; *I != 0; ++I) { + unsigned short modID = fallback; + VersionSet::Version select = VersionSet::NEWEST; + std::string str = *I; + bool modifierPresent = false; + for (std::list::const_iterator mod = mods.begin(); + mod != mods.end(); ++mod) { + if (modID == fallback && mod->ID == fallback) + select = mod->SelectVersion; + size_t const alength = strlen(mod->Alias); + switch(mod->Pos) { + case VersionSet::Modifier::POSTFIX: + if (str.compare(str.length() - alength, alength, + mod->Alias, 0, alength) != 0) + continue; + str.erase(str.length() - alength); + modID = mod->ID; + select = mod->SelectVersion; + break; + case VersionSet::Modifier::PREFIX: + continue; + case VersionSet::Modifier::NONE: + continue; + } + modifierPresent = true; + break; + } + + if (modifierPresent == true) { + bool const errors = helper.showErrors(false); + VersionSet const vset = VersionSet::FromString(Cache, std::string(*I), select, helper, true); + helper.showErrors(errors); + if (vset.empty() == false) { + versets[fallback].insert(vset); + continue; + } + } + versets[modID].insert(VersionSet::FromString(Cache, str, select , helper)); + } + return versets; +} + /*}}}*/ +// FromCommandLine - Return all versions specified on commandline /*{{{*/ +APT::VersionSet VersionSet::FromCommandLine(pkgCacheFile &Cache, const char **cmdline, + APT::VersionSet::Version const &fallback, CacheSetHelper &helper) { + VersionSet verset; + for (const char **I = cmdline; *I != 0; ++I) + verset.insert(VersionSet::FromString(Cache, *I, fallback, helper)); + return verset; +} + /*}}}*/ +// FromString - Returns all versions spedcified by a string /*{{{*/ +APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg, + APT::VersionSet::Version const &fallback, CacheSetHelper &helper, + bool const &onlyFromName) { + std::string ver; + bool verIsRel = false; + size_t const vertag = pkg.find_last_of("/="); + if (vertag != string::npos) { + ver = pkg.substr(vertag+1); + verIsRel = (pkg[vertag] == '/'); + pkg.erase(vertag); + } + PackageSet pkgset; + if (onlyFromName == false) + pkgset = PackageSet::FromString(Cache, pkg, helper); + else { + pkgset.insert(PackageSet::FromName(Cache, pkg, helper)); + } + + VersionSet verset; + bool errors = true; + if (pkgset.getConstructor() != PackageSet::UNKNOWN) + errors = helper.showErrors(false); + for (PackageSet::const_iterator P = pkgset.begin(); + P != pkgset.end(); ++P) { + if (vertag == string::npos) { + verset.insert(VersionSet::FromPackage(Cache, P, fallback, helper)); + continue; + } + pkgCache::VerIterator V; + if (ver == "installed") + V = getInstalledVer(Cache, P, helper); + else if (ver == "candidate") + V = getCandidateVer(Cache, P, helper); + else { + pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release : + pkgVersionMatch::Version)); + V = Match.Find(P); + if (V.end() == true) { + if (verIsRel == true) + _error->Error(_("Release '%s' for '%s' was not found"), + ver.c_str(), P.FullName(true).c_str()); + else + _error->Error(_("Version '%s' for '%s' was not found"), + ver.c_str(), P.FullName(true).c_str()); + continue; + } + } + if (V.end() == true) + continue; + helper.showSelectedVersion(P, V, ver, verIsRel); + verset.insert(V); + } + if (pkgset.getConstructor() != PackageSet::UNKNOWN) + helper.showErrors(errors); + return verset; +} + /*}}}*/ +// FromPackage - versions from package based on fallback /*{{{*/ +VersionSet VersionSet::FromPackage(pkgCacheFile &Cache, pkgCache::PkgIterator const &P, + VersionSet::Version const &fallback, CacheSetHelper &helper) { + VersionSet verset; + pkgCache::VerIterator V; + bool showErrors; + switch(fallback) { + case VersionSet::ALL: + if (P->VersionList != 0) + for (V = P.VersionList(); V.end() != true; ++V) + verset.insert(V); + else + verset.insert(helper.canNotFindAllVer(Cache, P)); + break; + case VersionSet::CANDANDINST: + verset.insert(getInstalledVer(Cache, P, helper)); + verset.insert(getCandidateVer(Cache, P, helper)); + break; + case VersionSet::CANDIDATE: + verset.insert(getCandidateVer(Cache, P, helper)); + break; + case VersionSet::INSTALLED: + verset.insert(getInstalledVer(Cache, P, helper)); + break; + case VersionSet::CANDINST: + showErrors = helper.showErrors(false); + V = getCandidateVer(Cache, P, helper); + if (V.end() == true) + V = getInstalledVer(Cache, P, helper); + helper.showErrors(showErrors); + if (V.end() == false) + verset.insert(V); + else + verset.insert(helper.canNotFindInstCandVer(Cache, P)); + break; + case VersionSet::INSTCAND: + showErrors = helper.showErrors(false); + V = getInstalledVer(Cache, P, helper); + if (V.end() == true) + V = getCandidateVer(Cache, P, helper); + helper.showErrors(showErrors); + if (V.end() == false) + verset.insert(V); + else + verset.insert(helper.canNotFindInstCandVer(Cache, P)); + break; + case VersionSet::NEWEST: + if (P->VersionList != 0) + verset.insert(P.VersionList()); + else + verset.insert(helper.canNotFindNewestVer(Cache, P)); + break; + } + return verset; +} + /*}}}*/ +// getCandidateVer - Returns the candidate version of the given package /*{{{*/ +pkgCache::VerIterator VersionSet::getCandidateVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) { + pkgCache::VerIterator Cand; + if (Cache.IsPolicyBuilt() == true || Cache.IsDepCacheBuilt() == false) + { + if (unlikely(Cache.GetPolicy() == 0)) + return pkgCache::VerIterator(Cache); + Cand = Cache.GetPolicy()->GetCandidateVer(Pkg); + } else { + Cand = Cache[Pkg].CandidateVerIter(Cache); + } + if (Cand.end() == true) + return helper.canNotFindCandidateVer(Cache, Pkg); + return Cand; +} + /*}}}*/ +// getInstalledVer - Returns the installed version of the given package /*{{{*/ +pkgCache::VerIterator VersionSet::getInstalledVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg, CacheSetHelper &helper) { + if (Pkg->CurrentVer == 0) + return helper.canNotFindInstalledVer(Cache, Pkg); + return Pkg.CurrentVer(); +} + /*}}}*/ +// canNotFindPkgName - handle the case no package has this name /*{{{*/ +pkgCache::PkgIterator CacheSetHelper::canNotFindPkgName(pkgCacheFile &Cache, + std::string const &str) { + if (ShowError == true) + _error->Error(_("Unable to locate package %s"), str.c_str()); + return pkgCache::PkgIterator(Cache, 0); +} + /*}}}*/ +// canNotFindTask - handle the case no package is found for a task /*{{{*/ +PackageSet CacheSetHelper::canNotFindTask(pkgCacheFile &Cache, std::string pattern) { + if (ShowError == true) + _error->Error(_("Couldn't find task '%s'"), pattern.c_str()); + return PackageSet(); +} + /*}}}*/ +// canNotFindRegEx - handle the case no package is found by a regex /*{{{*/ +PackageSet CacheSetHelper::canNotFindRegEx(pkgCacheFile &Cache, std::string pattern) { + if (ShowError == true) + _error->Error(_("Couldn't find any package by regex '%s'"), pattern.c_str()); + return PackageSet(); +} + /*}}}*/ +// canNotFindPackage - handle the case no package is found from a string/*{{{*/ +PackageSet CacheSetHelper::canNotFindPackage(pkgCacheFile &Cache, std::string const &str) { + return PackageSet(); +} + /*}}}*/ +// canNotFindAllVer /*{{{*/ +VersionSet CacheSetHelper::canNotFindAllVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg) { + if (ShowError == true) + _error->Error(_("Can't select versions from package '%s' as it purely virtual"), Pkg.FullName(true).c_str()); + return VersionSet(); +} + /*}}}*/ +// canNotFindInstCandVer /*{{{*/ +VersionSet CacheSetHelper::canNotFindInstCandVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg) { + if (ShowError == true) + _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str()); + return VersionSet(); +} + /*}}}*/ +// canNotFindInstCandVer /*{{{*/ +VersionSet CacheSetHelper::canNotFindCandInstVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg) { + if (ShowError == true) + _error->Error(_("Can't select installed nor candidate version from package '%s' as it has neither of them"), Pkg.FullName(true).c_str()); + return VersionSet(); +} + /*}}}*/ +// canNotFindNewestVer /*{{{*/ +pkgCache::VerIterator CacheSetHelper::canNotFindNewestVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg) { + if (ShowError == true) + _error->Error(_("Can't select newest version from package '%s' as it is purely virtual"), Pkg.FullName(true).c_str()); + return pkgCache::VerIterator(Cache, 0); +} + /*}}}*/ +// canNotFindCandidateVer /*{{{*/ +pkgCache::VerIterator CacheSetHelper::canNotFindCandidateVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg) { + if (ShowError == true) + _error->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg.FullName(true).c_str()); + return pkgCache::VerIterator(Cache, 0); +} + /*}}}*/ +// canNotFindInstalledVer /*{{{*/ +pkgCache::VerIterator CacheSetHelper::canNotFindInstalledVer(pkgCacheFile &Cache, + pkgCache::PkgIterator const &Pkg) { + if (ShowError == true) + _error->Error(_("Can't select installed version from package %s as it is not installed"), Pkg.FullName(true).c_str()); + return pkgCache::VerIterator(Cache, 0); +} + /*}}}*/ +} -- cgit v1.2.3 From f1a58ff8b6cb4086ae59a2c6b02a29baf97ac116 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 26 Jul 2010 10:53:32 +0200 Subject: - add a 'newest' pseudo target release as in pkg/newest * --- apt-pkg/cacheset.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'apt-pkg/cacheset.cc') diff --git a/apt-pkg/cacheset.cc b/apt-pkg/cacheset.cc index df7e99fd0..f17a9e0d5 100644 --- a/apt-pkg/cacheset.cc +++ b/apt-pkg/cacheset.cc @@ -323,7 +323,12 @@ APT::VersionSet VersionSet::FromString(pkgCacheFile &Cache, std::string pkg, V = getInstalledVer(Cache, P, helper); else if (ver == "candidate") V = getCandidateVer(Cache, P, helper); - else { + else if (ver == "newest") { + if (P->VersionList != 0) + V = P.VersionList(); + else + V = helper.canNotFindNewestVer(Cache, P); + } else { pkgVersionMatch Match(ver, (verIsRel == true ? pkgVersionMatch::Release : pkgVersionMatch::Version)); V = Match.Find(P); -- cgit v1.2.3