add _lockFS()/_unlockFS() to Adafruit_LittleFS
This commit is contained in:
1 parent
d643515b0a
commit
f8cfa3d727
4 files changed
+92
-89
No files matched your search
@@ -57,7 +57,8 @@ Adafruit_LittleFS::~Adafruit_LittleFS ()
|
||||
// User should format the disk and try again
|
||||
bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
|
||||
{
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
bool ret;
|
||||
// not a loop, just an quick way to short-circuit on error
|
||||
do {
|
||||
@@ -70,14 +71,16 @@ bool Adafruit_LittleFS::begin (struct lfs_config * cfg)
|
||||
_mounted = (err == LFS_ERR_OK);
|
||||
ret = _mounted;
|
||||
} while(0);
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Tear down and unmount file system
|
||||
void Adafruit_LittleFS::end(void)
|
||||
{
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
if (_mounted)
|
||||
{
|
||||
_mounted = false;
|
||||
@@ -85,12 +88,14 @@ void Adafruit_LittleFS::end(void)
|
||||
PRINT_LFS_ERR(err);
|
||||
(void)err;
|
||||
}
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
}
|
||||
|
||||
bool Adafruit_LittleFS::format (void)
|
||||
{
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
int err = LFS_ERR_OK;
|
||||
bool attemptMount = _mounted;
|
||||
// not a loop, just an quick way to short-circuit on error
|
||||
@@ -114,7 +119,8 @@ bool Adafruit_LittleFS::format (void)
|
||||
}
|
||||
// success!
|
||||
} while(0);
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
@@ -129,9 +135,11 @@ Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath,
|
||||
bool Adafruit_LittleFS::exists (char const *filepath)
|
||||
{
|
||||
struct lfs_info info;
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
bool ret = (0 == lfs_stat(&_lfs, filepath, &info));
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -143,7 +151,7 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
|
||||
const char* slash = filepath;
|
||||
if ( slash[0] == '/' ) slash++; // skip root '/'
|
||||
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
// make intermediate parent directory(ies)
|
||||
while ( NULL != (slash = strchr(slash, '/')) )
|
||||
@@ -170,27 +178,32 @@ bool Adafruit_LittleFS::mkdir (char const *filepath)
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
// Remove a file
|
||||
bool Adafruit_LittleFS::remove (char const *filepath)
|
||||
{
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
int err = lfs_remove(&_lfs, filepath);
|
||||
PRINT_LFS_ERR(err);
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
// Remove a folder
|
||||
bool Adafruit_LittleFS::rmdir (char const *filepath)
|
||||
{
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
int err = lfs_remove(&_lfs, filepath);
|
||||
PRINT_LFS_ERR(err);
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
@@ -203,10 +216,12 @@ bool Adafruit_LittleFS::rmdir_r (char const *filepath)
|
||||
to see if issues (such as the orphans in threaded linked list) are resolved.
|
||||
https://github.com/ARMmbed/littlefs/issues/43
|
||||
*/
|
||||
xSemaphoreTake(_mutex, portMAX_DELAY);
|
||||
_lockFS();
|
||||
|
||||
int err = lfs_remove(&_lfs, filepath);
|
||||
PRINT_LFS_ERR(err);
|
||||
xSemaphoreGive(_mutex);
|
||||
|
||||
_unlockFS();
|
||||
return LFS_ERR_OK == err;
|
||||
}
|
||||
|
||||
|
||||
@@ -67,20 +67,19 @@ class Adafruit_LittleFS
|
||||
// format file system
|
||||
bool format (void);
|
||||
|
||||
lfs_t* getFS(void)
|
||||
{
|
||||
return &_lfs;
|
||||
}
|
||||
/*------------------------------------------------------------------*/
|
||||
/* INTERNAL USAGE ONLY
|
||||
* Although declare as public, it is meant to be invoked by internal
|
||||
* code. User should not call these directly
|
||||
*------------------------------------------------------------------*/
|
||||
lfs_t* _getFS (void) { return &_lfs; }
|
||||
void _lockFS (void) { xSemaphoreTake(_mutex, portMAX_DELAY); }
|
||||
void _unlockFS(void) { xSemaphoreGive(_mutex); }
|
||||
|
||||
protected:
|
||||
bool _mounted;
|
||||
struct lfs_config* _lfs_cfg;
|
||||
lfs_t _lfs;
|
||||
|
||||
// these two functions need access to the private _mutex variable:
|
||||
friend void Adafruit_LittleFS_Namespace::File::_LockFilesystem(void);
|
||||
friend void Adafruit_LittleFS_Namespace::File::_UnlockFilesystem(void);
|
||||
//static_assert(configSUPPORT_STATIC_ALLOCATION == 1, "Currently only supports configuration with STATIC_ALLOCATION enabled");
|
||||
SemaphoreHandle_t _mutex;
|
||||
|
||||
private:
|
||||
|
||||
@@ -60,7 +60,7 @@ bool File::_open_file (char const *filepath, uint8_t mode)
|
||||
_file = (lfs_file_t*) rtos_malloc(sizeof(lfs_file_t));
|
||||
if (!_file) return false;
|
||||
|
||||
int rc = lfs_file_open(_fs->getFS(), _file, filepath, flags);
|
||||
int rc = lfs_file_open(_fs->_getFS(), _file, filepath, flags);
|
||||
|
||||
if ( rc )
|
||||
{
|
||||
@@ -70,7 +70,7 @@ bool File::_open_file (char const *filepath, uint8_t mode)
|
||||
}
|
||||
|
||||
// move to end of file
|
||||
if ( mode == FILE_O_WRITE ) lfs_file_seek(_fs->getFS(), _file, 0, LFS_SEEK_END);
|
||||
if ( mode == FILE_O_WRITE ) lfs_file_seek(_fs->_getFS(), _file, 0, LFS_SEEK_END);
|
||||
|
||||
_is_dir = false;
|
||||
}
|
||||
@@ -83,7 +83,7 @@ bool File::_open_dir (char const *filepath)
|
||||
_dir = (lfs_dir_t*) rtos_malloc(sizeof(lfs_dir_t));
|
||||
if (!_dir) return false;
|
||||
|
||||
int rc = lfs_dir_open(_fs->getFS(), _dir, filepath);
|
||||
int rc = lfs_dir_open(_fs->_getFS(), _dir, filepath);
|
||||
|
||||
if ( rc )
|
||||
{
|
||||
@@ -103,11 +103,12 @@ bool File::_open_dir (char const *filepath)
|
||||
bool File::open (char const *filepath, uint8_t mode)
|
||||
{
|
||||
bool ret = false;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
ret = this->_open(filepath, mode);
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool File::_open (char const *filepath, uint8_t mode)
|
||||
{
|
||||
bool ret = false;
|
||||
@@ -116,7 +117,7 @@ bool File::_open (char const *filepath, uint8_t mode)
|
||||
if ( this->_isOpen() ) _close();
|
||||
|
||||
struct lfs_info info;
|
||||
int rc = lfs_stat(_fs->getFS(), filepath, &info);
|
||||
int rc = lfs_stat(_fs->_getFS(), filepath, &info);
|
||||
|
||||
if ( LFS_ERR_OK == rc )
|
||||
{
|
||||
@@ -150,16 +151,16 @@ size_t File::write (uint8_t ch)
|
||||
size_t File::write (uint8_t const *buf, size_t size)
|
||||
{
|
||||
lfs_ssize_t wrcount = 0;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
wrcount = lfs_file_write(_fs->getFS(), _file, buf, size);
|
||||
wrcount = lfs_file_write(_fs->_getFS(), _file, buf, size);
|
||||
if (wrcount < 0)
|
||||
{
|
||||
wrcount = 0;
|
||||
};
|
||||
}
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return wrcount;
|
||||
}
|
||||
|
||||
@@ -178,108 +179,109 @@ int File::read (void)
|
||||
int File::read (void *buf, uint16_t nbyte)
|
||||
{
|
||||
int ret = 0;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_read(_fs->getFS(), _file, buf, nbyte);
|
||||
ret = lfs_file_read(_fs->_getFS(), _file, buf, nbyte);
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int File::peek (void)
|
||||
{
|
||||
int ret = -1;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
uint32_t pos = lfs_file_tell(_fs->getFS(), _file);
|
||||
uint32_t pos = lfs_file_tell(_fs->_getFS(), _file);
|
||||
uint8_t ch = 0;
|
||||
if (lfs_file_read(_fs->getFS(), _file, &ch, 1) > 0)
|
||||
if (lfs_file_read(_fs->_getFS(), _file, &ch, 1) > 0)
|
||||
{
|
||||
ret = static_cast<int>(ch);
|
||||
}
|
||||
(void)lfs_file_seek(_fs->getFS(), _file, pos, LFS_SEEK_SET);
|
||||
(void)lfs_file_seek(_fs->_getFS(), _file, pos, LFS_SEEK_SET);
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
int File::available (void)
|
||||
{
|
||||
int ret = 0;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
uint32_t size = lfs_file_size(_fs->getFS(), _file);
|
||||
uint32_t pos = lfs_file_tell(_fs->getFS(), _file);
|
||||
uint32_t size = lfs_file_size(_fs->_getFS(), _file);
|
||||
uint32_t pos = lfs_file_tell(_fs->_getFS(), _file);
|
||||
ret = size - pos;
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool File::seek (uint32_t pos)
|
||||
{
|
||||
bool ret = false;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_seek(_fs->getFS(), _file, pos, LFS_SEEK_SET) >= 0;
|
||||
ret = lfs_file_seek(_fs->_getFS(), _file, pos, LFS_SEEK_SET) >= 0;
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t File::position (void)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_tell(_fs->getFS(), _file);
|
||||
ret = lfs_file_tell(_fs->_getFS(), _file);
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint32_t File::size (void)
|
||||
{
|
||||
uint32_t ret = 0;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
ret = lfs_file_size(_fs->getFS(), _file);
|
||||
ret = lfs_file_size(_fs->_getFS(), _file);
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
void File::flush (void)
|
||||
{
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (!this->_is_dir)
|
||||
{
|
||||
lfs_file_sync(_fs->getFS(), _file);
|
||||
lfs_file_sync(_fs->_getFS(), _file);
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return;
|
||||
}
|
||||
|
||||
void File::close (void)
|
||||
{
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
this->_close();
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
}
|
||||
|
||||
void File::_close(void)
|
||||
{
|
||||
if ( this->_isOpen() )
|
||||
{
|
||||
if ( this->_is_dir )
|
||||
{
|
||||
lfs_dir_close(_fs->getFS(), _dir);
|
||||
lfs_dir_close(_fs->_getFS(), _dir);
|
||||
rtos_free(_dir);
|
||||
_dir = NULL;
|
||||
|
||||
@@ -288,7 +290,7 @@ void File::_close(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
lfs_file_close(this->_fs->getFS(), _file);
|
||||
lfs_file_close(this->_fs->_getFS(), _file);
|
||||
rtos_free(_file);
|
||||
_file = NULL;
|
||||
}
|
||||
@@ -299,12 +301,13 @@ File::operator bool (void)
|
||||
{
|
||||
return isOpen();
|
||||
}
|
||||
|
||||
bool File::isOpen(void)
|
||||
{
|
||||
bool ret = 0;
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
ret = this->_isOpen();
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
bool File::_isOpen(void)
|
||||
@@ -319,24 +322,24 @@ bool File::_isOpen(void)
|
||||
// suddenly (unexpectedly?) have different values.
|
||||
char const* File::name (void)
|
||||
{
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
char const* ret = this->_name;
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
bool File::isDirectory (void)
|
||||
{
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
bool ret = this->_is_dir;
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
|
||||
}
|
||||
|
||||
File File::openNextFile (uint8_t mode)
|
||||
{
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
|
||||
File ret(*_fs);
|
||||
if (this->_is_dir)
|
||||
@@ -348,7 +351,7 @@ File File::openNextFile (uint8_t mode)
|
||||
// Skip the "." and ".." entries ...
|
||||
do
|
||||
{
|
||||
rc = lfs_dir_read(_fs->getFS(), _dir, &info);
|
||||
rc = lfs_dir_read(_fs->_getFS(), _dir, &info);
|
||||
} while ( rc == 1 && (!strcmp(".", info.name) || !strcmp("..", info.name)) );
|
||||
|
||||
if ( rc == 1 )
|
||||
@@ -366,28 +369,18 @@ File File::openNextFile (uint8_t mode)
|
||||
PRINT_LFS_ERR(rc);
|
||||
}
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return ret;
|
||||
}
|
||||
|
||||
void File::rewindDirectory (void)
|
||||
{
|
||||
this->_LockFilesystem();
|
||||
_fs->_lockFS();
|
||||
if (this->_is_dir)
|
||||
{
|
||||
lfs_dir_rewind(_fs->getFS(), _dir);
|
||||
lfs_dir_rewind(_fs->_getFS(), _dir);
|
||||
}
|
||||
this->_UnlockFilesystem();
|
||||
_fs->_unlockFS();
|
||||
return;
|
||||
}
|
||||
|
||||
void File::_LockFilesystem(void)
|
||||
{
|
||||
xSemaphoreTake(this->_fs->_mutex, portMAX_DELAY);
|
||||
}
|
||||
void File::_UnlockFilesystem(void)
|
||||
{
|
||||
xSemaphoreGive(this->_fs->_mutex);
|
||||
}
|
||||
|
||||
|
||||
@@ -81,10 +81,6 @@ class File : public Stream
|
||||
File openNextFile (uint8_t mode = FILE_O_READ);
|
||||
void rewindDirectory (void);
|
||||
|
||||
// Has to be public, in order to grant 'friend' permissions to Adafruit_LittleFS private member _mutex
|
||||
void _LockFilesystem(void);
|
||||
void _UnlockFilesystem(void);
|
||||
|
||||
private:
|
||||
Adafruit_LittleFS* _fs;
|
||||
|
||||
|
||||
Reference in new issue
Block a user