From edbb71a017ea93f047d7b7b4953d878fd1be76f5 Mon Sep 17 00:00:00 2001 From: thewavelength Date: Sat, 24 Oct 2020 23:36:41 +0200 Subject: [PATCH 1/6] 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 | 22 ++++++++++++++-------- libraries/SPIFFS/src/SPIFFS.h | 5 ++++- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libraries/SPIFFS/src/SPIFFS.cpp b/libraries/SPIFFS/src/SPIFFS.cpp index db97fa2bf94..473c4d8abf4 100644 --- a/libraries/SPIFFS/src/SPIFFS.cpp +++ b/libraries/SPIFFS/src/SPIFFS.cpp @@ -48,16 +48,18 @@ SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl())) } -bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles) +bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel) { - if(esp_spiffs_mounted(NULL)){ + partitionLabel_ = 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 +80,9 @@ 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); + const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : 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 +94,8 @@ void SPIFFSFS::end() bool SPIFFSFS::format() { disableCore0WDT(); - esp_err_t err = esp_spiffs_format(NULL); + const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : NULL; + esp_err_t err = esp_spiffs_format(partitionLabel); enableCore0WDT(); if(err){ log_e("Formatting SPIFFS failed! Error: %d", err); @@ -103,7 +107,8 @@ bool SPIFFSFS::format() size_t SPIFFSFS::totalBytes() { size_t total,used; - if(esp_spiffs_info(NULL, &total, &used)){ + const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : NULL; + if(esp_spiffs_info(partitionLabel, &total, &used)){ return 0; } return total; @@ -112,7 +117,8 @@ size_t SPIFFSFS::totalBytes() size_t SPIFFSFS::usedBytes() { size_t total,used; - if(esp_spiffs_info(NULL, &total, &used)){ + const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : NULL; + 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 7d35da0439a..37e38ca5d02 100644 --- a/libraries/SPIFFS/src/SPIFFS.h +++ b/libraries/SPIFFS/src/SPIFFS.h @@ -23,11 +23,14 @@ class SPIFFSFS : public FS { public: SPIFFSFS(); - bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10); + 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: + String partitionLabel_; }; } From e9c9163bece967197d856e14c83c8b80d4f859cf Mon Sep 17 00:00:00 2001 From: thewavelength Date: Tue, 27 Oct 2020 16:09:28 +0100 Subject: [PATCH 2/6] Substitute String with strdup/char-array --- libraries/SPIFFS/src/SPIFFS.cpp | 28 +++++++++++++++------------- libraries/SPIFFS/src/SPIFFS.h | 3 ++- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/libraries/SPIFFS/src/SPIFFS.cpp b/libraries/SPIFFS/src/SPIFFS.cpp index 473c4d8abf4..200098a51e9 100644 --- a/libraries/SPIFFS/src/SPIFFS.cpp +++ b/libraries/SPIFFS/src/SPIFFS.cpp @@ -43,23 +43,29 @@ 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) { } +SPIFFSFS::~SPIFFSFS() +{ + delete partitionLabel_; + partitionLabel_ = NULL; +} + bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel) { - partitionLabel_ = partitionLabel; + partitionLabel_ = partitionLabel ? strdup(partitionLabel) : NULL; - if(esp_spiffs_mounted(partitionLabel)){ + if(esp_spiffs_mounted(partitionLabel_)){ log_w("SPIFFS Already Mounted!"); return true; } esp_vfs_spiffs_conf_t conf = { .base_path = basePath, - .partition_label = partitionLabel, + .partition_label = partitionLabel_, .max_files = maxOpenFiles, .format_if_mount_failed = false }; @@ -80,9 +86,8 @@ bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFi void SPIFFSFS::end() { - const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : NULL; - if(esp_spiffs_mounted(partitionLabel)){ - esp_err_t err = esp_vfs_spiffs_unregister(partitionLabel); + 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; @@ -94,8 +99,7 @@ void SPIFFSFS::end() bool SPIFFSFS::format() { disableCore0WDT(); - const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : NULL; - esp_err_t err = esp_spiffs_format(partitionLabel); + esp_err_t err = esp_spiffs_format(partitionLabel_); enableCore0WDT(); if(err){ log_e("Formatting SPIFFS failed! Error: %d", err); @@ -107,8 +111,7 @@ bool SPIFFSFS::format() size_t SPIFFSFS::totalBytes() { size_t total,used; - const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : NULL; - if(esp_spiffs_info(partitionLabel, &total, &used)){ + if(esp_spiffs_info(partitionLabel_, &total, &used)){ return 0; } return total; @@ -117,8 +120,7 @@ size_t SPIFFSFS::totalBytes() size_t SPIFFSFS::usedBytes() { size_t total,used; - const char * partitionLabel = partitionLabel_.length() ? partitionLabel_.c_str() : NULL; - if(esp_spiffs_info(partitionLabel, &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 37e38ca5d02..4d7eb5da688 100644 --- a/libraries/SPIFFS/src/SPIFFS.h +++ b/libraries/SPIFFS/src/SPIFFS.h @@ -23,6 +23,7 @@ class SPIFFSFS : public FS { public: SPIFFSFS(); + ~SPIFFSFS(); bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10, const char * partitionLabel=NULL); bool format(); size_t totalBytes(); @@ -30,7 +31,7 @@ class SPIFFSFS : public FS void end(); private: - String partitionLabel_; + char * partitionLabel_; }; } From 060c55f9330d9f67c0e254c4aa157dca90754ac8 Mon Sep 17 00:00:00 2001 From: thewavelength Date: Tue, 27 Oct 2020 21:11:20 +0100 Subject: [PATCH 3/6] Add null pointer check before delete --- libraries/SPIFFS/src/SPIFFS.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/libraries/SPIFFS/src/SPIFFS.cpp b/libraries/SPIFFS/src/SPIFFS.cpp index 200098a51e9..85d804589c7 100644 --- a/libraries/SPIFFS/src/SPIFFS.cpp +++ b/libraries/SPIFFS/src/SPIFFS.cpp @@ -50,7 +50,10 @@ SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl())), partitionLabel_(NULL) SPIFFSFS::~SPIFFSFS() { - delete partitionLabel_; + if (partitionLabel_){ + delete partitionLabel_; + } + partitionLabel_ = NULL; } From 0abd0dce732bf0859f02fd57a812cf8b26127cb2 Mon Sep 17 00:00:00 2001 From: thewavelength Date: Tue, 27 Oct 2020 21:12:45 +0100 Subject: [PATCH 4/6] Set to null only if delete was applied --- libraries/SPIFFS/src/SPIFFS.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/libraries/SPIFFS/src/SPIFFS.cpp b/libraries/SPIFFS/src/SPIFFS.cpp index 85d804589c7..0246a17e63d 100644 --- a/libraries/SPIFFS/src/SPIFFS.cpp +++ b/libraries/SPIFFS/src/SPIFFS.cpp @@ -52,9 +52,8 @@ SPIFFSFS::~SPIFFSFS() { if (partitionLabel_){ delete partitionLabel_; + partitionLabel_ = NULL; } - - partitionLabel_ = NULL; } bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel) From b1446b4196578c8af0900939a38c5e7819005244 Mon Sep 17 00:00:00 2001 From: thewavelength Date: Wed, 28 Oct 2020 04:00:36 +0100 Subject: [PATCH 5/6] Replace delete with free --- libraries/SPIFFS/src/SPIFFS.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries/SPIFFS/src/SPIFFS.cpp b/libraries/SPIFFS/src/SPIFFS.cpp index 0246a17e63d..76c6ac97cc5 100644 --- a/libraries/SPIFFS/src/SPIFFS.cpp +++ b/libraries/SPIFFS/src/SPIFFS.cpp @@ -51,7 +51,7 @@ SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl())), partitionLabel_(NULL) SPIFFSFS::~SPIFFSFS() { if (partitionLabel_){ - delete partitionLabel_; + free(partitionLabel_); partitionLabel_ = NULL; } } From 2cd4038d74801feca47608052c02e1d4e518bb28 Mon Sep 17 00:00:00 2001 From: thewavelength Date: Wed, 28 Oct 2020 13:23:37 +0100 Subject: [PATCH 6/6] Avoid memory leak --- libraries/SPIFFS/src/SPIFFS.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libraries/SPIFFS/src/SPIFFS.cpp b/libraries/SPIFFS/src/SPIFFS.cpp index 76c6ac97cc5..d3e1665491a 100644 --- a/libraries/SPIFFS/src/SPIFFS.cpp +++ b/libraries/SPIFFS/src/SPIFFS.cpp @@ -58,7 +58,14 @@ SPIFFSFS::~SPIFFSFS() bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel) { - partitionLabel_ = partitionLabel ? strdup(partitionLabel) : NULL; + if (partitionLabel_){ + free(partitionLabel_); + partitionLabel_ = NULL; + } + + if (partitionLabel){ + partitionLabel_ = strdup(partitionLabel); + } if(esp_spiffs_mounted(partitionLabel_)){ log_w("SPIFFS Already Mounted!");