ESP3D  3.0
Firmware for ESP boards connected to 3D Printer
fat_esp32_filesystem.cpp
Go to the documentation of this file.
1 /*
2 fat_esp32_filesystem.cpp - ESP3D fat 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_FAT_FILESYSTEM)
22 #include "../esp_filesystem.h"
23 #include "../../../core/genLinkedList.h"
24 #include <FS.h>
25 #include "FFat.h"
26 
27 extern File tFile_handle[ESP_MAX_OPENHANDLE];
28 
30 {
31  _started = FFat.begin();
32  return _started;
33 }
34 
36 {
37  FFat.end();
38  _started = false;
39 }
40 
42 {
43  return FFat.freeBytes();
44 }
45 
47 {
48  return FFat.totalBytes();
49 }
50 
52 {
53  return (FFat.totalBytes() - FFat.freeBytes());
54 }
55 
56 bool ESP_FileSystem::rename(const char *oldpath, const char *newpath)
57 {
58  return FFat.rename(oldpath,newpath);
59 }
60 
61 const char * ESP_FileSystem::FilesystemName()
62 {
63  return "FAT";
64 }
65 
67 {
68  return FFat.format();
69 }
70 
71 ESP_File ESP_FileSystem::open(const char* path, uint8_t mode)
72 {
73  //do some check
74  if(((strcmp(path,"/") == 0) && ((mode == ESP_FILE_WRITE) || (mode == ESP_FILE_APPEND))) || (strlen(path) == 0)) {
75  return ESP_File();
76  }
77  // path must start by '/'
78  if (path[0] != '/') {
79  return ESP_File();
80  }
81  if (mode != ESP_FILE_READ) {
82  //check container exists
83  String p = path;
84  p.remove(p.lastIndexOf('/') +1);
85  if (!exists(p.c_str())) {
86  log_esp3d("Error opening: %s", path);
87  return ESP_File();
88  }
89  }
90  File tmp = FFat.open(path, (mode == ESP_FILE_READ)?FILE_READ:(mode == ESP_FILE_WRITE)?FILE_WRITE:FILE_APPEND);
91  ESP_File esptmp(&tmp, tmp.isDirectory(),(mode == ESP_FILE_READ)?false:true, path);
92  return esptmp;
93 }
94 
95 bool ESP_FileSystem::exists(const char* path)
96 {
97  bool res = false;
98  //root should always be there if started
99  if (strcmp(path, "/") == 0) {
100  return _started;
101  }
102  res = FFat.exists(path);
103  if (!res) {
105  if (root) {
106  res = root.isDirectory();
107  }
108  }
109  return res;
110 }
111 
112 bool ESP_FileSystem::remove(const char *path)
113 {
114  return FFat.remove(path);
115 }
116 
117 bool ESP_FileSystem::mkdir(const char *path)
118 {
119  return FFat.mkdir(path);
120 }
121 
122 bool ESP_FileSystem::rmdir(const char *path)
123 {
124  if (!exists(path)) {
125  return false;
126  }
127  bool res = true;
128  GenLinkedList<String > pathlist;
129  String p = path;
130  pathlist.push(p);
131  while (pathlist.count() > 0) {
132  File dir = FFat.open(pathlist.getLast().c_str());
133  File f = dir.openNextFile();
134  bool candelete = true;
135  while (f) {
136  if (f.isDirectory()) {
137  candelete = false;
138  String newdir = f.name();
139  pathlist.push(newdir);
140  f.close();
141  f = File();
142  } else {
143  FFat.remove(f.name());
144  f.close();
145  f = dir.openNextFile();
146  }
147  }
148  if (candelete) {
149  if (pathlist.getLast() !="/") {
150  res = FFat.rmdir(pathlist.getLast().c_str());
151  }
152  pathlist.pop();
153  }
154  dir.close();
155  }
156  p = String();
157  log_esp3d("count %d", pathlist.count());
158  return res;
159 }
160 
162 {
163  for (uint8_t i = 0; i < ESP_MAX_OPENHANDLE; i++) {
164  tFile_handle[i].close();
165  tFile_handle[i] = File();
166  }
167 }
168 
169 ESP_File::ESP_File(void* handle, bool isdir, bool iswritemode, const char * path)
170 {
171  _isdir = isdir;
172  _dirlist = "";
173  _isfakedir = false;
174  _index = -1;
175  _filename = "";
176  _name = "";
177  _lastwrite = 0;
178  _iswritemode = iswritemode;
179  _size = 0;
180  if (!handle) {
181  return ;
182  }
183  bool set =false;
184  for (uint8_t i=0; (i < ESP_MAX_OPENHANDLE) && !set; i++) {
185  if (!tFile_handle[i]) {
186  tFile_handle[i] = *((File*)handle);
187  //filename
188  _filename = tFile_handle[i].name();
189 
190  //if root
191  if (_filename == "/") {
192  _filename = "/.";
193  }
194  if (_isdir) {
195  if (_filename[_filename.length()-1] != '.') {
196  if (_filename[_filename.length()-2] != '/') {
197  _filename+="/";
198  }
199  _filename+=".";
200  }
201  }
202  //name
203  if (_filename == "/.") {
204  _name = "/";
205  } else {
206  _name = _filename;
207  if (_name.endsWith("/.")) {
208  _name.remove( _name.length() - 2,2);
209  _isfakedir = true;
210  _isdir = true;
211  }
212  if (_name[0] == '/') {
213  _name.remove( 0, 1);
214  }
215  int pos = _name.lastIndexOf('/');
216  if (pos != -1) {
217  _name.remove( 0, pos+1);
218  }
219  }
220  //size
221  _size = tFile_handle[i].size();
222  //time
223  _lastwrite = tFile_handle[i].getLastWrite();
224  _index = i;
225  //log_esp3d("Opening File at index %d",_index);
226  set = true;
227  }
228  }
229 }
230 
231 void ESP_File::close()
232 {
233  if (_index != -1) {
234  //log_esp3d("Closing File at index %d", _index);
235  tFile_handle[_index].close();
236  //reopen if mode = write
237  //udate size + date
238  if (_iswritemode && !_isdir) {
239  File ftmp = FFat.open(_filename.c_str());
240  if (ftmp) {
241  _size = ftmp.size();
242  _lastwrite = ftmp.getLastWrite();
243  ftmp.close();
244  }
245  }
246  tFile_handle[_index] = File();
247  //log_esp3d("Closing File at index %d",_index);
248  _index = -1;
249  }
250 }
251 
253 {
254  if ((_index == -1) || !_isdir) {
255  log_esp3d("openNextFile failed");
256  return ESP_File();
257  }
258  File tmp = tFile_handle[_index].openNextFile();
259  while (tmp) {
260  log_esp3d("tmp name :%s %s", tmp.name(), (tmp.isDirectory())?"isDir":"isFile");
261  ESP_File esptmp(&tmp, tmp.isDirectory());
262  esptmp.close();
263  String sub = esptmp.filename();
264  sub.remove(0,_filename.length()-1);
265  int pos = sub.indexOf("/");
266  if (pos!=-1) {
267  //is subdir
268  sub = sub.substring(0,pos);
269  //log_esp3d("file name:%s name: %s %s sub:%s root:%s", esptmp.filename(), esptmp.name(), (esptmp.isDirectory())?"isDir":"isFile", sub.c_str(), _filename.c_str());
270  String tag = "*" + sub + "*";
271  //test if already in directory list
272  if (_dirlist.indexOf(tag) == -1) {//not in list so add it and return the info
273  _dirlist+= tag;
274  String fname = _filename.substring(0,_filename.length()-1) + sub + "/.";
275  //log_esp3d("Found dir name: %s filename:%s", sub.c_str(), fname.c_str());
276  esptmp = ESP_File(sub.c_str(), fname.c_str());
277  return esptmp;
278  } else { //already in list so ignore it
279  //log_esp3d("Dir name: %s already in list", sub.c_str());
280  tmp = tFile_handle[_index].openNextFile();
281  }
282  } else { //is file
283  //log_esp3d("file name:%s name: %s %s sub:%s root:%s", esptmp.filename(), esptmp.name(), (esptmp.isDirectory())?"isDir":"isFile", sub.c_str(), _filename.c_str());
284  if (sub == ".") {
285  //log_esp3d("Dir tag, ignore it");
286  tmp = tFile_handle[_index].openNextFile();
287  } else {
288  return esptmp;
289  }
290  }
291 
292  }
293  return ESP_File();
294 }
295 
296 
297 #endif //ESP_FAT_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_FileSystem::begin
static bool begin()
ESP_FILE_WRITE
#define ESP_FILE_WRITE
Definition: defines.h:121
ESP_GBFile::openNextFile
ESP_GBFile openNextFile()
Definition: esp_globalFS.cpp:711
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()
ESP_GBFile::close
void close()
Definition: esp_globalFS.cpp:390
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)
ESP_GBFile::name
const char * name() const
Definition: esp_globalFS.cpp:429
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)
ESP_File::isDirectory
bool isDirectory()
Definition: esp_filesystem.cpp:133