1
+ // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2
+ //
3
+ // Licensed under the Apache License, Version 2.0 (the "License");
4
+ // you may not use this file except in compliance with the License.
5
+ // You may obtain a copy of the License at
6
+ //
7
+ // http://www.apache.org/licenses/LICENSE-2.0
8
+ //
9
+ // Unless required by applicable law or agreed to in writing, software
10
+ // distributed under the License is distributed on an "AS IS" BASIS,
11
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ // See the License for the specific language governing permissions and
13
+ // limitations under the License.
14
+
15
+ #ifdef ESP32
16
+
17
+ #include " vfs_api.h"
18
+
19
+ extern " C" {
20
+ #include < sys/unistd.h>
21
+ #include < sys/stat.h>
22
+ #include < dirent.h>
23
+ #include " esp_spiffs.h"
24
+ }
25
+
26
+ #include " CustomSPIFFS.h"
27
+
28
+ using namespace fs ;
29
+
30
+ class CustomSPIFFSImpl : public VFSImpl
31
+ {
32
+ public:
33
+ CustomSPIFFSImpl ();
34
+ virtual ~CustomSPIFFSImpl () { }
35
+ virtual bool exists (const char * path);
36
+ };
37
+
38
+ CustomSPIFFSImpl::CustomSPIFFSImpl ()
39
+ {
40
+ }
41
+
42
+ bool CustomSPIFFSImpl::exists (const char * path)
43
+ {
44
+ File f = open (path, " r" );
45
+ return (f == true ) && !f.isDirectory ();
46
+ }
47
+
48
+ CustomSPIFFSFS::CustomSPIFFSFS () : FS(FSImplPtr(new CustomSPIFFSImpl()))
49
+ {
50
+ this ->_partition = NULL ;
51
+ }
52
+
53
+ bool CustomSPIFFSFS::begin (bool formatOnFail, const char * partition, const char * basePath, uint8_t maxOpenFiles)
54
+ {
55
+ this ->_partition = partition;
56
+
57
+ if (esp_spiffs_mounted (_partition)){
58
+ log_w (" SPIFFS Already Mounted!" );
59
+ return true ;
60
+ }
61
+
62
+ esp_vfs_spiffs_conf_t conf = {
63
+ .base_path = basePath,
64
+ .partition_label = _partition,
65
+ .max_files = maxOpenFiles,
66
+ .format_if_mount_failed = false
67
+ };
68
+
69
+ esp_err_t err = esp_vfs_spiffs_register (&conf);
70
+ if (err == ESP_FAIL && formatOnFail){
71
+ if (format ()){
72
+ err = esp_vfs_spiffs_register (&conf);
73
+ }
74
+ }
75
+ if (err != ESP_OK){
76
+ log_e (" Mounting SPIFFS failed! Error: %d" , err);
77
+ return false ;
78
+ }
79
+ _impl->mountpoint (basePath);
80
+ return true ;
81
+ }
82
+
83
+ void CustomSPIFFSFS::end ()
84
+ {
85
+ if (esp_spiffs_mounted (_partition)){
86
+ esp_err_t err = esp_vfs_spiffs_unregister (_partition);
87
+ if (err){
88
+ log_e (" Unmounting SPIFFS failed! Error: %d" , err);
89
+ return ;
90
+ }
91
+ _impl->mountpoint (_partition);
92
+ }
93
+ }
94
+
95
+ bool CustomSPIFFSFS::format ()
96
+ {
97
+ disableCore0WDT ();
98
+ esp_err_t err = esp_spiffs_format (_partition);
99
+ enableCore0WDT ();
100
+ if (err){
101
+ log_e (" Formatting SPIFFS failed! Error: %d" , err);
102
+ return false ;
103
+ }
104
+ return true ;
105
+ }
106
+
107
+ size_t CustomSPIFFSFS::totalBytes ()
108
+ {
109
+ size_t total,used;
110
+ if (esp_spiffs_info (_partition, &total, &used)){
111
+ return 0 ;
112
+ }
113
+ return total;
114
+ }
115
+
116
+ size_t CustomSPIFFSFS::usedBytes ()
117
+ {
118
+ size_t total,used;
119
+ if (esp_spiffs_info (_partition, &total, &used)){
120
+ return 0 ;
121
+ }
122
+ return used;
123
+ }
124
+ #endif
0 commit comments