Skip to content

Commit 6ca6be1

Browse files
committed
use static instead of malloc file/dir handle for lfs
1 parent fe375eb commit 6ca6be1

File tree

6 files changed

+53
-1744
lines changed

6 files changed

+53
-1744
lines changed

libraries/Adafruit_LittleFS/examples/Internal_ReadWrite/Internal_ReadWrite.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include <Adafruit_LittleFS.h>
1616
using namespace LittleFilesystem;
1717

18-
#define FILENAME "/adafruit2.txt"
18+
#define FILENAME "/adafruit.txt"
1919
#define CONTENTS "Adafruit Little File System test file contents"
2020

2121
File file(InternalFS);

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -32,38 +32,27 @@
3232

3333
using namespace LittleFilesystem;
3434

35-
3635
File::File (Adafruit_LittleFS &fs)
3736
{
3837
_fs = &fs;
39-
_file = NULL;
40-
_dir = NULL;
41-
_path = NULL;
4238
_is_dir = false;
39+
_opened = false;
40+
_name[0] = 0;
41+
_dir_path = NULL;
42+
43+
varclr(&_dir);
44+
varclr(&_file);
4345
}
4446

4547
File::File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs)
48+
: File(fs)
4649
{
47-
_fs = &fs;
48-
_file = NULL;
49-
_dir = NULL;
50-
_path = NULL;
51-
_is_dir = false;
52-
5350
this->open(filename, mode);
5451
}
5552

56-
File& File::operator = (const File &rhs)
57-
{
58-
// close if currently opened
59-
if ( _file ) close();
60-
memcpy(this, &rhs, sizeof(File));
61-
return *this;
62-
}
63-
6453
File::~File ()
6554
{
66-
55+
if ( _dir_path ) rtos_free(_dir_path);
6756
}
6857

6958
bool File::_open_file (char const *filepath, uint8_t mode)
@@ -73,53 +62,41 @@ bool File::_open_file (char const *filepath, uint8_t mode)
7362

7463
if ( flags )
7564
{
76-
lfs_file_t* fhdl = (lfs_file_t*) rtos_malloc(sizeof(lfs_file_t));
77-
if (!fhdl) return false;
78-
79-
int rc = lfs_file_open(_fs->getFS(), fhdl, filepath, flags);
65+
int rc = lfs_file_open(_fs->getFS(), &_file, filepath, flags);
8066

8167
if ( rc )
8268
{
8369
// failed to open
84-
rtos_free(fhdl);
8570
PRINT_LFS_ERR(rc);
86-
8771
return false;
8872
}
8973

9074
// move to end of file
91-
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), fhdl, 0, LFS_SEEK_END);
75+
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), &_file, 0, LFS_SEEK_END);
9276

93-
_file = fhdl;
77+
_opened = true;
9478
_is_dir = false;
95-
96-
_path = (char*) rtos_malloc(strlen(filepath) + 1);
97-
strcpy(_path, filepath);
9879
}
9980

10081
return true;
10182
}
10283

10384
bool File::_open_dir (char const *filepath)
10485
{
105-
File file(*_fs);
106-
107-
lfs_dir_t* fhdl = (lfs_dir_t*) rtos_malloc(sizeof(lfs_dir_t));
108-
int rc = lfs_dir_open(_fs->getFS(), fhdl, filepath);
86+
int rc = lfs_dir_open(_fs->getFS(), &_dir, filepath);
10987

11088
if ( rc )
11189
{
11290
// failed to open
113-
rtos_free(fhdl);
11491
PRINT_LFS_ERR(rc);
11592
return false;
11693
}
11794

118-
_dir = fhdl;
95+
_opened = true;
11996
_is_dir = true;
12097

121-
_path = (char*) rtos_malloc(strlen(filepath) + 1);
122-
strcpy(_path, filepath);
98+
_dir_path = (char*) rtos_malloc(strlen(filepath) + 1);
99+
strcpy(_dir_path, filepath);
123100

124101
return true;
125102
}
@@ -129,11 +106,11 @@ bool File::open (char const *filepath, uint8_t mode)
129106
bool ret = false;
130107

131108
// close if currently opened
132-
if ( _file ) close();
109+
if ( _opened ) close();
133110

134111
struct lfs_info info;
135-
136112
int rc = lfs_stat(_fs->getFS(), filepath, &info);
113+
137114
if ( LFS_ERR_OK == rc )
138115
{
139116
// file existed, open file or directory accordingly
@@ -149,6 +126,13 @@ bool File::open (char const *filepath, uint8_t mode)
149126
PRINT_LFS_ERR(rc);
150127
}
151128

129+
// save bare file name
130+
if (ret)
131+
{
132+
char const* splash = strrchr(filepath, '/');
133+
strncpy(_name, splash ? (splash + 1) : filepath, LFS_NAME_MAX);
134+
}
135+
152136
return ret;
153137
}
154138

@@ -162,7 +146,7 @@ size_t File::write (uint8_t const *buf, size_t size)
162146
{
163147
VERIFY(!_is_dir, 0);
164148

165-
lfs_ssize_t wrcount = lfs_file_write(_fs->getFS(), _file, buf, size);
149+
lfs_ssize_t wrcount = lfs_file_write(_fs->getFS(), &_file, buf, size);
166150
VERIFY(wrcount > 0, 0);
167151
return wrcount;
168152
}
@@ -177,7 +161,7 @@ int File::read (void)
177161
int File::read (void *buf, uint16_t nbyte)
178162
{
179163
VERIFY(!_is_dir, 0);
180-
return lfs_file_read(_fs->getFS(), _file, buf, nbyte);
164+
return lfs_file_read(_fs->getFS(), &_file, buf, nbyte);
181165
}
182166

