-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspiffs_api.cpp
More file actions
213 lines (174 loc) · 5.47 KB
/
spiffs_api.cpp
File metadata and controls
213 lines (174 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include <SPIFFS.h>
#include "spiffs_api.h"
bool startSPIFFS() {
Serial.println("Initializing SPIFFS...");
if (!SPIFFS.begin(true)) {
Serial.println("SPIFFS Mount Failed");
return false;
}
size_t totalBytes, usedBytes;
if (getSPIFFSInfo(totalBytes, usedBytes)) {
Serial.println("SPIFFS Initialized Successfully");
Serial.printf("Total: %d bytes, Used: %d bytes, Free: %d bytes\n",
totalBytes, usedBytes, totalBytes - usedBytes);
}
return true;
}
// Legacy function for backward compatibility
bool mountSPIFFS() {
return startSPIFFS();
}
bool saveToSPIFFS(const String& path, const String& data) {
if (data.length() == 0) {
Serial.println("Warning: Attempting to save empty data");
return false;
}
// Check if there's enough space
if (!checkSPIFFSSpace(data.length())) {
Serial.println("Error: Not enough SPIFFS space");
return false;
}
File file = SPIFFS.open(path, FILE_WRITE);
if (!file) {
Serial.println("Error: Failed to open file for writing: " + path);
return false;
}
size_t bytesWritten = file.print(data);
file.close();
if (bytesWritten == data.length()) {
Serial.println("File saved: " + path + " (" + String(bytesWritten) + " bytes)");
return true;
} else {
Serial.println("Write error: " + path + " (wrote " + String(bytesWritten) + "/" + String(data.length()) + " bytes)");
return false;
}
}
void printFile(const String& path) {
readAndPrintFile(path);
}
void readAndPrintFile(const String& path) {
if (!SPIFFS.exists(path)) {
Serial.println("Error: File does not exist: " + path);
return;
}
File file = SPIFFS.open(path, FILE_READ);
if (!file) {
Serial.println("Error: Failed to open file for reading: " + path);
return;
}
Serial.println("\n=== File Content: " + path + " ===");
Serial.println("Size: " + String(file.size()) + " bytes");
Serial.println("Content:");
Serial.println("---");
while (file.available()) {
Serial.write(file.read());
}
Serial.println("\n=== End of File ===\n");
file.close();
}
void listSPIFFSFiles() {
Serial.println("\n=== SPIFFS File List ===");
File root = SPIFFS.open("/");
if (!root) {
Serial.println("Error: Failed to open root directory");
return;
}
if (!root.isDirectory()) {
Serial.println("Error: Root is not a directory");
root.close();
return;
}
File file = root.openNextFile();
int fileCount = 0;
size_t totalSize = 0;
while (file) {
if (file.isDirectory()) {
Serial.printf("[DIR] %s\n", file.name());
} else {
size_t fileSize = file.size();
totalSize += fileSize;
fileCount++;
Serial.printf("[FILE] %s (%d bytes)\n", file.name(), fileSize);
}
file = root.openNextFile();
}
root.close();
Serial.printf("\nTotal: %d files, %d bytes\n", fileCount, totalSize);
// Show SPIFFS usage
size_t total, used;
if (getSPIFFSInfo(total, used)) {
Serial.printf("SPIFFS: %d/%d bytes used (%.1f%%)\n",
used, total, (used * 100.0) / total);
}
Serial.println("========================\n");
}
bool getSPIFFSInfo(size_t& totalBytes, size_t& usedBytes) {
totalBytes = SPIFFS.totalBytes();
usedBytes = SPIFFS.usedBytes();
if (totalBytes == 0) {
Serial.println("Warning: SPIFFS appears to be uninitialized");
return false;
}
return true;
}
bool checkSPIFFSSpace(size_t requiredBytes) {
size_t totalBytes, usedBytes;
if (!getSPIFFSInfo(totalBytes, usedBytes)) {
return false;
}
size_t availableBytes = totalBytes - usedBytes;
// Add some safety margin (10% or 1KB, whichever is larger)
size_t safetyMargin = max(totalBytes / 10, (size_t)1024);
if (requiredBytes + safetyMargin > availableBytes) {
Serial.printf("Insufficient space: need %d bytes, available %d bytes (with %d bytes safety margin)\n",
requiredBytes, availableBytes, safetyMargin);
return false;
}
return true;
}
bool deleteSPIFFSFile(const String& path) {
if (!SPIFFS.exists(path)) {
Serial.println("File does not exist: " + path);
return false;
}
if (SPIFFS.remove(path)) {
Serial.println("File deleted: " + path);
return true;
} else {
Serial.println("Failed to delete file: " + path);
return false;
}
}
void formatSPIFFS() {
Serial.println("WARNING: Formatting SPIFFS will erase all data!");
Serial.println("This operation cannot be undone.");
Serial.println("Type 'YES' to confirm formatting:");
// Wait for confirmation
unsigned long startTime = millis();
String confirmation = "";
while (millis() - startTime < 30000) { // 30 second timeout
if (Serial.available()) {
confirmation = Serial.readStringUntil('\n');
confirmation.trim();
break;
}
delay(100);
}
if (confirmation == "YES") {
Serial.println("Formatting SPIFFS...");
if (SPIFFS.format()) {
Serial.println("SPIFFS formatted successfully");
// Remount after format
SPIFFS.end();
if (SPIFFS.begin(true)) {
Serial.println("SPIFFS remounted");
} else {
Serial.println("Failed to remount SPIFFS after format");
}
} else {
Serial.println("Failed to format SPIFFS");
}
} else {
Serial.println("Format cancelled");
}
}