IOT LCD Extra Credit
Fork of FATFileSystem by
Diff: FATFileHandle.cpp
- Revision:
- 8:c4baca9a2c3d
- Parent:
- 4:3ff2606d5713
diff -r 094f84646b9f -r c4baca9a2c3d FATFileHandle.cpp --- a/FATFileHandle.cpp Fri Dec 11 16:16:25 2015 +0000 +++ b/FATFileHandle.cpp Thu Aug 25 09:34:29 2016 +0100 @@ -25,34 +25,42 @@ #include "FATFileHandle.h" -FATFileHandle::FATFileHandle(FIL fh) { +FATFileHandle::FATFileHandle(FIL fh, PlatformMutex * mutex): _mutex(mutex) { _fh = fh; } int FATFileHandle::close() { + lock(); int retval = f_close(&_fh); + unlock(); delete this; return retval; } ssize_t FATFileHandle::write(const void* buffer, size_t length) { + lock(); UINT n; FRESULT res = f_write(&_fh, buffer, length, &n); if (res) { debug_if(FFS_DBG, "f_write() failed: %d", res); + unlock(); return -1; } + unlock(); return n; } ssize_t FATFileHandle::read(void* buffer, size_t length) { + lock(); debug_if(FFS_DBG, "read(%d)\n", length); UINT n; FRESULT res = f_read(&_fh, buffer, length, &n); if (res) { debug_if(FFS_DBG, "f_read() failed: %d\n", res); + unlock(); return -1; } + unlock(); return n; } @@ -61,6 +69,7 @@ } off_t FATFileHandle::lseek(off_t position, int whence) { + lock(); if (whence == SEEK_END) { position += _fh.fsize; } else if(whence==SEEK_CUR) { @@ -69,22 +78,38 @@ FRESULT res = f_lseek(&_fh, position); if (res) { debug_if(FFS_DBG, "lseek failed: %d\n", res); + unlock(); return -1; } else { debug_if(FFS_DBG, "lseek OK, returning %i\n", _fh.fptr); + unlock(); return _fh.fptr; } } int FATFileHandle::fsync() { + lock(); FRESULT res = f_sync(&_fh); if (res) { debug_if(FFS_DBG, "f_sync() failed: %d\n", res); + unlock(); return -1; } + unlock(); return 0; } off_t FATFileHandle::flen() { - return _fh.fsize; + lock(); + off_t size = _fh.fsize; + unlock(); + return size; } + +void FATFileHandle::lock() { + _mutex->lock(); +} + +void FATFileHandle::unlock() { + _mutex->unlock(); +}