]>
Commit | Line | Data |
---|---|---|
b2e465d6 AL |
1 | // -*- mode: cpp; mode: fold -*- |
2 | // Description /*{{{*/ | |
acfe7306 | 3 | // $Id: versionmatch.cc,v 1.9 2003/05/19 17:58:26 doogie Exp $ |
b2e465d6 AL |
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 /*{{{*/ | |
ea542140 | 14 | #include<config.h> |
b2e465d6 | 15 | |
ea542140 | 16 | #include <apt-pkg/versionmatch.h> |
b2e465d6 AL |
17 | #include <apt-pkg/strutl.h> |
18 | #include <apt-pkg/error.h> | |
453b82a3 DK |
19 | #include <apt-pkg/pkgcache.h> |
20 | #include <apt-pkg/cacheiterators.h> | |
b2e465d6 | 21 | |
453b82a3 DK |
22 | #include <stddef.h> |
23 | #include <stdlib.h> | |
24 | #include <string.h> | |
25 | #include <string> | |
b2e465d6 | 26 | #include <stdio.h> |
8d9850a1 | 27 | #include <ctype.h> |
ae4a4f91 | 28 | #include <fnmatch.h> |
ae4a4f91 | 29 | #include <regex.h> |
b2e465d6 AL |
30 | /*}}}*/ |
31 | ||
8f3ba4e8 DK |
32 | using std::string; |
33 | ||
b2e465d6 AL |
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) | |
38 | { | |
4e0680f9 AL |
39 | MatchAll = false; |
40 | VerPrefixMatch = false; | |
41 | RelVerPrefixMatch = false; | |
42 | ||
b2e465d6 AL |
43 | if (Type == None || Data.length() < 1) |
44 | return; | |
45 | ||
46 | // Cut up the version representation | |
47 | if (Type == Version) | |
48 | { | |
49 | if (Data.end()[-1] == '*') | |
50 | { | |
51 | VerPrefixMatch = true; | |
8d9850a1 | 52 | VerStr = string(Data,0,Data.length()-1); |
b2e465d6 AL |
53 | } |
54 | else | |
55 | VerStr = Data; | |
56 | return; | |
57 | } | |
58 | ||
59 | if (Type == Release) | |
60 | { | |
61 | // All empty = match all | |
62 | if (Data == "*") | |
0a5e4a03 AL |
63 | { |
64 | MatchAll = true; | |
b2e465d6 | 65 | return; |
0a5e4a03 AL |
66 | } |
67 | ||
b2e465d6 | 68 | // Are we a simple specification? |
8d9850a1 | 69 | string::const_iterator I = Data.begin(); |
f7f0d6c7 | 70 | for (; I != Data.end() && *I != '='; ++I); |
b2e465d6 AL |
71 | if (I == Data.end()) |
72 | { | |
73 | // Temporary | |
74 | if (isdigit(Data[0])) | |
75 | RelVerStr = Data; | |
76 | else | |
efc487fb DK |
77 | RelRelease = Data; |
78 | ||
b8b7c37d | 79 | if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*') |
b2e465d6 AL |
80 | { |
81 | RelVerPrefixMatch = true; | |
82 | RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1); | |
83 | } | |
84 | return; | |
85 | } | |
86 | ||
87 | char Spec[300]; | |
88 | char *Fragments[20]; | |
89 | snprintf(Spec,sizeof(Spec),"%s",Data.c_str()); | |
90 | if (TokSplitString(',',Spec,Fragments, | |
91 | sizeof(Fragments)/sizeof(Fragments[0])) == false) | |
92 | { | |
93 | Type = None; | |
94 | return; | |
95 | } | |
96 | ||
97 | for (unsigned J = 0; Fragments[J] != 0; J++) | |
98 | { | |
99 | if (strlen(Fragments[J]) < 3) | |
100 | continue; | |
efc487fb | 101 | |
b2e465d6 AL |
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; | |
efc487fb DK |
108 | else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0) |
109 | RelCodename = Fragments[J]+2; | |
b2e465d6 AL |
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; | |
5dd4c8b8 DK |
114 | else if (stringcasecmp(Fragments[J],Fragments[J]+2,"b=") == 0) |
115 | RelArchitecture = Fragments[J]+2; | |
b2e465d6 | 116 | } |
efc487fb | 117 | |
b2e465d6 AL |
118 | if (RelVerStr.end()[-1] == '*') |
119 | { | |
120 | RelVerPrefixMatch = true; | |
121 | RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1); | |
122 | } | |
123 | return; | |
124 | } | |
125 | ||
126 | if (Type == Origin) | |
127 | { | |
99aa69c7 | 128 | if (Data[0] == '"' && Data.length() >= 2 && Data.end()[-1] == '"') |
ba91b151 DK |
129 | OrSite = Data.substr(1, Data.length() - 2); |
130 | else | |
131 | OrSite = Data; | |
b2e465d6 AL |
132 | return; |
133 | } | |
134 | } | |
135 | /*}}}*/ | |
136 | // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/ | |
137 | // --------------------------------------------------------------------- | |
138 | /* */ | |
139 | bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix) | |
b07aeb1a DK |
140 | { |
141 | if (A == NULL) | |
142 | return false; | |
143 | ||
b2e465d6 AL |
144 | const char *Ab = A; |
145 | const char *Ae = Ab + strlen(A); | |
146 | ||
147 | // Strings are not a compatible size. | |
d320fdc3 | 148 | if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) || |
b2e465d6 AL |
149 | (unsigned)(Ae - Ab) < B.length()) |
150 | return false; | |
151 | ||
152 | // Match (leading?) | |
8d9850a1 | 153 | if (stringcasecmp(B,Ab,Ab + B.length()) == 0) |
b2e465d6 AL |
154 | return true; |
155 | ||
156 | return false; | |
157 | } | |
158 | /*}}}*/ | |
159 | // VersionMatch::Find - Locate the best match for the select type /*{{{*/ | |
160 | // --------------------------------------------------------------------- | |
161 | /* */ | |
162 | pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg) | |
163 | { | |
164 | pkgCache::VerIterator Ver = Pkg.VersionList(); | |
f7f0d6c7 | 165 | for (; Ver.end() == false; ++Ver) |
b2e465d6 | 166 | { |
5bfd306e JAK |
167 | if (VersionMatches(Ver)) |
168 | return Ver; | |
b2e465d6 | 169 | } |
5bfd306e | 170 | |
b2e465d6 AL |
171 | // This will be Ended by now. |
172 | return Ver; | |
173 | } | |
b07aeb1a | 174 | /*}}}*/ |
ae4a4f91 | 175 | |
5bfd306e JAK |
176 | // VersionMatch::Find - Locate the best match for the select type /*{{{*/ |
177 | // --------------------------------------------------------------------- | |
178 | /* */ | |
179 | bool pkgVersionMatch::VersionMatches(pkgCache::VerIterator Ver) | |
180 | { | |
181 | if (Type == Version) | |
182 | { | |
183 | if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true) | |
184 | return true; | |
185 | if (ExpressionMatches(VerStr, Ver.VerStr()) == true) | |
186 | return true; | |
187 | return false; | |
188 | } | |
189 | ||
190 | for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF) | |
191 | if (FileMatch(VF.File()) == true) | |
192 | return true; | |
193 | ||
194 | return false; | |
195 | } | |
196 | /*}}}*/ | |
197 | ||
ae4a4f91 JAK |
198 | #ifndef FNM_CASEFOLD |
199 | #define FNM_CASEFOLD 0 | |
200 | #endif | |
201 | ||
b07aeb1a | 202 | bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)/*{{{*/ |
ae4a4f91 | 203 | { |
b07aeb1a DK |
204 | if (pattern == NULL || string == NULL) |
205 | return false; | |
ae4a4f91 | 206 | if (pattern[0] == '/') { |
ae4a4f91 JAK |
207 | size_t length = strlen(pattern); |
208 | if (pattern[length - 1] == '/') { | |
69c2ecbd | 209 | bool res = false; |
ae4a4f91 JAK |
210 | regex_t preg; |
211 | char *regex = strdup(pattern + 1); | |
212 | regex[length - 2] = '\0'; | |
213 | if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) { | |
4213f040 | 214 | _error->Warning("Invalid regular expression: %s", regex); |
ae4a4f91 JAK |
215 | } else if (regexec(&preg, string, 0, NULL, 0) == 0) { |
216 | res = true; | |
217 | } | |
218 | free(regex); | |
cec9f4f4 | 219 | regfree(&preg); |
ae4a4f91 JAK |
220 | return res; |
221 | } | |
222 | } | |
223 | return fnmatch(pattern, string, FNM_CASEFOLD) == 0; | |
224 | } | |
225 | bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string) | |
226 | { | |
227 | return ExpressionMatches(pattern.c_str(), string); | |
228 | } | |
b2e465d6 AL |
229 | /*}}}*/ |
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) | |
235 | { | |
236 | if (Type == Release) | |
237 | { | |
0a5e4a03 AL |
238 | if (MatchAll == true) |
239 | return true; | |
efc487fb | 240 | |
b2e465d6 | 241 | /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl; |
af87ab54 | 242 | cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/ |
efc487fb | 243 | |
b2e465d6 AL |
244 | if (RelVerStr.empty() == true && RelOrigin.empty() == true && |
245 | RelArchive.empty() == true && RelLabel.empty() == true && | |
efc487fb | 246 | RelRelease.empty() == true && RelCodename.empty() == true && |
5dd4c8b8 | 247 | RelComponent.empty() == true && RelArchitecture.empty() == true) |
b2e465d6 | 248 | return false; |
efc487fb | 249 | |
b2e465d6 | 250 | if (RelVerStr.empty() == false) |
b07aeb1a DK |
251 | if (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false && |
252 | ExpressionMatches(RelVerStr, File.Version()) == false) | |
b2e465d6 AL |
253 | return false; |
254 | if (RelOrigin.empty() == false) | |
b07aeb1a | 255 | if (!ExpressionMatches(RelOrigin,File.Origin())) |
b2e465d6 AL |
256 | return false; |
257 | if (RelArchive.empty() == false) | |
b07aeb1a | 258 | if (!ExpressionMatches(RelArchive,File.Archive())) |
efc487fb DK |
259 | return false; |
260 | if (RelCodename.empty() == false) | |
b07aeb1a | 261 | if (!ExpressionMatches(RelCodename,File.Codename())) |
efc487fb DK |
262 | return false; |
263 | if (RelRelease.empty() == false) | |
b07aeb1a DK |
264 | if (!ExpressionMatches(RelRelease,File.Archive()) && |
265 | !ExpressionMatches(RelRelease,File.Codename())) | |
efc487fb | 266 | return false; |
b2e465d6 | 267 | if (RelLabel.empty() == false) |
b07aeb1a | 268 | if (!ExpressionMatches(RelLabel,File.Label())) |
b2e465d6 AL |
269 | return false; |
270 | if (RelComponent.empty() == false) | |
b07aeb1a | 271 | if (!ExpressionMatches(RelComponent,File.Component())) |
b2e465d6 | 272 | return false; |
5dd4c8b8 | 273 | if (RelArchitecture.empty() == false) |
b07aeb1a | 274 | if (!ExpressionMatches(RelArchitecture,File.Architecture())) |
5dd4c8b8 | 275 | return false; |
b2e465d6 AL |
276 | return true; |
277 | } | |
efc487fb | 278 | |
b2e465d6 AL |
279 | if (Type == Origin) |
280 | { | |
acfe7306 | 281 | if (OrSite.empty() == false) { |
b07aeb1a | 282 | if (File.Site() == NULL) |
acfe7306 | 283 | return false; |
b07aeb1a | 284 | } |
ddb28652 DK |
285 | else if (File->Release == 0)// only 'bad' files like dpkg.status file has no release file |
286 | return false; | |
ae4a4f91 | 287 | return (ExpressionMatches(OrSite, File.Site())); /* both strings match */ |
b2e465d6 | 288 | } |
efc487fb | 289 | |
b2e465d6 AL |
290 | return false; |
291 | } | |
292 | /*}}}*/ |