merged from the mvo branch
[ntk/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 /*{{{*/
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>
b2e465d6
AL
21 /*}}}*/
22
23// VersionMatch::pkgVersionMatch - Constructor /*{{{*/
24// ---------------------------------------------------------------------
25/* Break up the data string according to the selected type */
26pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
27{
4e0680f9
AL
28 MatchAll = false;
29 VerPrefixMatch = false;
30 RelVerPrefixMatch = false;
31
b2e465d6
AL
32 if (Type == None || Data.length() < 1)
33 return;
34
35 // Cut up the version representation
36 if (Type == Version)
37 {
38 if (Data.end()[-1] == '*')
39 {
40 VerPrefixMatch = true;
8d9850a1 41 VerStr = string(Data,0,Data.length()-1);
b2e465d6
AL
42 }
43 else
44 VerStr = Data;
45 return;
46 }
47
48 if (Type == Release)
49 {
50 // All empty = match all
51 if (Data == "*")
0a5e4a03
AL
52 {
53 MatchAll = true;
b2e465d6 54 return;
0a5e4a03
AL
55 }
56
b2e465d6 57 // Are we a simple specification?
8d9850a1
AL
58 string::const_iterator I = Data.begin();
59 for (; I != Data.end() && *I != '='; I++);
b2e465d6
AL
60 if (I == Data.end())
61 {
62 // Temporary
63 if (isdigit(Data[0]))
64 RelVerStr = Data;
65 else
efc487fb
DK
66 RelRelease = Data;
67
b8b7c37d 68 if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
b2e465d6
AL
69 {
70 RelVerPrefixMatch = true;
71 RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
72 }
73 return;
74 }
75
76 char Spec[300];
77 char *Fragments[20];
78 snprintf(Spec,sizeof(Spec),"%s",Data.c_str());
79 if (TokSplitString(',',Spec,Fragments,
80 sizeof(Fragments)/sizeof(Fragments[0])) == false)
81 {
82 Type = None;
83 return;
84 }
85
86 for (unsigned J = 0; Fragments[J] != 0; J++)
87 {
88 if (strlen(Fragments[J]) < 3)
89 continue;
efc487fb 90
b2e465d6
AL
91 if (stringcasecmp(Fragments[J],Fragments[J]+2,"v=") == 0)
92 RelVerStr = Fragments[J]+2;
93 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"o=") == 0)
94 RelOrigin = Fragments[J]+2;
95 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"a=") == 0)
96 RelArchive = Fragments[J]+2;
efc487fb
DK
97 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"n=") == 0)
98 RelCodename = Fragments[J]+2;
b2e465d6
AL
99 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
100 RelLabel = Fragments[J]+2;
101 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
102 RelComponent = Fragments[J]+2;
103 }
efc487fb 104
b2e465d6
AL
105 if (RelVerStr.end()[-1] == '*')
106 {
107 RelVerPrefixMatch = true;
108 RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
109 }
110 return;
111 }
112
113 if (Type == Origin)
114 {
115 OrSite = Data;
116 return;
117 }
118}
119 /*}}}*/
120// VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
121// ---------------------------------------------------------------------
122/* */
123bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
124{
125 const char *Ab = A;
126 const char *Ae = Ab + strlen(A);
127
128 // Strings are not a compatible size.
d320fdc3 129 if (((unsigned)(Ae - Ab) != B.length() && Prefix == false) ||
b2e465d6
AL
130 (unsigned)(Ae - Ab) < B.length())
131 return false;
132
133 // Match (leading?)
8d9850a1 134 if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
b2e465d6
AL
135 return true;
136
137 return false;
138}
139 /*}}}*/
140// VersionMatch::Find - Locate the best match for the select type /*{{{*/
141// ---------------------------------------------------------------------
142/* */
143pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
144{
145 pkgCache::VerIterator Ver = Pkg.VersionList();
146 for (; Ver.end() == false; Ver++)
147 {
148 if (Type == Version)
149 {
150 if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
151 return Ver;
152 continue;
153 }
154
155 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
156 if (FileMatch(VF.File()) == true)
157 return Ver;
158 }
159
160 // This will be Ended by now.
161 return Ver;
162}
163 /*}}}*/
164// VersionMatch::FileMatch - Match against an index file /*{{{*/
165// ---------------------------------------------------------------------
166/* This matcher checks against the release file and the origin location
167 to see if the constraints are met. */
168bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
169{
170 if (Type == Release)
171 {
0a5e4a03
AL
172 if (MatchAll == true)
173 return true;
efc487fb 174
b2e465d6 175/* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
af87ab54 176 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
efc487fb 177
b2e465d6
AL
178 if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
179 RelArchive.empty() == true && RelLabel.empty() == true &&
efc487fb 180 RelRelease.empty() == true && RelCodename.empty() == true &&
b2e465d6
AL
181 RelComponent.empty() == true)
182 return false;
efc487fb 183
b2e465d6
AL
184 if (RelVerStr.empty() == false)
185 if (File->Version == 0 ||
186 MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false)
187 return false;
188 if (RelOrigin.empty() == false)
189 if (File->Origin == 0 ||
190 stringcasecmp(RelOrigin,File.Origin()) != 0)
191 return false;
192 if (RelArchive.empty() == false)
efc487fb 193 if (File->Archive == 0 ||
b2e465d6 194 stringcasecmp(RelArchive,File.Archive()) != 0)
efc487fb
DK
195 return false;
196 if (RelCodename.empty() == false)
197 if (File->Codename == 0 ||
198 stringcasecmp(RelCodename,File.Codename()) != 0)
199 return false;
200 if (RelRelease.empty() == false)
201 if ((File->Archive == 0 ||
202 stringcasecmp(RelRelease,File.Archive()) != 0) &&
203 (File->Codename == 0 ||
204 stringcasecmp(RelRelease,File.Codename()) != 0))
205 return false;
b2e465d6
AL
206 if (RelLabel.empty() == false)
207 if (File->Label == 0 ||
208 stringcasecmp(RelLabel,File.Label()) != 0)
209 return false;
210 if (RelComponent.empty() == false)
211 if (File->Component == 0 ||
af87ab54 212 stringcasecmp(RelComponent,File.Component()) != 0)
b2e465d6
AL
213 return false;
214 return true;
215 }
efc487fb 216
b2e465d6
AL
217 if (Type == Origin)
218 {
acfe7306
AL
219 if (OrSite.empty() == false) {
220 if (File->Site == 0 || OrSite != File.Site())
221 return false;
222 } else // so we are talking about file:// or status file
223 if (strcmp(File.Site(),"") == 0 && File->Archive != 0) // skip the status file
224 return false;
5955efd4 225 return (OrSite == File.Site()); /* both strings match */
b2e465d6 226 }
efc487fb 227
b2e465d6
AL
228 return false;
229}
230 /*}}}*/