]> git.saurik.com Git - apt.git/blame - apt-pkg/versionmatch.cc
do not modify DepIterator as we might check again
[apt.git] / apt-pkg / versionmatch.cc
CommitLineData
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>
19
20#include <stdio.h>
8d9850a1 21#include <ctype.h>
ae4a4f91
JAK
22#include <fnmatch.h>
23#include <sys/types.h>
24#include <regex.h>
b2e465d6
AL
25 /*}}}*/
26
8f3ba4e8
DK
27using std::string;
28
b2e465d6
AL
29// VersionMatch::pkgVersionMatch - Constructor /*{{{*/
30// ---------------------------------------------------------------------
31/* Break up the data string according to the selected type */
32pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
33{
4e0680f9
AL
34 MatchAll = false;
35 VerPrefixMatch = false;
36 RelVerPrefixMatch = false;
37
b2e465d6
AL
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;
8d9850a1 47 VerStr = string(Data,0,Data.length()-1);
b2e465d6
AL
48 }
49 else
50 VerStr = Data;
51 return;
52 }
53
54 if (Type == Release)
55 {
56 // All empty = match all
57 if (Data == "*")
0a5e4a03
AL
58 {
59 MatchAll = true;
b2e465d6 60 return;
0a5e4a03
AL
61 }
62
b2e465d6 63 // Are we a simple specification?
8d9850a1 64 string::const_iterator I = Data.begin();
f7f0d6c7 65 for (; I != Data.end() && *I != '='; ++I);
b2e465d6
AL
66 if (I == Data.end())
67 {
68 // Temporary
69 if (isdigit(Data[0]))
70 RelVerStr = Data;
71 else
efc487fb
DK
72 RelRelease = Data;
73
b8b7c37d 74 if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
b2e465d6
AL
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;
efc487fb 96
b2e465d6
AL
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;
efc487fb
DK
103 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0)
104 RelCodename = Fragments[J]+2;
b2e465d6
AL
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;
5dd4c8b8
DK
109 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"b=") == 0)
110 RelArchitecture = Fragments[J]+2;
b2e465d6 111 }
efc487fb 112
b2e465d6
AL
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 {
99aa69c7 123 if (Data[0] == '"' && Data.length() >= 2 && Data.end()[-1] == '"')
ba91b151
DK
124 OrSite = Data.substr(1, Data.length() - 2);
125 else
126 OrSite = Data;
b2e465d6
AL
127 return;
128 }
129}
130 /*}}}*/
131// VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
132// ---------------------------------------------------------------------
133/* */
134bool 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.
d320fdc3 140 if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
b2e465d6
AL
141 (unsigned)(Ae - Ab) < B.length())
142 return false;
143
144 // Match (leading?)
8d9850a1 145 if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
b2e465d6
AL
146 return true;
147
148 return false;
149}
150 /*}}}*/
151// VersionMatch::Find - Locate the best match for the select type /*{{{*/
152// ---------------------------------------------------------------------
153/* */
154pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
155{
156 pkgCache::VerIterator Ver = Pkg.VersionList();
f7f0d6c7 157 for (; Ver.end() == false; ++Ver)
b2e465d6
AL
158 {
159 if (Type == Version)
160 {
161 if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
162 return Ver;
05002864
JAK
163 if (ExpressionMatches(VerStr, Ver.VerStr()) == true)
164 return Ver;
b2e465d6
AL
165 continue;
166 }
167
f7f0d6c7 168 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; ++VF)
b2e465d6
AL
169 if (FileMatch(VF.File()) == true)
170 return Ver;
171 }
172
173 // This will be Ended by now.
174 return Ver;
175}
ae4a4f91
JAK
176
177#ifndef FNM_CASEFOLD
178#define FNM_CASEFOLD 0
179#endif
180
181bool pkgVersionMatch::ExpressionMatches(const char *pattern, const char *string)
182{
ae4a4f91 183 if (pattern[0] == '/') {
ae4a4f91
JAK
184 size_t length = strlen(pattern);
185 if (pattern[length - 1] == '/') {
69c2ecbd 186 bool res = false;
ae4a4f91
JAK
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) {
4213f040 191 _error->Warning("Invalid regular expression: %s", regex);
ae4a4f91
JAK
192 } else if (regexec(&preg, string, 0, NULL, 0) == 0) {
193 res = true;
194 }
195 free(regex);
cec9f4f4 196 regfree(&preg);
ae4a4f91
JAK
197 return res;
198 }
199 }
200 return fnmatch(pattern, string, FNM_CASEFOLD) == 0;
201}
202bool pkgVersionMatch::ExpressionMatches(const std::string& pattern, const char *string)
203{
204 return ExpressionMatches(pattern.c_str(), string);
205}
b2e465d6
AL
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. */
211bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
212{
213 if (Type == Release)
214 {
0a5e4a03
AL
215 if (MatchAll == true)
216 return true;
efc487fb 217
b2e465d6 218/* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
af87ab54 219 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
efc487fb 220
b2e465d6
AL
221 if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
222 RelArchive.empty() == true && RelLabel.empty() == true &&
efc487fb 223 RelRelease.empty() == true && RelCodename.empty() == true &&
5dd4c8b8 224 RelComponent.empty() == true && RelArchitecture.empty() == true)
b2e465d6 225 return false;
efc487fb 226
b2e465d6
AL
227 if (RelVerStr.empty() == false)
228 if (File->Version == 0 ||
ae4a4f91
JAK
229 (MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false &&
230 ExpressionMatches(RelVerStr, File.Version()) == false))
b2e465d6
AL
231 return false;
232 if (RelOrigin.empty() == false)
ae4a4f91 233 if (File->Origin == 0 || !ExpressionMatches(RelOrigin,File.Origin()))
b2e465d6
AL
234 return false;
235 if (RelArchive.empty() == false)
efc487fb 236 if (File->Archive == 0 ||
ae4a4f91 237 !ExpressionMatches(RelArchive,File.Archive()))
efc487fb
DK
238 return false;
239 if (RelCodename.empty() == false)
240 if (File->Codename == 0 ||
ae4a4f91 241 !ExpressionMatches(RelCodename,File.Codename()))
efc487fb
DK
242 return false;
243 if (RelRelease.empty() == false)
244 if ((File->Archive == 0 ||
ae4a4f91 245 !ExpressionMatches(RelRelease,File.Archive())) &&
efc487fb 246 (File->Codename == 0 ||
ae4a4f91 247 !ExpressionMatches(RelRelease,File.Codename())))
efc487fb 248 return false;
b2e465d6
AL
249 if (RelLabel.empty() == false)
250 if (File->Label == 0 ||
ae4a4f91 251 !ExpressionMatches(RelLabel,File.Label()))
b2e465d6
AL
252 return false;
253 if (RelComponent.empty() == false)
254 if (File->Component == 0 ||
ae4a4f91 255 !ExpressionMatches(RelComponent,File.Component()))
b2e465d6 256 return false;
5dd4c8b8
DK
257 if (RelArchitecture.empty() == false)
258 if (File->Architecture == 0 ||
ae4a4f91 259 !ExpressionMatches(RelArchitecture,File.Architecture()))
5dd4c8b8 260 return false;
b2e465d6
AL
261 return true;
262 }
efc487fb 263
b2e465d6
AL
264 if (Type == Origin)
265 {
acfe7306 266 if (OrSite.empty() == false) {
ba91b151 267 if (File->Site == 0)
acfe7306
AL
268 return false;
269 } else // so we are talking about file:// or status file
ba91b151 270 if (strcmp(File.Site(),"") == 0 && File->Archive != 0 && strcmp(File.Archive(),"now") == 0) // skip the status file
acfe7306 271 return false;
ae4a4f91 272 return (ExpressionMatches(OrSite, File.Site())); /* both strings match */
b2e465d6 273 }
efc487fb 274
b2e465d6
AL
275 return false;
276}
277 /*}}}*/