Skip to content

Commit bd8600c

Browse files
locatwddmesh
authored andcommitted
Various fixes for begin(),umount(),format()
1 parent fbd3990 commit bd8600c

File tree

5 files changed

+95
-47
lines changed

5 files changed

+95
-47
lines changed

Diff for: examples/InternalStoragePartitioning/InternalStoragePartitioning.ino

+2
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ void testAllPartitions(std::vector<Partition> partitions) {
7474
Serial.println("\t - Successfully mounted partition: /" + String(partitionName));
7575
Serial.println("\t - Testing file operations: ");
7676
testWriting(&thisPartition); // Test writing to a file in the partition
77+
thisPartition.unmount();
78+
freePartitionName(partitionName);
7779
}
7880

7981
Serial.println();

Diff for: src/InternalStorage.cpp

+38-20
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,32 @@ InternalStorage::InternalStorage(){
66

77
//Arduino_UnifiedStorage::debugPrint("[InternalStorage][INFO] No partitions found, restoring default partitions");
88
restoreDefaultPartitions();
9-
} else {
10-
int lastPartitionNumber = partitionsAvailable.size();
11-
FileSystems lastPartitionFileSystem = partitionsAvailable.back().fileSystemType;
12-
//Arduino_UnifiedStorage::debugPrint("[InternalStorage][INFO] Found " + String(lastPartitionNumber) + " partitions, using last partition as internal storage");
13-
14-
this -> partitionNumber = lastPartitionNumber;
15-
this -> fileSystemType = lastPartitionFileSystem;
16-
this -> partitionName = (char *)"internal";
9+
// re-read table
10+
partitionsAvailable = Partitioning::readPartitions(QSPIFBlockDeviceType::get_default_instance());
1711
}
12+
13+
int lastPartitionNumber = partitionsAvailable.size();
14+
FileSystems lastPartitionFileSystem = partitionsAvailable.back().fileSystemType;
15+
//Arduino_UnifiedStorage::debugPrint("[InternalStorage][INFO] Found " + String(lastPartitionNumber) + " partitions, using last partition as internal storage");
16+
17+
this -> partitionNumber = lastPartitionNumber;
18+
this -> fileSystemType = lastPartitionFileSystem;
19+
this -> partitionName = (char *)"internal";
20+
this -> blockDevice = BlockDeviceType::get_default_instance();
21+
this -> mbrBlockDevice = new MBRBlockDeviceType(this -> blockDevice, this->partitionNumber);
1822
}
1923

2024
InternalStorage::InternalStorage(int partition, const char * name, FileSystems fileSystemType){
2125
this -> partitionNumber = partition;
22-
this -> partitionName = (char *)name;
26+
this -> partitionName = name;
2327
this -> fileSystemType = fileSystemType;
28+
this -> blockDevice = BlockDeviceType::get_default_instance();
29+
this -> mbrBlockDevice = new MBRBlockDeviceType(this -> blockDevice, this->partitionNumber);
30+
}
31+
32+
InternalStorage::~InternalStorage()
33+
{
34+
delete this -> mbrBlockDevice;
2435
}
2536

