#include"FS.h"#include"SPIFFS.h"/* You only need to format SPIFFS the first time you run a
test or else use the SPIFFS plugin to create a partition
https://github.com/me-no-dev/arduino-esp32fs-plugin */#define FORMAT_SPIFFS_IF_FAILED true
voidlistDir(fs::FS&fs,constchar*dirname,uint8_tlevels){Serial.printf("Listing directory: %s\r\n",dirname);Fileroot=fs.open(dirname);if(!root){Serial.println("- failed to open directory");return;}if(!root.isDirectory()){Serial.println(" - not a directory");return;}Filefile=root.openNextFile();while(file){if(file.isDirectory()){Serial.print(" DIR : ");Serial.println(file.name());if(levels){listDir(fs,file.path(),levels-1);}}else{Serial.print(" FILE: ");Serial.print(file.name());Serial.print("\tSIZE: ");Serial.println(file.size());}file=root.openNextFile();}}
Currently, SPIFFS does not support directories, it produces a flat structure. If SPIFFS is mounted under /spiffs, then creating a file with the path /spiffs/tmp/myfile.txt will create a file called /tmp/myfile.txt in SPIFFS, instead of myfile.txt in the directory /spiffs/tmp.
voidreadFile(fs::FS&fs,constchar*path){Serial.printf("Reading file: %s\r\n",path);Filefile=fs.open(path);if(!file||file.isDirectory()){Serial.println("- failed to open file for reading");return;}Serial.println("- read from file:");while(file.available()){Serial.write(file.read());}file.close();}
voidwriteFile(fs::FS&fs,constchar*path,constchar*message){Serial.printf("Writing file: %s\r\n",path);Filefile=fs.open(path,FILE_WRITE);if(!file){Serial.println("- failed to open file for writing");return;}if(file.print(message)){Serial.println("- file written");}else{Serial.println("- write failed");}file.close();}
voidappendFile(fs::FS&fs,constchar*path,constchar*message){Serial.printf("Appending to file: %s\r\n",path);Filefile=fs.open(path,FILE_APPEND);if(!file){Serial.println("- failed to open file for appending");return;}if(file.print(message)){Serial.println("- message appended");}else{Serial.println("- append failed");}file.close();}
voidtestFileIO(fs::FS&fs,constchar*path){Serial.printf("Testing file I/O with %s\r\n",path);staticuint8_tbuf[512];size_tlen=0;Filefile=fs.open(path,FILE_WRITE);if(!file){Serial.println("- failed to open file for writing");return;}size_ti;Serial.print("- writing");uint32_tstart=millis();for(i=0;i<2048;i++){if((i&0x001F)==0x001F){Serial.print(".");}file.write(buf,512);}Serial.println("");uint32_tend=millis()-start;Serial.printf(" - %u bytes written in %u ms\r\n",2048*512,end);file.close();file=fs.open(path);start=millis();end=start;i=0;if(file&&!file.isDirectory()){len=file.size();size_tflen=len;start=millis();Serial.print("- reading");while(len){size_ttoRead=len;if(toRead>512){toRead=512;}file.read(buf,toRead);if((i++&0x001F)==0x001F){Serial.print(".");}len-=toRead;}Serial.println("");end=millis()-start;Serial.printf("- %u bytes read in %u ms\r\n",flen,end);file.close();}else{Serial.println("- failed to open file for reading");}}
voidsetup(){Serial.begin(115200);if(!SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)){Serial.println("SPIFFS Mount Failed");return;}listDir(SPIFFS,"/",0);writeFile(SPIFFS,"/hello.txt","Hello ");appendFile(SPIFFS,"/hello.txt","World!\r\n");readFile(SPIFFS,"/hello.txt");renameFile(SPIFFS,"/hello.txt","/foo.txt");readFile(SPIFFS,"/foo.txt");deleteFile(SPIFFS,"/foo.txt");testFileIO(SPIFFS,"/test.txt");deleteFile(SPIFFS,"/test.txt");Serial.println("Test complete");}