From 3cbfa2ffef2e9808310207ae791571b2ff13c85e Mon Sep 17 00:00:00 2001 From: thewavelength Date: Mon, 2 Nov 2020 17:47:36 +0100 Subject: [PATCH] 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. --- libraries/SPIFFS/src/SPIFFS.cpp | 35 ++++++++++++++++++++++++--------- libraries/SPIFFS/src/SPIFFS.h | 6 +++++- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/libraries/SPIFFS/src/SPIFFS.cpp b/libraries/SPIFFS/src/SPIFFS.cpp index db97fa2b..d3e16654 100644 --- a/libraries/SPIFFS/src/SPIFFS.cpp +++ b/libraries/SPIFFS/src/SPIFFS.cpp @@ -43,21 +43,38 @@ bool SPIFFSImpl::exists(const char* path) 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!"); return true; } esp_vfs_spiffs_conf_t conf = { .base_path = basePath, - .partition_label = NULL, + .partition_label = partitionLabel_, .max_files = maxOpenFiles, .format_if_mount_failed = false }; @@ -78,8 +95,8 @@ bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFi void SPIFFSFS::end() { - if(esp_spiffs_mounted(NULL)){ - esp_err_t err = esp_vfs_spiffs_unregister(NULL); + if(esp_spiffs_mounted(partitionLabel_)){ + esp_err_t err = esp_vfs_spiffs_unregister(partitionLabel_); if(err){ log_e("Unmounting SPIFFS failed! Error: %d", err); return; @@ -91,7 +108,7 @@ void SPIFFSFS::end() bool SPIFFSFS::format() { disableCore0WDT(); - esp_err_t err = esp_spiffs_format(NULL); + esp_err_t err = esp_spiffs_format(partitionLabel_); enableCore0WDT(); if(err){ log_e("Formatting SPIFFS failed! Error: %d", err); @@ -103,7 +120,7 @@ bool SPIFFSFS::format() size_t SPIFFSFS::totalBytes() { size_t total,used; - if(esp_spiffs_info(NULL, &total, &used)){ + if(esp_spiffs_info(partitionLabel_, &total, &used)){ return 0; } return total; @@ -112,7 +129,7 @@ size_t SPIFFSFS::totalBytes() size_t SPIFFSFS::usedBytes() { size_t total,used; - if(esp_spiffs_info(NULL, &total, &used)){ + if(esp_spiffs_info(partitionLabel_, &total, &used)){ return 0; } return used; diff --git a/libraries/SPIFFS/src/SPIFFS.h b/libraries/SPIFFS/src/SPIFFS.h index 7d35da04..4d7eb5da 100644 --- a/libraries/SPIFFS/src/SPIFFS.h +++ b/libraries/SPIFFS/src/SPIFFS.h @@ -23,11 +23,15 @@ class SPIFFSFS : public FS { public: 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(); size_t totalBytes(); size_t usedBytes(); void end(); + +private: + char * partitionLabel_; }; }