183167
int File::peek (void)
@@ -198,64 +182,55 @@ int File::available (void)
198182
bool File::seek (uint32_t pos)
199183
{
200184
VERIFY(!_is_dir, false);
201-
return lfs_file_seek(_fs->getFS(), _file, pos, LFS_SEEK_SET) >= 0;
185+
return lfs_file_seek(_fs->getFS(), &_file, pos, LFS_SEEK_SET) >= 0;
202186
}
203187

204188
uint32_t File::position (void)
205189
{
206190
VERIFY(!_is_dir, 0);
207-
return lfs_file_tell(_fs->getFS(), _file);
191+
return lfs_file_tell(_fs->getFS(), &_file);
208192
}
209193

210194
uint32_t File::size (void)
211195
{
212196
VERIFY(!_is_dir, 0);
213-
return lfs_file_size(_fs->getFS(), _file);
197+
return lfs_file_size(_fs->getFS(), &_file);
214198
}
215199

216200
void File::flush (void)
217201
{
218202
VERIFY(!_is_dir,);
219-
lfs_file_sync(_fs->getFS(), _file);
203+
lfs_file_sync(_fs->getFS(), &_file);
220204
}
221205

222206
void File::close (void)
223207
{
224-
if ( _file )
208+
if ( _opened )
225209
{
226210
if ( _is_dir )
227211
{
228-
lfs_dir_close(_fs->getFS(), _dir);
212+
lfs_dir_close(_fs->getFS(), &_dir);
229213
}
230214
else
231215
{
232-
lfs_file_close(_fs->getFS(), _file);
216+
lfs_file_close(_fs->getFS(), &_file);
233217
}
234-
235-
rtos_free(_file);
236218
}
237219

238-
if ( _path ) rtos_free(_path);
220+
_opened = false;
239221

240-
_file = NULL;
241-
_path = NULL;
222+
if ( _dir_path ) rtos_free(_dir_path);
223+
_dir_path = NULL;
242224
}
243225

244226
File::operator bool (void)
245227
{
246-
return _file != NULL;
228+
return _opened;
247229
}
248230

249231
char const* File::name (void)
250232
{
251-
// return barename only
252-
char* barename = strrchr(_path, '/');
253-
return barename ? (barename + 1) : _path;
254-
}
255-
256-
char const* File::path (void)
257-
{
258-
return _path;
233+
return _name;
259234
}
260235

261236
bool File::isDirectory (void)
@@ -277,16 +252,16 @@ File File::openNextFile (uint8_t mode)
277252
// skip "." and ".." entries
278253
do
279254
{
280-
rc = lfs_dir_read(_fs->getFS(), _dir, &info);
255+
rc = lfs_dir_read(_fs->getFS(), &_dir, &info);
281256
} while ( rc == 1 && (!strcmp(".", info.name) || !strcmp("..", info.name)) );
282257

283258
if ( rc == 1 )
284259
{
285260
// string cat name with current folder
286-
char filepath[strlen(_path) + 1 + strlen(info.name) + 1];
261+
char filepath[strlen(_dir_path) + 1 + strlen(info.name) + 1];
287262

288-
strcpy(filepath, _path);
289-
if ( !(_path[0] == '/' && _path[1] == 0) ) strcat(filepath, "/"); // only add '/' if cwd is not root
263+
strcpy(filepath, _dir_path);
264+
if ( !(_dir_path[0] == '/' && _dir_path[1] == 0) ) strcat(filepath, "/"); // only add '/' if cwd is not root
290265
strcat(filepath, info.name);
291266

292267
file.open(filepath, mode);
@@ -301,5 +276,5 @@ File File::openNextFile (uint8_t mode)
301276

302277
void File::rewindDirectory (void)
303278
{
304-
VERIFY_LFS(lfs_dir_rewind(_fs->getFS(), _dir),);
279+
VERIFY_LFS(lfs_dir_rewind(_fs->getFS(), &_dir),);
305280
}

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.h

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828
#define FILE_READ 0
2929
#define FILE_WRITE 1
3030

31-
#define FILE_NAME_MAX 255
32-
3331
// Forward declaration
3432
class Adafruit_LittleFS;
3533

@@ -41,7 +39,6 @@ class File : public Stream
4139
public:
4240
File (Adafruit_LittleFS &fs);
4341
File (char const *filename, uint8_t mode, Adafruit_LittleFS &fs);
44-
File & operator = (const File &rhs);
4542
virtual ~File ();
4643

4744
bool open (char const *filename, uint8_t mode);
@@ -71,7 +68,6 @@ class File : public Stream
7168
void close (void);
7269
operator bool (void);
7370
char const* name (void);
74-
char const* path (void);
7571

7672
bool isDirectory (void);
7773
File openNextFile (uint8_t mode = FILE_READ);
@@ -80,16 +76,19 @@ class File : public Stream
8076
private:
8177
Adafruit_LittleFS* _fs;
8278

83-
lfs_file_t* _file; // file hanlde
84-
lfs_dir_t* _dir; // dir handle
85-
86-
char* _path;
8779
bool _is_dir;
80+
bool _opened;
81+
82+
union {
83+
lfs_file_t _file;
84+
lfs_dir_t _dir;
85+
};
86+
87+
char* _dir_path;
88+
char _name[LFS_NAME_MAX+1];
8889

8990
bool _open_file(char const *filepath, uint8_t mode);
9091
bool _open_dir (char const *filepath);
91-
92-
friend class ::Adafruit_LittleFS;
9392
};
9493

9594
}

0 commit comments

Comments
 (0)