* apt-pkg/versionmatch.cc:
[ntk/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 <apt-pkg/versionmatch.h>
15
16 #include <apt-pkg/strutl.h>
17 #include <apt-pkg/error.h>
18
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <fnmatch.h>
22 #include <sys/types.h>
23 #include <regex.h>
24
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 {
32 MatchAll = false;
33 VerPrefixMatch = false;
34 RelVerPrefixMatch = false;
35
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;
45 VerStr = string(Data,0,Data.length()-1);
46 }
47 else
48 VerStr = Data;
49 return;
50 }
51
52 if (Type == Release)
53 {
54 // All empty = match all
55 if (Data == "*")
56 {
57 MatchAll = true;
58 return;
59 }
60
61 // Are we a simple specification?
62 string::const_iterator I = Data.begin();
63 for (; I != Data.end() && *I != '='; I++);
64 if (I == Data.end())
65 {
66 // Temporary
67 if (isdigit(Data[0]))
68 RelVerStr = Data;
69 else
70 RelRelease = Data;
71
72 if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
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;
94
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;
101 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0)
102 RelCodename = Fragments[J]+2;
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;
107 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"b=") == 0)
108 RelArchitecture = Fragments[J]+2;
109 }
110
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 {
121 OrSite = Data;
122 return;
123 }
124 }
125 /*}}}*/
126 // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
127 // ---------------------------------------------------------------------
128 /* */
129 bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
130 {
131 const char *Ab = A;
132 const char *Ae = Ab + strlen(A);
133
134 // Strings are not a compatible size.
135 if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
136 (unsigned)(Ae - Ab) < B.length())
137 return false;
138
139 // Match (leading?)
140 if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
141 return true;
142
143 return false;
144 }
145 /*}}}*/
146 // VersionMatch::Find - Locate the best match for the select type /*{{{*/
147 // ---------------------------------------------------------------------
148 /* */
149 pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
150 {
151 pkgCache::VerIterator Ver = Pkg.VersionList();
152 for (; Ver.end() == false; Ver++)
153 {
154 if (Type == Version)
155 {
156 if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
157 return Ver;
158 continue;
159 }
160
161 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
162 if (FileMatch(VF.File()) == true)
163 return Ver;
164 }
165
166 // This will be Ended by now.
167 return Ver;
168 }
169
170 #ifndef FNM_CASEFOLD
171 #define FNM_CASEFOLD 0
172 #endif
173
174 bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)
175 {
176 std::cerr << "MATCH " << pattern;
177 if (pattern[0] == '/') {
178 bool res = false;
179 size_t length = strlen(pattern);
180 if (pattern[length - 1] == '/') {
181 regex_t preg;
182 char *regex = strdup(pattern + 1);
183 regex[length - 2] = '\0';
184 if (regcomp(&preg, regex, REG_EXTENDED | REG_ICASE) != 0) {
185 std::cerr << "E: Invalid regular expression: " << regex << "\n";
186 } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
187 res = true;
188 }
189 free(regex);
190 return res;
191 }
192 }
193 return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
194 }
195 bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
196 {
197 return ExpressionMatches(pattern.c_str(), string);
198 }
199 /*}}}*/
200 // VersionMatch::FileMatch - Match against an index file /*{{{*/
201 // ---------------------------------------------------------------------
202 /* This matcher checks against the release file and the origin location
203 to see if the constraints are met. */
204 bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
205 {
206 if (Type == Release)
207 {
208 if (MatchAll == true)
209 return true;
210
211 /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
212 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
213
214 if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
215 RelArchive.empty() == true && RelLabel.empty() == true &&
216 RelRelease.empty() == true && RelCodename.empty() == true &&
217 RelComponent.empty() == true && RelArchitecture.empty() == true)
218 return false;
219
220 if (RelVerStr.empty() == false)
221 if (File->Version == 0 ||
222 (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
223 ExpressionMatches(RelVerStr, File.Version()) == false))
224 return false;
225 if (RelOrigin.empty() == false)
226 if (File->Origin == 0 || !ExpressionMatches(RelOrigin,File.Origin()))
227 return false;
228 if (RelArchive.empty() == false)
229 if (File->Archive == 0 ||
230 !ExpressionMatches(RelArchive,File.Archive()))
231 return false;
232 if (RelCodename.empty() == false)
233 if (File->Codename == 0 ||
234 !ExpressionMatches(RelCodename,File.Codename()))
235 return false;
236 if (RelRelease.empty() == false)
237 if ((File->Archive == 0 ||
238 !ExpressionMatches(RelRelease,File.Archive())) &&
239 (File->Codename == 0 ||
240 !ExpressionMatches(RelRelease,File.Codename())))
241 return false;
242 if (RelLabel.empty() == false)
243 if (File->Label == 0 ||
244 !ExpressionMatches(RelLabel,File.Label()))
245 return false;
246 if (RelComponent.empty() == false)
247 if (File->Component == 0 ||
248 !ExpressionMatches(RelComponent,File.Component()))
249 return false;
250 if (RelArchitecture.empty() == false)
251 if (File->Architecture == 0 ||
252 !ExpressionMatches(RelArchitecture,File.Architecture()))
253 return false;
254 return true;
255 }
256
257 if (Type == Origin)
258 {
259 if (OrSite.empty() == false) {
260 if (File->Site == 0 || !ExpressionMatches(OrSite, File.Site()))
261 return false;
262 } else // so we are talking about file:// or status file
263 if (strcmp(File.Site(),"") == 0 && File->Archive != 0) // skip the status file
264 return false;
265 return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
266 }
267
268 return false;
269 }
270 /*}}}*/