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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
|
/**
* SPDX-License-Identifier: Apache-2.0
*
* @file fileop.c
* @brief file operation functions
*/
#include "libredundancyfileop.h"
#include "fileop.h"
#include "file-util.h"
#include "static-configurator.h"
#include "crc16.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
int refop_file_get_with_validation(const char *file, uint8_t *data, int64_t bufsize, int64_t *readsize);
void refop_header_create(s_refop_file_header *head, uint16_t crc16value, uint64_t sizevalue);
int refop_header_validation(const s_refop_file_header *head);
int refop_file_test(const char *filename);
/**
* Redundancy data write.
*
* @param [in] event sd event loop handle
*
* @return refop_error_t
* @retval 0 Succeeded.
* @retval -1 Abnormal fail. Shall not continue.
* @retval -2 Lager than size limit.
*/
int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize)
{
struct refop_halndle *hndl = (struct refop_halndle *)handle;
int ret = -1, fd = -1;
ssize_t wsize = 0;
uint8_t *pbuf = NULL, *pdata = NULL;
uint16_t crc16value = 0;
int new_state = 0;
if (bufsize > refop_get_config_data_size_limit() || bufsize <= 0)
return -2;
// Fource remove new file - success and noent is ok.
ret = unlink(hndl->newfile);
if (ret < 0) {
if (errno != ENOENT)
return -1;
}
// Create write buffer. To reduce sync write operation
pbuf = (uint8_t*)malloc(bufsize + sizeof(s_refop_file_header));
if (pbuf == NULL)
return -1;
// Create write data
pdata = pbuf + sizeof(s_refop_file_header);
memcpy(pdata, data, bufsize);
crc16value = crc16(0xffff, pdata, bufsize);
refop_header_create((s_refop_file_header*)pbuf, crc16value, bufsize);
fd = open(hndl->newfile, (O_CLOEXEC | O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW), (S_IRUSR | S_IWUSR));
if (fd < 0) {
// All open error couldnt recover.
free(pbuf);
return -1;
}
wsize = safe_write(fd, pbuf, bufsize + sizeof(s_refop_file_header));
if (wsize < 0) {
// All open error couldnt recover.
(void)close(fd);
free(pbuf);
return -1;
}
// sync and close
(void)fsync(fd);
(void)close(fd);
free(pbuf);
return 0;
}
/**
* Redundancy data write.
*
* @param [in] event sd event loop handle
*
* @return refop_error_t
* @retval 0 Succeeded.
* @retval -1 Abnormal fail. Shall not continue.
*/
int refop_file_rotation(refop_handle_t handle)
{
struct refop_halndle *hndl = (struct refop_halndle *)handle;
int latest_state = -1, backup_state = -1;
int fd = -1;
//Get all file state
latest_state = refop_file_test(hndl->latestfile);
backup_state = refop_file_test(hndl->backupfile1);
if (latest_state == -2 || backup_state == -2)
return -1;
// Operation algorithm
// Current Next
// | latest | backup | | latest | backup |
// a1 | 1 | 2 | | new | 1 |
// a2 | 1 | x | | new | 1 |
// a3 | x | 2 | | new | 2 |
// a4 | x | x | | new | x |
// All error case of file was checked before this point such as stat check and new file create.
if (latest_state == 0) {
// a1 or a2
if (backup_state == 0) {
//a1
(void)unlink(hndl->backupfile1);
(void)rename(hndl->latestfile, hndl->backupfile1);
(void)rename(hndl->newfile, hndl->latestfile);
} else {
//a2
// nop (void)unlink(hndl->backupfile1);
(void)rename(hndl->latestfile, hndl->backupfile1);
(void)rename(hndl->newfile, hndl->latestfile);
}
} else {
//a3 or a4
if (backup_state == 0) {
//a3
// nop (void)unlink(hndl->backupfile1);
// nop (void)rename(hndl->latestfile, hndl->backupfile1);
(void)rename(hndl->newfile, hndl->latestfile);
} else {
//a4
// nop (void)unlink(hndl->backupfile1);
// nop (void)rename(hndl->latestfile, hndl->backupfile1);
(void)rename(hndl->newfile, hndl->latestfile);
}
}
// directry sync
fd = open(hndl->basedir, (O_CLOEXEC | O_DIRECTORY | O_NOFOLLOW));
if (fd >= 0) {
(void)fsync(fd);
(void)close(fd);
}
return 0;
}
/**
* Redundancy data write.
*
* @param [in] event sd event loop handle
*
* @return refop_error_t
* @retval 0 Succeeded.
* @retval 1 Succeeded with recover.
* @retval -1 Abnormal fail. Shall not continue.
* @retval -2 No data.
* @retval -3 Broaken data.
*/
int refop_file_pickup(refop_handle_t handle, uint8_t *data, int64_t bufsize, int64_t *readsize)
{
struct refop_halndle *hndl = (struct refop_halndle *)handle;
int ret1 = -1, ret2 = -1;
int64_t ressize = 0;
ret1 = refop_file_get_with_validation(hndl->latestfile, data, bufsize, &ressize);
if (ret1 == 0) {
// got valid data
(*readsize) = ressize;
return 0;
} else if (ret1 < -1) {
// latest file was broaken, file remove
(void)unlink(hndl->latestfile);
}
ret2 = refop_file_get_with_validation(hndl->backupfile1, data, bufsize, &ressize);
if (ret2 == 0) {
// got valid data
(*readsize) = ressize;
return 1;
} else if (ret2 < -1) {
// latest file was broaken, file remove
(void)unlink(hndl->latestfile);
}
if (ret1 == -1 && ret2 == -1)
return -2;
return -3;
}
/**
* Target file status check
*
* @param [in] filename Target file path
*
* @return int
* @retval 0 Target file is available.
* @retval -1 No target file.
* @retval -2 Abnormal fail.
*/
int refop_file_test(const char *filename)
{
struct stat sb;
int ret = -1;
//Check a directry
ret = stat(filename, &sb);
if (ret < 0) {
if (errno == ENOENT)
return -1;
else
return -2;
}
return 0;
}
/**
* Redundancy data write.
*
* @param [in] event sd event loop handle
*
* @return int
* @retval 0 succeeded.
* @retval -1 No file entry.
* @retval -2 Invalid file size.
* @retval -3 Invalid header.
* @retval -4 Abnomal request size (smaller than real size)
* @retval -5 Invalid data.
* @retval -6 Abnomal file responce.
*/
int refop_file_get_with_validation(const char *file, uint8_t *data, int64_t bufsize, int64_t *readsize)
{
s_refop_file_header head = {0};
uint8_t *pbuf = NULL, *pmalloc = NULL;
uint16_t crc16value = 0;
ssize_t size = 0;
int result = -1,ret = -1;
int fd = -1;
fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW));
if (fd < 0) {
if (errno == ENOENT)
return -1;
else
return -6;
}
size = safe_read(fd, &head, sizeof(head));
if (size != sizeof(head)) {
ret = -2;
goto invalid;
}
result = refop_header_validation(&head);
if (result != 0) {
ret = -3;
goto invalid;
}
if (head.size > bufsize) {
if (head.size <= refop_get_config_data_size_limit()) {
pmalloc = (uint8_t*)malloc(head.size);
pbuf = pmalloc;
} else {
ret = -4;
goto invalid;
}
} else {
pbuf = data;
}
size = safe_read(fd, pbuf, (size_t)head.size);
if (size != head.size) {
ret = -2;
goto invalid;
}
crc16value = crc16(0xffff, pbuf, head.size);
if (head.crc16 != crc16value) {
ret = -5;
goto invalid;
}
if (pmalloc != NULL) {
memcpy(data, pmalloc, bufsize);
free(pmalloc);
pmalloc = NULL;
(*readsize) = bufsize;
} else
(*readsize) = head.size;
(void)close(fd);
return 0;
invalid:
free(pmalloc); //free is NULL safe
if (fd != -1)
(void)close(fd);
return ret;
}
/**
* The refop header create
*
* @param [in] head The memory of file header.
* @param [in] crc16value The crc value of data block.
* @param [in] sizevalue The size of data block.
*/
void refop_header_create(s_refop_file_header *head, uint16_t crc16value, uint64_t sizevalue)
{
head->magic = REFOP_FILE_HEADER_MAGIC;
head->version = REFOP_FILE_HEADER_VERSION_V1;
head->version_inv = ~head->version;
head->crc16 = crc16value;
head->crc16_inv = ~head->crc16;
head->size = sizevalue;
head->size_inv = ~head->size;
}
/**
* The refop header validation
*
* @param [in] head The memory of file header.
*
* @return int
* @retval 0 succeeded.
* @retval -1 Invalid header.
*/
int refop_header_validation(const s_refop_file_header *head)
{
int ret = -1;
//magic check
if (head->magic != (uint32_t)REFOP_FILE_HEADER_MAGIC)
goto invalid;
if (head->version == (uint32_t)(~head->version_inv)) {
if (head->version != REFOP_FILE_HEADER_VERSION_V1)
goto invalid;
} else
goto invalid;
if (head->crc16 != (uint16_t)(~head->crc16_inv))
goto invalid;
if (head->size != (uint64_t)(~head->size_inv))
goto invalid;
ret = 0;
invalid:
return ret;
}
|