22
22
#include " Arduino.h"
23
23
24
24
bool FSClass::mount () {
25
- if (_mounted)
26
- return true ;
27
-
28
- _mounted = spiffs_mount ();
29
- return _mounted;
25
+ if (SPIFFS_mounted (&_filesystemStorageHandle)) return true ;
26
+ int res = spiffs_mount ();
27
+ if (res != 0 ){
28
+ int formated = SPIFFS_format (&_filesystemStorageHandle);
29
+ if (formated != 0 ) return false ;
30
+ res = spiffs_mount ();
31
+ }
32
+ return (res == 0 );
30
33
}
31
34
32
35
void FSClass::unmount () {
33
- if (!_mounted)
34
- return ;
35
-
36
- spiffs_unmount ();
37
- _mounted = false ;
36
+ if (SPIFFS_mounted (&_filesystemStorageHandle))
37
+ SPIFFS_unmount (&_filesystemStorageHandle);
38
38
}
39
39
40
40
bool FSClass::format () {
41
- return spiffs_format ();
41
+ if (!SPIFFS_mounted (&_filesystemStorageHandle)){
42
+ spiffs_mount ();
43
+ }
44
+ SPIFFS_unmount (&_filesystemStorageHandle);
45
+ int formated = SPIFFS_format (&_filesystemStorageHandle);
46
+ if (formated != 0 ) return false ;
47
+ return (spiffs_mount () == 0 );
42
48
}
43
49
44
50
bool FSClass::check () {
45
51
return SPIFFS_check (&_filesystemStorageHandle) == 0 ;
46
52
}
47
53
48
- bool FSClass::exists (const char *filename) {
54
+ bool FSClass::exists (char *filename) {
49
55
spiffs_stat stat = {0 };
50
56
if (SPIFFS_stat (&_filesystemStorageHandle, filename, &stat) < 0 )
51
57
return false ;
52
58
return stat.name [0 ] != ' \0 ' ;
53
59
}
54
60
55
- bool FSClass::create (const char *filepath){
61
+ bool FSClass::create (char *filepath){
56
62
return SPIFFS_creat (&_filesystemStorageHandle, filepath, 0 ) == 0 ;
57
63
}
58
64
59
- bool FSClass::remove (const char *filepath){
65
+ bool FSClass::remove (char *filepath){
60
66
return SPIFFS_remove (&_filesystemStorageHandle, filepath) == 0 ;
61
67
}
62
68
63
- bool FSClass::rename (const char *filename, const char *newname) {
69
+ bool FSClass::rename (char *filename, char *newname) {
64
70
return SPIFFS_rename (&_filesystemStorageHandle, filename, newname) == 0 ;
65
71
}
66
72
67
- FSFile FSClass::open (const char *filename, uint8_t mode) {
73
+ size_t FSClass::totalBytes (){
74
+ u32_t totalBytes;
75
+ u32_t usedBytes;
76
+ if (SPIFFS_info (&_filesystemStorageHandle, &totalBytes, &usedBytes) == 0 )
77
+ return totalBytes;
78
+ return 0 ;
79
+ }
80
+
81
+ size_t FSClass::usedBytes (){
82
+ u32_t totalBytes;
83
+ u32_t usedBytes;
84
+ if (SPIFFS_info (&_filesystemStorageHandle, &totalBytes, &usedBytes) == 0 )
85
+ return usedBytes;
86
+ return 0 ;
87
+ }
88
+
89
+ FSFile FSClass::open (char *filename, uint8_t mode) {
68
90
if (String (filename) == " " || String (filename) == " /" ) return FSFile (" /" );
69
91
int repeats = 0 ;
70
92
bool notExist;
@@ -76,7 +98,7 @@ FSFile FSClass::open(const char *filename, uint8_t mode) {
76
98
res = SPIFFS_open (&_filesystemStorageHandle, filename, (spiffs_flags)mode, 0 );
77
99
int code = SPIFFS_errno (&_filesystemStorageHandle);
78
100
if (res < 0 ){
79
- debugf (" open errno %d\n " , code);
101
+ SPIFFS_API_DBG_E (" open errno %d\n " , code);
80
102
notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE);
81
103
if (notExist && canRecreate)
82
104
remove (filename); // fix for deleted files
@@ -112,11 +134,11 @@ FSFile::FSFile(String path) {
112
134
_stats.type = SPIFFS_TYPE_DIR;
113
135
SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
114
136
} else {
115
- _file = SPIFFS_open (&_filesystemStorageHandle, path.c_str (), (spiffs_flags)FSFILE_READ, 0 );
137
+ _file = SPIFFS_open (&_filesystemStorageHandle, ( char *) path.c_str (), (spiffs_flags)FSFILE_READ, 0 );
116
138
if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ){
117
- debugf ( " stats errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
139
+ SPIFFS_API_DBG_E ( " fstat errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
118
140
}
119
- debugf (" FSFile name: %s, size: %d, type: %d\n " , _stats.name , _stats.size , _stats.type );
141
+ // debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
120
142
if (_stats.type == SPIFFS_TYPE_DIR){
121
143
SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
122
144
}
@@ -126,9 +148,9 @@ FSFile::FSFile(String path) {
126
148
FSFile::FSFile (file_t f) {
127
149
_file = f;
128
150
if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ){
129
- debugf ( " stats errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
151
+ SPIFFS_API_DBG_E ( " fstat errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
130
152
}
131
- debugf (" FSFile name: %s, size: %d, type: %d\n " , _stats.name , _stats.size , _stats.type );
153
+ // debugf("FSFile name: %s, size: %d, type: %d\n", _stats.name, _stats.size, _stats.type);
132
154
if (_stats.type == SPIFFS_TYPE_DIR){
133
155
SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
134
156
}
@@ -237,13 +259,13 @@ void FSFile::flush(){
237
259
bool FSFile::remove (){
238
260
if (! _file) return 0 ;
239
261
close ();
240
- return SPIFFS_remove (&_filesystemStorageHandle, (const char *)_stats.name ) == 0 ;
262
+ return SPIFFS_remove (&_filesystemStorageHandle, (char *)_stats.name ) == 0 ;
241
263
}
242
264
243
265
int FSFile::lastError (){
244
266
return SPIFFS_errno (&_filesystemStorageHandle);
245
267
}
246
268
247
269
void FSFile::clearError (){
248
- _filesystemStorageHandle.errno = SPIFFS_OK;
270
+ _filesystemStorageHandle.err_code = SPIFFS_OK;
249
271
}
0 commit comments