Skip to content

Commit

Permalink
LittleFS: add overrides for Stream::send (#8386)
Browse files Browse the repository at this point in the history
* littlefs: add overrides for Stream::send
  • Loading branch information
d-a-v authored Nov 29, 2021
1 parent d5444c4 commit 2492057
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
12 changes: 12 additions & 0 deletions cores/esp8266/FS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions libraries/LittleFS/examples/SpeedTest/SpeedTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,24 @@ 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() {
Serial.begin(115200);
Serial.printf("Beginning test\n");
Serial.flush();
DoTest(&TESTFS);
Serial.println("done");
}

void loop() {
Expand Down
26 changes: 25 additions & 1 deletion libraries/LittleFS/src/LittleFS.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -372,6 +372,30 @@ 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())
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;
Expand Down

0 comments on commit 2492057

Please sign in to comment.