From 309d4fc7f254136ce468ba7725b3b9681c811a8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20G=C3=B6ttgens?= Date: Fri, 14 Apr 2023 10:55:05 +0200 Subject: [PATCH] add flush to filesystem before closing write file. --- src/FSCommon.cpp | 1 + src/mesh/NodeDB.cpp | 1 + src/mesh/http/ContentHandler.cpp | 7 ++++++- src/modules/esp32/RangeTestModule.cpp | 3 ++- src/xmodem.cpp | 2 ++ 5 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/FSCommon.cpp b/src/FSCommon.cpp index 32e5d7e28..cc99caed8 100644 --- a/src/FSCommon.cpp +++ b/src/FSCommon.cpp @@ -34,6 +34,7 @@ bool copyFile(const char *from, const char *to) f2.write(cbuffer, i); } + f2.flush(); f2.close(); f1.close(); return true; diff --git a/src/mesh/NodeDB.cpp b/src/mesh/NodeDB.cpp index 02749feb8..7416743f0 100644 --- a/src/mesh/NodeDB.cpp +++ b/src/mesh/NodeDB.cpp @@ -490,6 +490,7 @@ bool NodeDB::saveProto(const char *filename, size_t protoSize, const pb_msgdesc_ } else { okay = true; } + f.flush(); f.close(); // brief window of risk here ;-) diff --git a/src/mesh/http/ContentHandler.cpp b/src/mesh/http/ContentHandler.cpp index 03aab54ae..eb8f8fd12 100644 --- a/src/mesh/http/ContentHandler.cpp +++ b/src/mesh/http/ContentHandler.cpp @@ -234,15 +234,18 @@ void htmlDeleteDir(const char *dirname) while (file) { if (file.isDirectory() && !String(file.name()).endsWith(".")) { htmlDeleteDir(file.name()); + file.flush(); file.close(); } else { String fileName = String(file.name()); + file.flush(); file.close(); LOG_DEBUG(" %s\n", fileName.c_str()); FSCom.remove(fileName); } file = root.openNextFile(); } + root.flush(); root.close(); } @@ -521,7 +524,7 @@ void handleFormUpload(HTTPRequest *req, HTTPResponse *res) std::string pathname = "/static/" + filename; // Create a new file to stream the data into - File file = FSCom.open(pathname.c_str(), "w"); + File file = FSCom.open(pathname.c_str(), FILE_O_WRITE); size_t fileLength = 0; didwrite = true; @@ -536,6 +539,7 @@ void handleFormUpload(HTTPRequest *req, HTTPResponse *res) // Abort the transfer if there is less than 50k space left on the filesystem. if (FSCom.totalBytes() - FSCom.usedBytes() < 51200) { + file.flush(); file.close(); res->println("

Write aborted! Reserving 50k on filesystem.

"); @@ -553,6 +557,7 @@ void handleFormUpload(HTTPRequest *req, HTTPResponse *res) } // enableLoopWDT(); + file.flush(); file.close(); res->printf("

Saved %d bytes to %s

", (int)fileLength, pathname.c_str()); } diff --git a/src/modules/esp32/RangeTestModule.cpp b/src/modules/esp32/RangeTestModule.cpp index 608e0868c..e8821f9a0 100644 --- a/src/modules/esp32/RangeTestModule.cpp +++ b/src/modules/esp32/RangeTestModule.cpp @@ -226,7 +226,7 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp) } else { LOG_ERROR("File write failed\n"); } - + fileToWrite.flush(); fileToWrite.close(); } @@ -275,6 +275,7 @@ bool RangeTestModuleRadio::appendFile(const meshtastic_MeshPacket &mp) // TODO: If quotes are found in the payload, it has to be escaped. fileToAppend.printf("\"%s\"\n", p.payload.bytes); + fileToAppend.flush(); fileToAppend.close(); return 1; diff --git a/src/xmodem.cpp b/src/xmodem.cpp index f8b5084dc..e8727c468 100644 --- a/src/xmodem.cpp +++ b/src/xmodem.cpp @@ -142,12 +142,14 @@ void XModemAdapter::handlePacket(meshtastic_XModem xmodemPacket) case meshtastic_XModem_Control_EOT: // End of transmission sendControl(meshtastic_XModem_Control_ACK); + file.flush(); file.close(); isReceiving = false; break; case meshtastic_XModem_Control_CAN: // Cancel transmission and remove file sendControl(meshtastic_XModem_Control_ACK); + file.flush(); file.close(); FSCom.remove(filename); isReceiving = false;