Skip to content

Commit 54b0037

Browse files
applied and tested fixes from PR#39
1 parent 7fd805e commit 54b0037

File tree

4 files changed

+77
-36
lines changed

4 files changed

+77
-36
lines changed

src/InternalStorage.cpp

+20-9
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){

src/InternalStorage.h

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ class InternalStorage : public Arduino_UnifiedStorage {
1919
* When using the default partitioning scheme the last partition would be the user partition.
2020
*/
2121
InternalStorage();
22+
23+
~InternalStorage();
24+
2225

2326
/**
2427
* Constructs an InternalStorage object with the specified partition, name, and file system.
@@ -105,7 +108,7 @@ class InternalStorage : public Arduino_UnifiedStorage {
105108
MBRBlockDeviceType * mbrBlockDevice;
106109
FileSystemType * fileSystem;
107110
int partitionNumber;
108-
char * partitionName;
111+
const char * partitionName;
109112
FileSystems fileSystemType;
110113

111114
};

src/Partitioning.cpp

+24-16
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,31 @@ 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+
if(fatProbeFileSystem -> mount(mbrBlocKDevice) == 0){
178+
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with FAT file system");
179+
fatProbeFileSystem -> unmount();
180+
partition.fileSystemType = FS_FAT;
181+
partitions.push_back(partition);
182+
183+
} else if (littleFsProbeFilesystem -> mount(mbrBlocKDevice) == 0){
184+
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with LittleFS file system");
185+
littleFsProbeFilesystem -> unmount();
186+
partition.fileSystemType = FS_LITTLEFS;
187+
partitions.push_back(partition);
188+
} else {
189+
Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is not formatted with a recognized file system");
190+
}
188191
}
189-
192+
193+
delete mbrBlocKDevice;
194+
delete fatProbeFileSystem;
195+
delete littleFsProbeFilesystem;
196+
190197
}
191198

199+
blockDevice->deinit();
192200
delete[] buffer;
193201
return partitions;
194202
}

src/Utils.h

+29-10
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);
@@ -215,4 +234,4 @@
215234

216235

217236

218-
#endif
237+
#endif

0 commit comments

Comments
 (0)