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