]> git.saurik.com Git - apt.git/blob - apt-pkg/versionmatch.cc
* methods/mirror.{cc,h}:
[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 /*}}}*/
22
23 // VersionMatch::pkgVersionMatch - Constructor /*{{{*/
24 // ---------------------------------------------------------------------
25 /* Break up the data string according to the selected type */
26 pkgVersionMatch::pkgVersionMatch(string Data,MatchType Type) : Type(Type)
27 {
28 MatchAll = false;
29 VerPrefixMatch = false;
30 RelVerPrefixMatch = false;
31
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;
41 VerStr = string(Data,0,Data.length()-1);
42 }
43 else
44 VerStr = Data;
45 return;
46 }
47
48 if (Type == Release)
49 {
50 // All empty = match all
51 if (Data == "*")
52 {
53 MatchAll = true;
54 return;
55 }
56
57 // Are we a simple specification?
58 string::const_iterator I = Data.begin();
59 for (; I != Data.end() && *I != '='; I++);
60 if (I == Data.end())
61 {
62 // Temporary
63 if (isdigit(Data[0]))
64 RelVerStr = Data;
65 else
66 RelArchive = Data;
67
68 if (RelVerStr.length() > 0 && RelVerStr.end()[-1] == '*')
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;
90
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;
97 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"l=") == 0)
98 RelLabel = Fragments[J]+2;
99 else if (stringcasecmp(Fragments[J],Fragments[J]+2,"c=") == 0)
100 RelComponent = Fragments[J]+2;
101 }
102
103 if (RelVerStr.end()[-1] == '*')
104 {
105 RelVerPrefixMatch = true;
106 RelVerStr = string(RelVerStr.begin(),RelVerStr.end()-1);
107 }
108 return;
109 }
110
111 if (Type == Origin)
112 {
113 OrSite = Data;
114 return;
115 }
116 }
117 /*}}}*/
118 // VersionMatch::MatchVer - Match a version string with prefixing /*{{{*/
119 // ---------------------------------------------------------------------
120 /* */
121 bool pkgVersionMatch::MatchVer(const char *A,string B,bool Prefix)
122 {
123 const char *Ab = A;
124 const char *Ae = Ab + strlen(A);
125
126 // Strings are not a compatible size.
127 if ((unsigned)(Ae - Ab) != B.length() && Prefix == false ||
128 (unsigned)(Ae - Ab) < B.length())
129 return false;
130
131 // Match (leading?)
132 if (stringcasecmp(B,Ab,Ab + B.length()) == 0)
133 return true;
134
135 return false;
136 }
137 /*}}}*/
138 // VersionMatch::Find - Locate the best match for the select type /*{{{*/
139 // ---------------------------------------------------------------------
140 /* */
141 pkgCache::VerIterator pkgVersionMatch::Find(pkgCache::PkgIterator Pkg)
142 {
143 pkgCache::VerIterator Ver = Pkg.VersionList();
144 for (; Ver.end() == false; Ver++)
145 {
146 if (Type == Version)
147 {
148 if (MatchVer(Ver.VerStr(),VerStr,VerPrefixMatch) == true)
149 return Ver;
150 continue;
151 }
152
153 for (pkgCache::VerFileIterator VF = Ver.FileList(); VF.end() == false; VF++)
154 if (FileMatch(VF.File()) == true)
155 return Ver;
156 }
157
158 // This will be Ended by now.
159 return Ver;
160 }
161 /*}}}*/
162 // VersionMatch::FileMatch - Match against an index file /*{{{*/
163 // ---------------------------------------------------------------------
164 /* This matcher checks against the release file and the origin location
165 to see if the constraints are met. */
166 bool pkgVersionMatch::FileMatch(pkgCache::PkgFileIterator File)
167 {
168 if (Type == Release)
169 {
170 if (MatchAll == true)
171 return true;
172
173 /* cout << RelVerStr << ',' << RelOrigin << ',' << RelArchive << ',' << RelLabel << endl;
174 cout << File.Version() << ',' << File.Origin() << ',' << File.Archive() << ',' << File.Label() << endl;*/
175
176 if (RelVerStr.empty() == true && RelOrigin.empty() == true &&
177 RelArchive.empty() == true && RelLabel.empty() == true &&
178 RelComponent.empty() == true)
179 return false;
180
181 if (RelVerStr.empty() == false)
182 if (File->Version == 0 ||
183 MatchVer(File.Version(),RelVerStr,RelVerPrefixMatch) == false)
184 return false;
185 if (RelOrigin.empty() == false)
186 if (File->Origin == 0 ||
187 stringcasecmp(RelOrigin,File.Origin()) != 0)
188 return false;
189 if (RelArchive.empty() == false)
190 {
191 if (File->Archive == 0 ||
192 stringcasecmp(RelArchive,File.Archive()) != 0)
193 return false;
194 }
195 if (RelLabel.empty() == false)
196 if (File->Label == 0 ||
197 stringcasecmp(RelLabel,File.Label()) != 0)
198 return false;
199 if (RelComponent.empty() == false)
200 if (File->Component == 0 ||
201 stringcasecmp(RelComponent,File.Component()) != 0)
202 return false;
203 return true;
204 }
205
206 if (Type == Origin)
207 {
208 if (OrSite.empty() == false) {
209 if (File->Site == 0 || OrSite != File.Site())
210 return false;
211 } else // so we are talking about file:// or status file
212 if (strcmp(File.Site(),"") == 0 && File->Archive != 0) // skip the status file
213 return false;
214 return (OrSite == File.Site()); /* both strings match */
215 }
216
217 return false;
218 }
219 /*}}}*/