]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: versionmatch.cc,v 1.9 2003/05/19 17:58:26 doogie Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | Version Matching | |
7 | ||
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. | |
10 | ||
11 | ##################################################################### */ | |
12 | /*}}}*/ | |
13 | // Include Files /*{{{*/ | |
14 | #include<config.h> | |
15 | ||
16 | #include <apt-pkg/versionmatch.h> | |
17 | #include <apt-pkg/strutl.h> | |
18 | #include <apt-pkg/error.h> | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <ctype.h> | |
22 | #include <fnmatch.h> | |
23 | #include <sys/types.h> | |
24 | #include <regex.h> | |
25 | /*}}}*/ | |
26 | ||
27 | using std::string; | |
28 | ||
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) | |
33 | { | |
34 | MatchAll = false; | |
35 | VerPrefixMatch = false; | |
36 | RelVerPrefixMatch = false; | |
37 | ||
38 | if (Type == None || Data.length() < 1) | |
39 | return; | |
40 | ||
41 | // Cut up the version representation | |
42 | if (Type == Version) | |
43 | { | |
44 | if (Data.end()[-1] == '*') | |
45 | { | |
46 | VerPrefixMatch = true; | |
47 | VerStr = string(Data,0,Data.length()-1); | |
48 | } | |
49 | else | |
50 | VerStr = Data; | |
51 | return; | |
52 | } | |
53 | ||
54 | if (Type == Release) | |
55 | { | |
56 | // All empty = match all | |
57 | if (Data == "*") | |
58 | { | |
59 | MatchAll = true; | |
60 | return; | |
61 | } | |
62 | ||
63 | // Are we a simple specification? | |
64 | string::const_iterator I = Data.begin(); | |
65 | for (; I != Data.end() && *I != '='; ++I); | |
66 | if (I == Data.end()) | |
67 | { | |
68 | // Temporary | |
69 | if (isdigit(Data[0])) | |
70 | RelVerStr = Data; | |
71 | else | |
72 | RelRelease = Data; | |
73 | ||
74 | if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*') | |
75 | { | |
76 | RelVerPrefixMatch = true; | |
77 | RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1); | |
78 | } | |
79 | return; | |
80 | } | |
81 | ||
82 | char Spec[300]; | |
83 | char *Fragments[20]; | |
84 | snprintf(Spec,sizeof(Spec),"%s",Data.c_str()); | |
85 | if (TokSplitString(',',Spec,Fragments, | |
86 | sizeof(Fragments)/sizeof(Fragments[0])) == false) | |
87 | { | |
88 | Type = None; | |
89 | return; | |
90 | } | |
91 | ||
92 | for (unsigned J = 0; Fragments[J] != 0; J++) | |
93 | { | |
94 | if (strlen(Fragments[J]) < 3) | |
95 | continue; | |
96 | ||
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; | |
111 | } | |
112 | ||
113 | if (RelVerStr.end()[-1] == '*') | |
114 | { | |
115 | RelVerPrefixMatch = true; | |
116 | RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1); | |
117 | } | |
118 | return; | |
119 | } | |
120 | ||
121 | if (Type == Origin) | |
122 | { | |
123 | if (Data[0] == '"' && Data.length() >= 2 && Data.end()[-1] == '"') | |
124 | OrSite = Data.substr(1, Data.length() - 2); | |
125 | else | |
126 | OrSite = Data; | |
127 | return; | |
128 | } | |
129 | } | |
130 | /*}}}*/ | |
131 | // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/ | |
132 | // --------------------------------------------------------------------- | |
133 | /* */ | |
134 | bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix) | |
135 | { | |
136 | const char *Ab = A; | |
137 | const char *Ae = Ab + strlen(A); | |
138 | ||
139 | // Strings are not a compatible size. | |
140 | if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) || | |
141 | (unsigned)(Ae - Ab) < B.length()) | |
142 | return false; | |
143 | ||
144 | // Match (leading?) | |
145 | if (stringcasecmp(B,Ab,Ab + B.length()) == 0) | |
146 | return true; | |
147 | ||
148 | return false; | |
149 | } | |
150 | /*}}}*/ | |
151 | // VersionMatch::Find - Locate the best match for the select type /*{{{*/ | |
152 | // --------------------------------------------------------------------- | |
153 | /* */ | |
154 | pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg) | |
155 | { | |
156 | pkgCache::VerIterator Ver = Pkg.VersionList(); | |
157 | for (; Ver.end() == false; ++Ver) | |
158 | { | |
159 | if (Type == Version) | |
160 | { | |
161 | if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true) | |
162 | return Ver; | |
163 | if (ExpressionMatches(VerStr, Ver.VerStr()) == true) | |
164 | return Ver; | |
165 | continue; | |
166 | } | |
167 | ||
168 | for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF) | |
169 | if (FileMatch(VF.File()) == true) | |
170 | return Ver; | |
171 | } | |
172 | ||
173 | // This will be Ended by now. | |
174 | return Ver; | |
175 | } | |
176 | ||
177 | #ifndef FNM_CASEFOLD | |
178 | #define FNM_CASEFOLD 0 | |
179 | #endif | |
180 | ||
181 | bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string) | |
182 | { | |
183 | if (pattern[0] == '/') { | |
184 | size_t length = strlen(pattern); | |
185 | if (pattern[length - 1] == '/') { | |
186 | bool res = false; | |
187 | regex_t preg; | |
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) { | |
193 | res = true; | |
194 | } | |
195 | free(regex); | |
196 | regfree(&preg); | |
197 | return res; | |
198 | } | |
199 | } | |
200 | return fnmatch(pattern, string, FNM_CASEFOLD) == 0; | |
201 | } | |
202 | bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string) | |
203 | { | |
204 | return ExpressionMatches(pattern.c_str(), string); | |
205 | } | |
206 | /*}}}*/ | |
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) | |
212 | { | |
213 | if (Type == Release) | |
214 | { | |
215 | if (MatchAll == true) | |
216 | return true; | |
217 | ||
218 | /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl; | |
219 | cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/ | |
220 | ||
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) | |
225 | return false; | |
226 | ||
227 | if (RelVerStr.empty() == false) | |
228 | if (File->Version == 0 || | |
229 | (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false && | |
230 | ExpressionMatches(RelVerStr, File.Version()) == false)) | |
231 | return false; | |
232 | if (RelOrigin.empty() == false) | |
233 | if (File->Origin == 0 || !ExpressionMatches(RelOrigin,File.Origin())) | |
234 | return false; | |
235 | if (RelArchive.empty() == false) | |
236 | if (File->Archive == 0 || | |
237 | !ExpressionMatches(RelArchive,File.Archive())) | |
238 | return false; | |
239 | if (RelCodename.empty() == false) | |
240 | if (File->Codename == 0 || | |
241 | !ExpressionMatches(RelCodename,File.Codename())) | |
242 | return false; | |
243 | if (RelRelease.empty() == false) | |
244 | if ((File->Archive == 0 || | |
245 | !ExpressionMatches(RelRelease,File.Archive())) && | |
246 | (File->Codename == 0 || | |
247 | !ExpressionMatches(RelRelease,File.Codename()))) | |
248 | return false; | |
249 | if (RelLabel.empty() == false) | |
250 | if (File->Label == 0 || | |
251 | !ExpressionMatches(RelLabel,File.Label())) | |
252 | return false; | |
253 | if (RelComponent.empty() == false) | |
254 | if (File->Component == 0 || | |
255 | !ExpressionMatches(RelComponent,File.Component())) | |
256 | return false; | |
257 | if (RelArchitecture.empty() == false) | |
258 | if (File->Architecture == 0 || | |
259 | !ExpressionMatches(RelArchitecture,File.Architecture())) | |
260 | return false; | |
261 | return true; | |
262 | } | |
263 | ||
264 | if (Type == Origin) | |
265 | { | |
266 | if (OrSite.empty() == false) { | |
267 | if (File->Site == 0) | |
268 | return 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 | |
271 | return false; | |
272 | return (ExpressionMatches(OrSite, File.Site())); /* both strings match */ | |
273 | } | |
274 | ||
275 | return false; | |
276 | } | |
277 | /*}}}*/ |