forked from cfengine/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvercmp_internal.c
209 lines (176 loc) · 6.02 KB
/
vercmp_internal.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
/*
Copyright 2017 Northern.tech AS
This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; version 3.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
To the extent this program is licensed as part of the Enterprise
versions of CFEngine, the applicable Commercial Open Source License
(COSL) may apply to this file if you as a licensee so wish it. See
included file COSL.txt.
*/
#include <cf3.defs.h>
#include <files_names.h>
#include <vercmp_internal.h>
#include <rlist.h>
static void ParsePackageVersion(char *version, Rlist **num, Rlist **sep);
VersionCmpResult ComparePackageVersionsInternal(const char *v1, const char *v2, PackageVersionComparator cmp)
{
Rlist *rp_pr, *rp_in;
int result = true;
int break_loop = false;
int cmp_result;
VersionCmpResult version_matched = VERCMP_NO_MATCH;
Rlist *numbers_pr = NULL, *separators_pr = NULL;
Rlist *numbers_in = NULL, *separators_in = NULL;
ParsePackageVersion(CanonifyChar(v1, ','), &numbers_pr, &separators_pr);
ParsePackageVersion(CanonifyChar(v2, ','), &numbers_in, &separators_in);
/* If the format of the version string doesn't match, we're already doomed */
Log(LOG_LEVEL_VERBOSE, "Check for compatible versioning model in (%s,%s)", v1, v2);
for (rp_pr = separators_pr, rp_in = separators_in; (rp_pr != NULL) && (rp_in != NULL);
rp_pr = rp_pr->next, rp_in = rp_in->next)
{
if (strcmp(RlistScalarValue(rp_pr), RlistScalarValue(rp_in)) != 0)
{
result = false;
break;
}
if ((rp_pr->next == NULL) && (rp_in->next == NULL))
{
result = true;
break;
}
}
if (result)
{
Log(LOG_LEVEL_VERBOSE, "Verified that versioning models are compatible");
}
else
{
Log(LOG_LEVEL_VERBOSE, "Versioning models for (%s,%s) were incompatible", v1, v2);
version_matched = VERCMP_ERROR;
}
int version_equal = (strcmp(v2, v1) == 0);
if (result)
{
for (rp_pr = numbers_pr, rp_in = numbers_in; (rp_pr != NULL) && (rp_in != NULL);
rp_pr = rp_pr->next, rp_in = rp_in->next)
{
cmp_result = strcmp(RlistScalarValue(rp_pr), RlistScalarValue(rp_in));
switch (cmp)
{
case PACKAGE_VERSION_COMPARATOR_EQ:
case PACKAGE_VERSION_COMPARATOR_NONE:
if (version_equal)
{
version_matched = VERCMP_MATCH;
}
break;
case PACKAGE_VERSION_COMPARATOR_NEQ:
if (!version_equal)
{
version_matched = VERCMP_MATCH;
}
break;
case PACKAGE_VERSION_COMPARATOR_GT:
if (cmp_result > 0)
{
version_matched = VERCMP_MATCH;
}
else if (cmp_result < 0)
{
break_loop = true;
}
break;
case PACKAGE_VERSION_COMPARATOR_LT:
if (cmp_result < 0)
{
version_matched = VERCMP_MATCH;
}
else if (cmp_result > 0)
{
break_loop = true;
}
break;
case PACKAGE_VERSION_COMPARATOR_GE:
if ((cmp_result > 0) || version_equal)
{
version_matched = VERCMP_MATCH;
}
else if (cmp_result < 0)
{
break_loop = true;
}
break;
case PACKAGE_VERSION_COMPARATOR_LE:
if ((cmp_result < 0) || version_equal)
{
version_matched = VERCMP_MATCH;
}
else if (cmp_result > 0)
{
break_loop = true;
}
break;
default:
break;
}
if ((version_matched == VERCMP_MATCH) || break_loop)
{
rp_pr = NULL;
rp_in = NULL;
break;
}
}
if (rp_pr != NULL)
{
if ((cmp == PACKAGE_VERSION_COMPARATOR_GT) || (cmp == PACKAGE_VERSION_COMPARATOR_GE))
{
version_matched = VERCMP_MATCH;
}
}
if (rp_in != NULL)
{
if ((cmp == PACKAGE_VERSION_COMPARATOR_LT) || (cmp == PACKAGE_VERSION_COMPARATOR_LE))
{
version_matched = VERCMP_MATCH;
}
}
}
RlistDestroy(numbers_pr);
RlistDestroy(numbers_in);
RlistDestroy(separators_pr);
RlistDestroy(separators_in);
return version_matched;
}
static void ParsePackageVersion(char *version, Rlist **num, Rlist **sep)
{
char *sp, numeral[30], separator[2];
if (version == NULL)
{
return;
}
for (sp = version; *sp != '\0'; sp++)
{
memset(numeral, 0, 30);
memset(separator, 0, 2);
/* Split in 2's complement */
sscanf(sp, "%29[0-9a-zA-Z]", numeral);
sp += strlen(numeral);
/* Append to end up with left->right (major->minor) comparison */
RlistAppendScalar(num, numeral);
if (*sp == '\0')
{
return;
}
sscanf(sp, "%1[^0-9a-zA-Z]", separator);
RlistAppendScalar(sep, separator);
}
}