1
1
#include " Folder.h"
2
- #include < Arduino.h>
3
2
#include < cstdio>
4
3
#include < cstring>
5
4
#include < cstdlib>
5
+ #include < sys/stat.h>
6
6
7
7
Folder::Folder () {}
8
8
@@ -11,11 +11,15 @@ Folder::Folder(const char* path) {
11
11
DIR* dir = opendir (path);
12
12
if (dir != nullptr ) {
13
13
this ->path = std::string (path);
14
+ debugPrint (" [Folder][INFO] Folder already existing, opening : " + String (path));
14
15
closedir (dir);
15
16
} else {
16
17
int result = mkdir (path, S_IRWXU | S_IRWXG | S_IRWXO);
17
18
if (result == 0 ) {
18
19
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));
19
23
}
20
24
}
21
25
}
@@ -26,6 +30,7 @@ Folder::Folder(String dirname) {
26
30
27
31
UFile Folder::createFile (const char * fileName, FileMode fmode) {
28
32
std::string filePath = this ->path + " /" + fileName;
33
+ debugPrint (" [Folder][createFile][INFO] Creating file: " + String (filePath.c_str ()));
29
34
UFile thisFile;
30
35
thisFile.open (filePath.c_str (), fmode);
31
36
return thisFile;
@@ -38,26 +43,32 @@ UFile Folder::createFile(String fileName, FileMode fmode) {
38
43
bool Folder::remove () {
39
44
// Check if the directory exists
40
45
if (!this ->exists ()){
46
+ debugPrint (" [Folder][remove][ERROR] Folder does not exist" );
41
47
return false ;
42
48
}
43
49
50
+ debugPrint (" [Folder][remove][INFO] Removing all files and folders in path: " + String (this ->path .c_str ()));
44
51
// Remove all files in the directory
45
52
std::vector<UFile> files = this ->getFiles ();
46
53
for (UFile file : files) {
47
54
file.remove ();
55
+ debugPrint (" [Folder][remove][INFO] Removing file: " + String (file.getPathAsString ()));
48
56
}
49
57
50
58
// Remove all subfolders in the directory
51
59
std::vector<Folder> folders = this ->getFolders ();
52
60
for (Folder directory : folders) {
53
61
directory.remove ();
62
+ debugPrint (" [Folder][remove][INFO] Removing folder: " + String (directory.getPathAsString ()));
54
63
}
55
64
56
65
// Remove the current directory
57
66
if (::remove (this ->path .c_str ()) == 0 ) {
67
+ debugPrint (" [Folder][remove][INFO] Removed current folder: " + String (this ->path .c_str ()));
58
68
return true ;
59
69
} else {
60
70
// Error occurred while removing the directory
71
+ debugPrint (" [Folder][remove][ERROR] Failed to remove current folder: " + String (this ->path .c_str ()));
61
72
return false ;
62
73
}
63
74
}
@@ -67,13 +78,16 @@ bool Folder::rename(const char* newDirname) {
67
78
std::string newPath = replaceLastPathComponent (this ->path , newDirname);
68
79
69
80
// 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 ()));
70
82
int result = ::rename (this ->path .c_str (), newPath.c_str ());
71
83
if (result == 0 ) {
72
84
// Update the internal directory path
73
85
this ->path = newPath;
86
+ debugPrint (" [Folder][rename][INFO] Succesfully renamed folder: " + String (this ->path .c_str ()));
74
87
return true ;
75
88
} else {
76
89
// Error occurred while renaming the directory
90
+ debugPrint (" [Folder][rename][ERROR] Failed to rename folder" );
77
91
return false ;
78
92
}
79
93
}
@@ -111,19 +125,23 @@ Folder Folder::createSubfolder(const char* subfolderName, bool overwrite) {
111
125
if (!overwrite){
112
126
errno = EEXIST;
113
127
closedir (dir);
128
+ debugPrint (" [Folder][createSubfolder][INFO] Folder already exists: " + String (subfolderPath.c_str ()));
114
129
return Folder (subfolderPath.c_str ());
115
130
116
131
} else {
117
132
closedir (dir);
133
+ debugPrint (" [Folder][createSubfolder][INFO] Overwriting existing folder: " + String (subfolderPath.c_str ()));
118
134
Folder (subfolderPath.c_str ()).remove ();
119
135
}
120
136
}
121
137
122
138
123
139
int result = mkdir (subfolderPath.c_str (), 0777 );
124
140
if (result == 0 ) {
141
+ debugPrint (" [Folder][createSubfolder][INFO] Folder created: " + String (subfolderPath.c_str ()));
125
142
return Folder (subfolderPath.c_str ());
126
143
} else {
144
+ debugPrint (" [Folder][createSubfolder][ERROR] Failed to create folder: " + String (subfolderPath.c_str ()));
127
145
return Folder ();
128
146
}
129
147
@@ -146,8 +164,10 @@ std::vector<UFile> Folder::getFiles() {
146
164
}
147
165
}
148
166
closedir (directory);
167
+ debugPrint (" [Folder][getFiles][INFO] " + String (ret.size ()) + " files found in folder: " + String (this ->path .c_str ()));
149
168
return ret;
150
169
} else {
170
+ debugPrint (" [Folder][getFiles][INFO] Failed to open folder: " + String (this ->path .c_str ()));
151
171
return std::vector<UFile>();
152
172
}
153
173
}
@@ -167,8 +187,10 @@ std::vector<Folder> Folder::getFolders() {
167
187
}
168
188
}
169
189
closedir (directory);
190
+ debugPrint (" [Folder][getFolders][INFO] " + String (ret.size ()) + " folders found in folder: " + String (this ->path .c_str ()));
170
191
return ret;
171
192
} else {
193
+ debugPrint (" [Folder][getFolders][ERROR] Failed to open folder: " + String (this ->path .c_str ()));
172
194
return std::vector<Folder>();
173
195
}
174
196
}
@@ -184,18 +206,23 @@ bool Folder::copyTo(const char* destinationPath, bool overwrite) {
184
206
185
207
DIR* dir = opendir (source.c_str ());
186
208
if (dir == nullptr ) {
209
+ debugPrint (" [Folder][copyTo][INFO] Failed to open source folder: " + String (source.c_str ()));
187
210
return false ;
188
211
}
189
212
190
213
if (opendir (destination.c_str ())){
191
214
if (overwrite){
215
+ debugPrint (" [Folder][copyTo][INFO] Overwriting existing folder: " + String (destination.c_str ()));
192
216
Folder (destination.c_str ()).remove ();
193
217
} else {
218
+ debugPrint (" [Folder][copyTo][INFO] Destination folder already exists and overwrite is disabled: " + String (destination.c_str ()));
194
219
return false ;
195
220
}
196
221
197
222
} else if (mkdir (destination.c_str (), 0777 ) != 0 && errno != EEXIST) {
223
+ debugPrint (" [Folder][copyTo][ERROR] Failed to create destination folder: " + String (destination.c_str ()));
198
224
closedir (dir);
225
+ return false ;
199
226
}
200
227
201
228
// Create destination directory if it doesn't exist
@@ -207,25 +234,29 @@ bool Folder::copyTo(const char* destinationPath, bool overwrite) {
207
234
208
235
struct stat fileInfo;
209
236
if (stat (sourcePath.c_str (), &fileInfo) != 0 ) {
237
+ debugPrint (" [Folder][copyTo][ERROR] Failed to get file info for source file: " + String (sourcePath.c_str ()));
210
238
closedir (dir);
211
239
return false ;
212
240
}
213
241
214
242
if (S_ISDIR (fileInfo.st_mode )) {
215
243
if (!copyFolder (sourcePath.c_str (), destinationPath.c_str ())) {
244
+ debugPrint (" [Folder][copyTo][ERROR] Failed to copy subfolder: " + String (sourcePath.c_str ()));
216
245
closedir (dir);
217
246
return false ;
218
247
}
219
248
} else {
220
249
// Copy regular files
221
250
FILE* sourceFile = fopen (sourcePath.c_str (), " r" );
222
251
if (sourceFile == nullptr ) {
252
+ debugPrint (" [Folder][copyTo][ERROR] Failed to open source file: " + String (sourcePath.c_str ()));
223
253
closedir (dir);
224
254
return false ;
225
255
}
226
256
227
257
FILE* destinationFile = fopen (destinationPath.c_str (), " w" );
228
258
if (destinationFile == nullptr ) {
259
+ debugPrint (" [Folder][copyTo][ERROR] Failed to create destination file: " + String (destination.c_str ()));
229
260
fclose (sourceFile);
230
261
closedir (dir);
231
262
return false ;
@@ -243,10 +274,12 @@ bool Folder::copyTo(const char* destinationPath, bool overwrite) {
243
274
}
244
275
245
276
closedir (dir);
277
+ debugPrint (" [Folder][copyTo][INFO] Folder copied to: " + String (destination.c_str ()));
246
278
return true ;
247
279
}
248
280
249
281
282
+
250
283
bool Folder::copyTo (String destination, bool overwrite) {
251
284
return this ->copyTo (destination.c_str (), overwrite);
252
285
}
@@ -259,14 +292,17 @@ bool Folder::moveTo(const char* destination, bool overwrite) {
259
292
std::string newPath = replaceFirstPathComponent (this ->path .c_str (), destination);
260
293
261
294
if (!this ->copyTo (destination, overwrite)) {
295
+ debugPrint (" [Folder][moveTo][ERROR] Failed to copy folder to destination: " + String (destination));
262
296
return false ; // Return false if the copy operation fails
263
297
} else {
264
298
if (::remove (this ->path .c_str ()) != 0 ) {
299
+ debugPrint (" [Folder][moveTo][ERROR] Failed to remove original folder: " + String (this ->path .c_str ()));
265
300
return false ;
266
301
}
267
302
}
268
303
269
304
this ->path = newPath;
305
+ debugPrint (" [Folder][moveTo][INFO] Folder moved to: " + String (newPath.c_str ()));
270
306
return true ;
271
307
}
272
308
0 commit comments