]> git.saurik.com Git - apt.git/blob - apt-pkg/deb/debversion.cc
* apt-pkg/deb/debrecords.cc:
[apt.git] / apt-pkg / deb / debversion.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: debversion.cc,v 1.8 2003/09/10 23:39:49 mdz Exp $
4 /* ######################################################################
5
6 Debian Version - Versioning system for Debian
7
8 This implements the standard Debian versioning system.
9
10 ##################################################################### */
11 /*}}}*/
12 // Include Files /*{{{*/
13 #define APT_COMPATIBILITY 986
14
15 #include <apt-pkg/debversion.h>
16 #include <apt-pkg/pkgcache.h>
17
18 #include <stdlib.h>
19 #include <ctype.h>
20 /*}}}*/
21
22 debVersioningSystem debVS;
23
24 // debVS::debVersioningSystem - Constructor /*{{{*/
25 // ---------------------------------------------------------------------
26 /* */
27 debVersioningSystem::debVersioningSystem()
28 {
29 Label = "Standard .deb";
30 }
31 /*}}}*/
32
33 // debVS::CmpFragment - Compare versions /*{{{*/
34 // ---------------------------------------------------------------------
35 /* This compares a fragment of the version. This is a slightly adapted
36 version of what dpkg uses. */
37 #define order(x) ((x) == '~' ? -1 \
38 : isdigit((x)) ? 0 \
39 : !(x) ? 0 \
40 : isalpha((x)) ? (x) \
41 : (x) + 256)
42 int debVersioningSystem::CmpFragment(const char *A,const char *AEnd,
43 const char *B,const char *BEnd)
44 {
45 if (A >= AEnd && B >= BEnd)
46 return 0;
47 if (A >= AEnd)
48 {
49 if (*B == '~') return 1;
50 return -1;
51 }
52 if (B >= BEnd)
53 {
54 if (*A == '~') return -1;
55 return 1;
56 }
57
58 /* Iterate over the whole string
59 What this does is to split the whole string into groups of
60 numeric and non numeric portions. For instance:
61 a67bhgs89
62 Has 4 portions 'a', '67', 'bhgs', '89'. A more normal:
63 2.7.2-linux-1
64 Has '2', '.', '7', '.' ,'-linux-','1' */
65 const char *lhs = A;
66 const char *rhs = B;
67 while (lhs != AEnd && rhs != BEnd)
68 {
69 int first_diff = 0;
70
71 while (lhs != AEnd && rhs != BEnd &&
72 (!isdigit(*lhs) || !isdigit(*rhs)))
73 {
74 int vc = order(*lhs);
75 int rc = order(*rhs);
76 if (vc != rc)
77 return vc - rc;
78 lhs++; rhs++;
79 }
80
81 while (*lhs == '0')
82 lhs++;
83 while (*rhs == '0')
84 rhs++;
85 while (isdigit(*lhs) && isdigit(*rhs))
86 {
87 if (!first_diff)
88 first_diff = *lhs - *rhs;
89 lhs++;
90 rhs++;
91 }
92
93 if (isdigit(*lhs))
94 return 1;
95 if (isdigit(*rhs))
96 return -1;
97 if (first_diff)
98 return first_diff;
99 }
100
101 // The strings must be equal
102 if (lhs == AEnd && rhs == BEnd)
103 return 0;
104
105 // lhs is shorter
106 if (lhs == AEnd)
107 {
108 if (*rhs == '~') return 1;
109 return -1;
110 }
111
112 // rhs is shorter
113 if (rhs == BEnd)
114 {
115 if (*lhs == '~') return -1;
116 return 1;
117 }
118
119 // Shouldnt happen
120 return 1;
121 }
122 /*}}}*/
123 // debVS::CmpVersion - Comparison for versions /*{{{*/
124 // ---------------------------------------------------------------------
125 /* This fragments the version into E:V-R triples and compares each
126 portion separately. */
127 int debVersioningSystem::DoCmpVersion(const char *A,const char *AEnd,
128 const char *B,const char *BEnd)
129 {
130 // Strip off the epoch and compare it
131 const char *lhs = A;
132 const char *rhs = B;
133 for (;lhs != AEnd && *lhs != ':'; lhs++);
134 for (;rhs != BEnd && *rhs != ':'; rhs++);
135 if (lhs == AEnd)
136 lhs = A;
137 if (rhs == BEnd)
138 rhs = B;
139
140 // Special case: a zero epoch is the same as no epoch,
141 // so remove it.
142 if (lhs != A)
143 {
144 for (; *A == '0'; ++A);
145 if (A == lhs)
146 {
147 ++A;
148 ++lhs;
149 }
150 }
151 if (rhs != B)
152 {
153 for (; *B == '0'; ++B);
154 if (B == rhs)
155 {
156 ++B;
157 ++rhs;
158 }
159 }
160
161 // Compare the epoch
162 int Res = CmpFragment(A,lhs,B,rhs);
163 if (Res != 0)
164 return Res;
165
166 // Skip the :
167 if (lhs != A)
168 lhs++;
169 if (rhs != B)
170 rhs++;
171
172 // Find the last -
173 const char *dlhs = AEnd-1;
174 const char *drhs = BEnd-1;
175 for (;dlhs > lhs && *dlhs != '-'; dlhs--);
176 for (;drhs > rhs && *drhs != '-'; drhs--);
177
178 if (dlhs == lhs)
179 dlhs = AEnd;
180 if (drhs == rhs)
181 drhs = BEnd;
182
183 // Compare the main version
184 Res = CmpFragment(lhs,dlhs,rhs,drhs);
185 if (Res != 0)
186 return Res;
187
188 // Skip the -
189 if (dlhs != lhs)
190 dlhs++;
191 if (drhs != rhs)
192 drhs++;
193
194 return CmpFragment(dlhs,AEnd,drhs,BEnd);
195 }
196 /*}}}*/
197 // debVS::CheckDep - Check a single dependency /*{{{*/
198 // ---------------------------------------------------------------------
199 /* This simply preforms the version comparison and switch based on
200 operator. If DepVer is 0 then we are comparing against a provides
201 with no version. */
202 bool debVersioningSystem::CheckDep(const char *PkgVer,
203 int Op,const char *DepVer)
204 {
205 if (DepVer == 0 || DepVer[0] == 0)
206 return true;
207 if (PkgVer == 0 || PkgVer[0] == 0)
208 return false;
209
210 // Perform the actual comparision.
211 int Res = CmpVersion(PkgVer,DepVer);
212 switch (Op & 0x0F)
213 {
214 case pkgCache::Dep::LessEq:
215 if (Res <= 0)
216 return true;
217 break;
218
219 case pkgCache::Dep::GreaterEq:
220 if (Res >= 0)
221 return true;
222 break;
223
224 case pkgCache::Dep::Less:
225 if (Res < 0)
226 return true;
227 break;
228
229 case pkgCache::Dep::Greater:
230 if (Res > 0)
231 return true;
232 break;
233
234 case pkgCache::Dep::Equals:
235 if (Res == 0)
236 return true;
237 break;
238
239 case pkgCache::Dep::NotEquals:
240 if (Res != 0)
241 return true;
242 break;
243 }
244
245 return false;
246 }
247 /*}}}*/
248 // debVS::UpstreamVersion - Return the upstream version string /*{{{*/
249 // ---------------------------------------------------------------------
250 /* This strips all the debian specific information from the version number */
251 string debVersioningSystem::UpstreamVersion(const char *Ver)
252 {
253 // Strip off the bit before the first colon
254 const char *I = Ver;
255 for (; *I != 0 && *I != ':'; I++);
256 if (*I == ':')
257 Ver = I + 1;
258
259 // Chop off the trailing -
260 I = Ver;
261 unsigned Last = strlen(Ver);
262 for (; *I != 0; I++)
263 if (*I == '-')
264 Last = I - Ver;
265
266 return string(Ver,Last);
267 }
268 /*}}}*/