Skip to content

Commit be6feb0

Browse files
d-a-vhasenradball
authored andcommitted
LittleFS: add overrides for Stream::send (esp8266#8386)
* littlefs: add overrides for Stream::send
1 parent b915891 commit be6feb0

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

cores/esp8266/FS.h

+12
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,18 @@ class File : public Stream
118118
time_t getCreationTime();
119119
void setTimeCallback(time_t (*cb)(void));
120120

121+
// Stream::send configuration
122+
123+
bool inputCanTimeout () override {
124+
// unavailable data can't become later available
125+
return false;
126+
}
127+
128+
bool outputCanTimeout () override {
129+
// free space for write can't increase later
130+
return false;
131+
}
132+
121133
protected:
122134
FileImplPtr _p;
123135
time_t (*_timeCallback)(void) = nullptr;

libraries/LittleFS/examples/SpeedTest/SpeedTest.ino

+11
Original file line numberDiff line numberDiff line change
@@ -132,13 +132,24 @@ void DoTest(FS *fs) {
132132
f.close();
133133
stop = millis();
134134
Serial.printf("==> Time to read 64KB in 1b chunks = %lu milliseconds = %s\n", stop - start, rate(start, stop, 65536));
135+
136+
137+
start = millis();
138+
auto dest = fs->open("/test1bw.bin", "w");
139+
f = fs->open("/test1b.bin", "r");
140+
auto copysize = f.sendAll(dest);
141+
dest.close();
142+
stop = millis();
143+
Serial.printf("==> Time to copy %d = %zd bytes = %lu milliseconds = %s\n", f.size(), copysize, stop - start, rate(start, stop, f.size()));
144+
f.close();
135145
}
136146

137147
void setup() {
138148
Serial.begin(115200);
139149
Serial.printf("Beginning test\n");
140150
Serial.flush();
141151
DoTest(&TESTFS);
152+
Serial.println("done");
142153
}
143154

144155
void loop() {

libraries/LittleFS/src/LittleFS.h

+25-1
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class LittleFSImpl : public FSImpl
241241
DEBUGV("lfs_format, lfs_setattr 't': rc=%d\n", rc);
242242
return false;
243243
}
244-
244+
245245
lfs_unmount(&_lfs);
246246
_mounted = false;
247247
}
@@ -372,6 +372,30 @@ class LittleFSFileImpl : public FileImpl
372372
}
373373
}
374374

375+
int availableForWrite () override {
376+
if (!_opened || !_fd) {
377+
return 0;
378+
}
379+
380+
const auto f = _getFD();
381+
const auto fs = _fs->getFS();
382+
383+
// check for remaining size in current block
384+
// ignore inline feature (per code in lfs_file_rawwrite())
385+
auto afw = fs->cfg->block_size - f->off;
386+
387+
if (afw == 0) {
388+
// current block is full
389+
// check for filesystem full (per code in lfs_alloc())
390+
if (!(fs->free.i == fs->free.size && fs->free.ack == 0)) {
391+
// fs is not full, return a full sector as free space
392+
afw = fs->cfg->block_size;
393+
}
394+
}
395+
396+
return afw;
397+
}
398+
375399
size_t write(const uint8_t *buf, size_t size) override {
376400
if (!_opened || !_fd || !buf) {
377401
return 0;

0 commit comments

Comments
 (0)