]>
git.saurik.com Git - apt.git/blob - apt-pkg/cacheset.cc
5dbd1a4dfa4322a476d3c980139ee252c0fb052d
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Simple wrapper around a std::set to provide a similar interface to
6 a set of cache structures as to the complete set of all structures
7 in the pkgCache. Currently only Package is supported.
9 ##################################################################### */
11 // Include Files /*{{{*/
12 #include <apt-pkg/aptconfiguration.h>
13 #include <apt-pkg/error.h>
14 #include <apt-pkg/cacheset.h>
15 #include <apt-pkg/strutl.h>
16 #include <apt-pkg/versionmatch.h>
25 // FromRegEx - Return all packages in the cache matching a pattern /*{{{*/
26 PackageSet
PackageSet::FromRegEx(pkgCacheFile
&Cache
, std::string pattern
, std::ostream
&out
) {
28 std::string arch
= "native";
29 static const char * const isregex
= ".?+*|[^$";
31 if (pattern
.find_first_of(isregex
) == std::string::npos
)
34 size_t archfound
= pattern
.find_last_of(':');
35 if (archfound
!= std::string::npos
) {
36 arch
= pattern
.substr(archfound
+1);
37 if (arch
.find_first_of(isregex
) == std::string::npos
)
38 pattern
.erase(archfound
);
45 if ((Res
= regcomp(&Pattern
, pattern
.c_str() , REG_EXTENDED
| REG_ICASE
| REG_NOSUB
)) != 0) {
47 regerror(Res
, &Pattern
, Error
, sizeof(Error
));
48 _error
->Error(_("Regex compilation error - %s"), Error
);
52 for (pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->GrpBegin(); Grp
.end() == false; ++Grp
)
54 if (regexec(&Pattern
, Grp
.Name(), 0, 0, 0) != 0)
56 pkgCache::PkgIterator Pkg
= Grp
.FindPkg(arch
);
57 if (Pkg
.end() == true) {
58 if (archfound
== std::string::npos
) {
59 std::vector
<std::string
> archs
= APT::Configuration::getArchitectures();
60 for (std::vector
<std::string
>::const_iterator a
= archs
.begin();
61 a
!= archs
.end() && Pkg
.end() != true; ++a
)
62 Pkg
= Grp
.FindPkg(*a
);
64 if (Pkg
.end() == true)
68 ioprintf(out
, _("Note, selecting %s for regex '%s'\n"),
69 Pkg
.FullName(true).c_str(), pattern
.c_str());
79 // FromCommandLine - Return all packages specified on commandline /*{{{*/
80 PackageSet
PackageSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
, std::ostream
&out
) {
82 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
83 PackageSet pset
= FromString(Cache
, *I
, out
);
84 pkgset
.insert(pset
.begin(), pset
.end());
89 // FromString - Return all packages matching a specific string /*{{{*/
90 PackageSet
PackageSet::FromString(pkgCacheFile
&Cache
, const char * const str
, std::ostream
&out
) {
91 pkgCache::GrpIterator Grp
= Cache
.GetPkgCache()->FindGrp(str
);
92 if (Grp
.end() == false) {
93 pkgCache::PkgIterator Pkg
= Grp
.FindPreferredPkg();
98 PackageSet regex
= FromRegEx(Cache
, str
, out
);
99 if (regex
.empty() == true)
100 _error
->Warning(_("Unable to locate package %s"), str
);
104 // FromCommandLine - Return all versions specified on commandline /*{{{*/
105 APT::VersionSet
VersionSet::FromCommandLine(pkgCacheFile
&Cache
, const char **cmdline
,
106 APT::VersionSet::Version
const &fallback
, std::ostream
&out
) {
108 for (const char **I
= cmdline
; *I
!= 0; ++I
) {
109 std::string pkg
= *I
;
111 bool verIsRel
= false;
112 size_t const vertag
= pkg
.find_last_of("/=");
113 if (vertag
!= string::npos
) {
114 ver
= pkg
.substr(vertag
+1);
115 verIsRel
= (pkg
[vertag
] == '/');
118 PackageSet pkgset
= PackageSet::FromString(Cache
, pkg
.c_str(), out
);
119 for (PackageSet::const_iterator P
= pkgset
.begin();
120 P
!= pkgset
.end(); ++P
) {
121 if (vertag
!= string::npos
) {
122 pkgVersionMatch
Match(ver
, (verIsRel
== true ? pkgVersionMatch::Release
:
123 pkgVersionMatch::Version
));
124 pkgCache::VerIterator V
= Match
.Find(P
);
125 if (V
.end() == true) {
126 if (verIsRel
== true)
127 _error
->Error(_("Release '%s' for '%s' was not found"),
128 ver
.c_str(), P
.FullName(true).c_str());
130 _error
->Error(_("Version '%s' for '%s' was not found"),
131 ver
.c_str(), P
.FullName(true).c_str());
134 if (strcmp(ver
.c_str(), V
.VerStr()) != 0)
135 ioprintf(out
, _("Selected version %s (%s) for %s\n"),
136 V
.VerStr(), V
.RelStr().c_str(), P
.FullName(true).c_str());
139 pkgCache::VerIterator V
;
141 case VersionSet::ALL
:
142 for (V
= P
.VersionList(); V
.end() != true; ++V
)
145 case VersionSet::CANDANDINST
:
146 verset
.insert(getInstalledVer(Cache
, P
));
147 verset
.insert(getCandidateVer(Cache
, P
));
149 case VersionSet::CANDIDATE
:
150 verset
.insert(getCandidateVer(Cache
, P
));
152 case VersionSet::INSTALLED
:
153 verset
.insert(getInstalledVer(Cache
, P
));
155 case VersionSet::CANDINST
:
156 V
= getCandidateVer(Cache
, P
, true);
158 V
= getInstalledVer(Cache
, P
, true);
159 if (V
.end() == false)
162 _error
->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P
.FullName(true).c_str());
164 case VersionSet::INSTCAND
:
165 V
= getInstalledVer(Cache
, P
, true);
167 V
= getCandidateVer(Cache
, P
, true);
168 if (V
.end() == false)
171 _error
->Error(_("Can't select installed nor candidate version from package %s as it has neither of them"), P
.FullName(true).c_str());
173 case VersionSet::NEWEST
:
174 if (P
->VersionList
!= 0)
175 verset
.insert(P
.VersionList());
177 _error
->Error(_("Can't select newest version from package %s as it is purely virtual"), P
.FullName(true).c_str());
186 // getCandidateVer - Returns the candidate version of the given package /*{{{*/
187 pkgCache::VerIterator
VersionSet::getCandidateVer(pkgCacheFile
&Cache
,
188 pkgCache::PkgIterator
const &Pkg
, bool const &AllowError
) {
189 if (unlikely(Cache
.BuildDepCache() == false))
190 return pkgCache::VerIterator(*Cache
);
191 pkgCache::VerIterator Cand
= Cache
[Pkg
].InstVerIter(Cache
);
192 if (AllowError
== false && Cand
.end() == true)
193 _error
->Error(_("Can't select candidate version from package %s as it has no candidate"), Pkg
.FullName(true).c_str());
197 // getInstalledVer - Returns the installed version of the given package /*{{{*/
198 pkgCache::VerIterator
VersionSet::getInstalledVer(pkgCacheFile
&Cache
,
199 pkgCache::PkgIterator
const &Pkg
, bool const &AllowError
) {
200 if (AllowError
== false && Pkg
->CurrentVer
== 0)
201 _error
->Error(_("Can't select installed version from package %s as it is not installed"), Pkg
.FullName(true).c_str());
202 return Pkg
.CurrentVer();