Skip to content

Commit 3cbfa2f

Browse files
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.
1 parent 360e04f commit 3cbfa2f

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

Diff for: libraries/SPIFFS/src/SPIFFS.cpp

+26-9
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,38 @@ bool SPIFFSImpl::exists(const char* path)
4343
return (f == true) && !f.isDirectory();
4444
}
4545

46-
SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl()))
46+
SPIFFSFS::SPIFFSFS() : FS(FSImplPtr(new SPIFFSImpl())), partitionLabel_(NULL)
4747
{
4848

4949
}
5050

51-
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles)
51+
SPIFFSFS::~SPIFFSFS()
5252
{
53-
if(esp_spiffs_mounted(NULL)){
53+
if (partitionLabel_){
54+
free(partitionLabel_);
55+
partitionLabel_ = NULL;
56+
}
57+
}
58+
59+
bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel)
60+
{
61+
if (partitionLabel_){
62+
free(partitionLabel_);
63+
partitionLabel_ = NULL;
64+
}
65+
66+
if (partitionLabel){
67+
partitionLabel_ = strdup(partitionLabel);
68+
}
69+
70+
if(esp_spiffs_mounted(partitionLabel_)){
5471
log_w("SPIFFS Already Mounted!");
5572
return true;
5673
}
5774

5875
esp_vfs_spiffs_conf_t conf = {
5976
.base_path = basePath,
60-
.partition_label = NULL,
77+
.partition_label = partitionLabel_,
6178
.max_files = maxOpenFiles,
6279
.format_if_mount_failed = false
6380
};
@@ -78,8 +95,8 @@ bool SPIFFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFi
7895

7996
void SPIFFSFS::end()
8097
{
81-
if(esp_spiffs_mounted(NULL)){
82-
esp_err_t err = esp_vfs_spiffs_unregister(NULL);
98+
if(esp_spiffs_mounted(partitionLabel_)){
99+
esp_err_t err = esp_vfs_spiffs_unregister(partitionLabel_);
83100
if(err){
84101
log_e("Unmounting SPIFFS failed! Error: %d", err);
85102
return;
@@ -91,7 +108,7 @@ void SPIFFSFS::end()
91108
bool SPIFFSFS::format()
92109
{
93110
disableCore0WDT();
94-
esp_err_t err = esp_spiffs_format(NULL);
111+
esp_err_t err = esp_spiffs_format(partitionLabel_);
95112
enableCore0WDT();
96113
if(err){
97114
log_e("Formatting SPIFFS failed! Error: %d", err);
@@ -103,7 +120,7 @@ bool SPIFFSFS::format()
103120
size_t SPIFFSFS::totalBytes()
104121
{
105122
size_t total,used;
106-
if(esp_spiffs_info(NULL, &total, &used)){
123+
if(esp_spiffs_info(partitionLabel_, &total, &used)){
107124
return 0;
108125
}
109126
return total;
@@ -112,7 +129,7 @@ size_t SPIFFSFS::totalBytes()
112129
size_t SPIFFSFS::usedBytes()
113130
{
114131
size_t total,used;
115-
if(esp_spiffs_info(NULL, &total, &used)){
132+
if(esp_spiffs_info(partitionLabel_, &total, &used)){
116133
return 0;
117134
}
118135
return used;

Diff for: libraries/SPIFFS/src/SPIFFS.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ class SPIFFSFS : public FS
2323
{
2424
public:
2525
SPIFFSFS();
26-
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10);
26+
~SPIFFSFS();
27+
bool begin(bool formatOnFail=false, const char * basePath="/spiffs", uint8_t maxOpenFiles=10, const char * partitionLabel=NULL);
2728
bool format();
2829
size_t totalBytes();
2930
size_t usedBytes();
3031
void end();
32+
33+
private:
34+
char * partitionLabel_;
3135
};
3236

3337
}

0 commit comments

Comments
 (0)