-
Notifications
You must be signed in to change notification settings - Fork 3
/
conf.c
275 lines (234 loc) · 6.62 KB
/
conf.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
/* conf.c */
/*
* Copyright (c) 2022 Stephen D. Adams <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <uuid.h>
#include "muxfs.h"
/*
* 2 decimals at most 10 digits long (the string length of the decimal
* representation of UINT32_MAX), plus 2 dots, plus the length of "release".
*/
#define MUXFS_VERSION_STRING_LENTH_MAX ((2*10)+2+7)
/* 2^64 is about 1.8e10^19 */
#define MUXFS_DECIMAL_UINT64_LENGTH_MAX 20
struct muxfs_dev_conf_checklist {
int has_version, has_alg, has_array_uuid, has_dev_uuid, has_seq_zero_time;
};
static int
muxfs_conf_version_parse(struct muxfs_dev_conf *conf, const char *version,
size_t version_len)
{
char *b, *e;
size_t len, comp_count;
uint32_t *comp;
char version_string_buf[MUXFS_VERSION_STRING_LENTH_MAX + 1];
const char *errstr;
comp_count = 0;
memset(version_string_buf, 0, MUXFS_VERSION_STRING_LENTH_MAX + 1);
memcpy(version_string_buf, version, version_len);
b = version_string_buf;
while ((e = memchr(b, '.', version_len)) != NULL) {
len = e - b;
switch (comp_count) {
case 0:
comp = &conf->version.number;
break;
case 1:
comp = &conf->version.revision;
break;
default:
return 1;
}
++comp_count;
*e = '\0';
*comp = strtonum(b, 0, INT32_MAX, &errstr);
if (errstr != NULL)
return 1;
version_len -= (len + 1);
b = e + 1;
}
/*
* The version components listed in the conf file are separated by dots
* and we do not expect a final dot at the end so there is one more
* component to process.
*/
if (version_len == 0)
return 1;
len = version_len;
if (strncmp("current", b, len))
conf->version.flavor = VF_CURRENT;
else if (strncmp("release", b, len))
conf->version.flavor = VF_RELEASE;
else if (strncmp("stable", b, len))
conf->version.flavor = VF_STABLE;
else
return 1;
return 0;
}
static int
muxfs_uuid_read(uint8_t *dest, const char *src, size_t len)
{
char *str;
uint32_t status;
uuid_t uuid;
str = calloc(len + 1, sizeof(char));
if (str == NULL)
return 1;
memcpy(str, src, len);
uuid_from_string(str, &uuid, &status);
free(str);
if (status != uuid_s_ok)
return 1;
uuid_enc_le(dest, &uuid);
return 0;
}
static int
muxfs_conf_line_parse(struct muxfs_dev_conf *conf, const char *line,
size_t len, struct muxfs_dev_conf_checklist *cl)
{
const char *eq, *key, *val;
size_t key_len, val_len;
char seq_zero_time_buf[MUXFS_DECIMAL_UINT64_LENGTH_MAX + 1];
const char *errstr;
if ((eq = memchr(line, '=', len)) == NULL)
return 1;
key = line;
key_len = eq - line;
val = eq + 1;
val_len = len - (key_len + 1);
if (strncmp(key, "version", key_len) == 0) {
if (muxfs_conf_version_parse(conf, val, val_len))
return 1;
cl->has_version = 1;
} else if (strncmp(key, "chk_alg", key_len) == 0) {
if (muxfs_chk_str_to_type(&conf->chk_alg_type, val, val_len))
return 1;
cl->has_alg = 1;
} else if (strncmp(key, "array_uuid", key_len) == 0) {
if (muxfs_uuid_read(conf->array_uuid, val, val_len))
return 1;
cl->has_array_uuid = 1;
} else if (strncmp(key, "dev_uuid", key_len) == 0) {
if (muxfs_uuid_read(conf->dev_uuid, val, val_len))
return 1;
cl->has_dev_uuid = 1;
} else if (strncmp(key, "seq_zero_time", key_len) == 0) {
if (val_len > MUXFS_DECIMAL_UINT64_LENGTH_MAX)
return 1;
memset(seq_zero_time_buf, 0,
MUXFS_DECIMAL_UINT64_LENGTH_MAX + 1);
memcpy(seq_zero_time_buf, val, val_len);
conf->seq_zero_time = strtonum(seq_zero_time_buf, 0,
INT64_MAX, &errstr);
if (errstr != NULL)
return 1;
cl->has_seq_zero_time = 1;
} else
return 1;
return 0;
}
static int
muxfs_conf_check(struct muxfs_dev_conf *conf,
struct muxfs_dev_conf_checklist cl)
{
if (!(cl.has_alg && cl.has_array_uuid && cl.has_dev_uuid &&
cl.has_version && cl.has_seq_zero_time))
return 1;
if (conf->version.number != muxfs_program_version.number)
return 1;
if (conf->version.revision != muxfs_program_version.revision)
return 1;
return 0;
}
MUXFS int
muxfs_conf_parse(struct muxfs_dev_conf *conf, int fd)
{
char buf[MUXFS_BLOCK_SIZE];
ssize_t readsz, bufsz, linesz;
char *eol;
struct muxfs_dev_conf_checklist cl;
memset(conf, 0, sizeof(*conf));
memset(buf, 0, MUXFS_BLOCK_SIZE);
bufsz = 0;
while ((readsz = read(fd, buf + bufsz, MUXFS_BLOCK_SIZE - bufsz)) > 0) {
if (readsz == -1)
return 1;
bufsz += readsz;
while ((eol = memchr(buf, '\n', bufsz)) != NULL) {
linesz = eol - buf;
if (muxfs_conf_line_parse(conf, buf, linesz, &cl))
return 1;
memmove(buf, eol + 1, bufsz - (linesz + 1));
bufsz -= (linesz + 1);
}
if (bufsz == MUXFS_BLOCK_SIZE) {
/* The line is longer than the buffer. */
return 1;
}
}
if (bufsz > 0) {
/* The last line doesn't end with a newline character. */
return 1;
}
if (muxfs_conf_check(conf, cl))
return 1;
return 0;
}
static const char *
muxfs_version_flavor_str(enum muxfs_version_flavor flavor)
{
switch (flavor) {
case VF_CURRENT:
return "current";
case VF_RELEASE:
return "release";
case VF_STABLE:
return "stable";
}
exit(-1); /* Programming error. */
}
MUXFS int
muxfs_conf_write(struct muxfs_dev_conf *conf, int fd)
{
uuid_t uuid;
char *uuid_str;
uint32_t uuid_status;
if (ftruncate(fd, 0))
return 1;
dprintf(fd, "version=%u.%u.%s\n", conf->version.number,
conf->version.revision,
muxfs_version_flavor_str(conf->version.flavor));
dprintf(fd, "chk_alg=%s\n",
muxfs_chk_type_to_str(conf->chk_alg_type));
uuid_dec_le(conf->array_uuid, &uuid);
uuid_to_string(&uuid, &uuid_str, &uuid_status);
if (uuid_status != uuid_s_ok)
return 1;
dprintf(fd, "array_uuid=%s\n", uuid_str);
free(uuid_str);
uuid_dec_le(conf->dev_uuid, &uuid);
uuid_to_string(&uuid, &uuid_str, &uuid_status);
if (uuid_status != uuid_s_ok)
return 1;
dprintf(fd, "dev_uuid=%s\n", uuid_str);
free(uuid_str);
dprintf(fd, "seq_zero_time=%llu\n", (uint64_t)conf->seq_zero_time);
return 0;
}