Skip to content

Commit 616162f

Browse files
committed
refactor lfs
1 parent 86d63af commit 616162f

File tree

2 files changed

+29
-28
lines changed

2 files changed

+29
-28
lines changed

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,42 +66,41 @@ File::~File ()
6666

6767
}
6868

69-
File File::_open_file (char const *filepath, uint8_t mode)
69+
bool File::_open_file (char const *filepath, uint8_t mode)
7070
{
71-
File file(*_fs);
72-
7371
int flags = (mode == FILE_READ) ? LFS_O_RDONLY :
7472
(mode == FILE_WRITE) ? (LFS_O_RDWR | LFS_O_CREAT) : 0;
7573

7674
if ( flags )
7775
{
7876
lfs_file_t* fhdl = (lfs_file_t*) rtos_malloc(sizeof(lfs_file_t));
79-
VERIFY(fhdl, file);
77+
if (!fhdl) return false;
8078

8179
int rc = lfs_file_open(_fs->getFS(), fhdl, filepath, flags);
8280

8381
if ( rc )
8482
{
83+
// failed to open
8584
rtos_free(fhdl);
8685
PRINT_LFS_ERR(rc);
86+
87+
return false;
8788
}
88-
else
89-
{
90-
// move to end of file
91-
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), fhdl, 0, LFS_SEEK_END);
9289

93-
file._file = fhdl;
94-
file._is_dir = false;
90+
// move to end of file
91+
if ( mode == FILE_WRITE ) lfs_file_seek(_fs->getFS(), fhdl, 0, LFS_SEEK_END);
9592

96-
file._path = (char*) rtos_malloc(strlen(filepath) + 1);
97-
strcpy(file._path, filepath);
98-
}
93+
_file = fhdl;
94+
_is_dir = false;
95+
96+
_path = (char*) rtos_malloc(strlen(filepath) + 1);
97+
strcpy(_path, filepath);
9998
}
10099

101-
return file;
100+
return true;
102101
}
103102

104-
File File::_open_dir (char const *filepath)
103+
bool File::_open_dir (char const *filepath)
105104
{
106105
File file(*_fs);
107106

@@ -110,23 +109,25 @@ File File::_open_dir (char const *filepath)
110109

111110
if ( rc )
112111
{
112+
// failed to open
113113
rtos_free(fhdl);
114114
PRINT_LFS_ERR(rc);
115+
return false;
115116
}
116-
else
117-
{
118-
file._dir = fhdl;
119-
file._is_dir = true;
120117

121-
file._path = (char*) rtos_malloc(strlen(filepath) + 1);
122-
strcpy(file._path, filepath);
123-
}
118+
_dir = fhdl;
119+
_is_dir = true;
124120

125-
return file;
121+
_path = (char*) rtos_malloc(strlen(filepath) + 1);
122+
strcpy(_path, filepath);
123+
124+
return true;
126125
}
127126

128127
bool File::open (char const *filepath, uint8_t mode)
129128
{
129+
bool ret = false;
130+
130131
// close if currently opened
131132
if ( _file ) close();
132133

@@ -136,19 +137,19 @@ bool File::open (char const *filepath, uint8_t mode)
136137
if ( LFS_ERR_OK == rc )
137138
{
138139
// file existed, open file or directory accordingly
139-
*this = (info.type == LFS_TYPE_REG) ? _open_file(filepath, mode) : _open_dir(filepath);
140+
ret = (info.type == LFS_TYPE_REG) ? _open_file(filepath, mode) : _open_dir(filepath);
140141
}
141142
else if ( LFS_ERR_NOENT == rc )
142143
{
143144
// file not existed, only proceed with FILE_WRITE mode
144-
if ( mode == FILE_WRITE ) *this = _open_file(filepath, mode);
145+
if ( mode == FILE_WRITE ) ret = _open_file(filepath, mode);
145146
}
146147
else
147148
{
148149
PRINT_LFS_ERR(rc);
149150
}
150151

151-
return _file != NULL;
152+
return ret;
152153
}
153154

154155
size_t File::write (uint8_t ch)

libraries/Adafruit_LittleFS/src/Adafruit_LittleFS_File.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class File : public Stream
8686
char* _path;
8787
bool _is_dir;
8888

89-
File _open_file(char const *filepath, uint8_t mode);
90-
File _open_dir (char const *filepath);
89+
bool _open_file(char const *filepath, uint8_t mode);
90+
bool _open_dir (char const *filepath);
9191

9292
friend class ::Adafruit_LittleFS;
9393
};

0 commit comments

Comments
 (0)