@@ -41,6 +41,10 @@ bool FSClass::format() {
4141 return spiffs_format ();
4242}
4343
44+ bool FSClass::check () {
45+ return SPIFFS_check (&_filesystemStorageHandle) == 0 ;
46+ }
47+
4448bool FSClass::exists (const char *filename) {
4549 spiffs_stat stat = {0 };
4650 if (SPIFFS_stat (&_filesystemStorageHandle, filename, &stat) < 0 )
@@ -61,6 +65,7 @@ bool FSClass::rename(const char *filename, const char *newname) {
6165}
6266
6367FSFile FSClass::open (const char *filename, uint8_t mode) {
68+ if (String (filename) == " " || String (filename) == " /" ) return FSFile (" /" );
6469 int repeats = 0 ;
6570 bool notExist;
6671 bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT;
@@ -84,95 +89,140 @@ FSFile FSClass::open(const char *filename, uint8_t mode) {
8489 return FSFile ();
8590}
8691
92+ FSFile FSClass::open (spiffs_dirent* entry, uint8_t mode){
93+ int res = SPIFFS_open_by_dirent (&_filesystemStorageHandle, entry, (spiffs_flags)mode, 0 );
94+ if (res){
95+ return FSFile (res);
96+ }
97+ return FSFile ();
98+ }
99+
87100FSClass FS;
88101
89102FSFile::FSFile () {
90103 _file = 0 ;
91104 _stats = {0 };
92105}
93106
107+ FSFile::FSFile (String path) {
108+ if (path == " /" ){
109+ _file = 0x1 ;
110+ os_sprintf ((char *)_stats.name , " %s" , (char *)path.c_str ());
111+ _stats.size = 0 ;
112+ _stats.type = SPIFFS_TYPE_DIR;
113+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
114+ } else {
115+ _file = SPIFFS_open (&_filesystemStorageHandle, path.c_str (), (spiffs_flags)FSFILE_READ, 0 );
116+ if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ){
117+ debugf (" stats errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
118+ }
119+ debugf (" FSFile name: %s, size: %d, type: %d\n " , _stats.name , _stats.size , _stats.type );
120+ if (_stats.type == SPIFFS_TYPE_DIR){
121+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
122+ }
123+ }
124+ }
125+
94126FSFile::FSFile (file_t f) {
95127 _file = f;
96128 if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ){
97- debugf (" mount errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
129+ debugf (" stats errno %d\n " , SPIFFS_errno (&_filesystemStorageHandle));
130+ }
131+ debugf (" FSFile name: %s, size: %d, type: %d\n " , _stats.name , _stats.size , _stats.type );
132+ if (_stats.type == SPIFFS_TYPE_DIR){
133+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
98134 }
99135}
100136
101137void FSFile::close () {
102138 if (! _file) return ;
103- SPIFFS_close (&_filesystemStorageHandle, _file);
139+ if (_stats.type == SPIFFS_TYPE_DIR){
140+ SPIFFS_closedir (&_dir);
141+ }
142+ if (os_strlen ((char *)_stats.name ) > 1 )
143+ SPIFFS_close (&_filesystemStorageHandle, _file);
104144 _file = 0 ;
105145}
106146
147+ void FSFile::rewindDirectory () {
148+ if (! _file || !isDirectory ()) return ;
149+ SPIFFS_closedir (&_dir);
150+ SPIFFS_opendir (&_filesystemStorageHandle, (char *)_stats.name , &_dir);
151+ }
152+
153+ FSFile FSFile::openNextFile (){
154+ if (! _file || !isDirectory ()) return FSFile ();
155+ struct spiffs_dirent e;
156+ struct spiffs_dirent *pe = &e;
157+ if ((pe = SPIFFS_readdir (&_dir, pe))){
158+ return FS.open ((char *)pe->name );
159+ }
160+ return FSFile ();
161+ }
162+
107163uint32_t FSFile::size () {
108164 if (! _file) return 0 ;
109- uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
110- SPIFFS_lseek (&_filesystemStorageHandle, _file, 0 , SPIFFS_SEEK_END);
111- uint32_t size = SPIFFS_tell (&_filesystemStorageHandle, _file);
112- SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
113- return size;
165+ if (SPIFFS_fstat (&_filesystemStorageHandle, _file, &_stats) != 0 ) return 0 ;
166+ return _stats.size ;
114167}
115168
116169uint32_t FSFile::seek (uint32_t pos) {
117- if (! _file) return 0 ;
170+ if (! _file || isDirectory () ) return 0 ;
118171 return SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
119172}
120173
121174uint32_t FSFile::position () {
122- if (! _file) return 0 ;
175+ if (! _file || isDirectory () ) return 0 ;
123176 return SPIFFS_tell (&_filesystemStorageHandle, _file);
124177}
125178
126179bool FSFile::eof () {
127- if (! _file) return 0 ;
180+ if (! _file || isDirectory () ) return 0 ;
128181 return SPIFFS_eof (&_filesystemStorageHandle, _file);
129182}
130183
131184bool FSFile::isDirectory (void ) {
132- return false ;
185+ return _stats. type == SPIFFS_TYPE_DIR ;
133186}
134187
135188int FSFile::read (void *buf, uint16_t nbyte) {
136- if (! _file) return -1 ;
189+ if (! _file || isDirectory () ) return -1 ;
137190 return SPIFFS_read (&_filesystemStorageHandle, _file, buf, nbyte);
138191}
139192
140193int FSFile::read () {
141- if (! _file) return -1 ;
194+ if (! _file || isDirectory () ) return -1 ;
142195 int val;
143196 if (SPIFFS_read (&_filesystemStorageHandle, _file, &val, 1 ) != 1 ) return -1 ;
144197 return val;
145198}
146199
147200int FSFile::peek () {
148- if (! _file) return 0 ;
201+ if (! _file || isDirectory () ) return 0 ;
149202 int c = read ();
150203 SPIFFS_lseek (&_filesystemStorageHandle, _file, -1 , SPIFFS_SEEK_CUR);
151204 return c;
152205}
153206
154207int FSFile::available () {
155- if (! _file) return 0 ;
208+ if (! _file || isDirectory () ) return 0 ;
156209 uint32_t pos = SPIFFS_tell (&_filesystemStorageHandle, _file);
157- SPIFFS_lseek (&_filesystemStorageHandle, _file, 0 , SPIFFS_SEEK_END);
158- uint32_t size = SPIFFS_tell (&_filesystemStorageHandle, _file);
159- SPIFFS_lseek (&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
160- return size - pos;
210+ return _stats.size - pos;
161211}
162212
163213size_t FSFile::write (const uint8_t *buf, size_t size){
164- if (! _file) return 0 ;
214+ if (! _file || isDirectory () ) return 0 ;
165215 int res = SPIFFS_write (&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
166216 return (res > 0 )?(size_t )res:0 ;
167217}
168218
169219size_t FSFile::write (uint8_t val) {
170- if (! _file) return 0 ;
220+ if (! _file || isDirectory () ) return 0 ;
171221 return write (&val, 1 );
172222}
173223
174224void FSFile::flush (){
175- if (! _file) return ;
225+ if (! _file || isDirectory () ) return ;
176226 SPIFFS_fflush (&_filesystemStorageHandle, _file);
177227}
178228
@@ -191,24 +241,5 @@ void FSFile::clearError(){
191241}
192242
193243char * FSFile::name (){
194- return 0 ;
244+ return ( char *)_stats. name ;
195245}
196-
197-
198-
199-
200-
201-
202- /*
203- spiffs_DIR *dirOpen(spiffs_DIR *d){
204- return SPIFFS_opendir(&_filesystemStorageHandle, 0, d);
205- }
206-
207- int dirClose(spiffs_DIR *d){
208- return SPIFFS_closedir(d);
209- }
210-
211- file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){
212- return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0);
213- }
214- */
0 commit comments