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