]>
git.saurik.com Git - apt.git/blob - apt-pkg/versionmatch.cc
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 /*{{{*/
14 #include <apt-pkg/versionmatch.h>
16 #include <apt-pkg/strutl.h>
17 #include <apt-pkg/error.h>
22 #include <sys/types.h>
27 // VersionMatch::pkgVersionMatch - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* Break up the data string according to the selected type */
30 pkgVersionMatch::pkgVersionMatch(string Data
,MatchType Type
) : Type(Type
)
33 VerPrefixMatch
= false;
34 RelVerPrefixMatch
= false;
36 if (Type
== None
|| Data
.length() < 1)
39 // Cut up the version representation
42 if (Data
.end()[-1] == '*')
44 VerPrefixMatch
= true;
45 VerStr
= string(Data
,0,Data
.length()-1);
54 // All empty = match all
61 // Are we a simple specification?
62 string::const_iterator I
= Data
.begin();
63 for (; I
!= Data
.end() && *I
!= '='; I
++);
72 if (RelVerStr
.length() > 0 && RelVerStr
.end()[-1] == '*')
74 RelVerPrefixMatch
= true;
75 RelVerStr
= string(RelVerStr
.begin(),RelVerStr
.end()-1);
82 snprintf(Spec
,sizeof(Spec
),"%s",Data
.c_str());
83 if (TokSplitString(',',Spec
,Fragments
,
84 sizeof(Fragments
)/sizeof(Fragments
[0])) == false)
90 for (unsigned J
= 0; Fragments
[J
] != 0; J
++)
92 if (strlen(Fragments
[J
]) < 3)
95 if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"v=") == 0)
96 RelVerStr
= Fragments
[J
]+2;
97 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"o=") == 0)
98 RelOrigin
= Fragments
[J
]+2;
99 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"a=") == 0)
100 RelArchive
= Fragments
[J
]+2;
101 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"n=") == 0)
102 RelCodename
= Fragments
[J
]+2;
103 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"l=") == 0)
104 RelLabel
= Fragments
[J
]+2;
105 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"c=") == 0)
106 RelComponent
= Fragments
[J
]+2;
107 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"b=") == 0)
108 RelArchitecture
= Fragments
[J
]+2;
111 if (RelVerStr
.end()[-1] == '*')
113 RelVerPrefixMatch
= true;
114 RelVerStr
= string(RelVerStr
.begin(),RelVerStr
.end()-1);
121 if (Data
[0] == '"' && Data
.length() >= 2 && Data
.end()[-1] == '"')
122 OrSite
= Data
.substr(1, Data
.length() - 2);
129 // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
130 // ---------------------------------------------------------------------
132 bool pkgVersionMatch::MatchVer(const char *A
,string B
,bool Prefix
)
135 const char *Ae
= Ab
+ strlen(A
);
137 // Strings are not a compatible size.
138 if (((unsigned)(Ae
- Ab
) != B
.length() && Prefix
== false) ||
139 (unsigned)(Ae
- Ab
) < B
.length())
143 if (stringcasecmp(B
,Ab
,Ab
+ B
.length()) == 0)
149 // VersionMatch::Find - Locate the best match for the select type /*{{{*/
150 // ---------------------------------------------------------------------
152 pkgCache::VerIterator
pkgVersionMatch::Find(pkgCache::PkgIterator Pkg
)
154 pkgCache::VerIterator Ver
= Pkg
.VersionList();
155 for (; Ver
.end() == false; Ver
++)
159 if (MatchVer(Ver
.VerStr(),VerStr
,VerPrefixMatch
) == true)
161 if (ExpressionMatches(VerStr
, Ver
.VerStr()) == true)
166 for (pkgCache::VerFileIterator VF
= Ver
.FileList(); VF
.end() == false; VF
++)
167 if (FileMatch(VF
.File()) == true)
171 // This will be Ended by now.
176 #define FNM_CASEFOLD 0
179 bool pkgVersionMatch::ExpressionMatches(const char *pattern
, const char *string
)
181 if (pattern
[0] == '/') {
183 size_t length
= strlen(pattern
);
184 if (pattern
[length
- 1] == '/') {
186 char *regex
= strdup(pattern
+ 1);
187 regex
[length
- 2] = '\0';
188 if (regcomp(&preg
, regex
, REG_EXTENDED
| REG_ICASE
) != 0) {
189 _error
->Warning("Invalid regular expression: %s", regex
);
190 } else if (regexec(&preg
, string
, 0, NULL
, 0) == 0) {
198 return fnmatch(pattern
, string
, FNM_CASEFOLD
) == 0;
200 bool pkgVersionMatch::ExpressionMatches(const std::string
& pattern
, const char *string
)
202 return ExpressionMatches(pattern
.c_str(), string
);
205 // VersionMatch::FileMatch - Match against an index file /*{{{*/
206 // ---------------------------------------------------------------------
207 /* This matcher checks against the release file and the origin location
208 to see if the constraints are met. */
209 bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File
)
213 if (MatchAll
== true)
216 /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
217 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
219 if (RelVerStr
.empty() == true && RelOrigin
.empty() == true &&
220 RelArchive
.empty() == true && RelLabel
.empty() == true &&
221 RelRelease
.empty() == true && RelCodename
.empty() == true &&
222 RelComponent
.empty() == true && RelArchitecture
.empty() == true)
225 if (RelVerStr
.empty() == false)
226 if (File
->Version
== 0 ||
227 (MatchVer(File
.Version(),RelVerStr
,RelVerPrefixMatch
) == false &&
228 ExpressionMatches(RelVerStr
, File
.Version()) == false))
230 if (RelOrigin
.empty() == false)
231 if (File
->Origin
== 0 || !ExpressionMatches(RelOrigin
,File
.Origin()))
233 if (RelArchive
.empty() == false)
234 if (File
->Archive
== 0 ||
235 !ExpressionMatches(RelArchive
,File
.Archive()))
237 if (RelCodename
.empty() == false)
238 if (File
->Codename
== 0 ||
239 !ExpressionMatches(RelCodename
,File
.Codename()))
241 if (RelRelease
.empty() == false)
242 if ((File
->Archive
== 0 ||
243 !ExpressionMatches(RelRelease
,File
.Archive())) &&
244 (File
->Codename
== 0 ||
245 !ExpressionMatches(RelRelease
,File
.Codename())))
247 if (RelLabel
.empty() == false)
248 if (File
->Label
== 0 ||
249 !ExpressionMatches(RelLabel
,File
.Label()))
251 if (RelComponent
.empty() == false)
252 if (File
->Component
== 0 ||
253 !ExpressionMatches(RelComponent
,File
.Component()))
255 if (RelArchitecture
.empty() == false)
256 if (File
->Architecture
== 0 ||
257 !ExpressionMatches(RelArchitecture
,File
.Architecture()))
264 if (OrSite
.empty() == false) {
267 } else // so we are talking about file:// or status file
268 if (strcmp(File
.Site(),"") == 0 && File
->Archive
!= 0 && strcmp(File
.Archive(),"now") == 0) // skip the status file
270 return (ExpressionMatches(OrSite
, File
.Site())); /* both strings match */