Add partition label argument to SPIFFS (#4443)

* Add partition label argument to SPIFFSSPIFFS currently assumes there is only ever one partition.This change allows a user to pass the label argument (defaults to NULL)to SPIFFS::begin() so a specific SPIFFS partition can be referenced.This change does not break compatibility.
This commit is contained in:
thewavelength 2020-11-02 17:47:36 +01:00 committed by GitHub
parent 360e04fa36
commit 3cbfa2ffef
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 10 deletions

View File

@ -43,21 +43,38 @@ bool SPIFFSImpl::exists(const char* path)
return (f == true) && !f.isDirectory(); return (f == true) && !f.isDirectory();
} }
SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl())) SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl())), partitionLabel_(NULL)
{ {
} }
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles) SPIFFSFS::~SPIFFSFS()
{ {
if(esp_spiffs_mounted(NULL)){ if (partitionLabel_){
free(partitionLabel_);
partitionLabel_ = NULL;
}
}
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel)
{
if (partitionLabel_){
free(partitionLabel_);
partitionLabel_ = NULL;
}
if (partitionLabel){
partitionLabel_ = strdup(partitionLabel);
}
if(esp_spiffs_mounted(partitionLabel_)){
log_w("SPIFFS Already Mounted!"); log_w("SPIFFS Already Mounted!");
return true; return true;
} }
esp_vfs_spiffs_conf_t conf = { esp_vfs_spiffs_conf_t conf = {
.base_path = basePath, .base_path = basePath,
.partition_label = NULL, .partition_label = partitionLabel_,
.max_files = maxOpenFiles, .max_files = maxOpenFiles,
.format_if_mount_failed = false .format_if_mount_failed = false
}; };
@ -78,8 +95,8 @@ bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFi
void SPIFFSFS::end() void SPIFFSFS::end()
{ {
if(esp_spiffs_mounted(NULL)){ if(esp_spiffs_mounted(partitionLabel_)){
esp_err_t err = esp_vfs_spiffs_unregister(NULL); esp_err_t err = esp_vfs_spiffs_unregister(partitionLabel_);
if(err){ if(err){
log_e("Unmounting SPIFFS failed! Error: %d", err); log_e("Unmounting SPIFFS failed! Error: %d", err);
return; return;
@ -91,7 +108,7 @@ void SPIFFSFS::end()
bool SPIFFSFS::format() bool SPIFFSFS::format()
{ {
disableCore0WDT(); disableCore0WDT();
esp_err_t err = esp_spiffs_format(NULL); esp_err_t err = esp_spiffs_format(partitionLabel_);
enableCore0WDT(); enableCore0WDT();
if(err){ if(err){
log_e("Formatting SPIFFS failed! Error: %d", err); log_e("Formatting SPIFFS failed! Error: %d", err);
@ -103,7 +120,7 @@ bool SPIFFSFS::format()
size_t SPIFFSFS::totalBytes() size_t SPIFFSFS::totalBytes()
{ {
size_t total,used; size_t total,used;
if(esp_spiffs_info(NULL, &total, &used)){ if(esp_spiffs_info(partitionLabel_, &total, &used)){
return 0; return 0;
} }
return total; return total;
@ -112,7 +129,7 @@ size_t SPIFFSFS::totalBytes()
size_t SPIFFSFS::usedBytes() size_t SPIFFSFS::usedBytes()
{ {
size_t total,used; size_t total,used;
if(esp_spiffs_info(NULL, &total, &used)){ if(esp_spiffs_info(partitionLabel_, &total, &used)){
return 0; return 0;
} }
return used; return used;

View File

@ -23,11 +23,15 @@ class SPIFFSFS : public FS
{ {
public: public:
SPIFFSFS(); SPIFFSFS();
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10); ~SPIFFSFS();
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10, const char * partitionLabel=NULL);
bool format(); bool format();
size_t totalBytes(); size_t totalBytes();
size_t usedBytes(); size_t usedBytes();
void end(); void end();
private:
char * partitionLabel_;
}; };
} }