]> git.saurik.com Git - apt.git/blob - apt-pkg/versionmatch.cc
call URIStart in cdrom and file method
[apt.git] / apt-pkg / versionmatch.cc
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 #include <apt-pkg/pkgcache.h>
20 #include <apt-pkg/cacheiterators.h>
21
22 #include <stddef.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <string>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <fnmatch.h>
29 #include <regex.h>
30 /*}}}*/
31
32 using std::string;
33
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 {
39 MatchAll = false;
40 VerPrefixMatch = false;
41 RelVerPrefixMatch = false;
42
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;
52 VerStr = string(Data,0,Data.length()-1);
53 }
54 else
55 VerStr = Data;
56 return;
57 }
58
59 if (Type == Release)
60 {
61 // All empty = match all
62 if (Data == "*")
63 {
64 MatchAll = true;
65 return;
66 }
67
68 // Are we a simple specification?
69 string::const_iterator I = Data.begin();
70 for (; I != Data.end() && *I != '='; ++I);
71 if (I == Data.end())
72 {
73 // Temporary
74 if (isdigit(Data[0]))
75 RelVerStr = Data;
76 else
77 RelRelease = Data;
78
79 if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
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;
101
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;
108 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0)
109 RelCodename = Fragments[J]+2;
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;
114 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"b=") == 0)
115 RelArchitecture = Fragments[J]+2;
116 }
117
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 {
128 if (Data[0] == '"' && Data.length() >= 2 && Data.end()[-1] == '"')
129 OrSite = Data.substr(1, Data.length() - 2);
130 else
131 OrSite = Data;
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)
140 {
141 if (A == NULL)
142 return false;
143
144 const char *Ab = A;
145 const char *Ae = Ab + strlen(A);
146
147 // Strings are not a compatible size.
148 if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
149 (unsigned)(Ae - Ab) < B.length())
150 return false;
151
152 // Match (leading?)
153 if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
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();
165 for (; Ver.end() == false; ++Ver)
166 {
167 if (Type == Version)
168 {
169 if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
170 return Ver;
171 if (ExpressionMatches(VerStr, Ver.VerStr()) == true)
172 return Ver;
173 continue;
174 }
175
176 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF)
177 if (FileMatch(VF.File()) == true)
178 return Ver;
179 }
180
181 // This will be Ended by now.
182 return Ver;
183 }
184 /*}}}*/
185
186 #ifndef FNM_CASEFOLD
187 #define FNM_CASEFOLD 0
188 #endif
189
190 bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)/*{{{*/
191 {
192 if (pattern == NULL || string == NULL)
193 return false;
194 if (pattern[0] == '/') {
195 size_t length = strlen(pattern);
196 if (pattern[length - 1] == '/') {
197 bool res = false;
198 regex_t preg;
199 char *regex = strdup(pattern + 1);
200 regex[length - 2] = '\0';
201 if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) {
202 _error->Warning("Invalid regular expression: %s", regex);
203 } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
204 res = true;
205 }
206 free(regex);
207 regfree(&preg);
208 return res;
209 }
210 }
211 return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
212 }
213 bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
214 {
215 return ExpressionMatches(pattern.c_str(), string);
216 }
217 /*}}}*/
218 // VersionMatch::FileMatch - Match against an index file /*{{{*/
219 // ---------------------------------------------------------------------
220 /* This matcher checks against the release file and the origin location
221 to see if the constraints are met. */
222 bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
223 {
224 if (Type == Release)
225 {
226 if (MatchAll == true)
227 return true;
228
229 /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
230 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
231
232 if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
233 RelArchive.empty() == true && RelLabel.empty() == true &&
234 RelRelease.empty() == true && RelCodename.empty() == true &&
235 RelComponent.empty() == true && RelArchitecture.empty() == true)
236 return false;
237
238 if (RelVerStr.empty() == false)
239 if (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
240 ExpressionMatches(RelVerStr, File.Version()) == false)
241 return false;
242 if (RelOrigin.empty() == false)
243 if (!ExpressionMatches(RelOrigin,File.Origin()))
244 return false;
245 if (RelArchive.empty() == false)
246 if (!ExpressionMatches(RelArchive,File.Archive()))
247 return false;
248 if (RelCodename.empty() == false)
249 if (!ExpressionMatches(RelCodename,File.Codename()))
250 return false;
251 if (RelRelease.empty() == false)
252 if (!ExpressionMatches(RelRelease,File.Archive()) &&
253 !ExpressionMatches(RelRelease,File.Codename()))
254 return false;
255 if (RelLabel.empty() == false)
256 if (!ExpressionMatches(RelLabel,File.Label()))
257 return false;
258 if (RelComponent.empty() == false)
259 if (!ExpressionMatches(RelComponent,File.Component()))
260 return false;
261 if (RelArchitecture.empty() == false)
262 if (!ExpressionMatches(RelArchitecture,File.Architecture()))
263 return false;
264 return true;
265 }
266
267 if (Type == Origin)
268 {
269 if (OrSite.empty() == false) {
270 if (File.Site() == NULL)
271 return false;
272 } else // so we are talking about file:// or status file
273 {
274 pkgCache::RlsFileIterator const RlsFile = File.ReleaseFile();
275 if (strcmp(File.Site(),"") == 0 && RlsFile->Archive != 0 && strcmp(RlsFile.Archive(),"now") == 0) // skip the status file
276 return false;
277 }
278 return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
279 }
280
281 return false;
282 }
283 /*}}}*/