ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
littlefs_esp8266_filesystem .cpp
Go to the documentation of this file.
1 /*
2 littlefs_esp8266_filesystem.cpp - ESP3D littlefs filesystem configuration class
3 
4  Copyright (c) 2014 Luc Lebosse. All rights reserved.
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) any later version.
10 
11  This library is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public
17  License along with this library; if not, write to the Free Software
18  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 #include "../../../include/esp3d_config.h"
21 #if (FILESYSTEM_FEATURE == ESP_LITTLEFS_FILESYSTEM)
22 #include "../esp_filesystem.h"
23 #include "../../../core/genLinkedList.h"
24 #include <FS.h>
25 #include <LittleFS.h>
26 
27 Dir tDir_handle[ESP_MAX_OPENHANDLE];
28 extern File tFile_handle[ESP_MAX_OPENHANDLE];
29 
31 {
32  _started = LittleFS.begin();
33  return _started;
34 }
36 {
37  _started = false;
38  LittleFS.end();
39 }
40 
42 {
43  return totalBytes() - usedBytes();
44 }
45 
47 {
48  fs::FSInfo info;
49  LittleFS.info (info);
50  return info.totalBytes;
51 }
52 
54 {
55  fs::FSInfo info;
56  LittleFS.info (info);
57  return info.usedBytes;
58 }
59 
60 bool ESP_FileSystem::rename(const char *oldpath, const char *newpath)
61 {
62  return LittleFS.rename(oldpath,newpath);
63 }
64 
65 const char * ESP_FileSystem::FilesystemName()
66 {
67  return "LittleFS";
68 }
69 
71 {
72  return LittleFS.format();
73 }
74 
75 ESP_File ESP_FileSystem::open(const char* path, uint8_t mode)
76 {
77  //do some check
78  if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) {
79  return ESP_File();
80  }
81  // path must start by '/'
82  if (path[0] != '/') {
83  return ESP_File();
84  }
85  File ftmp = LittleFS.open(path, (mode == ESP_FILE_READ)?"r":(mode == ESP_FILE_WRITE)?"w":"a");
86  if(ftmp) {
87  log_esp3d("Success openening: %s", path);
88  if (ftmp.isFile()) {
89  log_esp3d("It is a file");
90  ESP_File esptmp(&ftmp, false,(mode == ESP_FILE_READ)?false:true, path);
91  return esptmp;
92  }
93  if (ftmp.isDirectory()) {
94  log_esp3d("It is a Directory");
95  }
96  ftmp.close();
97  }
98  log_esp3d("Opening as Directory");
99  Dir dtmp = LittleFS.openDir(path);
100  ESP_File esptmp(&dtmp, true, false, path);
101  return esptmp;
102 }
103 
104 bool ESP_FileSystem::exists(const char* path)
105 {
106  bool res = false;
107  //root should always be there if started
108  if (strcmp(path, "/") == 0) {
109  return _started;
110  }
111  String spath = path;
112  spath.trim();
113  if (spath[spath.length()-1] == '/') {
114  if (spath!="/") {
115  spath.remove(spath.length()-1);
116  }
117  }
118  return LittleFS.exists(spath.c_str());
119 }
120 
121 bool ESP_FileSystem::remove(const char *path)
122 {
123  return LittleFS.remove(path);
124 }
125 
126 bool ESP_FileSystem::mkdir(const char *path)
127 {
128  String spath = path;
129  spath.trim();
130  if (spath[spath.length()-1] == '/') {
131  if (spath!="/") {
132  spath.remove(spath.length()-1);
133  }
134  }
135  return LittleFS.mkdir(spath.c_str());
136 }
137 
138 bool ESP_FileSystem::rmdir(const char *path)
139 {
140  if (!exists(path)) {
141  return false;
142  }
143  bool res = true;
144  GenLinkedList<String > pathlist;
145  String spath = path;
146  spath.trim();
147  if (spath[spath.length()-1] != '/') {
148  spath+="/";
149  }
150  if (spath[0] != '/') {
151  spath ="/" + spath;
152  }
153  pathlist.push(spath);
154  while (pathlist.count() > 0) {
155  spath=pathlist.getLast();
156  bool candelete = true;
157  if (LittleFS.exists(spath.c_str())) {
158  Dir dir = LittleFS.openDir(pathlist.getLast().c_str());
159  while (dir.next()) {
160  if (dir.isDirectory()) {
161  candelete = false;
162  String newdir = pathlist.getLast() + dir.fileName() + "/";
163  pathlist.push(newdir);
164  } else {
165  log_esp3d("remove %s", dir.fileName().c_str());
166  String s = spath + dir.fileName();
167  LittleFS.remove(s);
168  }
169  }
170  }
171  if (candelete) {
172  if (spath !="/") {
173  if (spath[spath.length()-1] == '/') {
174  spath.remove(spath.length()-1);
175  }
176  if (LittleFS.exists(spath.c_str())) {
177  res = LittleFS.rmdir(spath.c_str());
178  }
179  log_esp3d("rmdir %s %d", spath.c_str(), res);
180  }
181  pathlist.pop();
182  }
183  }
184  return res;
185 }
186 
188 {
189  for (uint8_t i = 0; i < ESP_MAX_OPENHANDLE; i++) {
190  tDir_handle[i] = Dir();
191  tFile_handle[i].close();
192  tFile_handle[i] = File();
193  }
194 }
195 
196 ESP_File::ESP_File(void* handle, bool isdir, bool iswritemode, const char * path)
197 {
198  _isdir = isdir;
199  _dirlist = "";
200  _isfakedir = false;
201  _index = -1;
202  _filename = "";
203  _name = "";
204  _lastwrite = 0;
205  _iswritemode = iswritemode;
206  _size = 0;
207  if (!handle) {
208  return ;
209  }
210  bool set =false;
211  if (_isdir) {
212  for (uint8_t i=0; (i < ESP_MAX_OPENHANDLE) && !set; i++) {
213  if (tDir_handle[i].fileName().length() == 0) {
214  tDir_handle[i] = *((Dir *)handle);
215  _index = i;
216  //Path = filename
217  if (path) {
218  _filename = path;
219  _filename.trim();
220  if (!((_filename[_filename.length()-1] == '/') || (_filename == "/"))) {
221  _filename+="/";
222  }
223  //log_esp3d("Filename: %s", _filename.c_str());
224  //Name
225  if (_filename == "/") {
226  _name = "/";
227  } else {
228  _name.remove( 0, _name.lastIndexOf('/')+1);
229  }
230  }
231  //log_esp3d("Name: %s index: %d", _name.c_str(), _index);
232  set = true;
233  }
234  }
235  return;
236  }
237 
238  for (uint8_t i=0; (i < ESP_MAX_OPENHANDLE) && !set; i++) {
239  if (!tFile_handle[i]) {
240  tFile_handle[i] = *((File*)handle);
241  //filename
242  _filename = tFile_handle[i].name();
243  if (_isdir) {
244  if (!((_filename[_filename.length()-1] == '/') || (_filename == "/"))) {
245  _filename+="/";
246  }
247  }
248  //name
249  if (_filename == "/") {
250  _name = "/";
251  } else {
252  _name = _filename;
253  if (_name[0] == '/') {
254  _name.remove( 0, 1);
255  }
256  int pos = _name.lastIndexOf('/');
257  if (pos != -1) {
258  _name.remove( 0, pos+1);
259  }
260  }
261  //size
262  _size = tFile_handle[i].size();
263  //time
264  //TODO - not yet implemented in esp core
265  //_lastwrite = tFile_handle[i].getLastWrite();
266  _lastwrite = 0;
267  _index = i;
268  //log_esp3d("Opening File at index %d",_index);
269  set = true;
270  }
271  }
272 }
273 
274 void ESP_File::close()
275 {
276  if (_index != -1) {
277  if (_isdir) {
278  //log_esp3d("Closing Dir at index %d", _index);
279  tDir_handle[_index] = Dir();
280  _index = -1;
281  return;
282  }
283  //log_esp3d("Closing File at index %d", _index);
284  tFile_handle[_index].close();
285  //reopen if mode = write
286  //udate size + date
287  if (_iswritemode && !_isdir) {
288  File ftmp = LittleFS.open(_filename.c_str(), "r");
289  if (ftmp) {
290  _size = ftmp.size();
291  //TODO - not yet implemented in esp core
292  //_lastwrite = ftmp.getLastWrite();
293  _lastwrite = 0;
294  ftmp.close();
295  }
296  }
297  //log_esp3d("Closing File at index %d",_index);
298  _index = -1;
299  }
300 }
301 
303 {
304  if ((_index == -1) || !_isdir) {
305  log_esp3d("openNextFile failed");
306  return ESP_File();
307  }
308  if(tDir_handle[_index].next()) {
309  String name = tDir_handle[_index].fileName();
310  log_esp3d("Getting next file from %s", _filename.c_str());
311  log_esp3d("name :%s %s", name.c_str(), (tDir_handle[_index].isDirectory())?"isDir":"isFile");
312  String s = _filename;
313  if(s[s.length()-1]!='/') {
314  s+="/";
315  }
316  s+=name.c_str();
317  if (tDir_handle[_index].isFile()) {
318  ESP_File esptmp(name.c_str(), s.c_str(), false, tDir_handle[_index].fileSize()) ;
319  return esptmp;
320  } else {
321  log_esp3d("Found dir name: %s filename:%s",name.c_str(), s.c_str());
322  ESP_File esptmp = ESP_File(name.c_str(), s.c_str());
323  return esptmp;
324  }
325 
326  }
327  return ESP_File();
328 }
329 
330 #endif //ESP_LITTLEFS_FILESYSTEM
GenLinkedList
Definition: genLinkedList.h:30
ESP_File::close
void close()
ESP_FileSystem::closeAll
static void closeAll()
ESP_FileSystem::FilesystemName
static const char * FilesystemName()
ESP_GBFile::isDirectory
bool isDirectory()
Definition: esp_globalFS.cpp:485
ESP_FileSystem::begin
static bool begin()
ESP_FILE_WRITE
#define ESP_FILE_WRITE
Definition: defines.h:121
ESP_File::name
const char * name() const
Definition: esp_filesystem.cpp:123
ESP_FILE_READ
#define ESP_FILE_READ
Definition: defines.h:120
ESP_MAX_OPENHANDLE
#define ESP_MAX_OPENHANDLE
Definition: esp_filesystem.cpp:29
GenLinkedList::getLast
T getLast()
Definition: genLinkedList.h:215
ESP_FILE_APPEND
#define ESP_FILE_APPEND
Definition: defines.h:122
ESP_FileSystem::usedBytes
static size_t usedBytes()
GenLinkedList::push
bool push(T data)
Definition: genLinkedList.h:99
ESP_FileSystem::remove
static bool remove(const char *path)
ESP_FileSystem::totalBytes
static size_t totalBytes()
ESP_FileSystem::mkdir
static bool mkdir(const char *path)
GenLinkedList::pop
T pop()
Definition: genLinkedList.h:121
ESP_File::openNextFile
ESP_File openNextFile()
ESP_FileSystem::format
static bool format()
ESP_File
Definition: esp_filesystem.h:30
GenLinkedList::count
size_t count()
Definition: genLinkedList.h:93
tFile_handle
File tFile_handle[ESP_MAX_OPENHANDLE]
Definition: esp_filesystem.cpp:30
ESP_FileSystem::rmdir
static bool rmdir(const char *path)
ESP_FileSystem::rename
static bool rename(const char *oldpath, const char *newpath)
ESP_FileSystem::freeBytes
static size_t freeBytes()
log_esp3d
#define log_esp3d(format,...)
Definition: debug_esp3d.h:29
ESP_FileSystem::end
static void end()
ESP_File::ESP_File
ESP_File(void *handle=nullptr, bool isdir=false, bool iswritemode=false, const char *path=nullptr)
dir
FTPFile dir
Definition: FtpServer.cpp:103
ESP_FileSystem::exists
static bool exists(const char *path)
ESP_FileSystem::open
static ESP_File open(const char *path, uint8_t mode=ESP_FILE_READ)