Skip to content

Commit 2ea7e0c

Browse files
fixed warningins
1 parent ac8699a commit 2ea7e0c

12 files changed

+166
-55
lines changed

Diff for: .vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"ranges": "cpp",
4747
"stdexcept": "cpp",
4848
"streambuf": "cpp",
49-
"typeinfo": "cpp"
49+
"typeinfo": "cpp",
50+
"cerrno": "cpp"
5051
}
5152
}

Diff for: examples/InternalStoragePartitioning/InternalStoragePartitioning.ino

+1-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,7 @@ void testAllPartitions(std::vector<Partition> partitions) {
7777
}
7878
}
7979

80-
String prettyPrintFileSystemType(FileSystems f){
81-
if(f == 0) return "FAT";
82-
else if(f == 1) return "LitlleFS";
83-
else return "";
84-
}
80+
8581

8682
void listPartitions(){
8783
std::vector<Partition> partitions = InternalStorage::readPartitions();

Diff for: src/Arduino_UnifiedStorage.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#ifndef UnifiedStorage_H
22
#define UnifiedStorage_H
33

4+
45
#include "Arduino.h"
56
#include "Arduino_POSIXStorage.h"
67
#include "Boards.h"

Diff for: src/Folder.cpp

+37-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#include "Folder.h"
2-
#include <Arduino.h>
32
#include <cstdio>
43
#include <cstring>
54
#include <cstdlib>
5+
#include <sys/stat.h>
66

77
Folder::Folder() {}
88

@@ -11,11 +11,15 @@ Folder::Folder(const char* path) {
1111
DIR* dir = opendir(path);
1212
if (dir != nullptr) {
1313
this->path = std::string(path);
14+
debugPrint("[Folder][INFO] Folder already existing, opening : " + String(path));
1415
closedir(dir);
1516
} else {
1617
int result = mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
1718
if (result == 0) {
1819
this->path = std::string(path);
20+
debugPrint("[Folder][INFO] Created folder: " + String(this->path.c_str()));
21+
} else {
22+
debugPrint("[Folder][ERROR] Failed to create folder: " + String(path));
1923
}
2024
}
2125
}
@@ -26,6 +30,7 @@ Folder::Folder(String dirname) {
2630

2731
UFile Folder::createFile(const char* fileName, FileMode fmode) {
2832
std::string filePath = this->path + "/" + fileName;
33+
debugPrint("[Folder][createFile][INFO] Creating file: " + String(filePath.c_str()));
2934
UFile thisFile;
3035
thisFile.open(filePath.c_str(), fmode);
3136
return thisFile;
@@ -38,26 +43,32 @@ UFile Folder::createFile(String fileName, FileMode fmode) {
3843
bool Folder::remove() {
3944
// Check if the directory exists
4045
if(!this->exists()){
46+
debugPrint("[Folder][remove][ERROR] Folder does not exist");
4147
return false;
4248
}
4349

50+
debugPrint("[Folder][remove][INFO] Removing all files and folders in path: " + String(this->path.c_str()));
4451
// Remove all files in the directory
4552
std::vector<UFile> files = this->getFiles();
4653
for (UFile file : files) {
4754
file.remove();
55+
debugPrint("[Folder][remove][INFO] Removing file: " + String(file.getPathAsString()));
4856
}
4957

5058
// Remove all subfolders in the directory
5159
std::vector<Folder> folders = this->getFolders();
5260
for (Folder directory : folders) {
5361
directory.remove();
62+
debugPrint("[Folder][remove][INFO] Removing folder: " + String(directory.getPathAsString()));
5463
}
5564

5665
// Remove the current directory
5766
if (::remove(this->path.c_str()) == 0) {
67+
debugPrint("[Folder][remove][INFO] Removed current folder: " + String(this->path.c_str()));
5868
return true;
5969
} else {
6070
// Error occurred while removing the directory
71+
debugPrint("[Folder][remove][ERROR] Failed to remove current folder: " + String(this->path.c_str()));
6172
return false;
6273
}
6374
}
@@ -67,13 +78,16 @@ bool Folder::rename(const char* newDirname) {
6778
std::string newPath = replaceLastPathComponent(this->path, newDirname);
6879

6980
// actually perform the POSIX command to rename the folder
81+
debugPrint("[Folder][rename][INFO] Renaming folder: " + String(this->path.c_str()) + " to " + String(newPath.c_str()));
7082
int result = ::rename(this->path.c_str(), newPath.c_str());
7183
if (result == 0) {
7284
// Update the internal directory path
7385
this->path = newPath;
86+
debugPrint("[Folder][rename][INFO] Succesfully renamed folder: " + String(this->path.c_str()));
7487
return true;
7588
} else {
7689
// Error occurred while renaming the directory
90+
debugPrint("[Folder][rename][ERROR] Failed to rename folder");
7791
return false;
7892
}
7993
}
@@ -111,19 +125,23 @@ Folder Folder::createSubfolder(const char* subfolderName, bool overwrite) {
111125
if(!overwrite){
112126
errno = EEXIST;
113127
closedir(dir);
128+
debugPrint("[Folder][createSubfolder][INFO] Folder already exists: " + String(subfolderPath.c_str()));
114129
return Folder(subfolderPath.c_str());
115130

116131
} else {
117132
closedir(dir);
133+
debugPrint("[Folder][createSubfolder][INFO] Overwriting existing folder: " + String(subfolderPath.c_str()));
118134
Folder(subfolderPath.c_str()).remove();
119135
}
120136
}
121137

122138

123139
int result = mkdir(subfolderPath.c_str(), 0777);
124140
if (result == 0) {
141+
debugPrint("[Folder][createSubfolder][INFO] Folder created: " + String(subfolderPath.c_str()));
125142
return Folder(subfolderPath.c_str());
126143
} else {
144+
debugPrint("[Folder][createSubfolder][ERROR] Failed to create folder: " + String(subfolderPath.c_str()));
127145
return Folder();
128146
}
129147

@@ -146,8 +164,10 @@ std::vector<UFile> Folder::getFiles() {
146164
}
147165
}
148166
closedir(directory);
167+
debugPrint("[Folder][getFiles][INFO] " + String(ret.size()) + " files found in folder: " + String(this->path.c_str()));
149168
return ret;
150169
} else {
170+
debugPrint("[Folder][getFiles][INFO] Failed to open folder: " + String(this->path.c_str()));
151171
return std::vector<UFile>();
152172
}
153173
}
@@ -167,8 +187,10 @@ std::vector<Folder> Folder::getFolders() {
167187
}
168188
}
169189
closedir(directory);
190+
debugPrint("[Folder][getFolders][INFO] " + String(ret.size()) + " folders found in folder: " + String(this->path.c_str()));
170191
return ret;
171192
} else {
193+
debugPrint("[Folder][getFolders][ERROR] Failed to open folder: " + String(this->path.c_str()));
172194
return std::vector<Folder>();
173195
}
174196
}
@@ -184,18 +206,23 @@ bool Folder::copyTo(const char* destinationPath, bool overwrite) {
184206

185207
DIR* dir = opendir(source.c_str());
186208
if (dir == nullptr) {
209+
debugPrint("[Folder][copyTo][INFO] Failed to open source folder: " + String(source.c_str()));
187210
return false;
188211
}
189212

190213
if(opendir(destination.c_str())){
191214
if(overwrite){
215+
debugPrint("[Folder][copyTo][INFO] Overwriting existing folder: " + String(destination.c_str()));
192216
Folder(destination.c_str()).remove();
193217
} else {
218+
debugPrint("[Folder][copyTo][INFO] Destination folder already exists and overwrite is disabled: " + String(destination.c_str()));
194219
return false;
195220
}
196221

197222
} else if (mkdir(destination.c_str(), 0777) != 0 && errno != EEXIST) {
223+
debugPrint("[Folder][copyTo][ERROR] Failed to create destination folder: " + String(destination.c_str()));
198224
closedir(dir);
225+
return false;
199226
}
200227

201228
// Create destination directory if it doesn't exist
@@ -207,25 +234,29 @@ bool Folder::copyTo(const char* destinationPath, bool overwrite) {
207234

208235
struct stat fileInfo;
209236
if (stat(sourcePath.c_str(), &fileInfo) != 0) {
237+
debugPrint("[Folder][copyTo][ERROR] Failed to get file info for source file: " + String(sourcePath.c_str()));
210238
closedir(dir);
211239
return false;
212240
}
213241

214242
if (S_ISDIR(fileInfo.st_mode)) {
215243
if (!copyFolder(sourcePath.c_str(), destinationPath.c_str())) {
244+
debugPrint("[Folder][copyTo][ERROR] Failed to copy subfolder: " + String(sourcePath.c_str()));
216245
closedir(dir);
217246
return false;
218247
}
219248
} else {
220249
// Copy regular files
221250
FILE* sourceFile = fopen(sourcePath.c_str(), "r");
222251
if (sourceFile == nullptr) {
252+
debugPrint("[Folder][copyTo][ERROR] Failed to open source file: " + String(sourcePath.c_str()));
223253
closedir(dir);
224254
return false;
225255
}
226256

227257
FILE* destinationFile = fopen(destinationPath.c_str(), "w");
228258
if (destinationFile == nullptr) {
259+
debugPrint("[Folder][copyTo][ERROR] Failed to create destination file: " + String(destination.c_str()));
229260
fclose(sourceFile);
230261
closedir(dir);
231262
return false;
@@ -243,10 +274,12 @@ bool Folder::copyTo(const char* destinationPath, bool overwrite) {
243274
}
244275

245276
closedir(dir);
277+
debugPrint("[Folder][copyTo][INFO] Folder copied to: " + String(destination.c_str()));
246278
return true;
247279
}
248280

249281

282+
250283
bool Folder::copyTo(String destination, bool overwrite) {
251284
return this->copyTo(destination.c_str(), overwrite);
252285
}
@@ -259,14 +292,17 @@ bool Folder::moveTo(const char* destination, bool overwrite) {
259292
std::string newPath = replaceFirstPathComponent(this->path.c_str(), destination);
260293

261294
if (!this->copyTo(destination, overwrite)) {
295+
debugPrint("[Folder][moveTo][ERROR] Failed to copy folder to destination: " + String(destination));
262296
return false; // Return false if the copy operation fails
263297
} else {
264298
if (::remove(this->path.c_str()) != 0) {
299+
debugPrint("[Folder][moveTo][ERROR] Failed to remove original folder: " + String(this->path.c_str()));
265300
return false;
266301
}
267302
}
268303

269304
this->path = newPath;
305+
debugPrint("[Folder][moveTo][INFO] Folder moved to: " + String(newPath.c_str()));
270306
return true;
271307
}
272308

Diff for: src/InternalStorage.cpp

+18-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
InternalStorage::InternalStorage(){
44
std::vector<Partition> partitionsAvailable = Partitioning::readPartitions(QSPIFBlockDeviceType::get_default_instance());
55
if(partitionsAvailable.size() == 0){
6+
7+
debugPrint("[InternalStorage][INFO] No partitions found, restoring default partitions");
68
restoreDefaultPartitions();
79
} else {
810
int lastPartitionNumber = partitionsAvailable.size();
911
FileSystems lastPartitionFileSystem = partitionsAvailable.back().fileSystemType;
12+
debugPrint("[InternalStorage][INFO] Found " + String(lastPartitionNumber) + " partitions, using last partition as internal storage");
1013

1114
this -> partitionNumber = lastPartitionNumber;
1215
this -> fileSystemType = lastPartitionFileSystem;
@@ -52,11 +55,16 @@ bool InternalStorage::begin(){
5255

5356
if(this -> fileSystemType == FS_FAT){
5457
this -> fileSystem = new FATFileSystemType(this->partitionName);
58+
debugPrint("[InternalStorage][begin][INFO] Mounting partition " + String(this->partitionNumber) + " as FAT");
5559
} else {
5660
this -> fileSystem = new LittleFileSystemType(this->partitionName);
61+
debugPrint("[InternalStorage][begin][INFO] Mounting partition " + String(this->partitionNumber) + " as LittleFS");
5762
}
5863

5964
int err = this -> fileSystem -> mount(mbrBlockDevice);
65+
if(err!=0){
66+
debugPrint("[InternalStorage][ERROR] Could not mount partition " + String(this->partitionNumber) + " as " + prettyPrintFileSystemType(this->fileSystemType) + ", error code: " + String(errno));
67+
}
6068
return err == 0;
6169
}
6270

@@ -76,10 +84,18 @@ bool InternalStorage::format(FileSystems fs){
7684

7785
if(fs == FS_FAT){
7886
this -> fileSystem = new FATFileSystemType(this->partitionName);
79-
return this -> fileSystem -> reformat(this-> mbrBlockDevice) == 0;
87+
int err = this -> fileSystem -> reformat(this-> mbrBlockDevice);
88+
if(err != 0){
89+
debugPrint("[InternalStorage][format][ERROR] Error formatting partition " + String(this->partitionNumber) + " as FAT: " + String(errno));
90+
}
91+
return err == 0;
8092
} if (fs == FS_LITTLEFS) {
8193
this -> fileSystem = new LittleFileSystemType(this->partitionName);
82-
return this -> fileSystem -> reformat(this-> mbrBlockDevice) == 0;
94+
int err = this -> fileSystem -> reformat(this-> mbrBlockDevice);
95+
if(err != 0){
96+
debugPrint("[InternalStorage][format][ERROR] Error formatting partition " + String(this->partitionNumber) + " as LittleFS: " + String(errno));
97+
}
98+
return err == 0;
8399
}
84100

85101
return false;

0 commit comments

Comments
 (0)