Skip to content

Commit 0a46e4e

Browse files
Add availableForWrite handler
1 parent 94094c0 commit 0a46e4e

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/FatLib/FatFile.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -1495,5 +1495,26 @@ size_t FatFile::write(const void* buf, size_t nbyte) {
14951495
return -1;
14961496
}
14971497

1498+
//-------------------------------------------------------
1499+
// EFP3 - Match upstream Arduino hack
1500+
int FatFile::availableSpaceForWrite() {
1501+
// error if not a normal file or is read-only
1502+
if (!isWritable() || m_vol->m_blockDev->isBusy()) {
1503+
return 0;
1504+
}
1505+
// seek to end of file if append flag
1506+
if ((m_flags & FILE_FLAG_APPEND)) {
1507+
if (!seekSet(m_fileSize)) {
1508+
return 0;
1509+
}
1510+
}
1511+
uint8_t sectorOfCluster = m_vol->sectorOfCluster(m_curPosition);
1512+
uint16_t sectorOffset = m_curPosition & m_vol->sectorMask();
1513+
if (sectorOfCluster == 0 && sectorOffset == 0) {
1514+
return 0;
1515+
}
1516+
return m_vol->bytesPerSector() - sectorOffset - 1;
1517+
}
1518+
//-------------------------------------------------------
14981519

14991520
}; // namespace sdfat

src/FatLib/FatFile.h

+2
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,8 @@ class FatFile {
937937
*
938938
*/
939939
size_t write(const void* buf, size_t count);
940+
941+
int availableSpaceForWrite();
940942
//------------------------------------------------------------------------------
941943
#if ENABLE_ARDUINO_SERIAL
942944
/** List directory contents.

0 commit comments

Comments
 (0)