From 08b0ac9654d0041a78c61025a4114a59b0045f4d Mon Sep 17 00:00:00 2001 From: Daniel Agar Date: Tue, 7 Dec 2021 12:59:35 -0500 Subject: [PATCH] dataman: retry file read/write failures and report seek errors --- src/modules/dataman/dataman.cpp | 65 +++++++++++++++++++++++++++++---- 1 file changed, 57 insertions(+), 8 deletions(-) diff --git a/src/modules/dataman/dataman.cpp b/src/modules/dataman/dataman.cpp index 4251d867e6..82d9041cd6 100644 --- a/src/modules/dataman/dataman.cpp +++ b/src/modules/dataman/dataman.cpp @@ -470,11 +470,39 @@ _file_write(dm_item_t item, unsigned index, dm_persitence_t persistence, const v count += DM_SECTOR_HDR_SIZE; - if (lseek(dm_operations_data.file.fd, offset, SEEK_SET) != offset) { - return -1; + bool write_success = false; + + for (int i = 0; i < 2; i++) { + int ret_seek = lseek(dm_operations_data.file.fd, offset, SEEK_SET); + + if (ret_seek < 0) { + PX4_ERR("file write lseek failed %d", errno); + continue; + } + + if (ret_seek != offset) { + PX4_ERR("file write lseek failed, incorrect offset %d vs %d", ret_seek, offset); + continue; + } + + int ret_write = write(dm_operations_data.file.fd, buffer, count); + + if (ret_write < 0) { + PX4_ERR("file write failed %d", errno); + continue; + } + + if (ret_write != (ssize_t)count) { + PX4_ERR("file write failed, wrote %d bytes, expected %zu", ret_write, count); + continue; + + } else { + write_success = true; + break; + } } - if ((write(dm_operations_data.file.fd, buffer, count)) != (ssize_t)count) { + if (!write_success) { return -1; } @@ -547,16 +575,37 @@ _file_read(dm_item_t item, unsigned index, void *buf, size_t count) return -E2BIG; } - /* Read the prefix and data */ int len = -1; + bool read_success = false; - if (lseek(dm_operations_data.file.fd, offset, SEEK_SET) == offset) { + for (int i = 0; i < 2; i++) { + int ret_seek = lseek(dm_operations_data.file.fd, offset, SEEK_SET); + + if (ret_seek < 0) { + PX4_ERR("file read lseek failed %d", errno); + continue; + } + + if (ret_seek != offset) { + PX4_ERR("file read lseek failed, incorrect offset %d vs %d", ret_seek, offset); + continue; + } + + /* Read the prefix and data */ len = read(dm_operations_data.file.fd, buffer, count + DM_SECTOR_HDR_SIZE); + + /* Check for read error */ + if (len >= 0) { + read_success = true; + break; + + } else { + PX4_ERR("file read failed %d", errno); + } } - /* Check for read error */ - if (len < 0) { - return -errno; + if (!read_success) { + return -1; } /* A zero length entry is a empty entry */