From 84a7b3d92def54fb8138fea9e2997f98f06cc4c9 Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 26 Nov 2021 10:26:14 +0100 Subject: [PATCH 1/2] littlefs: add overrides for Stream::send --- cores/esp8266/FS.h | 12 ++++++++++ .../LittleFS/examples/SpeedTest/SpeedTest.ino | 11 +++++++++ libraries/LittleFS/src/LittleFS.h | 23 ++++++++++++++++++- 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/cores/esp8266/FS.h b/cores/esp8266/FS.h index f8d68d56ba..7099321077 100644 --- a/cores/esp8266/FS.h +++ b/cores/esp8266/FS.h @@ -118,6 +118,18 @@ class File : public Stream time_t getCreationTime(); void setTimeCallback(time_t (*cb)(void)); + // Stream::send configuration + + bool inputCanTimeout () override { + // unavailable data can't become later available + return false; + } + + bool outputCanTimeout () override { + // free space for write can't increase later + return false; + } + protected: FileImplPtr _p; time_t (*_timeCallback)(void) = nullptr; diff --git a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino index 483494b471..35e0ac66e6 100644 --- a/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino +++ b/libraries/LittleFS/examples/SpeedTest/SpeedTest.ino @@ -132,6 +132,16 @@ void DoTest(FS *fs) { f.close(); stop = millis(); Serial.printf("==> Time to read 64KB in 1b chunks = %lu milliseconds = %s\n", stop - start, rate(start, stop, 65536)); + + + start = millis(); + auto dest = fs->open("/test1bw.bin", "w"); + f = fs->open("/test1b.bin", "r"); + auto copysize = f.sendAll(dest); + dest.close(); + stop = millis(); + Serial.printf("==> Time to copy %d = %zd bytes = %lu milliseconds = %s\n", f.size(), copysize, stop - start, rate(start, stop, f.size())); + f.close(); } void setup() { @@ -139,6 +149,7 @@ void setup() { Serial.printf("Beginning test\n"); Serial.flush(); DoTest(&TESTFS); + Serial.println("done"); } void loop() { diff --git a/libraries/LittleFS/src/LittleFS.h b/libraries/LittleFS/src/LittleFS.h index 4fea86bddf..e0813b1068 100644 --- a/libraries/LittleFS/src/LittleFS.h +++ b/libraries/LittleFS/src/LittleFS.h @@ -241,7 +241,7 @@ class LittleFSImpl : public FSImpl DEBUGV("lfs_format, lfs_setattr 't': rc=%d\n", rc); return false; } - + lfs_unmount(&_lfs); _mounted = false; } @@ -372,6 +372,27 @@ class LittleFSFileImpl : public FileImpl } } + int availableForWrite () override { + const auto f = _getFD(); + const auto fs = _fs->getFS(); + + // check for remaining size in current block + // ignore inline feature + // (per code in lfs_file_rawwrite()) + auto afw = fs->cfg->block_size - f->off; + + if (afw == 0) { + // current block is full + // check for filesystem full per code in lfs_alloc() + if (!(fs->free.i == fs->free.size && fs->free.ack == 0)) { + // fs is not full, return a full sector as free space + afw = fs->cfg->block_size; + } + } + + return afw; + } + size_t write(const uint8_t *buf, size_t size) override { if (!_opened || !_fd || !buf) { return 0; From 99ae1eeec279c8bbe1ff547e58a5eb500fc14a8d Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 26 Nov 2021 10:34:25 +0100 Subject: [PATCH 2/2] +guards --- libraries/LittleFS/src/LittleFS.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libraries/LittleFS/src/LittleFS.h b/libraries/LittleFS/src/LittleFS.h index e0813b1068..a3afbfe77f 100644 --- a/libraries/LittleFS/src/LittleFS.h +++ b/libraries/LittleFS/src/LittleFS.h @@ -373,17 +373,20 @@ class LittleFSFileImpl : public FileImpl } int availableForWrite () override { + if (!_opened || !_fd) { + return 0; + } + const auto f = _getFD(); const auto fs = _fs->getFS(); // check for remaining size in current block - // ignore inline feature - // (per code in lfs_file_rawwrite()) + // ignore inline feature (per code in lfs_file_rawwrite()) auto afw = fs->cfg->block_size - f->off; if (afw == 0) { // current block is full - // check for filesystem full per code in lfs_alloc() + // check for filesystem full (per code in lfs_alloc()) if (!(fs->free.i == fs->free.size && fs->free.ack == 0)) { // fs is not full, return a full sector as free space afw = fs->cfg->block_size;