use static instead of malloc file/dir handle for lfs
This commit is contained in:
1 parent
fe375eb448
commit
6ca6be16c9
6 files changed
+53
-1744
No files matched your search
@@ -32,38 +32,27 @@
|
||||
|
||||
using namespace LittleFilesystem;
|
||||
|
||||
|
||||
File::File (Adafruit_LittleFS &fs)
|
||||
{
|
||||
_fs = &fs;
|
||||
_file = NULL;
|
||||
_dir = NULL;
|
||||
_path = NULL;
|
||||
_is_dir = false;
|
||||
_opened = false;
|
||||
_name[0] = 0;
|
||||
_dir_path = NULL;
|
||||
|
||||
varclr(&_dir);
|
||||
varclr(&_file);
|
||||
}
|
||||
|
||||
File::File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
|
||||
: File(fs)
|
||||
{
|
||||
_fs = &fs;
|
||||
_file = NULL;
|
||||
_dir = NULL;
|
||||
_path = NULL;
|
||||
_is_dir = false;
|
||||
|
||||
this->open(filename, mode);
|
||||
}
|
||||
|
||||
File& File::operator = (const File &rhs)
|
||||
{
|
||||
// close if currently opened
|
||||
if ( _file ) close();
|
||||
memcpy(this, &rhs, sizeof(File));
|
||||
return *this;
|
||||
}
|
||||
|
||||
File::~File ()
|
||||
{
|
||||
|
||||
if ( _dir_path ) rtos_free(_dir_path);
|
||||
}
|
||||
|
||||
bool File::_open_file (char const *filepath, uint8_t mode)
|
||||
@@ -73,28 +62,20 @@ bool File::_open_file (char const *filepath, uint8_t mode)
|
||||
|
||||
if ( flags )
|
||||
{
|
||||
lfs_file_t* fhdl = (lfs_file_t*) rtos_malloc(sizeof(lfs_file_t));
|
||||
if (!fhdl) return false;
|
||||
|
||||
int rc = lfs_file_open(_fs->getFS(), fhdl, filepath, flags);
|
||||
int rc = lfs_file_open(_fs->getFS(), &_file, filepath, flags);
|
||||
|
||||
if ( rc )
|
||||
{
|
||||
// failed to open
|
||||
rtos_free(fhdl);
|
||||
PRINT_LFS_ERR(rc);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
// move to end of file
|
||||
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), fhdl, 0, LFS_SEEK_END);
|
||||
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), &_file, 0, LFS_SEEK_END);
|
||||
|
||||
_file = fhdl;
|
||||
_opened = true;
|
||||
_is_dir = false;
|
||||
|
||||
_path = (char*) rtos_malloc(strlen(filepath) + 1);
|
||||
strcpy(_path, filepath);
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -102,24 +83,20 @@ bool File::_open_file (char const *filepath, uint8_t mode)
|
||||
|
||||
bool File::_open_dir (char const *filepath)
|
||||
{
|
||||
File file(*_fs);
|
||||
|
||||
lfs_dir_t* fhdl = (lfs_dir_t*) rtos_malloc(sizeof(lfs_dir_t));
|
||||
int rc = lfs_dir_open(_fs->getFS(), fhdl, filepath);
|
||||
int rc = lfs_dir_open(_fs->getFS(), &_dir, filepath);
|
||||
|
||||
if ( rc )
|
||||
{
|
||||
// failed to open
|
||||
rtos_free(fhdl);
|
||||
PRINT_LFS_ERR(rc);
|
||||
return false;
|
||||
}
|
||||
|
||||
_dir = fhdl;
|
||||
_opened = true;
|
||||
_is_dir = true;
|
||||
|
||||
_path = (char*) rtos_malloc(strlen(filepath) + 1);
|
||||
strcpy(_path, filepath);
|
||||
_dir_path = (char*) rtos_malloc(strlen(filepath) + 1);
|
||||
strcpy(_dir_path, filepath);
|
||||
|
||||
return true;
|
||||
}
|
||||
@@ -129,11 +106,11 @@ bool File::open (char const *filepath, uint8_t mode)
|
||||
bool ret = false;
|
||||
|
||||
// close if currently opened
|
||||
if ( _file ) close();
|
||||
if ( _opened ) close();
|
||||
|
||||
struct lfs_info info;
|
||||
|
||||
int rc = lfs_stat(_fs->getFS(), filepath, &info);
|
||||
|
||||
if ( LFS_ERR_OK == rc )
|
||||
{
|
||||
// file existed, open file or directory accordingly
|
||||
@@ -149,6 +126,13 @@ bool File::open (char const *filepath, uint8_t mode)
|
||||
PRINT_LFS_ERR(rc);
|
||||
}
|
||||
|
||||
// save bare file name
|
||||
if (ret)
|
||||
{
|
||||
char const* splash = strrchr(filepath, '/');
|
||||
strncpy(_name, splash ? (splash + 1) : filepath, LFS_NAME_MAX);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
@@ -162,7 +146,7 @@ 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);
|
||||
lfs_ssize_t wrcount = lfs_file_write(_fs->getFS(), &_file, buf, size);
|
||||
VERIFY(wrcount > 0, 0);
|
||||
return wrcount;
|
||||
}
|
||||
@@ -177,7 +161,7 @@ int File::read (void)
|
||||
int File::read (void *buf, uint16_t nbyte)
|
||||
{
|
||||
VERIFY(!_is_dir, 0);
|
||||
return lfs_file_read(_fs->getFS(), _file, buf, nbyte);
|
||||
return lfs_file_read(_fs->getFS(), &_file, buf, nbyte);
|
||||
}
|
||||
|
||||
int File::peek (void)
|
||||
@@ -198,64 +182,55 @@ int File::available (void)
|
||||
bool File::seek (uint32_t pos)
|
||||
{
|
||||
VERIFY(!_is_dir, false);
|
||||
return lfs_file_seek(_fs->getFS(), _file, pos, LFS_SEEK_SET) >= 0;
|
||||
return lfs_file_seek(_fs->getFS(), &_file, pos, LFS_SEEK_SET) >= 0;
|
||||
}
|
||||
|
||||
uint32_t File::position (void)
|
||||
{
|
||||
VERIFY(!_is_dir, 0);
|
||||
return lfs_file_tell(_fs->getFS(), _file);
|
||||
return lfs_file_tell(_fs->getFS(), &_file);
|
||||
}
|
||||
|
||||
uint32_t File::size (void)
|
||||
{
|
||||
VERIFY(!_is_dir, 0);
|
||||
return lfs_file_size(_fs->getFS(), _file);
|
||||
return lfs_file_size(_fs->getFS(), &_file);
|
||||
}
|
||||
|
||||
void File::flush (void)
|
||||
{
|
||||
VERIFY(!_is_dir,);
|
||||
lfs_file_sync(_fs->getFS(), _file);
|
||||
lfs_file_sync(_fs->getFS(), &_file);
|
||||
}
|
||||
|
||||
void File::close (void)
|
||||
{
|
||||
if ( _file )
|
||||
if ( _opened )
|
||||
{
|
||||
if ( _is_dir )
|
||||
{
|
||||
lfs_dir_close(_fs->getFS(), _dir);
|
||||
lfs_dir_close(_fs->getFS(), &_dir);
|
||||
}
|
||||
else
|
||||
{
|
||||
lfs_file_close(_fs->getFS(), _file);
|
||||
lfs_file_close(_fs->getFS(), &_file);
|
||||
}
|
||||
|
||||
rtos_free(_file);
|
||||
}
|
||||
|
||||
if ( _path ) rtos_free(_path);
|
||||
_opened = false;
|
||||
|
||||
_file = NULL;
|
||||
_path = NULL;
|
||||
if ( _dir_path ) rtos_free(_dir_path);
|
||||
_dir_path = NULL;
|
||||
}
|
||||
|
||||
File::operator bool (void)
|
||||
{
|
||||
return _file != NULL;
|
||||
return _opened;
|
||||
}
|
||||
|
||||
char const* File::name (void)
|
||||
{
|
||||
// return barename only
|
||||
char* barename = strrchr(_path, '/');
|
||||
return barename ? (barename + 1) : _path;
|
||||
}
|
||||
|
||||
char const* File::path (void)
|
||||
{
|
||||
return _path;
|
||||
return _name;
|
||||
}
|
||||
|
||||
bool File::isDirectory (void)
|
||||
@@ -277,16 +252,16 @@ File File::openNextFile (uint8_t mode)
|
||||
// skip "." 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 )
|
||||
{
|
||||
// string cat name with current folder
|
||||
char filepath[strlen(_path) + 1 + strlen(info.name) + 1];
|
||||
char filepath[strlen(_dir_path) + 1 + strlen(info.name) + 1];
|
||||
|
||||
strcpy(filepath, _path);
|
||||
if ( !(_path[0] == '/' && _path[1] == 0) ) strcat(filepath, "/"); // only add '/' if cwd is not root
|
||||
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);
|
||||
@@ -301,5 +276,5 @@ File File::openNextFile (uint8_t mode)
|
||||
|
||||
void File::rewindDirectory (void)
|
||||
{
|
||||
VERIFY_LFS(lfs_dir_rewind(_fs->getFS(), _dir),);
|
||||
VERIFY_LFS(lfs_dir_rewind(_fs->getFS(), &_dir),);
|
||||
}
|
||||
Reference in new issue
Block a user