Skip to content

Commit 9a518cd

Browse files
authored
LITTLEFS update - partition label and multiple partitions, idea copied from SPIFFS (#5023)
Note, maxOpenFiles parameter is unused but kept for compatibility.
1 parent 81b7c47 commit 9a518cd

File tree

4 files changed

+80
-19
lines changed

4 files changed

+80
-19
lines changed

Diff for: libraries/LITTLEFS/examples/LITTLEFS_test/LITTLEFS_test.ino

+21-5
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44

55
/* You only need to format LITTLEFS the first time you run a
66
test or else use the LITTLEFS plugin to create a partition
7-
https://github.com/lorol/arduino-esp32littlefs-plugin */
7+
https://github.com/lorol/arduino-esp32littlefs-plugin
88
9+
If you test two partitions, you need to use a custom
10+
partition.csv file, see in the sketch folder */
11+
12+
//#define TWOPART
13+
914
#define FORMAT_LITTLEFS_IF_FAILED true
1015

1116
void listDir(fs::FS &fs, const char * dirname, uint8_t levels){
@@ -123,9 +128,8 @@ void deleteFile(fs::FS &fs, const char * path){
123128
}
124129
}
125130

126-
// SPIFFS-like write and delete file
131+
// SPIFFS-like write and delete file, better use #define CONFIG_LITTLEFS_SPIFFS_COMPAT 1
127132

128-
// See: https://github.com/esp8266/Arduino/blob/master/libraries/LittleFS/src/LittleFS.cpp#L60
129133
void writeFile2(fs::FS &fs, const char * path, const char * message){
130134
if(!fs.exists(path)){
131135
if (strchr(path, '/')) {
@@ -158,7 +162,6 @@ void writeFile2(fs::FS &fs, const char * path, const char * message){
158162
file.close();
159163
}
160164

161-
// See: https://github.com/esp8266/Arduino/blob/master/libraries/LittleFS/src/LittleFS.h#L149
162165
void deleteFile2(fs::FS &fs, const char * path){
163166
Serial.printf("Deleting file and empty folders on path: %s\r\n", path);
164167

@@ -239,6 +242,19 @@ void testFileIO(fs::FS &fs, const char * path){
239242

240243
void setup(){
241244
Serial.begin(115200);
245+
246+
#ifdef TWOPART
247+
if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED, "/lfs2", 5, "part2")){
248+
Serial.println("part2 Mount Failed");
249+
return;
250+
}
251+
appendFile(LITTLEFS, "/hello0.txt", "World0!\r\n");
252+
readFile(LITTLEFS, "/hello0.txt");
253+
LITTLEFS.end();
254+
255+
Serial.println( "Done with part2, work with the first lfs partition..." );
256+
#endif
257+
242258
if(!LITTLEFS.begin(FORMAT_LITTLEFS_IF_FAILED)){
243259
Serial.println("LITTLEFS Mount Failed");
244260
return;
@@ -264,7 +280,7 @@ void setup(){
264280
testFileIO(LITTLEFS, "/test.txt");
265281
deleteFile(LITTLEFS, "/test.txt");
266282

267-
Serial.println( "Test complete" );
283+
Serial.println( "Test complete" );
268284
}
269285

270286
void loop(){
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Name, Type, SubType, Offset, Size, Flags
2+
nvs, data, nvs, 0x9000, 0x5000,
3+
otadata, data, ota, 0xe000, 0x2000,
4+
app0, app, ota_0, 0x10000, 0x100000,
5+
app1, app, ota_1, ,0x100000,
6+
spiffs, data, spiffs, ,0x1D0000,
7+
part2, data, spiffs, ,0x20000,
8+
#1048576

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

+46-13
Original file line numberDiff line numberDiff line change
@@ -12,38 +12,71 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
static constexpr const char LFS_NAME[] = "spiffs";
15+
1616

1717
#include "vfs_api.h"
1818

1919
extern "C" {
2020
#include <sys/unistd.h>
2121
#include <sys/stat.h>
2222
#include <dirent.h>
23-
#undef B110
24-
#undef B1000000
25-
#include "esp_littlefs.h"
23+
#include "esp_littlefs.h"
2624
}
2725

2826
#include "LITTLEFS.h"
2927

3028
using namespace fs;
3129

32-
LITTLEFSFS::LITTLEFSFS() : FS(FSImplPtr(new VFSImpl()))
30+
class LITTLEFSImpl : public VFSImpl
3331
{
32+
public:
33+
LITTLEFSImpl();
34+
virtual ~LITTLEFSImpl() { }
35+
virtual bool exists(const char* path);
36+
};
3437

38+
LITTLEFSImpl::LITTLEFSImpl()
39+
{
40+
}
41+
42+
bool LITTLEFSImpl::exists(const char* path)
43+
{
44+
File f = open(path, "r");
45+
return (f == true);
3546
}
3647

37-
bool LITTLEFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFilesUnused)
48+
LITTLEFSFS::LITTLEFSFS() : FS(FSImplPtr(new LITTLEFSImpl())), partitionLabel_(NULL)
3849
{
39-
if(esp_littlefs_mounted(LFS_NAME)){
50+
}
51+
52+
LITTLEFSFS::~LITTLEFSFS()
53+
{
54+
if (partitionLabel_){
55+
free(partitionLabel_);
56+
partitionLabel_ = NULL;
57+
}
58+
}
59+
60+
bool LITTLEFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpenFiles, const char * partitionLabel)
61+
{
62+
63+
if (partitionLabel_){
64+
free(partitionLabel_);
65+
partitionLabel_ = NULL;
66+
}
67+
68+
if (partitionLabel){
69+
partitionLabel_ = strdup(partitionLabel);
70+
}
71+
72+
if(esp_littlefs_mounted(partitionLabel_)){
4073
log_w("LITTLEFS Already Mounted!");
4174
return true;
4275
}
4376

4477
esp_vfs_littlefs_conf_t conf = {
4578
.base_path = basePath,
46-
.partition_label = LFS_NAME,
79+
.partition_label = partitionLabel_,
4780
.format_if_mount_failed = false
4881
};
4982

@@ -63,8 +96,8 @@ bool LITTLEFSFS::begin(bool formatOnFail, const char * basePath, uint8_t maxOpen
6396

6497
void LITTLEFSFS::end()
6598
{
66-
if(esp_littlefs_mounted(LFS_NAME)){
67-
esp_err_t err = esp_vfs_littlefs_unregister(LFS_NAME);
99+
if(esp_littlefs_mounted(partitionLabel_)){
100+
esp_err_t err = esp_vfs_littlefs_unregister(partitionLabel_);
68101
if(err){
69102
log_e("Unmounting LITTLEFS failed! Error: %d", err);
70103
return;
@@ -76,7 +109,7 @@ void LITTLEFSFS::end()
76109
bool LITTLEFSFS::format()
77110
{
78111
disableCore0WDT();
79-
esp_err_t err = esp_littlefs_format(LFS_NAME);
112+
esp_err_t err = esp_littlefs_format(partitionLabel_);
80113
enableCore0WDT();
81114
if(err){
82115
log_e("Formatting LITTLEFS failed! Error: %d", err);
@@ -88,7 +121,7 @@ bool LITTLEFSFS::format()
88121
size_t LITTLEFSFS::totalBytes()
89122
{
90123
size_t total,used;
91-
if(esp_littlefs_info(LFS_NAME, &total, &used)){
124+
if(esp_littlefs_info(partitionLabel_, &total, &used)){
92125
return 0;
93126
}
94127
return total;
@@ -97,7 +130,7 @@ size_t LITTLEFSFS::totalBytes()
97130
size_t LITTLEFSFS::usedBytes()
98131
{
99132
size_t total,used;
100-
if(esp_littlefs_info(LFS_NAME, &total, &used)){
133+
if(esp_littlefs_info(partitionLabel_, &total, &used)){
101134
return 0;
102135
}
103136
return used;

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ class LITTLEFSFS : public FS
2323
{
2424
public:
2525
LITTLEFSFS();
26-
bool begin(bool formatOnFail=false, const char * basePath="/littlefs", uint8_t maxOpenFiles=5);
26+
~LITTLEFSFS();
27+
bool begin(bool formatOnFail=false, const char * basePath="/littlefs", uint8_t maxOpenFiles=10, const char * partitionLabel="spiffs");
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)