Also serialize Adafruit_LittleFS_File.

This solution appears to work.  However, it does not
follow all best practices.  For example, the use of
friend declarations, and the need to make the lock/unlock
functions in File public (in order to get friend status).
However, it passes the stress test without errors.
This commit is contained in:
Henry Gabryjelski committed 2020-01-06 08:25:53 -08:00
1 parent 35b5d4b548
commit 39faf9f823
4 files changed
+212 -63

No files matched your search

@@ -121,10 +121,8 @@ bool Adafruit_LittleFS::format (void)
// Open a file or folder
Adafruit_LittleFS_Namespace::File Adafruit_LittleFS::open (char const *filepath, uint8_t mode)
{
xSemaphoreTake(_mutex, portMAX_DELAY);
Adafruit_LittleFS_Namespace::File ret = Adafruit_LittleFS_Namespace::File(filepath, mode, *this);
xSemaphoreGive(_mutex);
return ret;
// No lock is required here ... the File() object will synchronize with the mutex provided
return Adafruit_LittleFS_Namespace::File(filepath, mode, *this);
}
// Check if file or folder exists
@@ -77,6 +77,9 @@ class Adafruit_LittleFS
struct lfs_config* _lfs_cfg;
lfs_t _lfs;
// these two functions need access to the private _mutex variable:
friend void Adafruit_LittleFS_Namespace::File::DoNotCallFromOutsideClass_LockFilesystem(void);
friend void Adafruit_LittleFS_Namespace::File::DoNotCallFromOutsideClass_UnlockFilesystem(void);
//static_assert(configSUPPORT_STATIC_ALLOCATION == 1, "Currently only supports configuration with STATIC_ALLOCATION enabled");
SemaphoreHandle_t _mutex;
@@ -46,9 +46,20 @@ File::File (Adafruit_LittleFS &fs)
File::File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
: File(fs)
{
// public constructor calls public API open(), which will obtain the mutex
this->open(filename, mode);
}
File::File (uint8_t differentiator, char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
: File(fs)
{
// private constructor calls private API _open(), which does NOT obtain the mutex
(void)differentiator;
this->_open(filename, mode);
}
bool File::_open_file (char const *filepath, uint8_t mode)
{
int flags = (mode == FILE_O_READ) ? LFS_O_RDONLY :
@@ -99,12 +110,23 @@ bool File::_open_dir (char const *filepath)
return true;
}
// TODO: wrap close() because it must be called by open()
// TODO: wrap open() because it is called by openNextFile()
bool File::open (char const *filepath, uint8_t mode)
{
bool ret = false;
this->DoNotCallFromOutsideClass_LockFilesystem();
ret = this->_open(filepath, mode);
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
bool File::_open (char const *filepath, uint8_t mode)
{
bool ret = false;
// close if currently opened
if ( (*this) ) close();
if ( this->_isOpen() ) _close();
struct lfs_info info;
int rc = lfs_stat(_fs->getFS(), filepath, &info);
@@ -130,151 +152,260 @@ bool File::open (char const *filepath, uint8_t mode)
char const* splash = strrchr(filepath, '/');
strncpy(_name, splash ? (splash + 1) : filepath, LFS_NAME_MAX);
}
return ret;
}
size_t File::write (uint8_t ch)
{
VERIFY(!_is_dir, 0);
return write(&ch, 1);
}
size_t File::write (uint8_t const *buf, size_t size)
{
VERIFY(!_is_dir, 0);
lfs_ssize_t wrcount = lfs_file_write(_fs->getFS(), _file, buf, size);
VERIFY(wrcount > 0, 0);
lfs_ssize_t wrcount = 0;
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
wrcount = lfs_file_write(_fs->getFS(), _file, buf, size);
if (wrcount < 0)
{
wrcount = 0;
};
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return wrcount;
}
int File::read (void)
{
VERIFY(!_is_dir, -1);
// this thin wrapper relies on called function to synchronize
int ret = -1;
uint8_t ch;
return (read(&ch, 1) > 0) ? ch : -1;
if (read(&ch, 1) > 0)
{
ret = static_cast<int>(ch);
}
return ret;
}
int File::read (void *buf, uint16_t nbyte)
{
VERIFY(!_is_dir, 0);
return lfs_file_read(_fs->getFS(), _file, buf, nbyte);
int ret = 0;
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
ret = lfs_file_read(_fs->getFS(), _file, buf, nbyte);
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
int File::peek (void)
{
VERIFY(!_is_dir, -1);
int ch = read();
uint32_t pos = position();
seek((pos > 0) ? (pos - 1) : 0);
return ch;
int ret = -1;
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
uint32_t pos = lfs_file_tell(_fs->getFS(), _file);
uint8_t ch = 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);
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
int File::available (void)
{
return size() - position();
int ret = 0;
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
uint32_t size = lfs_file_size(_fs->getFS(), _file);
uint32_t pos = lfs_file_tell(_fs->getFS(), _file);
ret = size - pos;
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
bool File::seek (uint32_t pos)
{
VERIFY(!_is_dir, false);
return lfs_file_seek(_fs->getFS(), _file, pos, LFS_SEEK_SET) >= 0;
bool ret = false;
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
ret = lfs_file_seek(_fs->getFS(), _file, pos, LFS_SEEK_SET) >= 0;
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
uint32_t File::position (void)
{
VERIFY(!_is_dir, 0);
return lfs_file_tell(_fs->getFS(), _file);
uint32_t ret = 0;
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
ret = lfs_file_tell(_fs->getFS(), _file);
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
uint32_t File::size (void)
{
VERIFY(!_is_dir, 0);
return lfs_file_size(_fs->getFS(), _file);
uint32_t ret = 0;
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
ret = lfs_file_size(_fs->getFS(), _file);
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
void File::flush (void)
{
VERIFY(!_is_dir,);
lfs_file_sync(_fs->getFS(), _file);
this->DoNotCallFromOutsideClass_LockFilesystem();
if (!this->_is_dir)
{
lfs_file_sync(_fs->getFS(), _file);
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return;
}
void File::close (void)
{
if ( (*this) )
this->DoNotCallFromOutsideClass_LockFilesystem();
this->_close();
this->DoNotCallFromOutsideClass_UnlockFilesystem();
}
void File::_close(void)
{
if ( this->_isOpen() )
{
if ( _is_dir )
if ( this->_is_dir )
{
lfs_dir_close(_fs->getFS(), _dir);
rtos_free(_dir);
_dir = NULL;
if ( _dir_path ) rtos_free(_dir_path);
if ( this->_dir_path ) rtos_free(_dir_path);
_dir_path = NULL;
}
else
{
lfs_file_close(_fs->getFS(), _file);
lfs_file_close(this->_fs->getFS(), _file);
rtos_free(_file);
_file = NULL;
}
}
}
[[deprecated("Recommend use of isOpen() for clarity")]]
File::operator bool (void)
{
bool ret = false;
this->DoNotCallFromOutsideClass_LockFilesystem();
ret = this->_isOpen();
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
bool File::isOpen(void)
{
bool ret = 0;
this->DoNotCallFromOutsideClass_LockFilesystem();
ret = this->_isOpen();
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
bool File::_isOpen(void)
{
return (_file != NULL) || (_dir != NULL);
}
// WARNING -- although marked as `const`, the values pointed
// to may change. For example, if the same File
// object has `open()` called with a different
// file or directory name, this same pointer will
// suddenly (unexpectedly?) have different values.
char const* File::name (void)
{
return _name;
this->DoNotCallFromOutsideClass_LockFilesystem();
char const* ret = this->_name;
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
bool File::isDirectory (void)
{
return _is_dir;
this->DoNotCallFromOutsideClass_LockFilesystem();
bool ret = this->_is_dir;
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
File File::openNextFile (uint8_t mode)
{
File file(*_fs);
this->DoNotCallFromOutsideClass_LockFilesystem();
if ( !_is_dir ) return file;
struct lfs_info info;
int rc;
// lfs_dir_read return 0 when reaching end of directory, 1 if found an entry
// skip "." and ".." entries
do
File ret(*_fs);
if (this->_is_dir)
{
rc = lfs_dir_read(_fs->getFS(), _dir, &info);
} while ( rc == 1 && (!strcmp(".", info.name) || !strcmp("..", info.name)) );
struct lfs_info info;
int rc;
if ( rc == 1 )
{
// string cat name with current folder
char filepath[strlen(_dir_path) + 1 + strlen(info.name) + 1];
// lfs_dir_read returns 0 when reaching end of directory, 1 if found an entry
// Skip the "." and ".." entries ...
do
{
rc = lfs_dir_read(_fs->getFS(), _dir, &info);
} while ( rc == 1 && (!strcmp(".", info.name) || !strcmp("..", info.name)) );
strcpy(filepath, _dir_path);
if ( !(_dir_path[0] == '/' && _dir_path[1] == 0) ) strcat(filepath, "/"); // only add '/' if cwd is not root
strcat(filepath, info.name);
if ( rc == 1 )
{
// string cat name with current folder
char filepath[strlen(_dir_path) + 1 + strlen(info.name) + 1]; // potential for significant stack usage
strcpy(filepath, _dir_path);
if ( !(_dir_path[0] == '/' && _dir_path[1] == 0) ) strcat(filepath, "/"); // only add '/' if cwd is not root
strcat(filepath, info.name);
file.open(filepath, mode);
(void)ret._open(filepath, mode); // return value is ignored ... caller is expected to check isOpened()
}
else if ( rc < 0 )
{
PRINT_LFS_ERR(rc);
}
}
else if ( rc < 0 )
{
PRINT_LFS_ERR(rc);
}
return file;
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return ret;
}
void File::rewindDirectory (void)
{
VERIFY_LFS(lfs_dir_rewind(_fs->getFS(), _dir),);
this->DoNotCallFromOutsideClass_LockFilesystem();
if (this->_is_dir)
{
lfs_dir_rewind(_fs->getFS(), _dir);
}
this->DoNotCallFromOutsideClass_UnlockFilesystem();
return;
}
void File::DoNotCallFromOutsideClass_LockFilesystem(void)
{
xSemaphoreTake(this->_fs->_mutex, portMAX_DELAY);
}
void File::DoNotCallFromOutsideClass_UnlockFilesystem(void)
{
xSemaphoreGive(this->_fs->_mutex);
}
@@ -44,6 +44,12 @@ class File : public Stream
File (Adafruit_LittleFS &fs);
File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs);
private:
// openNextFile() needs a constructor that does NOT take the mutex.
File (uint8_t differentiator, char const *filename, uint8_t mode, Adafruit_LittleFS &fs);
public:
bool open (char const *filename, uint8_t mode);
//------------- Stream API -------------//
@@ -69,13 +75,21 @@ class File : public Stream
uint32_t size (void);
void close (void);
__attribute__((deprecated("Recommend use of isOpen() for clarity")))
operator bool (void);
bool isOpen(void);
char const* name (void);
bool isDirectory (void);
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 DoNotCallFromOutsideClass_LockFilesystem(void);
void DoNotCallFromOutsideClass_UnlockFilesystem(void);
private:
Adafruit_LittleFS* _fs;
@@ -89,8 +103,11 @@ class File : public Stream
char* _dir_path;
char _name[LFS_NAME_MAX+1];
bool _open(char const *filepath, uint8_t mode);
bool _open_file(char const *filepath, uint8_t mode);
bool _open_dir (char const *filepath);
void _close(void);
bool _isOpen(void);
};
}