From c2e52af1e2cab956bfaa6088f80d3e6c6bf20f70 Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 10 Jul 2024 11:33:56 +1000 Subject: [PATCH] AP_Filesystem: make fgets() much more efficient normally fgets is on a buffered FILE handle. For AP_Filesystem we use an unbuffered file descriptor. This means we were reading one byte at a time from the file this uses lseek to make fgets() much more efficient by reading the max buffer size at a time in the file --- libraries/AP_Filesystem/AP_Filesystem.cpp | 26 +++++++++++++++++------ 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/libraries/AP_Filesystem/AP_Filesystem.cpp b/libraries/AP_Filesystem/AP_Filesystem.cpp index c780c5a9b1663..09bfcd05a7ad4 100644 --- a/libraries/AP_Filesystem/AP_Filesystem.cpp +++ b/libraries/AP_Filesystem/AP_Filesystem.cpp @@ -321,19 +321,31 @@ bool AP_Filesystem::fgets(char *buf, uint8_t buflen, int fd) { const Backend &backend = backend_by_fd(fd); + // we will need to seek back to the right location at the end + auto offset_start = backend.fs.lseek(fd, 0, SEEK_CUR); + if (offset_start < 0) { + return false; + } + + auto n = backend.fs.read(fd, buf, buflen); + if (n <= 0) { + return false; + } + uint8_t i = 0; - for (; i