Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 37108bb

Browse files
ficetoficeto
ficeto
authored and
ficeto
committed
Rework SPIFFS API to be more Arduino like
SD Style commands and Stream API
1 parent bf427f1 commit 37108bb

File tree

5 files changed

+295
-276
lines changed

5 files changed

+295
-276
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,178 +1,208 @@
1-
/****
2-
* Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3-
* Created 2015 by Skurydin Alexey
4-
* http://github.com/anakod/Sming
5-
* All files of the Sming Core are provided under the LGPL v3 license.
6-
****/
7-
1+
/*
2+
FileSystem.cpp - SPIFS implementation for esp8266
3+
4+
Copyright (c) 2015 Hristo Gochkov. All rights reserved.
5+
This file is part of the esp8266 core for Arduino environment.
6+
7+
This library is free software; you can redistribute it and/or
8+
modify it under the terms of the GNU Lesser General Public
9+
License as published by the Free Software Foundation; either
10+
version 2.1 of the License, or (at your option) any later version.
11+
12+
This library is distributed in the hope that it will be useful,
13+
but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
Lesser General Public License for more details.
16+
17+
You should have received a copy of the GNU Lesser General Public
18+
License along with this library; if not, write to the Free Software
19+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20+
*/
821
#include "FileSystem.h"
9-
#include "WString.h"
22+
#include "Arduino.h"
23+
24+
boolean FSClass::mount(){
25+
if(_mounted) return true;
26+
_mounted = spiffs_mount();
27+
return _mounted;
28+
}
29+
30+
void FSClass::unmount(){
31+
if(!_mounted) return;
32+
spiffs_unmount();
33+
_mounted = false;
34+
}
35+
36+
boolean FSClass::format(){
37+
return spiffs_format();
38+
}
39+
40+
boolean FSClass::exists(const char *filename){
41+
spiffs_stat stat = {0};
42+
if (SPIFFS_stat(&_filesystemStorageHandle, filename, &stat) < 0) return false;
43+
return stat.name[0] != '\0';
44+
}
1045

