aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorNaoto Yamaguchi <naoto.yamaguchi@aisin.co.jp>2022-07-22 03:20:44 +0900
committerNaoto Yamaguchi <naoto.yamaguchi@aisin.co.jp>2022-08-05 00:36:26 +0900
commit86f13970718ef5e0758ac4edfe7da2c354d19668 (patch)
tree5b70cbda20b2e50efc7eb1e63f16f0e8d57a905f /lib
parent079a02e20320e8018d1b2c879c759e4b5c9d30fb (diff)
Reformat code
The librefop define the coding style using clang-format. This patch reformat code using clang-format. Signed-off-by: Naoto Yamaguchi <naoto.yamaguchi@aisin.co.jp> Change-Id: I6f6647317f2916c583ca68e1259da2e08a989657
Diffstat (limited to 'lib')
-rw-r--r--lib/crc16.h60
-rw-r--r--lib/file-util.c14
-rw-r--r--lib/fileop.c143
-rw-r--r--lib/fileop.h32
-rw-r--r--lib/libredundancyfileop.c49
5 files changed, 149 insertions, 149 deletions
diff --git a/lib/crc16.h b/lib/crc16.h
index f04b7fc..9a002d2 100644
--- a/lib/crc16.h
+++ b/lib/crc16.h
@@ -33,48 +33,36 @@
/* CRC table for standard ANSI CRC-16 with polynom 0x8005 */
static const uint16_t crc16_lookup[] = {
- 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
- 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
- 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
- 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
- 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
- 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
- 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
- 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
- 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
- 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
- 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
- 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
- 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
- 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
- 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
- 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
- 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
- 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
- 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
- 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
- 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
- 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
- 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
- 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
- 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
- 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
- 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
- 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
- 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
- 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
- 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
- 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
+ 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241, 0xc601, 0x06c0, 0x0780, 0xc741,
+ 0x0500, 0xc5c1, 0xc481, 0x0440, 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
+ 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841, 0xd801, 0x18c0, 0x1980, 0xd941,
+ 0x1b00, 0xdbc1, 0xda81, 0x1a40, 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
+ 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641, 0xd201, 0x12c0, 0x1380, 0xd341,
+ 0x1100, 0xd1c1, 0xd081, 0x1040, 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
+ 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441, 0x3c00, 0xfcc1, 0xfd81, 0x3d40,
+ 0xff01, 0x3fc0, 0x3e80, 0xfe41, 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
+ 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41, 0xee01, 0x2ec0, 0x2f80, 0xef41,
+ 0x2d00, 0xedc1, 0xec81, 0x2c40, 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
+ 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041, 0xa001, 0x60c0, 0x6180, 0xa141,
+ 0x6300, 0xa3c1, 0xa281, 0x6240, 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
+ 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41, 0xaa01, 0x6ac0, 0x6b80, 0xab41,
+ 0x6900, 0xa9c1, 0xa881, 0x6840, 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
+ 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40, 0xb401, 0x74c0, 0x7580, 0xb541,
+ 0x7700, 0xb7c1, 0xb681, 0x7640, 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
+ 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241, 0x9601, 0x56c0, 0x5780, 0x9741,
+ 0x5500, 0x95c1, 0x9481, 0x5440, 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
+ 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841, 0x8801, 0x48c0, 0x4980, 0x8941,
+ 0x4b00, 0x8bc1, 0x8a81, 0x4a40, 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
+ 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641, 0x8201, 0x42c0, 0x4380, 0x8341,
+ 0x4100, 0x81c1, 0x8081, 0x4040
};
-static __inline uint16_t
-crc16_byte(uint16_t ccrc, const uint8_t b)
+static __inline uint16_t crc16_byte(uint16_t ccrc, const uint8_t b)
{
return crc16_lookup[((ccrc) ^ (b)) & 0xffu] ^ ((ccrc) >> 8);
}
-static __inline uint16_t
-crc16(uint16_t crc, const uint8_t *data, size_t len)
+static __inline uint16_t crc16(uint16_t crc, const uint8_t *data, size_t len)
{
while (len--)
crc = crc16_byte(crc, *data++);
diff --git a/lib/file-util.c b/lib/file-util.c
index a45c946..652cfd3 100644
--- a/lib/file-util.c
+++ b/lib/file-util.c
@@ -6,9 +6,9 @@
*/
#include "file-util.h"
+#include <errno.h>
#include <stdint.h>
#include <unistd.h>
-#include <errno.h>
/**
* INTR safe read
@@ -16,11 +16,11 @@
*/
ssize_t safe_read(int fd, void *buf, size_t count)
{
- ssize_t size = 0,ressize = 0;
+ ssize_t size = 0, ressize = 0;
size_t reqsize = 0;
uint8_t *pbuf = NULL;
- pbuf = (uint8_t*)buf;
+ pbuf = (uint8_t *) buf;
reqsize = count;
do {
@@ -36,7 +36,7 @@ ssize_t safe_read(int fd, void *buf, size_t count)
pbuf += size;
ressize += size;
- } while((ressize < reqsize) && (size != 0));
+ } while ((ressize < reqsize) && (size != 0));
return ressize;
}
@@ -47,11 +47,11 @@ ssize_t safe_read(int fd, void *buf, size_t count)
*/
ssize_t safe_write(int fd, void *buf, size_t count)
{
- ssize_t size = 0,ressize = 0;
+ ssize_t size = 0, ressize = 0;
size_t reqsize = 0;
uint8_t *pbuf = NULL;
- pbuf = (uint8_t*)buf;
+ pbuf = (uint8_t *) buf;
reqsize = count;
do {
@@ -67,7 +67,7 @@ ssize_t safe_write(int fd, void *buf, size_t count)
pbuf += size;
ressize += size;
- } while((ressize < reqsize) && (size != 0));
+ } while ((ressize < reqsize) && (size != 0));
return ressize;
}
diff --git a/lib/fileop.c b/lib/fileop.c
index 7fd029e..5dc0842 100644
--- a/lib/fileop.c
+++ b/lib/fileop.c
@@ -4,21 +4,21 @@
* @file fileop.c
* @brief file operation functions
*/
-#include "librefop.h"
#include "fileop.h"
+#include "crc16.h"
#include "file-util.h"
+#include "librefop.h"
#include "static-configurator.h"
-#include "crc16.h"
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <stdio.h>
-#include <sys/types.h>
-#include <sys/stat.h>
+#include <errno.h>
#include <fcntl.h>
+#include <sys/stat.h>
+#include <sys/types.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);
@@ -27,18 +27,20 @@ int refop_header_validation(const s_refop_file_header *head);
int refop_file_test(const char *filename);
/**
- * Redundancy data write.
+ * This function create new datafile with header.
*
- * @param [in] event sd event loop handle
+ * @param [in] handle Refop handle.
+ * @param [in] data Porinter to write data
+ * @param [in] bufsize Write dara size
*
- * @return refop_error_t
+ * @return int
* @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;
+ struct refop_halndle *hndl = (struct refop_halndle *) handle;
int ret = -1, fd = -1;
ssize_t wsize = 0;
uint8_t *pbuf = NULL, *pdata = NULL;
@@ -48,7 +50,7 @@ int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize)
if (bufsize > refop_get_config_data_size_limit() || bufsize <= 0)
return -2;
- // Fource remove new file - success and noent is ok.
+ // Fource remove new file - success and noent are both ok.
ret = unlink(hndl->newfile);
if (ret < 0) {
if (errno != ENOENT)
@@ -56,7 +58,7 @@ int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize)
}
// Create write buffer. To reduce sync write operation
- pbuf = (uint8_t*)malloc(bufsize + sizeof(s_refop_file_header));
+ pbuf = (uint8_t *) malloc(bufsize + sizeof(s_refop_file_header));
if (pbuf == NULL)
return -1;
@@ -65,7 +67,7 @@ int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize)
memcpy(pdata, data, bufsize);
crc16value = crc16(0xffff, pdata, bufsize);
- refop_header_create((s_refop_file_header*)pbuf, crc16value, 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) {
@@ -76,14 +78,14 @@ int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize)
wsize = safe_write(fd, pbuf, bufsize + sizeof(s_refop_file_header));
if (wsize < 0) {
- (void)close(fd);
+ (void) close(fd);
free(pbuf);
return -1;
}
// sync and close
- (void)fsync(fd);
- (void)close(fd);
+ (void) fsync(fd);
+ (void) close(fd);
free(pbuf);
return 0;
@@ -91,21 +93,22 @@ int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize)
/**
- * Redundancy data write.
+ * This function is implemented file rotation algorithm.
+ * The detail of file rotation algorithm describe in README file.
*
- * @param [in] event sd event loop handle
+ * @param [in] handle Refop handle.
*
- * @return refop_error_t
+ * @return int
* @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;
+ struct refop_halndle *hndl = (struct refop_halndle *) handle;
int latest_state = -1, backup_state = -1;
int fd = -1;
- //Get all file state
+ // Get all file state
latest_state = refop_file_test(hndl->latestfile);
backup_state = refop_file_test(hndl->backupfile1);
@@ -124,47 +127,48 @@ int refop_file_rotation(refop_handle_t handle)
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);
+ // a1
+ (void) unlink(hndl->backupfile1);
+ (void) rename(hndl->latestfile, hndl->backupfile1);
+ (void) rename(hndl->newfile, hndl->latestfile);
} else {
- //a2
+ // a2
// nop (void)unlink(hndl->backupfile1);
- (void)rename(hndl->latestfile, hndl->backupfile1);
- (void)rename(hndl->newfile, hndl->latestfile);
+ (void) rename(hndl->latestfile, hndl->backupfile1);
+ (void) rename(hndl->newfile, hndl->latestfile);
}
} else {
- //a3 or a4
+ // a3 or a4
if (backup_state == 0) {
- //a3
+ // a3
// nop (void)unlink(hndl->backupfile1);
// nop (void)rename(hndl->latestfile, hndl->backupfile1);
- (void)rename(hndl->newfile, hndl->latestfile);
+ (void) rename(hndl->newfile, hndl->latestfile);
} else {
- //a4
+ // a4
// nop (void)unlink(hndl->backupfile1);
// nop (void)rename(hndl->latestfile, hndl->backupfile1);
- (void)rename(hndl->newfile, hndl->latestfile);
+ (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);
+ (void) fsync(fd);
+ (void) close(fd);
}
return 0;
}
/**
- * Redundancy data write.
+ * This function is implemented file pick up algorithm that is including validation.
+ * The detail of file rotation algorithm describe in README file.
*
- * @param [in] event sd event loop handle
+ * @param [in] handle Refop handle.
*
- * @return refop_error_t
+ * @return int
* @retval 0 Succeeded.
* @retval 1 Succeeded with recover.
* @retval -1 Abnormal fail. Shall not continue.
@@ -173,7 +177,7 @@ int refop_file_rotation(refop_handle_t handle)
*/
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;
+ struct refop_halndle *hndl = (struct refop_halndle *) handle;
int ret1 = -1, ret2 = -1;
int64_t ressize = 0;
@@ -185,7 +189,7 @@ int refop_file_pickup(refop_handle_t handle, uint8_t *data, int64_t bufsize, int
return 0;
} else if (ret1 < -1) {
// latest file was broken, file remove
- (void)unlink(hndl->latestfile);
+ (void) unlink(hndl->latestfile);
}
ret2 = refop_file_get_with_validation(hndl->backupfile1, data, bufsize, &ressize);
@@ -195,19 +199,19 @@ int refop_file_pickup(refop_handle_t handle, uint8_t *data, int64_t bufsize, int
return 1;
} else if (ret2 < -1) {
// backup file was broken, file remove
- (void)unlink(hndl->latestfile);
+ (void) unlink(hndl->latestfile);
}
if (ret1 == -1 && ret2 == -1)
- return -2;
+ return -2; // No data
- return -3;
+ return -3; // Broken data
}
/**
- * Target file status check
+ * Confirmation of existence of target file.
*
- * @param [in] filename Target file path
+ * @param [in] filename Target file name with path.
*
* @return int
* @retval 0 Target file is available.
@@ -219,7 +223,7 @@ int refop_file_test(const char *filename)
struct stat sb;
int ret = -1;
- //Check a directry
+ // Check a directry
ret = stat(filename, &sb);
if (ret < 0) {
if (errno == ENOENT)
@@ -232,9 +236,13 @@ int refop_file_test(const char *filename)
}
/**
- * Redundancy data write.
+ * File read function with validation.
+ * File validation use invert value verification and data verification using crc16.
*
- * @param [in] event sd event loop handle
+ * @param [in] file File name with path.
+ * @param [in] data Read data buffer
+ * @param [in] bufsize Buffer size for read data buffer (bytes).
+ * @param [in] readsize Readed size
*
* @return int
* @retval 0 succeeded.
@@ -247,29 +255,29 @@ int refop_file_test(const char *filename)
*/
int refop_file_get_with_validation(const char *file, uint8_t *data, int64_t bufsize, int64_t *readsize)
{
- s_refop_file_header head = {0};
+ 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 result = -1, ret = -1;
int fd = -1;
fd = open(file, (O_CLOEXEC | O_RDONLY | O_NOFOLLOW));
if (fd < 0) {
if (errno == ENOENT)
ret = -1;
- else
+ else
ret = -6;
goto invalid;
}
-
+
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;
@@ -278,7 +286,7 @@ int refop_file_get_with_validation(const char *file, uint8_t *data, int64_t bufs
if (head.size > bufsize) {
if (head.size <= refop_get_config_data_size_limit()) {
- pmalloc = (uint8_t*)malloc(head.size);
+ pmalloc = (uint8_t *) malloc(head.size);
pbuf = pmalloc;
} else {
ret = -4;
@@ -288,7 +296,7 @@ int refop_file_get_with_validation(const char *file, uint8_t *data, int64_t bufs
pbuf = data;
}
- size = safe_read(fd, pbuf, (size_t)head.size);
+ size = safe_read(fd, pbuf, (size_t) head.size);
if (size != head.size) {
ret = -2;
goto invalid;
@@ -308,23 +316,23 @@ int refop_file_get_with_validation(const char *file, uint8_t *data, int64_t bufs
} else
(*readsize) = head.size;
- (void)close(fd);
+ (void) close(fd);
return 0;
invalid:
- free(pmalloc); //free is NULL safe
-
+ free(pmalloc); // free is NULL safe
+
if (fd >= 0)
- (void)close(fd);
+ (void) close(fd);
return ret;
}
/**
- * The refop header create
+ * The refop header create from args.
*
- * @param [in] head The memory of file header.
+ * @param [in] head Pointer for file header.
* @param [in] crc16value The crc value of data block.
* @param [in] sizevalue The size of data block.
*/
@@ -345,7 +353,7 @@ void refop_header_create(s_refop_file_header *head, uint16_t crc16value, uint64_
/**
* The refop header validation
*
- * @param [in] head The memory of file header.
+ * @param [in] head Pointer for file header.
*
* @return int
* @retval 0 succeeded.
@@ -355,24 +363,27 @@ int refop_header_validation(const s_refop_file_header *head)
{
int ret = -1;
- //magic check
- if (head->magic != (uint32_t)REFOP_FILE_HEADER_MAGIC)
+ // magic check
+ if (head->magic != (uint32_t) REFOP_FILE_HEADER_MAGIC)
goto invalid;
+ // header format version check
if (head->version == (uint32_t)(~head->version_inv)) {
if (head->version != REFOP_FILE_HEADER_VERSION_V1)
goto invalid;
- } else
+ } else
goto invalid;
+ // crc16 value check
if (head->crc16 != (uint16_t)(~head->crc16_inv))
goto invalid;
+ // data size check
if (head->size != (uint64_t)(~head->size_inv))
goto invalid;
ret = 0;
-
+
invalid:
return ret;
}
diff --git a/lib/fileop.h b/lib/fileop.h
index d005fd5..207d2c8 100644
--- a/lib/fileop.h
+++ b/lib/fileop.h
@@ -7,10 +7,11 @@
#ifndef REFOP_FILEOP_H
#define REFOP_FILEOP_H
//-----------------------------------------------------------------------------
+#include <librefop.h>
+#include <linux/limits.h>
#include <stdbool.h>
-#include <stdint.h>
#include <stddef.h>
-#include <linux/limits.h>
+#include <stdint.h>
//-----------------------------------------------------------------------------
#ifdef __cplusplus
@@ -18,31 +19,30 @@ extern "C" {
#endif
//-----------------------------------------------------------------------------
struct __attribute__((packed)) s_refop_file_header_v1 {
- uint32_t magic; /* 4 *//**< Magic code */
- uint32_t version; /* 8 *//**< Data format version */
- uint32_t version_inv; /* 12 *//**< Data format version (inversion value) */
- uint16_t crc16; /* 14 *//**< Data block crc */
- uint16_t crc16_inv; /* 16 *//**< Data block crc (inversion value) */
- uint64_t size; /* 24 *//**< Data block size */
- uint64_t size_inv; /* 32 *//**< Data block size (inversion value) */
+ uint32_t magic; /* 4 */ /**< Magic code */
+ uint32_t version; /* 8 */ /**< Data format version */
+ uint32_t version_inv; /* 12 */ /**< Data format version (inversion value) */
+ uint16_t crc16; /* 14 */ /**< Data block crc */
+ uint16_t crc16_inv; /* 16 */ /**< Data block crc (inversion value) */
+ uint64_t size; /* 24 */ /**< Data block size */
+ uint64_t size_inv; /* 32 */ /**< Data block size (inversion value) */
};
-#define REFOP_FILE_HEADER_MAGIC ((uint32_t)0x96962323)
-#define REFOP_FILE_HEADER_VERSION_V1 ((uint32_t)0x00000001)
+#define REFOP_FILE_HEADER_MAGIC ((uint32_t) 0x96962323)
+#define REFOP_FILE_HEADER_VERSION_V1 ((uint32_t) 0x00000001)
typedef struct s_refop_file_header_v1 s_refop_file_header;
struct refop_halndle {
- char latestfile[PATH_MAX];
- char backupfile1[PATH_MAX];
- char newfile[PATH_MAX];
- char basedir[PATH_MAX];
+ char latestfile[PATH_MAX]; /**< Internal buffer for the latest file name */
+ char backupfile1[PATH_MAX]; /**< Internal buffer for the backup file name */
+ char newfile[PATH_MAX]; /**< Internal buffer for the new file name */
+ char basedir[PATH_MAX]; /**< Internal buffer for the file operation base dir */
};
//-----------------------------------------------------------------------------
int refop_new_file_write(refop_handle_t handle, uint8_t *data, int64_t bufsize);
int refop_file_rotation(refop_handle_t handle);
-
int refop_file_pickup(refop_handle_t handle, uint8_t *data, int64_t bufsize, int64_t *readsize);
//-----------------------------------------------------------------------------
diff --git a/lib/libredundancyfileop.c b/lib/libredundancyfileop.c
index ef792d1..6889d9f 100644
--- a/lib/libredundancyfileop.c
+++ b/lib/libredundancyfileop.c
@@ -4,13 +4,13 @@
* @file libredundancyfileop.c
* @brief The redundancy file operation library
*/
-#include "librefop.h"
#include "fileop.h"
+#include "librefop.h"
-#include <sys/types.h>
+#include <errno.h>
#include <sys/stat.h>
+#include <sys/types.h>
#include <unistd.h>
-#include <errno.h>
#include <stdlib.h>
#include <string.h>
@@ -42,7 +42,7 @@ refop_error_t refop_create_redundancy_handle(refop_handle_t *handle, const char
if ((handle == NULL) || (directry == NULL) || (filename == NULL))
return REFOP_ARGERROR;
- //Check a directry
+ // Check a directry
ret = stat(directry, &sb);
if (ret < 0) {
if ((errno == EACCES) || (errno == ELOOP) || (errno == ENOENT) || (errno == ENOTDIR))
@@ -53,35 +53,36 @@ refop_error_t refop_create_redundancy_handle(refop_handle_t *handle, const char
return REFOP_SYSERROR;
}
- //Handle memory allocate
- hndl = (struct refop_halndle*)malloc(sizeof(struct refop_halndle));
+ // Handle memory allocate
+ hndl = (struct refop_halndle *) malloc(sizeof(struct refop_halndle));
if (hndl == NULL)
return REFOP_SYSERROR;
memset(hndl, 0, sizeof(struct refop_halndle));
- //Create file path
- dirlen = strnlen(directry,PATH_MAX);
- filelen = strnlen(filename,PATH_MAX);
- if ((dirlen + filelen + 10 + 1) > PATH_MAX || (dirlen == 0) || (filelen == 0)) { //file suffix = max 10 byte, / = max 1 byte
+ // Create file path
+ dirlen = strnlen(directry, PATH_MAX);
+ filelen = strnlen(filename, PATH_MAX);
+ if ((dirlen + filelen + 10 + 1) > PATH_MAX || (dirlen == 0) ||
+ (filelen == 0)) { // file suffix = max 10 byte, / = max 1 byte
// Path error
free(hndl);
return REFOP_ARGERROR;
}
// string length was checked, safe.
- (void)strncpy(hndl->latestfile, directry, PATH_MAX);
- if (hndl->latestfile[dirlen-1] != '/')
- (void)strcat(hndl->latestfile, "/");
+ (void) strncpy(hndl->latestfile, directry, PATH_MAX);
+ if (hndl->latestfile[dirlen - 1] != '/')
+ (void) strcat(hndl->latestfile, "/");
- (void)strncpy(hndl->basedir, hndl->latestfile, PATH_MAX);
+ (void) strncpy(hndl->basedir, hndl->latestfile, PATH_MAX);
- (void)strcat(hndl->latestfile, filename);
+ (void) strcat(hndl->latestfile, filename);
- (void)strncpy(hndl->backupfile1, hndl->latestfile, PATH_MAX);
- (void)strcat(hndl->backupfile1, c_bk1_suffix);
+ (void) strncpy(hndl->backupfile1, hndl->latestfile, PATH_MAX);
+ (void) strcat(hndl->backupfile1, c_bk1_suffix);
- (void)strncpy(hndl->newfile, hndl->latestfile, PATH_MAX);
- (void)strcat(hndl->newfile, c_new_suffix);
+ (void) strncpy(hndl->newfile, hndl->latestfile, PATH_MAX);
+ (void) strcat(hndl->newfile, c_new_suffix);
(*handle) = hndl;
@@ -100,7 +101,7 @@ refop_error_t refop_release_redundancy_handle(refop_handle_t handle)
{
if (handle == NULL)
return REFOP_ARGERROR;
-
+
free(handle);
return REFOP_SUCCESS;
@@ -120,7 +121,7 @@ refop_error_t refop_release_redundancy_handle(refop_handle_t handle)
*/
refop_error_t refop_set_redundancy_data(refop_handle_t handle, uint8_t *data, int64_t datasize)
{
- struct refop_halndle *hndl = (struct refop_halndle *)handle;
+ struct refop_halndle *hndl = (struct refop_halndle *) handle;
int ret = -1;
if (handle == NULL || data == NULL || datasize < 0)
@@ -136,7 +137,7 @@ refop_error_t refop_set_redundancy_data(refop_handle_t handle, uint8_t *data, in
ret = refop_file_rotation(handle);
if (ret < 0) {
- (void)unlink(hndl->newfile);
+ (void) unlink(hndl->newfile);
return REFOP_SYSERROR;
}
@@ -178,7 +179,7 @@ refop_error_t refop_get_redundancy_data(refop_handle_t handle, uint8_t *data, in
result = REFOP_BROAKEN;
else
result = REFOP_SYSERROR;
-
+
return result;
}
@@ -194,7 +195,7 @@ refop_error_t refop_get_redundancy_data(refop_handle_t handle, uint8_t *data, in
*/
refop_error_t refop_remove_redundancy_data(refop_handle_t handle)
{
- struct refop_halndle *hndl = (struct refop_halndle *)handle;
+ struct refop_halndle *hndl = (struct refop_halndle *) handle;
refop_error_t errorret = REFOP_SUCCESS;
int ret = -1;