]>
git.saurik.com Git - apt.git/blob - apt-pkg/versionmatch.cc
c215f522c09b6e7b2dac79235bc0ce37291e2a62
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: versionmatch.cc,v 1.9 2003/05/19 17:58:26 doogie Exp $
4 /* ######################################################################
8 This module takes a matching string and a type and locates the version
9 record that satisfies the constraint described by the matching string.
11 ##################################################################### */
13 // Include Files /*{{{*/
16 #include <apt-pkg/versionmatch.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/error.h>
19 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/cacheiterators.h>
34 // VersionMatch::pkgVersionMatch - Constructor /*{{{*/
35 // ---------------------------------------------------------------------
36 /* Break up the data string according to the selected type */
37 pkgVersionMatch::pkgVersionMatch(string Data
,MatchType Type
) : Type(Type
)
40 VerPrefixMatch
= false;
41 RelVerPrefixMatch
= false;
43 if (Type
== None
|| Data
.length() < 1)
46 // Cut up the version representation
49 if (Data
.end()[-1] == '*')
51 VerPrefixMatch
= true;
52 VerStr
= string(Data
,0,Data
.length()-1);
61 // All empty = match all
68 // Are we a simple specification?
69 string::const_iterator I
= Data
.begin();
70 for (; I
!= Data
.end() && *I
!= '='; ++I
);
79 if (RelVerStr
.length() > 0 && RelVerStr
.end()[-1] == '*')
81 RelVerPrefixMatch
= true;
82 RelVerStr
= string(RelVerStr
.begin(),RelVerStr
.end()-1);
89 snprintf(Spec
,sizeof(Spec
),"%s",Data
.c_str());
90 if (TokSplitString(',',Spec
,Fragments
,
91 sizeof(Fragments
)/sizeof(Fragments
[0])) == false)
97 for (unsigned J
= 0; Fragments
[J
] != 0; J
++)
99 if (strlen(Fragments
[J
]) < 3)
102 if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"v=") == 0)
103 RelVerStr
= Fragments
[J
]+2;
104 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"o=") == 0)
105 RelOrigin
= Fragments
[J
]+2;
106 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"a=") == 0)
107 RelArchive
= Fragments
[J
]+2;
108 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"n=") == 0)
109 RelCodename
= Fragments
[J
]+2;
110 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"l=") == 0)
111 RelLabel
= Fragments
[J
]+2;
112 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"c=") == 0)
113 RelComponent
= Fragments
[J
]+2;
114 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"b=") == 0)
115 RelArchitecture
= Fragments
[J
]+2;
118 if (RelVerStr
.end()[-1] == '*')
120 RelVerPrefixMatch
= true;
121 RelVerStr
= string(RelVerStr
.begin(),RelVerStr
.end()-1);
128 if (Data
[0] == '"' && Data
.length() >= 2 && Data
.end()[-1] == '"')
129 OrSite
= Data
.substr(1, Data
.length() - 2);
136 // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
137 // ---------------------------------------------------------------------
139 bool pkgVersionMatch::MatchVer(const char *A
,string B
,bool Prefix
)
145 const char *Ae
= Ab
+ strlen(A
);
147 // Strings are not a compatible size.
148 if (((unsigned)(Ae
- Ab
) != B
.length() && Prefix
== false) ||
149 (unsigned)(Ae
- Ab
) < B
.length())
153 if (stringcasecmp(B
,Ab
,Ab
+ B
.length()) == 0)
159 // VersionMatch::Find - Locate the best match for the select type /*{{{*/
160 // ---------------------------------------------------------------------
162 pkgCache::VerIterator
pkgVersionMatch::Find(pkgCache::PkgIterator Pkg
)
164 pkgCache::VerIterator Ver
= Pkg
.VersionList();
165 for (; Ver
.end() == false; ++Ver
)
167 if (VersionMatches(Ver
))
171 // This will be Ended by now.
176 // VersionMatch::Find - Locate the best match for the select type /*{{{*/
177 // ---------------------------------------------------------------------
179 bool pkgVersionMatch::VersionMatches(pkgCache::VerIterator Ver
)
183 if (MatchVer(Ver
.VerStr(),VerStr
,VerPrefixMatch
) == true)
185 if (ExpressionMatches(VerStr
, Ver
.VerStr()) == true)
190 for (pkgCache::VerFileIterator VF
= Ver
.FileList(); VF
.end() == false; ++VF
)
191 if (FileMatch(VF
.File()) == true)
199 #define FNM_CASEFOLD 0
202 bool pkgVersionMatch::ExpressionMatches(const char *pattern
, const char *string
)/*{{{*/
204 if (pattern
== NULL
|| string
== NULL
)
206 if (pattern
[0] == '/') {
207 size_t length
= strlen(pattern
);
208 if (pattern
[length
- 1] == '/') {
211 char *regex
= strdup(pattern
+ 1);
212 regex
[length
- 2] = '\0';
213 if (regcomp(&preg
, regex
, REG_EXTENDED
| REG_ICASE
) != 0) {
214 _error
->Warning("Invalid regular expression: %s", regex
);
215 } else if (regexec(&preg
, string
, 0, NULL
, 0) == 0) {
223 return fnmatch(pattern
, string
, FNM_CASEFOLD
) == 0;
225 bool pkgVersionMatch::ExpressionMatches(const std::string
& pattern
, const char *string
)
227 return ExpressionMatches(pattern
.c_str(), string
);
230 // VersionMatch::FileMatch - Match against an index file /*{{{*/
231 // ---------------------------------------------------------------------
232 /* This matcher checks against the release file and the origin location
233 to see if the constraints are met. */
234 bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File
)
238 if (MatchAll
== true)
241 /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
242 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
244 if (RelVerStr
.empty() == true && RelOrigin
.empty() == true &&
245 RelArchive
.empty() == true && RelLabel
.empty() == true &&
246 RelRelease
.empty() == true && RelCodename
.empty() == true &&
247 RelComponent
.empty() == true && RelArchitecture
.empty() == true)
250 if (RelVerStr
.empty() == false)
251 if (MatchVer(File
.Version(),RelVerStr
,RelVerPrefixMatch
) == false &&
252 ExpressionMatches(RelVerStr
, File
.Version()) == false)
254 if (RelOrigin
.empty() == false)
255 if (!ExpressionMatches(RelOrigin
,File
.Origin()))
257 if (RelArchive
.empty() == false)
258 if (!ExpressionMatches(RelArchive
,File
.Archive()))
260 if (RelCodename
.empty() == false)
261 if (!ExpressionMatches(RelCodename
,File
.Codename()))
263 if (RelRelease
.empty() == false)
264 if (!ExpressionMatches(RelRelease
,File
.Archive()) &&
265 !ExpressionMatches(RelRelease
,File
.Codename()))
267 if (RelLabel
.empty() == false)
268 if (!ExpressionMatches(RelLabel
,File
.Label()))
270 if (RelComponent
.empty() == false)
271 if (!ExpressionMatches(RelComponent
,File
.Component()))
273 if (RelArchitecture
.empty() == false)
274 if (!ExpressionMatches(RelArchitecture
,File
.Architecture()))
281 if (OrSite
.empty() == false) {
282 if (File
.Site() == NULL
)
284 } else // so we are talking about file:// or status file
286 pkgCache::RlsFileIterator
const RlsFile
= File
.ReleaseFile();
287 if (strcmp(File
.Site(),"") == 0 && RlsFile
->Archive
!= 0 && strcmp(RlsFile
.Archive(),"now") == 0) // skip the status file
290 return (ExpressionMatches(OrSite
, File
.Site())); /* both strings match */