11#include " Folder.h"
2- #include < Arduino.h>
32#include < cstdio>
43#include < cstring>
54#include < cstdlib>
5+ #include < sys/stat.h>
66
77Folder::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
2731UFile 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) {
3843bool 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+
250283bool 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
0 commit comments