diff --git a/src/InternalStorage.cpp b/src/InternalStorage.cpp index 034ff81..aed8e4e 100644 --- a/src/InternalStorage.cpp +++ b/src/InternalStorage.cpp @@ -6,21 +6,32 @@ InternalStorage::InternalStorage(){ //Arduino_UnifiedStorage::debugPrint("[InternalStorage][INFO] No partitions found, restoring default partitions"); restoreDefaultPartitions(); - } else { - int lastPartitionNumber = partitionsAvailable.size(); - FileSystems lastPartitionFileSystem = partitionsAvailable.back().fileSystemType; - //Arduino_UnifiedStorage::debugPrint("[InternalStorage][INFO] Found " + String(lastPartitionNumber) + " partitions, using last partition as internal storage"); - - this -> partitionNumber = lastPartitionNumber; - this -> fileSystemType = lastPartitionFileSystem; - this -> partitionName = (char *)"internal"; + // re-read table + partitionsAvailable = Partitioning::readPartitions(QSPIFBlockDeviceType::get_default_instance()); } + + int lastPartitionNumber = partitionsAvailable.size(); + FileSystems lastPartitionFileSystem = partitionsAvailable.back().fileSystemType; + //Arduino_UnifiedStorage::debugPrint("[InternalStorage][INFO] Found " + String(lastPartitionNumber) + " partitions, using last partition as internal storage"); + + this -> partitionNumber = lastPartitionNumber; + this -> fileSystemType = lastPartitionFileSystem; + this -> partitionName = (char *)"internal"; + this -> blockDevice = BlockDeviceType::get_default_instance(); + this -> mbrBlockDevice = new MBRBlockDeviceType(this -> blockDevice, this->partitionNumber); } InternalStorage::InternalStorage(int partition, const char * name, FileSystems fileSystemType){ this -> partitionNumber = partition; - this -> partitionName = (char *)name; + this -> partitionName = name; this -> fileSystemType = fileSystemType; + this -> blockDevice = BlockDeviceType::get_default_instance(); + this -> mbrBlockDevice = new MBRBlockDeviceType(this -> blockDevice, this->partitionNumber); +} + +InternalStorage::~InternalStorage() +{ + delete this -> mbrBlockDevice; } bool InternalStorage::begin(FileSystems fileSystemType){ diff --git a/src/InternalStorage.h b/src/InternalStorage.h index cfe4b10..ef99878 100644 --- a/src/InternalStorage.h +++ b/src/InternalStorage.h @@ -19,6 +19,9 @@ class InternalStorage : public Arduino_UnifiedStorage { * When using the default partitioning scheme the last partition would be the user partition. */ InternalStorage(); + + ~InternalStorage(); + /** * Constructs an InternalStorage object with the specified partition, name, and file system. @@ -105,7 +108,7 @@ class InternalStorage : public Arduino_UnifiedStorage { MBRBlockDeviceType * mbrBlockDevice; FileSystemType * fileSystem; int partitionNumber; - char * partitionName; + const char * partitionName; FileSystems fileSystemType; }; diff --git a/src/Partitioning.cpp b/src/Partitioning.cpp index 2a4f196..9fee103 100644 --- a/src/Partitioning.cpp +++ b/src/Partitioning.cpp @@ -135,7 +135,7 @@ std::vector Partitioning::readPartitions(BlockDeviceType * blockDevic returnCode = blockDevice->read(buffer, 512 - buffer_size, buffer_size); if (returnCode) { Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][ERROR] Unable to read the Master Boot Record"); - + blockDevice->deinit(); delete[] buffer; return partitions; } @@ -146,6 +146,7 @@ std::vector Partitioning::readPartitions(BlockDeviceType * blockDevic if (table->signature[0] != mbrMagicNumbers[0] || table->signature[1] != mbrMagicNumbers[1]) { Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] MBR Not Found - Flash Memory doesn't have partitions."); + blockDevice->deinit(); delete[] buffer; return partitions; } @@ -171,24 +172,31 @@ std::vector Partitioning::readPartitions(BlockDeviceType * blockDevic MBRBlockDeviceType * mbrBlocKDevice = new MBRBlockDeviceType(blockDevice, partitionIndex); FATFileSystemType * fatProbeFileSystem = new FATFileSystemType("probing"); LittleFileSystemType * littleFsProbeFilesystem = new LittleFileSystemType("probing"); - - if(fatProbeFileSystem -> mount(mbrBlocKDevice) == 0){ - Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with FAT file system"); - fatProbeFileSystem -> unmount(); - partition.fileSystemType = FS_FAT; - partitions.push_back(partition); - - } else if (littleFsProbeFilesystem -> mount(mbrBlocKDevice) == 0){ - Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with LittleFS file system"); - littleFsProbeFilesystem -> unmount(); - partition.fileSystemType = FS_LITTLEFS; - partitions.push_back(partition); - } else { - Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is not formatted with a recognized file system"); + + if(mbrBlocKDevice && fatProbeFileSystem && littleFsProbeFilesystem){ + if(fatProbeFileSystem -> mount(mbrBlocKDevice) == 0){ + Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with FAT file system"); + fatProbeFileSystem -> unmount(); + partition.fileSystemType = FS_FAT; + partitions.push_back(partition); + + } else if (littleFsProbeFilesystem -> mount(mbrBlocKDevice) == 0){ + Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is formatted with LittleFS file system"); + littleFsProbeFilesystem -> unmount(); + partition.fileSystemType = FS_LITTLEFS; + partitions.push_back(partition); + } else { + Arduino_UnifiedStorage::debugPrint("[Partitioning][readPartitions][INFO] Partition " + String(partitionIndex) + " is not formatted with a recognized file system"); + } } - + + delete mbrBlocKDevice; + delete fatProbeFileSystem; + delete littleFsProbeFilesystem; + } + blockDevice->deinit(); delete[] buffer; return partitions; } diff --git a/src/Utils.h b/src/Utils.h index 628664c..692c29e 100644 --- a/src/Utils.h +++ b/src/Utils.h @@ -56,11 +56,17 @@ // Dynamically allocate memory for the string and copy the generated value char* dynamicName = new char[strlen(partitionName) + 1]; - strcpy(dynamicName, partitionName); + if(dynamicName) + { + strcpy(dynamicName, partitionName); + } return dynamicName; } +[[gnu::unused]] static void freePartitionName(const char* partitionName) { + delete[] partitionName; +} [[gnu::unused]] static bool copyFolder(const char* source, const char* destination) { DIR* dir = opendir(source); @@ -88,7 +94,16 @@ size_t destinationPathLength = strlen(destination) + strlen(entry->d_name) + 1; char* sourcePath = new char[sourcePathLength]; + if(!sourcePath) + { + return false; + } char* destinationPath = new char[destinationPathLength]; + if(!destinationPath) + { + delete[] sourcePath; + return false; + } snprintf(sourcePath, sourcePathLength, "%s/%s", source, entry->d_name); @@ -97,8 +112,8 @@ struct stat fileInfo; if (stat(sourcePath, &fileInfo) != 0) { closedir(dir); - delete(sourcePath); - delete(destinationPath); + delete[] sourcePath; + delete[] destinationPath; return false; } @@ -106,8 +121,8 @@ // Recursively copy subdirectories if (!copyFolder(sourcePath, destinationPath)) { closedir(dir); - delete(sourcePath); - delete(destinationPath); + delete[] sourcePath; + delete[] destinationPath; return false; } } else { @@ -115,8 +130,8 @@ FILE* sourceFile = fopen(sourcePath, "r"); if (sourceFile == nullptr) { closedir(dir); - delete(sourcePath); - delete(destinationPath); + delete[] sourcePath; + delete[] destinationPath; return false; } @@ -124,8 +139,8 @@ if (destinationFile == nullptr) { fclose(sourceFile); closedir(dir); - delete(sourcePath); - delete(destinationPath); + delete[] sourcePath; + delete[] destinationPath; return false; } @@ -137,6 +152,10 @@ fclose(sourceFile); fclose(destinationFile); } + + delete[] sourcePath; + delete[] destinationPath; + } closedir(dir); @@ -215,4 +234,4 @@ -#endif +#endif \ No newline at end of file