From f4f8bf97fb82dc6ea241718e65b4bc11fef85968 Mon Sep 17 00:00:00 2001 From: Laximas Date: Mon, 12 Feb 2024 16:37:44 +0200 Subject: [PATCH] dlt-receive: Synchronize file's in-core state with storage device Add fsync check to ensure consistency between console payload and DLT file Signed-off-by: Mike Konstantakos --- src/console/dlt-receive.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/console/dlt-receive.c b/src/console/dlt-receive.c index 4ffc304f9..b3a5c03bc 100644 --- a/src/console/dlt-receive.c +++ b/src/console/dlt-receive.c @@ -88,6 +88,7 @@ #include "dlt-control-common.h" #define DLT_RECEIVE_ECU_ID "RECV" +#define FSYNC_BYTE_THRESHOLD (1000 * 1000) DltClient dltclient; @@ -641,6 +642,7 @@ int dlt_receive_message_callback(DltMessage *message, void *data) { DltReceiveData *dltdata; static char text[DLT_RECEIVE_BUFSIZE]; + static uint32_t bytes_since_last_fsync = 0; struct iovec iov[2]; int bytes_written; @@ -711,11 +713,20 @@ int dlt_receive_message_callback(DltMessage *message, void *data) bytes_written = (int)writev(dltdata->ohandle, iov, 2); dltdata->totalbytes += bytes_written; + bytes_since_last_fsync += bytes_written; if (0 > bytes_written) { printf("dlt_receive_message_callback: writev(dltdata->ohandle, iov, 2); returned an error!"); return -1; } + else if (bytes_since_last_fsync >= FSYNC_BYTE_THRESHOLD) { + if (fsync(dltdata->ohandle) < 0) { + printf("dlt_receive_message_callback: fsync failed!"); + } + else { + bytes_since_last_fsync = 0; + } + } } }