forked from ElementsProject/lightning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeprecation.c
147 lines (130 loc) · 3.24 KB
/
deprecation.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
#include "config.h"
#include <assert.h>
#include <ccan/str/str.h>
#include <ccan/tal/tal.h>
#include <common/deprecation.h>
#include <stdlib.h>
#define MONTH_VAL (10)
#define YEAR_VAL (12 * MONTH_VAL)
/* Returns 1 + patchnumber + 10*minor + 120*major. 0 on malformed */
u32 version_to_number(const char *version)
{
char *yend, *mend;
long year, month, patchlevel;
if (version[0] != 'v')
return 0;
year = strtol(version + 1, ¥d, 10);
if (yend == version + 1 || *yend != '.')
return 0;
if (year > 99)
return 0;
month = strtol(yend + 1, &mend, 10);
if (mend == yend + 1)
return 0;
if (month < 1 || month > 12)
return 0;
if (*mend == '.') {
char *endp;
patchlevel = strtol(mend + 1, &endp, 10);
if (endp == mend + 1)
return 0;
if (patchlevel >= MONTH_VAL)
return 0;
} else
patchlevel = 0;
return 1 + year*YEAR_VAL + month*MONTH_VAL + patchlevel;
}
enum deprecation_level {
/* Will be deprecated in future */
DEPRECATED_SOON,
/* Deprecated, but still ok unless explicitly disabled */
DEPRECATED,
/* Deprecated, and we whine about it */
DEPRECATED_COMPLAIN,
/* Deprecated, and we only enable it if they begged */
DEPRECATED_BEG,
};
static enum deprecation_level deprecation(const char *start,
const char *end)
{
long cur;
long startnum;
long endnum;
/* Versions are hard. Consider these:
* v23.05
* -- A released version
* v23.05rc1
* -- A release candidate
* v23.05rc4-11-g1e96146
* -- Development off rc4, OR a user's local mods.
* v23.05-1-gf165dc0-modded
* -- Development off 23.05, OR a user's local mods.
*/
/* If master has moved since release, we want to increment
* deprecations. If a user has made local mods, we don't!
* Fortunately, the Makefile sets "CLN_NEXT_VERSION", and
* we can simply use this.
*/
cur = version_to_number(CLN_NEXT_VERSION);
assert(cur);
startnum = version_to_number(start);
assert(startnum);
if (end) {
endnum = version_to_number(end);
assert(endnum);
assert(endnum >= startnum);
} else /* 6 months later */
endnum = startnum + 6 * MONTH_VAL;
if (cur < startnum)
return DEPRECATED_SOON;
if (cur < endnum)
return DEPRECATED;
if (cur == endnum)
return DEPRECATED_COMPLAIN;
return DEPRECATED_BEG;
}
bool deprecated_ok_(bool deprecated_apis,
const char *feature,
const char *start,
const char *end,
const char **begs,
void (*complain)(const char *feature, bool allowing, void *cbarg),
void *cbarg)
{
enum deprecation_level level;
bool allow;
/* Not deprecated at all? */
if (!start)
return true;
level = deprecation(start, end);
switch (level) {
case DEPRECATED_SOON:
return false;
case DEPRECATED:
/* Complain if we're disallowing becuase it's deprecated */
allow = deprecated_apis;
if (!allow)
goto complain;
goto no_complain;
case DEPRECATED_COMPLAIN:
allow = deprecated_apis;
/* Always complain about these! */
goto complain;
case DEPRECATED_BEG:
allow = false;
for (size_t i = 0; i < tal_count(begs); i++) {
if (streq(feature, begs[i]))
allow = true;
}
/* Don't complain about begging: they've explicitly noted this! */
if (allow)
goto no_complain;
goto complain;
}
abort();
complain:
if (complain)
complain(feature, allow, cbarg);
no_complain:
return allow;
}