]>
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 /*{{{*/
16 #include <apt-pkg/versionmatch.h>
17 #include <apt-pkg/strutl.h>
18 #include <apt-pkg/error.h>
23 #include <sys/types.h>
29 // VersionMatch::pkgVersionMatch - Constructor /*{{{*/
30 // ---------------------------------------------------------------------
31 /* Break up the data string according to the selected type */
32 pkgVersionMatch::pkgVersionMatch(string Data
,MatchType Type
) : Type(Type
)
35 VerPrefixMatch
= false;
36 RelVerPrefixMatch
= false;
38 if (Type
== None
|| Data
.length() < 1)
41 // Cut up the version representation
44 if (Data
.end()[-1] == '*')
46 VerPrefixMatch
= true;
47 VerStr
= string(Data
,0,Data
.length()-1);
56 // All empty = match all
63 // Are we a simple specification?
64 string::const_iterator I
= Data
.begin();
65 for (; I
!= Data
.end() && *I
!= '='; ++I
);
74 if (RelVerStr
.length() > 0 && RelVerStr
.end()[-1] == '*')
76 RelVerPrefixMatch
= true;
77 RelVerStr
= string(RelVerStr
.begin(),RelVerStr
.end()-1);
84 snprintf(Spec
,sizeof(Spec
),"%s",Data
.c_str());
85 if (TokSplitString(',',Spec
,Fragments
,
86 sizeof(Fragments
)/sizeof(Fragments
[0])) == false)
92 for (unsigned J
= 0; Fragments
[J
] != 0; J
++)
94 if (strlen(Fragments
[J
]) < 3)
97 if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"v=") == 0)
98 RelVerStr
= Fragments
[J
]+2;
99 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"o=") == 0)
100 RelOrigin
= Fragments
[J
]+2;
101 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"a=") == 0)
102 RelArchive
= Fragments
[J
]+2;
103 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"n=") == 0)
104 RelCodename
= Fragments
[J
]+2;
105 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"l=") == 0)
106 RelLabel
= Fragments
[J
]+2;
107 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"c=") == 0)
108 RelComponent
= Fragments
[J
]+2;
109 else if (stringcasecmp(Fragments
[J
],Fragments
[J
]+2,"b=") == 0)
110 RelArchitecture
= Fragments
[J
]+2;
113 if (RelVerStr
.end()[-1] == '*')
115 RelVerPrefixMatch
= true;
116 RelVerStr
= string(RelVerStr
.begin(),RelVerStr
.end()-1);
123 if (Data
[0] == '"' && Data
.length() >= 2 && Data
.end()[-1] == '"')
124 OrSite
= Data
.substr(1, Data
.length() - 2);
131 // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
132 // ---------------------------------------------------------------------
134 bool pkgVersionMatch::MatchVer(const char *A
,string B
,bool Prefix
)
137 const char *Ae
= Ab
+ strlen(A
);
139 // Strings are not a compatible size.
140 if (((unsigned)(Ae
- Ab
) != B
.length() && Prefix
== false) ||
141 (unsigned)(Ae
- Ab
) < B
.length())
145 if (stringcasecmp(B
,Ab
,Ab
+ B
.length()) == 0)
151 // VersionMatch::Find - Locate the best match for the select type /*{{{*/
152 // ---------------------------------------------------------------------
154 pkgCache::VerIterator
pkgVersionMatch::Find(pkgCache::PkgIterator Pkg
)
156 pkgCache::VerIterator Ver
= Pkg
.VersionList();
157 for (; Ver
.end() == false; ++Ver
)
161 if (MatchVer(Ver
.VerStr(),VerStr
,VerPrefixMatch
) == true)
163 if (ExpressionMatches(VerStr
, Ver
.VerStr()) == true)
168 for (pkgCache::VerFileIterator VF
= Ver
.FileList(); VF
.end() == false; ++VF
)
169 if (FileMatch(VF
.File()) == true)
173 // This will be Ended by now.
178 #define FNM_CASEFOLD 0
181 bool pkgVersionMatch::ExpressionMatches(const char *pattern
, const char *string
)
183 if (pattern
[0] == '/') {
184 size_t length
= strlen(pattern
);
185 if (pattern
[length
- 1] == '/') {
188 char *regex
= strdup(pattern
+ 1);
189 regex
[length
- 2] = '\0';
190 if (regcomp(&preg
, regex
, REG_EXTENDED
| REG_ICASE
) != 0) {
191 _error
->Warning("Invalid regular expression: %s", regex
);
192 } else if (regexec(&preg
, string
, 0, NULL
, 0) == 0) {
200 return fnmatch(pattern
, string
, FNM_CASEFOLD
) == 0;
202 bool pkgVersionMatch::ExpressionMatches(const std::string
& pattern
, const char *string
)
204 return ExpressionMatches(pattern
.c_str(), string
);
207 // VersionMatch::FileMatch - Match against an index file /*{{{*/
208 // ---------------------------------------------------------------------
209 /* This matcher checks against the release file and the origin location
210 to see if the constraints are met. */
211 bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File
)
215 if (MatchAll
== true)
218 /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
219 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
221 if (RelVerStr
.empty() == true && RelOrigin
.empty() == true &&
222 RelArchive
.empty() == true && RelLabel
.empty() == true &&
223 RelRelease
.empty() == true && RelCodename
.empty() == true &&
224 RelComponent
.empty() == true && RelArchitecture
.empty() == true)
227 if (RelVerStr
.empty() == false)
228 if (File
->Version
== 0 ||
229 (MatchVer(File
.Version(),RelVerStr
,RelVerPrefixMatch
) == false &&
230 ExpressionMatches(RelVerStr
, File
.Version()) == false))
232 if (RelOrigin
.empty() == false)
233 if (File
->Origin
== 0 || !ExpressionMatches(RelOrigin
,File
.Origin()))
235 if (RelArchive
.empty() == false)
236 if (File
->Archive
== 0 ||
237 !ExpressionMatches(RelArchive
,File
.Archive()))
239 if (RelCodename
.empty() == false)
240 if (File
->Codename
== 0 ||
241 !ExpressionMatches(RelCodename
,File
.Codename()))
243 if (RelRelease
.empty() == false)
244 if ((File
->Archive
== 0 ||
245 !ExpressionMatches(RelRelease
,File
.Archive())) &&
246 (File
->Codename
== 0 ||
247 !ExpressionMatches(RelRelease
,File
.Codename())))
249 if (RelLabel
.empty() == false)
250 if (File
->Label
== 0 ||
251 !ExpressionMatches(RelLabel
,File
.Label()))
253 if (RelComponent
.empty() == false)
254 if (File
->Component
== 0 ||
255 !ExpressionMatches(RelComponent
,File
.Component()))
257 if (RelArchitecture
.empty() == false)
258 if (File
->Architecture
== 0 ||
259 !ExpressionMatches(RelArchitecture
,File
.Architecture()))
266 if (OrSite
.empty() == false) {
269 } else // so we are talking about file:// or status file
270 if (strcmp(File
.Site(),"") == 0 && File
->Archive
!= 0 && strcmp(File
.Archive(),"now") == 0) // skip the status file
272 return (ExpressionMatches(OrSite
, File
.Site())); /* both strings match */