forked from freebsd/pkg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpkg_version.c
356 lines (322 loc) · 9.99 KB
/
pkg_version.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*-
* Copyright (c) 2011 Philippe Pepiot <[email protected]>
* Copyright (c) 2014 Vsevolod Stakhov <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer
* in this position and unchanged.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "pkg.h"
#include "private/event.h"
#include "private/pkg.h"
/*
* split_version(pkgname, endname, epoch, revision) returns a pointer to
* the version portion of a package name and the two special components.
*
* Syntax is: ${PORTNAME}-${PORTVERSION}[_${PORTREVISION}][,${PORTEPOCH}]
*
* Written by Oliver Eikemeier
* Based on work of Jeremy D. Lea.
*/
static const char *
split_version(const char *pkgname, const char **endname,
unsigned long *epoch, unsigned long *revision)
{
char *ch;
const char *versionstr;
const char *endversionstr;
if (pkgname == NULL) {
pkg_emit_error("%s: Passed NULL pkgname.", __func__);
return (NULL);
}
/* Look for the last '-' the the pkgname */
ch = strrchr(pkgname, '-');
/* Cheat if we are just passed a version, not a valid package name */
versionstr = ch ? ch + 1 : pkgname;
/*
* Look for the last '_' in the version string, advancing the
* end pointer.
*/
ch = strrchr(versionstr, '_');
if (revision != NULL)
*revision = ch ? strtoul(ch + 1, NULL, 10) : 0;
endversionstr = ch;
/* Look for the last ',' in the remaining version string */
ch = strrchr(endversionstr ? endversionstr + 1 : versionstr, ',');
if (epoch != NULL)
*epoch = ch ? strtoul(ch + 1, NULL, 10) : 0;
if (ch && !endversionstr)
endversionstr = ch;
/*
* set the pointer behind the last character of the version without
* revision or epoch
*/
if (endname)
*endname = endversionstr ? endversionstr :
strrchr(versionstr, '\0');
return versionstr;
}
/*
* PORTVERSIONs are composed of components separated by dots. A component
* consists of a version number, a letter and a patchlevel number. This does
* not conform to the porter's handbook, but let us formulate rules that
* fit the current practice and are far simpler than to make decisions
* based on the order of netters and lumbers. Besides, people use versions
* like 10b2 in the ports...
*/
typedef struct {
#ifdef __LONG_LONG_SUPPORTED
long long n;
long long pl;
#else
long n;
long pl;
#endif
int a;
} version_component;
/*
* get_component(position, component) gets the value of the next component
* (number - letter - number triple) and returns a pointer to the next character
* after any leading separators
*
* - components are separated by dots
* - characters !~ [a-zA-Z0-9.+*] are treated as separators
* (1.0:2003.09.16 = 1.0.2003.09.16), this may not be what you expect:
* 1.0.1:2003.09.16 < 1.0:2003.09.16
* - consecutive separators are collapsed (10..1 = 10.1)
* - missing separators are inserted, essentially
* letter number letter => letter number . letter (10a1b2 = 10a1.b2)
* - missing components are assumed to be equal to 0 (10 = 10.0 = 10.0.0)
* - the letter sort order is: [none], a, b, ..., z; numbers without letters
* sort first (10 < 10a < 10b)
* - missing version numbers (in components starting with a letter) sort as -1
* (a < 0, 10.a < 10)
* - a separator is inserted before the special strings "pl", "alpha", "beta",
* "pre" and "rc".
* - "pl" sorts before every other letter, "alpha", "beta", "pre" and "rc"
* sort as a, b, p and r. (10alpha = 10.a < 10, but 10 < 10a; pl11 < alpha3
* < 0.1beta2 = 0.1.b2 < 0.1)
* - other strings use only the first letter for sorting, case is ignored
* (1.d2 = 1.dev2 = 1.Development2)
* - The special component `*' is guaranteed to be the smallest possible
* component (2.* < 2pl1 < 2alpha3 < 2.9f7 < 3.*)
* - components separated by `+' are handled by version_cmp below
*
* Oliver Eikemeier
*/
static const struct stage {
const char *name;
size_t namelen;
int value;
} stages[] = {
{ "pl", 2, 0 },
{ "snap", 4, 1 },
#define ABASE 2 /* Last special early-sorted prefix + 1 */
{ "alpha", 5, 'a'-'a'+ABASE },
{ "beta", 4, 'b'-'a'+ABASE },
{ "pre", 3, 'p'-'a'+ABASE },
{ "rc", 2, 'r'-'a'+ABASE },
};
static const char *
get_component(const char *position, version_component *component)
{
const char *pos = position;
int hasstage = 0, haspatchlevel = 0;
if (!pos) {
pkg_emit_error("%s: Passed NULL position.", __func__);
return (NULL);
}
/* handle version number */
if (isdigit(*pos)) {
char *endptr;
#ifdef __LONG_LONG_SUPPORTED
component->n = strtoll(pos, &endptr, 10);
#else
component->n = strtol(pos, &endptr, 10);
#endif
/* should we test for errno == ERANGE? */
pos = endptr;
} else if (*pos == '*') {
component->n = -2;
do {
pos++;
} while(*pos && *pos != '+');
} else {
component->n = -1;
hasstage = 1;
}
/* handle letter */
if (isalpha(*pos)) {
int c = tolower(*pos);
haspatchlevel = 1;
/* handle special suffixes */
if (isalpha(pos[1])) {
unsigned int i;
for (i = 0; i < nitems(stages); i++) {
const struct stage *stage = &stages[i];
size_t len = stage->namelen;
if (strncasecmp(pos, stage->name, len) == 0 &&
!isalpha(pos[stage->namelen])) {
if (hasstage) {
/* stage to value */
component->a = stage->value;
pos += stage->namelen;
} else {
/* insert dot */
component->a = 0;
haspatchlevel = 0;
}
c = 0;
break;
}
}
}
/* unhandled above */
if (c) {
/* use the first letter and skip following */
component->a = c - 'a' + ABASE;
do {
++pos;
} while (isalpha(*pos));
}
} else {
component->a = 0;
haspatchlevel = 0;
}
if (haspatchlevel) {
/* handle patch number */
if (isdigit(*pos)) {
char *endptr;
#ifdef __LONG_LONG_SUPPORTED
component->pl = strtoll(pos, &endptr, 10);
#else
component->pl = strtol(pos, &endptr, 10);
#endif
/* should we test for errno == ERANGE? */
pos = endptr;
} else {
component->pl = -1;
}
} else {
component->pl = 0;
}
/* skip trailing separators */
while (*pos && !isdigit(*pos) && !isalpha(*pos)
&& *pos != '+' && *pos != '*')
pos++;
return pos;
}
/*
* version_cmp(pkg1, pkg2) returns -1, 0 or 1 depending on if the version
* components of pkg1 is less than, equal to or greater than pkg2. No
* comparison of the basenames is done.
*
* The port version is defined by:
* ${PORTVERSION}[_${PORTREVISION}][,${PORTEPOCH}]
* ${PORTEPOCH} supersedes ${PORTVERSION} supersedes ${PORTREVISION}.
* See the commit log for revision 1.349 of ports/Mk/bsd.port.mk
* for more information.
*
* The epoch and revision are defined to be a single number, while the rest
* of the version should conform to the porting guidelines. It can contain
* multiple components, separated by a period, including letters.
*
* The tests allow for significantly more latitude in the version numbers
* than is allowed in the guidelines. No point in enforcing them here.
* That's what portlint is for.
*
* Jeremy D. Lea.
* reimplemented by Oliver Eikemeier
*/
int
pkg_version_cmp(const char * const pkg1, const char * const pkg2)
{
const char *v1, *v2, *ve1, *ve2;
unsigned long e1, e2, r1, r2;
int result = 0;
v1 = split_version(pkg1, &ve1, &e1, &r1);
v2 = split_version(pkg2, &ve2, &e2, &r2);
assert (v1 != NULL && v2 != NULL);
/* Check epoch, port version, and port revision, in that order. */
if (e1 != e2)
result = (e1 < e2 ? -1 : 1);
/* Shortcut check for equality before invoking the parsing routines. */
if (result == 0 &&
(ve1 - v1 != ve2 - v2 || strncasecmp(v1, v2, ve1 - v1) != 0)) {
/*
* Loop over different components (the parts separated by dots).
* If any component differs, there is an inequality.
*/
while (result == 0 && (v1 < ve1 || v2 < ve2)) {
int block_v1 = 0;
int block_v2 = 0;
version_component vc1 = {0, 0, 0};
version_component vc2 = {0, 0, 0};
if (v1 < ve1 && *v1 != '+') {
v1 = get_component(v1, &vc1);
assert (v1 != NULL);
} else {
block_v1 = 1;
}
if (v2 < ve2 && *v2 != '+') {
v2 = get_component(v2, &vc2);
assert (v2 != NULL);
} else {
block_v2 = 1;
}
if (block_v1 && block_v2) {
if (v1 < ve1)
v1++;
if (v2 < ve2)
v2++;
} else if (vc1.n != vc2.n) {
result = (vc1.n < vc2.n ? -1 : 1);
} else if (vc1.a != vc2.a) {
result = (vc1.a < vc2.a ? -1 : 1);
} else if (vc1.pl != vc2.pl) {
result = (vc1.pl < vc2.pl ? -1 : 1);
}
}
}
/* Compare FreeBSD revision numbers. */
if (result == 0 && r1 != r2)
result = (r1 < r2 ? -1 : 1);
return (result);
}
pkg_change_t
pkg_version_change_between(const struct pkg * pkg1, const struct pkg *pkg2)
{
if (pkg2 == NULL)
return PKG_REINSTALL;
switch (pkg_version_cmp(pkg2->version, pkg1->version)) {
case -1:
return (PKG_UPGRADE);
default: /* placate the compiler */
case 0:
return (PKG_REINSTALL);
case 1:
return (PKG_DOWNGRADE);
}
}