11-
file_t fileOpen(const String name, FileOpenFlags flags)
12-
{
46+
boolean FSClass::create(const char *filepath){
47+
return SPIFFS_creat(&_filesystemStorageHandle, filepath, 0) == 0;
48+
}
49+
50+
boolean FSClass::remove(const char *filepath){
51+
return SPIFFS_remove(&_filesystemStorageHandle, filepath) == 0;
52+
}
53+
54+
boolean FSClass::rename(const char *filename, const char *newname){
55+
return SPIFFS_rename(&_filesystemStorageHandle, filename, newname) == 0;
56+
}
57+
58+
FSFile FSClass::open(const char *filename, uint8_t mode){
1359
int repeats = 0;
1460
bool notExist;
15-
bool canRecreate = (flags & eFO_CreateIfNotExist) == eFO_CreateIfNotExist;
61+
bool canRecreate = (mode & SPIFFS_CREAT) == SPIFFS_CREAT;
1662
int res;
1763

18-
do
19-
{
20-
notExist = false;
21-
res = SPIFFS_open(&_filesystemStorageHandle, name.c_str(), (spiffs_flags)flags, 0);
22-
int code = SPIFFS_errno(&_filesystemStorageHandle);
23-
if (res < 0)
24-
{
25-
debugf("open errno %d\n", code);
26-
notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE);
27-
//debugf("recreate? %d %d %d", notExist, canRecreate, (repeats < 3));
28-
if (notExist && canRecreate)
29-
fileDelete(name); // fix for deleted files
30-
}
64+
do{
65+
notExist = false;
66+
res = SPIFFS_open(&_filesystemStorageHandle, filename, (spiffs_flags)mode, 0);
67+
int code = SPIFFS_errno(&_filesystemStorageHandle);
68+
if (res < 0){
69+
debugf("open errno %d\n", code);
70+
notExist = (code == SPIFFS_ERR_NOT_FOUND || code == SPIFFS_ERR_DELETED || code == SPIFFS_ERR_FILE_DELETED || code == SPIFFS_ERR_IS_FREE);
71+
if (notExist && canRecreate)
72+
remove(filename); // fix for deleted files
73+
}
3174
} while (notExist && canRecreate && repeats++ < 3);
32-
33-
return res;
75+
76+
if(res){
77+
return FSFile(res);
78+
}
79+
return FSFile();
3480
}
3581

36-
void fileClose(file_t file)
37-
{
38-
SPIFFS_close(&_filesystemStorageHandle, file);
82+
FSClass FS;
83+
84+
FSFile::FSFile(){
85+
_file = 0;
86+
_stats = {0};
3987
}
4088

41-
size_t fileWrite(file_t file, const void* data, size_t size)
42-
{
43-
int res = SPIFFS_write(&_filesystemStorageHandle, file, (void *)data, size);
44-
if (res < 0)
45-
{
46-
debugf("write errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
47-
return res;
89+
FSFile::FSFile(file_t f){
90+
_file = f;
91+
if(SPIFFS_fstat(&_filesystemStorageHandle, _file, &_stats) != 0){
92+
debugf("mount errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
4893
}
49-
return res;
5094
}
5195

52-
size_t fileRead(file_t file, void* data, size_t size)
53-
{
54-
int res = SPIFFS_read(&_filesystemStorageHandle, file, data, size);
55-
if (res < 0)
56-
{
57-
debugf("read errno %d\n", SPIFFS_errno(&_filesystemStorageHandle));
58-
return res;
59-
}
60-
return res;
96+
void FSFile::close(){
97+
if (! _file) return;
98+
SPIFFS_close(&_filesystemStorageHandle, _file);
99+
_file = 0;
61100
}
62101

63-
int fileSeek(file_t file, int offset, SeekOriginFlags origin)
64-
{
65-
return SPIFFS_lseek(&_filesystemStorageHandle, file, offset, origin);
102+
uint32_t FSFile::size(){
103+
if(! _file) return 0;
104+
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
105+
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
106+
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
107+
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
108+
return size;
66109
}
67110

68-
bool fileIsEOF(file_t file)
69-
{
70-
return SPIFFS_eof(&_filesystemStorageHandle, file);
111+
uint32_t FSFile::seek(uint32_t pos){
112+
if (! _file) return 0;
113+
return SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
71114
}
72115

73-
int32_t fileTell(file_t file)
74-
{
75-
return SPIFFS_tell(&_filesystemStorageHandle, file);
116+
uint32_t FSFile::position(){
117+
if (! _file) return 0;
118+
return SPIFFS_tell(&_filesystemStorageHandle, _file);
76119
}
77120

78-
int fileFlush(file_t file)
79-
{
80-
return SPIFFS_fflush(&_filesystemStorageHandle, file);
121+
boolean FSFile::eof(){
122+
if (! _file) return 0;
123+
return SPIFFS_eof(&_filesystemStorageHandle, _file);
81124
}
82125

83-
int fileStats(const String name, spiffs_stat *stat)
84-
{
85-
return SPIFFS_stat(&_filesystemStorageHandle, name.c_str(), stat);
126+
boolean FSFile::isDirectory(void){
127+
return false;
86128
}
87129

88-
int fileStats(file_t file, spiffs_stat *stat)
89-
{
90-
return SPIFFS_fstat(&_filesystemStorageHandle, file, stat);
130+
int FSFile::read(void *buf, uint16_t nbyte){
131+
if (! _file) return -1;
132+
return SPIFFS_read(&_filesystemStorageHandle, _file, buf, nbyte);
91133
}
92134

93-
void fileDelete(const String name)
94-
{
95-
SPIFFS_remove(&_filesystemStorageHandle, name.c_str());
135+
int FSFile::read(){
136+
if (! _file) return -1;
137+
int val;
138+
if(SPIFFS_read(&_filesystemStorageHandle, _file, &val, 1) != 1) return -1;
139+
return val;
96140
}
97141

98-
void fileDelete(file_t file)
99-
{
100-
SPIFFS_fremove(&_filesystemStorageHandle, file);
142+
int FSFile::peek() {
143+
if (! _file) return 0;
144+
int c = read();
145+
SPIFFS_lseek(&_filesystemStorageHandle, _file, -1, SPIFFS_SEEK_CUR);
146+
return c;
101147
}
102148

103-
bool fileExist(const String name)
104-
{
105-
spiffs_stat stat = {0};
106-
if (fileStats(name.c_str(), &stat) < 0) return false;
107-
return stat.name[0] != '\0';
149+
int FSFile::available() {
150+
if (! _file) return 0;
151+
uint32_t pos = SPIFFS_tell(&_filesystemStorageHandle, _file);
152+
SPIFFS_lseek(&_filesystemStorageHandle, _file, 0, SPIFFS_SEEK_END);
153+
uint32_t size = SPIFFS_tell(&_filesystemStorageHandle, _file);
154+
SPIFFS_lseek(&_filesystemStorageHandle, _file, pos, SPIFFS_SEEK_SET);
155+
return size - pos;
156+
}
157+
158+
size_t FSFile::write(const uint8_t *buf, size_t size){
159+
if (! _file) return 0;
160+
return SPIFFS_write(&_filesystemStorageHandle, _file, (uint8_t *)buf, size);
161+
}
162+
163+
size_t FSFile::write(uint8_t val) {
164+
if (! _file) return 0;
165+
return write(&val, 1);
108166
}
109167

168+
void FSFile::flush(){
169+
if (! _file) return;
170+
SPIFFS_fflush(&_filesystemStorageHandle, _file);
171+
}
110172

111-
int fileLastError(file_t fd)
112-
{
173+
uint32_t FSFile::remove(){
174+
if (! _file) return 0;
175+
return SPIFFS_fremove(&_filesystemStorageHandle, _file);
176+
_file = 0;
177+
}
178+
179+
int FSFile::lastError(){
113180
return SPIFFS_errno(&_filesystemStorageHandle);
114181
}
115182

116-
void fileClearLastError(file_t fd)
117-
{
183+
void FSFile::clearError(){
118184
_filesystemStorageHandle.errno = SPIFFS_OK;
119185
}
120186

121-
void fileSetContent(const String fileName, const char *content)
122-
{
123-
file_t file = fileOpen(fileName.c_str(), eFO_CreateNewAlways | eFO_WriteOnly);
124-
fileWrite(file, content, os_strlen(content));
125-
fileClose(file);
126-
}
127-
128-
uint32_t fileGetSize(const String fileName)
129-
{
130-
file_t file = fileOpen(fileName.c_str(), eFO_ReadOnly);
131-
// Get size
132-
fileSeek(file, 0, eSO_FileEnd);
133-
int size = fileTell(file);
134-
fileClose(file);
135-
return size;
136-
}
137-
138-
String fileGetContent(const String fileName)
139-
{
140-
file_t file = fileOpen(fileName.c_str(), eFO_ReadOnly);
141-
// Get size
142-
fileSeek(file, 0, eSO_FileEnd);
143-
int size = fileTell(file);
144-
if (size <= 0)
145-
{
146-
fileClose(file);
147-
return "";
148-
}
149-
fileSeek(file, 0, eSO_FileStart);
150-
char* buffer = new char[size + 1];
151-
buffer[size] = 0;
152-
fileRead(file, buffer, size);
153-
fileClose(file);
154-
String res = buffer;
155-
delete[] buffer;
156-
return res;
157-
}
158-
159-
int fileGetContent(const String fileName, char* buffer, int bufSize)
160-
{
161-
if (buffer == NULL || bufSize == 0) return 0;
162-
*buffer = 0;
163-
164-
file_t file = fileOpen(fileName.c_str(), eFO_ReadOnly);
165-
// Get size
166-
fileSeek(file, 0, eSO_FileEnd);
167-
int size = fileTell(file);
168-
if (size <= 0 || bufSize <= size)
169-
{
170-
fileClose(file);
171-
return 0;
172-
}
173-
buffer[size] = 0;
174-
fileSeek(file, 0, eSO_FileStart);
175-
fileRead(file, buffer, size);
176-
fileClose(file);
177-
return size;
187+
char * FSFile::name(){
188+
return 0;
189+
}
190+
191+
192+
193+
194+
195+
196+
/*
197+
spiffs_DIR *dirOpen(spiffs_DIR *d){
198+
return SPIFFS_opendir(&_filesystemStorageHandle, 0, d);
199+
}
200+
201+
int dirClose(spiffs_DIR *d){
202+
return SPIFFS_closedir(d);
203+
}
204+
205+
file_t dirOpenFile(spiffs_dirent* entry, uint8_t flags){
206+
return SPIFFS_open_by_dirent(&_filesystemStorageHandle, entry, (spiffs_flags)flags, 0);
178207
}
208+
*/

0 commit comments

Comments
 (0)