From e976c6592ce9c93dd4a91de916bcef53534326d6 Mon Sep 17 00:00:00 2001 From: Henry Gabryjelski Date: Fri, 20 Dec 2019 00:42:52 -0800 Subject: [PATCH] Serialize at the LFS layer --- .../src/Adafruit_LittleFS.cpp | 72 +++++++++++++++++++ .../Adafruit_LittleFS/src/Adafruit_LittleFS.h | 22 +++++- 2 files changed, 93 insertions(+), 1 deletion(-) diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp index 0c24f4b2..8eed7e8e 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.cpp @@ -44,6 +44,7 @@ Adafruit_LittleFS::Adafruit_LittleFS (struct lfs_config* cfg) varclr(&_lfs); _lfs_cfg = cfg; _mounted = false; + _mutex = xSemaphoreCreateMutexStatic(&this->xMutexStorageSpace); } Adafruit_LittleFS::~Adafruit_LittleFS () @@ -55,6 +56,14 @@ Adafruit_LittleFS::~Adafruit_LittleFS () // Return true if mounted successfully else probably corrupted. // User should format the disk and try again bool Adafruit_LittleFS::begin (struct lfs_config * cfg) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + bool retval = this->xWrap_begin(cfg); + xSemaphoreGive(_mutex); + return retval; +} + +bool Adafruit_LittleFS::xWrap_begin (struct lfs_config * cfg) { if ( _mounted ) return true; @@ -69,6 +78,13 @@ bool Adafruit_LittleFS::begin (struct lfs_config * cfg) // Tear down and unmount file system void Adafruit_LittleFS::end(void) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + this->xWrap_end(); + xSemaphoreGive(_mutex); +} + +void Adafruit_LittleFS::xWrap_end(void) { if (!_mounted) return; @@ -77,6 +93,14 @@ void Adafruit_LittleFS::end(void) } bool Adafruit_LittleFS::format (void) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + bool retval = this->xWrap_format(); + xSemaphoreGive(_mutex); + return retval; +} + +bool Adafruit_LittleFS::xWrap_format (void) { // if already mounted: umount -> format -> remount if(_mounted) VERIFY_LFS(lfs_unmount(&_lfs), false); @@ -90,12 +114,28 @@ bool Adafruit_LittleFS::format (void) // Open a file or folder Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + Adafruit_LittleFS_Namespace::File retval = this->xWrap_open(filepath, mode); + xSemaphoreGive(_mutex); + return retval; +} + +Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::xWrap_open (char const *filepath, uint8_t mode) { return Adafruit_LittleFS_Namespace::File(filepath, mode, *this); } // Check if file or folder exists bool Adafruit_LittleFS::exists (char const *filepath) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + bool retval = this->xWrap_exists(filepath); + xSemaphoreGive(_mutex); + return retval; +} + +bool Adafruit_LittleFS::xWrap_exists (char const *filepath) { struct lfs_info info; return 0 == lfs_stat(&_lfs, filepath, &info); @@ -103,6 +143,14 @@ bool Adafruit_LittleFS::exists (char const *filepath) // Create a directory, create intermediate parent if needed bool Adafruit_LittleFS::mkdir (char const *filepath) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + bool retval = this->xWrap_mkdir(filepath); + xSemaphoreGive(_mutex); + return retval; +} + +bool Adafruit_LittleFS::xWrap_mkdir (char const *filepath) { const char* slash = filepath; if ( slash[0] == '/' ) slash++; // skip root '/' @@ -135,6 +183,14 @@ bool Adafruit_LittleFS::mkdir (char const *filepath) // Remove a file bool Adafruit_LittleFS::remove (char const *filepath) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + bool retval = this->xWrap_remove(filepath); + xSemaphoreGive(_mutex); + return retval; +} + +bool Adafruit_LittleFS::xWrap_remove (char const *filepath) { VERIFY_LFS(lfs_remove(&_lfs, filepath), false); return true; @@ -142,6 +198,14 @@ bool Adafruit_LittleFS::remove (char const *filepath) // Remove a folder bool Adafruit_LittleFS::rmdir (char const *filepath) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + bool retval = this->xWrap_rmdir(filepath); + xSemaphoreGive(_mutex); + return retval; +} + +bool Adafruit_LittleFS::xWrap_rmdir (char const *filepath) { VERIFY_LFS(lfs_remove(&_lfs, filepath)); return true; @@ -149,6 +213,14 @@ bool Adafruit_LittleFS::rmdir (char const *filepath) // Remove a folder recursively bool Adafruit_LittleFS::rmdir_r (char const *filepath) +{ + while (pdTRUE != xSemaphoreTake(_mutex, portMAX_DELAY)) {} + bool retval = this->xWrap_rmdir_r(filepath); + xSemaphoreGive(_mutex); + return retval; +} + +bool Adafruit_LittleFS::xWrap_rmdir_r (char const *filepath) { /* adafruit: lfs is modified to remove non-empty folder, According to below issue, comment these 2 line won't corrupt filesystem diff --git a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h index 4514646a..b63b9065 100644 --- a/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h +++ b/libraries/Adafruit_LittleFS/src/Adafruit_LittleFS.h @@ -30,8 +30,8 @@ // Internal Flash uses ARM Little FileSystem // https://github.com/ARMmbed/littlefs #include "littlefs/lfs.h" - #include "Adafruit_LittleFS_File.h" +#include "rtos.h" // tied to FreeRTOS for serialization class Adafruit_LittleFS { @@ -76,6 +76,26 @@ class Adafruit_LittleFS bool _mounted; struct lfs_config* _lfs_cfg; lfs_t _lfs; + + //static_assert(configSUPPORT_STATIC_ALLOCATION == 1, "Currently only supports configuration with STATIC_ALLOCATION enabled"); + SemaphoreHandle_t _mutex; + + private: + StaticSemaphore_t xMutexStorageSpace; + + // these wrapped functions are needed to simplify + // change to serialize access via a mutex, at least in part + // because the VERIFY_LFS() macro includes a return statement, + // which would otherwise exit the functions without releasing the mutex. + bool xWrap_begin(struct lfs_config * cfg = NULL); + void xWrap_end(void); + Adafruit_LittleFS_Namespace::File xWrap_open (char const *filename, uint8_t mode = Adafruit_LittleFS_Namespace::FILE_O_READ); + bool xWrap_exists (char const *filepath); + bool xWrap_mkdir (char const *filepath); + bool xWrap_remove (char const *filepath); + bool xWrap_rmdir (char const *filepath); + bool xWrap_rmdir_r (char const *filepath); + bool xWrap_format (void); }; #if !CFG_DEBUG