Skip to content

Commit 6079652

Browse files
committed
FileSystem: interface changes and some additions
- rename mount to begin to be more in line with other libraries - add rename and remove methods - remove freestanding functions (mount,open,openDir) from public API until that part is ready - fix resource leak in SPIFFSDirImpl
1 parent 80c16e8 commit 6079652

File tree

4 files changed

+150
-37
lines changed

4 files changed

+150
-37
lines changed

hardware/esp8266com/esp8266/cores/esp8266/FS.cpp

Lines changed: 103 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,16 @@ bool Dir::next() {
143143
return _impl->next();
144144
}
145145

146+
bool FS::begin() {
147+
if (!_impl) {
148+
return false;
149+
}
150+
return _impl->begin();
151+
}
146152

153+
File FS::open(const String& path, const char* mode) {
154+
return open(path.c_str(), mode);
155+
}
147156

148157
File FS::open(const char* path, const char* mode) {
149158
if (!_impl) {
@@ -160,46 +169,40 @@ File FS::open(const char* path, const char* mode) {
160169
return File(_impl->open(path, om, am));
161170
}
162171

163-
File FS::open(const String& path, const char* mode) {
164-
return FS::open(path.c_str(), mode);
165-
}
166-
167172
Dir FS::openDir(const char* path) {
168173
if (!_impl) {
169174
return Dir();
170175
}
171-
172176
return Dir(_impl->openDir(path));
173177
}
174178

175179
Dir FS::openDir(const String& path) {
176-
return FS::openDir(path.c_str());
180+
return openDir(path.c_str());
177181
}
178182

179-
struct MountEntry {
180-
FSImplPtr fs;
181-
String path;
182-
MountEntry* next;
183-
};
183+
bool FS::remove(const char* path) {
184+
if (!_impl) {
185+
return false;
186+
}
187+
return _impl->remove(path);
188+
}
184189

185-
static MountEntry* s_mounted = nullptr;
190+
bool FS::remove(const String& path) {
191+
return remove(path.c_str());
192+
}
186193

187-
template<>
188-
bool mount<FS>(FS& fs, const char* mountPoint) {
189-
FSImplPtr p = fs._impl;
190-
if (!p || !p->mount()) {
191-
DEBUGV("FSImpl mount failed\r\n");
194+
bool FS::rename(const char* pathFrom, const char* pathTo) {
195+
if (!_impl) {
192196
return false;
193197
}
198+
return _impl->rename(pathFrom, pathTo);
199+
}
194200

195-
MountEntry* entry = new MountEntry;
196-
entry->fs = p;
197-
entry->path = mountPoint;
198-
entry->next = s_mounted;
199-
s_mounted = entry;
200-
return true;
201+
bool FS::rename(const String& pathFrom, const String& pathTo) {
202+
return rename(pathFrom.c_str(), pathTo.c_str());
201203
}
202204

205+
203206
static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
204207
switch (mode[0]) {
205208
case 'r':
@@ -228,3 +231,80 @@ static bool sflags(const char* mode, OpenMode& om, AccessMode& am) {
228231
}
229232
return true;
230233
}
234+
235+
236+
#if defined(FS_FREESTANDING_FUNCTIONS)
237+
238+
/*
239+
TODO: move these functions to public API:
240+
*/
241+
File open(const char* path, const char* mode);
242+
File open(String& path, const char* mode);
243+
244+
Dir openDir(const char* path);
245+
Dir openDir(String& path);
246+
247+
template<>
248+
bool mount<FS>(FS& fs, const char* mountPoint);
249+
/*
250+
*/
251+
252+
253+
struct MountEntry {
254+
FSImplPtr fs;
255+
String path;
256+
MountEntry* next;
257+
};
258+
259+
static MountEntry* s_mounted = nullptr;
260+
261+
template<>
262+
bool mount<FS>(FS& fs, const char* mountPoint) {
263+
FSImplPtr p = fs._impl;
264+
if (!p || !p->mount()) {
265+
DEBUGV("FSImpl mount failed\r\n");
266+
return false;
267+
}
268+
269+
!make sure mountPoint has trailing '/' here
270+
271+
MountEntry* entry = new MountEntry;
272+
entry->fs = p;
273+
entry->path = mountPoint;
274+
entry->next = s_mounted;
275+
s_mounted = entry;
276+
return true;
277+
}
278+
279+
280+
/*
281+
iterate over MountEntries and look for the ones which match the path
282+
*/
283+
File open(const char* path, const char* mode) {
284+
OpenMode om;
285+
AccessMode am;
286+
if (!sflags(mode, om, am)) {
287+
DEBUGV("open: invalid mode `%s`\r\n", mode);
288+
return File();
289+
}
290+
291+
for (MountEntry* entry = s_mounted; entry; entry = entry->next) {
292+
size_t offset = entry->path.length();
293+
if (strstr(path, entry->path.c_str())) {
294+
File result = entry->fs->open(path + offset);
295+
if (result)
296+
return result;
297+
}
298+
}
299+
300+
return File();
301+
}
302+
303+
File open(const String& path, const char* mode) {
304+
return FS::open(path.c_str(), mode);
305+
}
306+
307+
Dir openDir(const String& path) {
308+
return openDir(path.c_str());
309+
}
310+
#endif

hardware/esp8266com/esp8266/cores/esp8266/FS.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
#include <Arduino.h>
2525
#include <memory>
2626

27+
class File;
28+
class Dir;
29+
2730
class FileImpl;
2831
typedef std::shared_ptr<FileImpl> FileImplPtr;
2932
class FSImpl;
@@ -82,25 +85,26 @@ class FS
8285
{
8386
public:
8487
FS(FSImplPtr impl) : _impl(impl) { }
88+
89+
bool begin();
90+
8591
File open(const char* path, const char* mode);
8692
File open(const String& path, const char* mode);
93+
8794
Dir openDir(const char* path);
8895
Dir openDir(const String& path);
8996

97+
bool remove(const char* path);
98+
bool remove(const String& path);
99+
100+
bool rename(const char* pathFrom, const char* pathTo);
101+
bool rename(const String& pathFrom, const String& pathTo);
102+
90103
protected:
91104
FSImplPtr _impl;
92105

93-
template <typename Tfs>
94-
friend bool mount(Tfs& fs, const char* mountPoint);
95106
};
96107

97108
extern FS SPIFFS;
98109

99-
template<>
100-
bool mount<FS>(FS& fs, const char* mountPoint);
101-
102-
File open(const char* path, const char* mode);
103-
104-
Dir openDir(const char* path);
105-
106110
#endif //FS_H

hardware/esp8266com/esp8266/cores/esp8266/FSImpl.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,19 @@ enum AccessMode {
5050

5151
class DirImpl {
5252
public:
53+
virtual ~DirImpl() { }
5354
virtual FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) = 0;
5455
virtual const char* fileName() = 0;
5556
virtual bool next() = 0;
5657
};
5758

5859
class FSImpl {
5960
public:
61+
virtual bool begin() = 0;
6062
virtual FileImplPtr open(const char* path, OpenMode openMode, AccessMode accessMode) = 0;
6163
virtual DirImplPtr openDir(const char* path) = 0;
62-
virtual bool mount() = 0;
64+
virtual bool rename(const char* pathFrom, const char* pathTo) = 0;
65+
virtual bool remove(const char* path) = 0;
6366

6467
};
6568

hardware/esp8266com/esp8266/cores/esp8266/spiffs_api.cpp

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,43 @@ class SPIFFSImpl : public FSImpl {
5757

5858
DirImplPtr openDir(const char* path) override;
5959

60-
bool mount() override {
60+
bool rename(const char* pathFrom, const char* pathTo) override {
61+
char tmpNameFrom[SPIFFS_OBJ_NAME_LEN];
62+
strlcpy(tmpNameFrom, pathFrom, sizeof(tmpNameFrom));
63+
char tmpNameTo[SPIFFS_OBJ_NAME_LEN];
64+
strlcpy(tmpNameTo, pathTo, sizeof(tmpNameTo));
65+
auto rc = SPIFFS_rename(&_fs, tmpNameFrom, tmpNameTo);
66+
if (rc != SPIFFS_OK) {
67+
DEBUGV("SPIFFS_rename: rc=%d, from=`%s`, to=`%s`\r\n", rc,
68+
pathFrom, pathTo);
69+
return false;
70+
}
71+
return true;
72+
}
73+
74+
bool remove(const char* path) override {
75+
char tmpName[SPIFFS_OBJ_NAME_LEN];
76+
strlcpy(tmpName, path, sizeof(tmpName));
77+
auto rc = SPIFFS_remove(&_fs, tmpName);
78+
if (rc != SPIFFS_OK) {
79+
DEBUGV("SPIFFS_remove: rc=%d path=`%s`\r\n", rc, path);
80+
return false;
81+
}
82+
return true;
83+
}
84+
85+
bool begin() override {
6186
if (SPIFFS_mounted(&_fs) != 0) {
6287
return true;
6388
}
64-
6589
if (_tryMount()) {
6690
return true;
6791
}
68-
6992
auto rc = SPIFFS_format(&_fs);
7093
if (rc != SPIFFS_OK) {
7194
DEBUGV("SPIFFS_format: rc=%d, err=%d\r\n", rc, _fs.err_code);
7295
return false;
7396
}
74-
7597
return _tryMount();
7698
}
7799

@@ -252,6 +274,10 @@ class SPIFFSDirImpl : public DirImpl {
252274
{
253275
}
254276

277+
~SPIFFSDirImpl() override {
278+
SPIFFS_closedir(&_dir);
279+
}
280+
255281
FileImplPtr openFile(OpenMode openMode, AccessMode accessMode) override {
256282
if (!_valid) {
257283
return FileImplPtr();

0 commit comments

Comments
 (0)