Skip to content

Commit f2e7ef6

Browse files
committed
revert to use malloc for lfs file/dir data due to lfs pointer matching
1 parent f5aeb38 commit f2e7ef6

File tree

2 files changed

+33
-35
lines changed

2 files changed

+33
-35
lines changed

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ File::File (Adafruit_LittleFS &fs)
3636
{
3737
_fs = &fs;
3838
_is_dir = false;
39-
_opened = false;
4039
_name[0] = 0;
4140
_dir_path = NULL;
4241

43-
varclr(&_dir);
44-
varclr(&_file);
42+
_dir = NULL;
43+
_file = NULL;
4544
}
4645

4746
File::File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
@@ -50,19 +49,17 @@ File::File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
5049
this->open(filename, mode);
5150
}
5251

53-
File::~File ()
54-
{
55-
if ( _dir_path ) rtos_free(_dir_path);
56-
}
57-
5852
bool File::_open_file (char const *filepath, uint8_t mode)
5953
{
6054
int flags = (mode == FILE_READ) ? LFS_O_RDONLY :
6155
(mode == FILE_WRITE) ? (LFS_O_RDWR | LFS_O_CREAT) : 0;
6256

6357
if ( flags )
6458
{
65-
int rc = lfs_file_open(_fs->getFS(), &_file, filepath, flags);
59+
_file = (lfs_file_t*) rtos_malloc(sizeof(lfs_file_t));
60+
if (!_file) return false;
61+
62+
int rc = lfs_file_open(_fs->getFS(), _file, filepath, flags);
6663

6764
if ( rc )
6865
{
@@ -72,9 +69,8 @@ bool File::_open_file (char const *filepath, uint8_t mode)
7269
}
7370

7471
// move to end of file
75-
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), &_file, 0, LFS_SEEK_END);
72+
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), _file, 0, LFS_SEEK_END);
7673

77-
_opened = true;
7874
_is_dir = false;
7975
}
8076

@@ -83,7 +79,10 @@ bool File::_open_file (char const *filepath, uint8_t mode)
8379

8480
bool File::_open_dir (char const *filepath)
8581
{
86-
int rc = lfs_dir_open(_fs->getFS(), &_dir, filepath);
82+
_dir = (lfs_dir_t*) rtos_malloc(sizeof(lfs_dir_t));
83+
if (!_dir) return false;
84+
85+
int rc = lfs_dir_open(_fs->getFS(), _dir, filepath);
8786

8887
if ( rc )
8988
{
@@ -92,7 +91,6 @@ bool File::_open_dir (char const *filepath)
9291
return false;
9392
}
9493

95-
_opened = true;
9694
_is_dir = true;
9795

9896
_dir_path = (char*) rtos_malloc(strlen(filepath) + 1);
@@ -106,7 +104,7 @@ bool File::open (char const *filepath, uint8_t mode)
106104
bool ret = false;
107105

108106
// close if currently opened
109-
if ( _opened ) close();
107+
if ( (*this) ) close();
110108

111109
struct lfs_info info;
112110
int rc = lfs_stat(_fs->getFS(), filepath, &info);
@@ -146,7 +144,7 @@ size_t File::write (uint8_t const *buf, size_t size)
146144
{
147145
VERIFY(!_is_dir, 0);
148146

149-
lfs_ssize_t wrcount = lfs_file_write(_fs->getFS(), &_file, buf, size);
147+
lfs_ssize_t wrcount = lfs_file_write(_fs->getFS(), _file, buf, size);
150148
VERIFY(wrcount > 0, 0);
151149
return wrcount;
152150
}
@@ -161,7 +159,7 @@ int File::read (void)
161159
int File::read (void *buf, uint16_t nbyte)
162160
{
163161
VERIFY(!_is_dir, 0);
164-
return lfs_file_read(_fs->getFS(), &_file, buf, nbyte);
162+
return lfs_file_read(_fs->getFS(), _file, buf, nbyte);
165163
}
166164

167165
int File::peek (void)
@@ -182,50 +180,52 @@ int File::available (void)
182180
bool File::seek (uint32_t pos)
183181
{
184182
VERIFY(!_is_dir, false);
185-
return lfs_file_seek(_fs->getFS(), &_file, pos, LFS_SEEK_SET) >= 0;
183+
return lfs_file_seek(_fs->getFS(), _file, pos, LFS_SEEK_SET) >= 0;
186184
}
187185

188186
uint32_t File::position (void)
189187
{
190188
VERIFY(!_is_dir, 0);
191-
return lfs_file_tell(_fs->getFS(), &_file);
189+
return lfs_file_tell(_fs->getFS(), _file);
192190
}
193191

194192
uint32_t File::size (void)
195193
{
196194
VERIFY(!_is_dir, 0);
197-
return lfs_file_size(_fs->getFS(), &_file);
195+
return lfs_file_size(_fs->getFS(), _file);
198196
}
199197

200198
void File::flush (void)
201199
{
202200
VERIFY(!_is_dir,);
203-
lfs_file_sync(_fs->getFS(), &_file);
201+
lfs_file_sync(_fs->getFS(), _file);
204202
}
205203

206204
void File::close (void)
207205
{
208-
if ( _opened )
206+
if ( (*this) )
209207
{
210208
if ( _is_dir )
211209
{
212-
lfs_dir_close(_fs->getFS(), &_dir);
210+
lfs_dir_close(_fs->getFS(), _dir);
211+
rtos_free(_dir);
212+
_dir = NULL;
213+
214+
if ( _dir_path ) rtos_free(_dir_path);
215+
_dir_path = NULL;
213216
}
214217
else
215218
{
216-
lfs_file_close(_fs->getFS(), &_file);
219+
lfs_file_close(_fs->getFS(), _file);
220+
rtos_free(_file);
221+
_file = NULL;
217222
}
218223
}
219-
220-
_opened = false;
221-
222-
if ( _dir_path ) rtos_free(_dir_path);
223-
_dir_path = NULL;
224224
}
225225

226226
File::operator bool (void)
227227
{
228-
return _opened;
228+
return (_file != NULL) || (_dir != NULL);
229229
}
230230

231231
char const* File::name (void)
@@ -252,7 +252,7 @@ File File::openNextFile (uint8_t mode)
252252
// skip "." and ".." entries
253253
do
254254
{
255-
rc = lfs_dir_read(_fs->getFS(), &_dir, &info);
255+
rc = lfs_dir_read(_fs->getFS(), _dir, &info);
256256
} while ( rc == 1 && (!strcmp(".", info.name) || !strcmp("..", info.name)) );
257257

258258
if ( rc == 1 )
@@ -276,5 +276,5 @@ File File::openNextFile (uint8_t mode)
276276

277277
void File::rewindDirectory (void)
278278
{
279-
VERIFY_LFS(lfs_dir_rewind(_fs->getFS(), &_dir),);
279+
VERIFY_LFS(lfs_dir_rewind(_fs->getFS(), _dir),);
280280
}

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class File : public Stream
3939
public:
4040
File (Adafruit_LittleFS &fs);
4141
File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs);
42-
virtual ~File ();
4342

4443
bool open (char const *filename, uint8_t mode);
4544

@@ -77,11 +76,10 @@ class File : public Stream
7776
Adafruit_LittleFS* _fs;
7877

7978
bool _is_dir;
80-
bool _opened;
8179

8280
union {
83-
lfs_file_t _file;
84-
lfs_dir_t _dir;
81+
lfs_file_t* _file;
82+
lfs_dir_t* _dir;
8583
};
8684

8785
char* _dir_path;

0 commit comments

Comments
 (0)