-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfsxcore.ino
332 lines (262 loc) · 10.9 KB
/
fsxcore.ino
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
#include "fsx.h"
// SD Card | ESP32
// D2 12 // not for onebit
// D3 13 // not for onebit
// CMD 15
// VSS GND
// VDD 3.3V
// CLK 14
// VSS GND
// D0 2 (add 1K pull up after flashing)
// D1 43
std::vector<struct fsd> fsdlist;
//------------------------------------------------------------------------------
void refresh_fsd_usage(){
for ( auto &efes : fsdlist ){
switch ( efes.fsnumber ){
Serial.printf("fsnumber : %d\n", efes.fsnumber);
case FSDSD_MMC:
efes.totalBytes = SD_MMC.totalBytes(); efes.freeBytes = SD_MMC.totalBytes() - SD_MMC.usedBytes(); efes.usedBytes = SD_MMC.usedBytes();
break;
case FSDSPIFFS:
efes.totalBytes = SPIFFS.totalBytes(); efes.freeBytes = SPIFFS.totalBytes() - SPIFFS.usedBytes(); efes.usedBytes = SPIFFS.usedBytes();
break;
case FSDLITTLEFS:
efes.totalBytes = LITTLEFS.totalBytes(); efes.freeBytes = LITTLEFS.totalBytes() - LITTLEFS.usedBytes(); efes.usedBytes = LITTLEFS.usedBytes();
break;
case FSDFFAT:
efes.totalBytes = FFat.totalBytes(); efes.freeBytes = FFat.freeBytes(); efes.usedBytes = FFat.totalBytes()- FFat.freeBytes();
break;
default:
break;
}
}
}
//------------------------------------------------------------------------------
void mount_fs(){
String output;
if( SD_MMC.begin("/sdcard", true) ){ // onebit mode change to fals for 2bitmode
fsdlist.push_back( (struct fsd){ &SD_MMC,"SD_MMC","/sdcard",FSDSD_MMC,SD_MMC.totalBytes(),SD_MMC.totalBytes()-SD_MMC.usedBytes(), SD_MMC.usedBytes()} );
Serial.printf( "sd mmc totalbytes: %llu, used bytes %llu\n", SD_MMC.totalBytes(), SD_MMC.usedBytes() );
// delay(1000);
}
if( SPIFFS.begin() ){
fsdlist.push_back( (struct fsd){ &SPIFFS,"SPIFFS","/spiffs",FSDSPIFFS,SPIFFS.totalBytes(),SPIFFS.totalBytes()-SPIFFS.usedBytes(), SPIFFS.usedBytes()} );
// delay(1000);
}
if( LITTLEFS.begin() ){
fsdlist.push_back( (struct fsd){ &LITTLEFS, "LITTLEFS","/littlefs",FSDLITTLEFS, LITTLEFS.totalBytes(), LITTLEFS.totalBytes()-LITTLEFS.usedBytes(), LITTLEFS.usedBytes()} );
// delay(1000);
}
if( FFat.begin() ){
fsdlist.push_back( (struct fsd){ &FFat,"FFat","/ffat",FSDFFAT,FFat.totalBytes(),FFat.freeBytes(), FFat.totalBytes()- FFat.freeBytes()} );
// delay(1000);
}
if ( psramFound() ){
lister.open();
directory.open("/directory.html");
uploadform.open("/upload.html");
settingsform.open("/settings.html");
logo.open("/favicon.ico");
}
}
//------------------------------------------------------------------------------
char *nameoffs( fs::FS *ff ){
for ( auto &efes : fsdlist ){
if ( efes.f == ff )return efes.fsname;
}
return( NULL );
}
//------------------------------------------------------------------------------
void maketargetpath( String& filename, String& targetdir, String& newpath ){
newpath = targetdir + "/";
newpath += filename.substring( filename.lastIndexOf( '/' ) + 1 );
}
//------------------------------------------------------------------------------
fs::FS *fsfromfile( const char *fullfilename ){
for ( auto &efes : fsdlist ){
if ( strncmp( efes.fsmount, fullfilename,strlen( efes.fsmount ) ) == 0 )return efes.f;
}
return( NULL );
}
//---------------------------------------------------------------------------
bool isdir( const char *fullfilename ){
DIR *dir = opendir(fullfilename);
if (!dir){
//Serial.printf( "%s is not a directory. Cannot list.\n", basepath);
return(false);
}
closedir( dir );
return(true);
}
//---------------------------------------------------------------------------
bool sourceintarget( String& sourcepath, String& targetpath ){
if ( !isdir( sourcepath.c_str() ) )return(false);
if ( targetpath.startsWith( sourcepath + "/" ) ) return(true);
return(false);
}
//---------------------------------------------------------------------------
void deltree( fs::FS *ff, const char *path ){
fs::File dir = ff->open( path );
if ( !dir )return;
if(!dir.isDirectory()){
Serial.printf("%s is a file\n", path);
dir.close();
Serial.printf( "result of removing file %s: %d\n", path, ff->remove( path ) );
return;
}
Serial.printf("%s is a directory\n", path);
fs::File entry, nextentry;
while ( entry = dir.openNextFile() ){
if ( entry.isDirectory() ){
deltree( ff, entry.name() );
esp_task_wdt_reset();
} else{
char* tmpname = strdup( entry.name() );
entry.close();// openNextFile opens the file, LittleFS will refuse to delete the file if open;
// FAT doesn't mind
Serial.printf( "result of removing file %s: %d\n", tmpname, ff->remove( tmpname ) );
free( tmpname );
esp_task_wdt_reset();
}
}
dir.close();
Serial.printf( "result of removing directory %s: %d\n", path, ff->rmdir( path ) );
}
//------------------------------------------------------------------------------
// returns 0 - ok
// returns 1 - source directory not found
// returns 2 - source file not found by copyfile
// return 3 - source directory in target directory
// return 4 - no memory for read buffer
// return 5 - error reading source file
// return 6 - error writing target file
// return 7 - error removing target directory
int copytree( fs::FS *sourcefs, String &sourcefspath, fs::FS *targetfs, String &targetfspath){
int result;
if ( sourcefspath.length() == 0 ) sourcefspath = "/";
Serial.println("Copying tree from [" + sourcefspath + "]");
fs::File dir = sourcefs->open( sourcefspath );
if ( !dir ){
Serial.println("failed to open inputdirectory" + sourcefspath );
return(1);
}
// copyfile will copy the file if source is a file, or create a directory if source is a directory
result = copyfile( sourcefs, sourcefspath, targetfs, targetfspath);
esp_task_wdt_reset();
if(!dir.isDirectory() || result ){
if( !dir.isDirectory() ) Serial.println(sourcefspath + " is a file\n");
dir.close();
return( result);
}
Serial.println( sourcefspath + " is a directory\n");
fs::File entry;
while ( entry = dir.openNextFile() ){
String tmpname = String(entry.name());
entry.close();// openNextFile opens the file, LittleFS will refuse to delete the file if open;
// FAT doesn't mind
result = copytree( sourcefs, tmpname, targetfs, targetfspath + "/" + tmpname.substring( tmpname.lastIndexOf( '/' ) + 1 ) );
if ( result ) return( result );
}
dir.close();
return(0);
}
//------------------------------------------------------------------------------
int copyfile( fs::FS *sourcefs, String &sourcefspath, fs::FS *targetfs, String &targetfspath){
size_t filesize, bytesread=0, totalbytesread=0,totalbyteswritten=0, byteswritten=0, writeresult;
size_t readbuffer_size = (1024*32);
uint8_t *readbuffer;
Serial.println( "Copy "+ sourcefspath + " to "+ targetfspath );
File sourcefile = sourcefs->open ( sourcefspath, FILE_READ );
if ( !sourcefile ){ Serial.println( "Error opening "+ sourcefspath + "for read "); return( 2 );}
if ( sourcefile.isDirectory() ){
sourcefile.close();
if ( sourcefs == targetfs && sourceintarget( sourcefspath, targetfspath ) ){
return( 3);
}
if ( !targetfs->exists( targetfspath ) ){
if ( ! targetfs->mkdir( targetfspath ) )return( 7 );
}
}
if ( psramFound() ){
Serial.printf( "Allocating %u bytes of psram\n", readbuffer_size);
readbuffer = (uint8_t *)ps_malloc( readbuffer_size );
}else{
readbuffer_size = 1024;
Serial.printf( "Allocating %u bytes from heap\n", readbuffer_size);
readbuffer = (uint8_t *)malloc( readbuffer_size );
}
if ( ! readbuffer ){
sourcefile.close();
Serial.println( "Error allocating memory ");
return( 4 );
}
File targetfile = targetfs->open ( targetfspath, FILE_WRITE );
if ( !targetfile ){ sourcefile.close(); Serial.println( "Error opening "+ targetfspath + "for write"); return( false);}
filesize = sourcefile.size();
while( totalbyteswritten < filesize ){
bytesread = sourcefile.read( readbuffer, (filesize - totalbytesread)> readbuffer_size ? readbuffer_size :(filesize - totalbytesread) );
if ( bytesread < 0 ){
sourcefile.close();
targetfile.close();
free(readbuffer);
targetfs->remove( targetfspath );
Serial.println( "Error reading "+ sourcefspath);
return(5);
}
totalbytesread += bytesread;
esp_task_wdt_reset();
byteswritten = 0;
while( byteswritten < bytesread ){
writeresult= targetfile.write( &readbuffer[ byteswritten ], bytesread - byteswritten );
if ( writeresult == 0 ){
sourcefile.close();
targetfile.close();
free(readbuffer);
targetfs->remove( targetfspath );
Serial.println( "Error writing "+ targetfspath);
return(6);
}
byteswritten += writeresult;
}
totalbyteswritten += byteswritten;
}
sourcefile.close();
targetfile.close();
free(readbuffer);
return(0);
}
//------------------------------------------------------------------------------
size_t fileoffset( const char *fullfilename ){
const char *s;
for ( s= fullfilename + 1; *s != '/';++s );
return( s - fullfilename );
}
//------------------------------------------------------------------------------
int makepath( fs::FS &ff, String &pstring ){
char *s, *pathstring = (char *)pstring.c_str();
int mode=0, slashcount=0;
s = pathstring;
while ( *s ){
switch( mode ){
case 0: // if file system included switch mode at slashcount 2 instead of 1
if ( *s == '/')++slashcount;
if ( slashcount == 1 ){ mode = 1; }
break;
case 1:
if ( *s == '/'){
*s = 0;
if ( !ff.mkdir( pathstring ) ){
Serial.printf( "Failed to create directory %s\n", pathstring);
return(0);
}
Serial.printf( "Created directory %s\n", pathstring);
*s = '/';
}
break;
}
++s;
}
Serial.printf( "Directories created for %s\n", pathstring);
return(1);
}