2637
bool InternalStorage::begin(FileSystems fileSystemType){
@@ -49,9 +60,6 @@ std::vector<Partition> InternalStorage::readPartitions(){
4960
}
5061

5162
bool InternalStorage::begin(){
52-
53-
this -> blockDevice = BlockDeviceType::get_default_instance();
54-
this -> mbrBlockDevice = new MBRBlockDeviceType(this->blockDevice, this->partitionNumber);
5563

5664
if(this -> fileSystemType == FS_FAT){
5765
this -> fileSystem = new FATFileSystemType(this->partitionName);
@@ -61,15 +69,23 @@ bool InternalStorage::begin(){
6169
Arduino_UnifiedStorage::debugPrint("[InternalStorage][begin][INFO] Mounting partition " + String(this->partitionNumber) + " as LittleFS");
6270
}
6371

64-
int err = this -> fileSystem -> mount(mbrBlockDevice);
72+
int err = this -> fileSystem -> mount(this -> mbrBlockDevice);
6573
if(err!=0){
6674
Arduino_UnifiedStorage::debugPrint("[InternalStorage][ERROR] Could not mount partition " + String(this->partitionNumber) + " as " + prettyPrintFileSystemType(this->fileSystemType) + ", error code: " + String(errno));
6775
}
6876
return err == 0;
6977
}
7078

7179
bool InternalStorage::unmount(){
72-
int err = this -> fileSystem -> unmount();
80+
int err = 0;
81+
82+
if(this -> fileSystem)
83+
{
84+
err = this -> fileSystem -> unmount();
85+
delete this -> fileSystem;
86+
this -> fileSystem = NULL;
87+
}
88+
7389
return err == 0;
7490
}
7591

@@ -78,23 +94,25 @@ Folder InternalStorage::getRootFolder(){
7894
}
7995

8096
bool InternalStorage::format(FileSystems fs){
81-
this -> begin();
97+
FileSystemType * tmpFileSystem = nullptr;
8298
this -> unmount();
8399
this -> fileSystemType = fs;
84100

85101
if(fs == FS_FAT){
86-
this -> fileSystem = new FATFileSystemType(this->partitionName);
87-
int err = this -> fileSystem -> reformat(this-> mbrBlockDevice);
102+
tmpFileSystem = new FATFileSystemType(this->partitionName);
103+
int err = tmpFileSystem -> reformat(this-> mbrBlockDevice);
88104
if(err != 0){
89105
Arduino_UnifiedStorage::debugPrint("[InternalStorage][format][ERROR] Error formatting partition " + String(this->partitionNumber) + " as FAT: " + String(errno));
90-
}
106+
}
107+
delete tmpFileSystem;
91108
return err == 0;
92109
} if (fs == FS_LITTLEFS) {
93-
this -> fileSystem = new LittleFileSystemType(this->partitionName);
94-
int err = this -> fileSystem -> reformat(this-> mbrBlockDevice);
110+
tmpFileSystem = new LittleFileSystemType(this->partitionName);
111+
int err = tmpFileSystem -> reformat(this-> mbrBlockDevice);
95112
if(err != 0){
96113
Arduino_UnifiedStorage::debugPrint("[InternalStorage][format][ERROR] Error formatting partition " + String(this->partitionNumber) + " as LittleFS: " + String(errno));
97114
}
115+
delete tmpFileSystem;
98116
return err == 0;
99117
}
100118

Diff for: src/InternalStorage.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class InternalStorage : public Arduino_UnifiedStorage {
2929
*/
3030
InternalStorage(int partition, const char *name, FileSystems fs);
3131

32+
~InternalStorage();
33+
3234
/**
3335
* Initializes the internal storage.
3436
*
@@ -105,7 +107,7 @@ class InternalStorage : public Arduino_UnifiedStorage {
105107
MBRBlockDeviceType * mbrBlockDevice;
106108
FileSystemType * fileSystem;
107109
int partitionNumber;
108-
char * partitionName;
110+
const char * partitionName;
109111
FileSystems fileSystemType;
110112

111113
};

Diff for: src/Partitioning.cpp

+24-17
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ std::vector<Partition> Partitioning::readPartitions(BlockDeviceType * blockDevic
135135
returnCode = blockDevice->read(buffer, 512 - buffer_size, buffer_size);
136136
if (returnCode) {
137137
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][ERROR] Unable to read the Master Boot Record");
138-
138+
blockDevice->deinit();
139139
delete[] buffer;
140140
return partitions;
141141
}
@@ -146,6 +146,7 @@ std::vector<Partition> Partitioning::readPartitions(BlockDeviceType * blockDevic
146146
if (table->signature[0] != mbrMagicNumbers[0] || table->signature[1] != mbrMagicNumbers[1]) {
147147

148148
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] MBR Not Found - Flash Memory doesn't have partitions.");
149+
blockDevice->deinit();
149150
delete[] buffer;
150151
return partitions;
151152
}
@@ -171,24 +172,30 @@ std::vector<Partition> Partitioning::readPartitions(BlockDeviceType * blockDevic
171172
MBRBlockDeviceType * mbrBlocKDevice = new MBRBlockDeviceType(blockDevice, partitionIndex);
172173
FATFileSystemType * fatProbeFileSystem = new FATFileSystemType("probing");
173174
LittleFileSystemType * littleFsProbeFilesystem = new LittleFileSystemType("probing");
174-
175-
if(fatProbeFileSystem -> mount(mbrBlocKDevice) == 0){
176-
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with FAT file system");
177-
fatProbeFileSystem -> unmount();
178-
partition.fileSystemType = FS_FAT;
179-
partitions.push_back(partition);
180-
181-
} else if (littleFsProbeFilesystem -> mount(mbrBlocKDevice) == 0){
182-
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with LittleFS file system");
183-
littleFsProbeFilesystem -> unmount();
184-
partition.fileSystemType = FS_LITTLEFS;
185-
partitions.push_back(partition);
186-
} else {
187-
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is not formatted with a recognized file system");
175+
176+
if(mbrBlocKDevice && fatProbeFileSystem && littleFsProbeFilesystem)
177+
{
178+
if(fatProbeFileSystem -> mount(mbrBlocKDevice) == 0){
179+
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with FAT file system");
180+
fatProbeFileSystem -> unmount();
181+
partition.fileSystemType = FS_FAT;
182+
partitions.push_back(partition);
183+
184+
} else if (littleFsProbeFilesystem -> mount(mbrBlocKDevice) == 0){
185+
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with LittleFS file system");
186+
littleFsProbeFilesystem -> unmount();
187+
partition.fileSystemType = FS_LITTLEFS;
188+
partitions.push_back(partition);
189+
} else {
190+
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is not formatted with a recognized file system");
191+
}
188192
}
189-
190-
}
191193

194+
delete mbrBlocKDevice;
195+
delete fatProbeFileSystem;
196+
delete littleFsProbeFilesystem;
197+
}
198+
blockDevice->deinit();
192199
delete[] buffer;
193200
return partitions;
194201
}

Diff for: src/Utils.h

+28-9
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@
5656

5757
// Dynamically allocate memory for the string and copy the generated value
5858
char* dynamicName = new char[strlen(partitionName) + 1];
59-
strcpy(dynamicName, partitionName);
59+
if(dynamicName)
60+
{
61+
strcpy(dynamicName, partitionName);
62+
}
6063

6164
return dynamicName;
6265
}
6366

67+
[[gnu::unused]] static void freePartitionName(const char* partitionName) {
68+
delete[] partitionName;
69+
}
6470

6571
[[gnu::unused]] static bool copyFolder(const char* source, const char* destination) {
6672
DIR* dir = opendir(source);
@@ -88,7 +94,16 @@
8894
size_t destinationPathLength = strlen(destination) + strlen(entry->d_name) + 1;
8995

9096
char* sourcePath = new char[sourcePathLength];
97+
if(!sourcePath)
98+
{
99+
return false;
100+
}
91101
char* destinationPath = new char[destinationPathLength];
102+
if(!destinationPath)
103+
{
104+
delete[] sourcePath;
105+
return false;
106+
}
92107

93108
snprintf(sourcePath, sourcePathLength, "%s/%s", source, entry->d_name);
94109

@@ -97,35 +112,35 @@
97112
struct stat fileInfo;
98113
if (stat(sourcePath, &fileInfo) != 0) {
99114
closedir(dir);
100-
delete(sourcePath);
101-
delete(destinationPath);
115+
delete[] sourcePath;
116+
delete[] destinationPath;
102117
return false;
103118
}
104119

105120
if (S_ISDIR(fileInfo.st_mode)) {
106121
// Recursively copy subdirectories
107122
if (!copyFolder(sourcePath, destinationPath)) {
108123
closedir(dir);
109-
delete(sourcePath);
110-
delete(destinationPath);
124+
delete[] sourcePath;
125+
delete[] destinationPath;
111126
return false;
112127
}
113128
} else {
114129
// Copy regular files
115130
FILE* sourceFile = fopen(sourcePath, "r");
116131
if (sourceFile == nullptr) {
117132
closedir(dir);
118-
delete(sourcePath);
119-
delete(destinationPath);
133+
delete[] sourcePath;
134+
delete[] destinationPath;
120135
return false;
121136
}
122137

123138
FILE* destinationFile = fopen(destinationPath, "w");
124139
if (destinationFile == nullptr) {
125140
fclose(sourceFile);
126141
closedir(dir);
127-
delete(sourcePath);
128-
delete(destinationPath);
142+
delete[] sourcePath;
143+
delete[] destinationPath;
129144
return false;
130145
}
131146

@@ -137,6 +152,10 @@
137152
fclose(sourceFile);
138153
fclose(destinationFile);
139154
}
155+
156+
delete[] sourcePath;
157+
delete[] destinationPath;
158+
140159
}
141160

142161
closedir(dir);

0 commit comments

Comments
 (0)