@@ -39,6 +39,7 @@ extern "C" void app_main(void) {
3939 }
4040
4141 const std::string_view test_dir = " sandbox" ;
42+ const std::string_view sub_dir = " subdir" ;
4243 const std::string_view test_file = " test.csv" ;
4344 const std::string_view file_contents = " Hello World!" ;
4445 // use posix api
@@ -108,6 +109,22 @@ extern "C" void app_main(void) {
108109 logger.info (" Renamed '{}' to '{}'" , file, file2);
109110 }
110111
112+ // make a subdirectory
113+ std::string sub = sandbox + " /" + std::string (sub_dir);
114+ mkdir (sub.c_str (), 0755 );
115+ logger.info (" Created subdirectory {}" , sub);
116+
117+ // make a file in the subdirectory
118+ std::string sub_file = sub + " /subfile.txt" ;
119+ FILE *sub_fp = fopen (sub_file.c_str (), " w" );
120+ if (sub_fp == nullptr ) {
121+ logger.error (" Couldn't open {} for writing!" , sub_file);
122+ } else {
123+ fwrite (file_contents.data (), 1 , file_contents.size (), sub_fp);
124+ fclose (sub_fp);
125+ logger.info (" Wrote '{}' to {}" , file_contents, sub_file);
126+ }
127+
111128 // list files in a directory
112129 auto &fs = espp::FileSystem::get ();
113130 espp::FileSystem::ListConfig config;
@@ -146,7 +163,7 @@ extern "C" void app_main(void) {
146163 }
147164
148165 // cleanup
149- auto items = {file, file2, sandbox};
166+ auto items = {sub_file, sub, file, file2, sandbox};
150167 for (auto &item : items) {
151168 // use stat to figure out if it exists
152169 auto code = stat (item.c_str (), &st);
@@ -234,6 +251,24 @@ extern "C" void app_main(void) {
234251 logger.info (" Renamed {} to {}" , file.string (), file2.string ());
235252 }
236253
254+ // make a subdirectory
255+ fs::path sub = sandbox / fs::path{sub_dir};
256+ fs::create_directory (sub, ec);
257+ if (ec) {
258+ logger.error (" Could not create directory {} - {}" , sub.string (), ec.message ());
259+ } else {
260+ logger.info (" Created subdirectory {}" , sub.string ());
261+ }
262+
263+ // make a file in the subdirectory
264+ fs::path sub_file = sub / " subfile.txt" ;
265+ std::ofstream sub_ofs (sub_file);
266+ sub_ofs << file_contents;
267+ sub_ofs.close ();
268+ sub_ofs.flush ();
269+ logger.info (" Wrote '{}' to {}" , file_contents, sub_file.string ());
270+
271+ // list files in a directory
237272 logger.info (" Directory iterator:" );
238273 logger.warn (
239274 " NOTE: directory_iterator is not implemented in esp-idf right now :( (as of v5.2.2)" );
@@ -247,23 +282,30 @@ extern "C" void app_main(void) {
247282 logger.info (" \t This is expected since directory_iterator is not implemented in esp-idf." );
248283 }
249284
250- // cleanup
251- auto items = {file, file2, sandbox};
252- for (const auto &item : items) {
253- if (!fs::exists (item)) {
254- logger.warn (" Not removing '{}', it doesn't exist!" , item.string ());
255- continue ;
256- }
257- // and if it is a directory
258- bool is_dir = fs::is_directory (item);
259- auto err = is_dir ? rmdir (item.string ().c_str ()) : unlink (item.string ().c_str ());
260- if (err) {
261- logger.error (" Could not remove {}, error code: {}" , item.string (), err);
262- } else {
263- logger.info (" Cleaned up {}" , item.string ());
264- }
265- // fs::remove(item, ec); // NOTE: cannot use fs::remove since it seems POSIX remove()
266- // doesn't work
285+ logger.info (" Recursive directory listing:" );
286+ auto &espp_fs = espp::FileSystem::get ();
287+ auto files = espp_fs.get_files_in_path (sandbox, true , true );
288+ for (const auto &f : files) {
289+ logger.info (" \t {}" , f);
290+ }
291+
292+ // cleanup, use convenience functions
293+ // NOTE: cannot use fs::remove since it seems POSIX remove() doesn't work
294+ // We'll use espp::FileSystem::remove, which works for both files and
295+ // directories, and will recursively remove all the contents of the
296+ // directory.
297+ espp_fs.set_log_level (espp::Logger::Verbosity::DEBUG);
298+ if (!espp_fs.remove (sandbox, ec)) {
299+ logger.error (" Could not remove {}" , sandbox.string ());
300+ } else {
301+ logger.info (" Cleaned up {}" , sandbox.string ());
302+ }
303+
304+ // now list entries in root (recursively) after cleanup
305+ logger.info (" Recursive directory listing after cleanup:" );
306+ files = espp_fs.get_files_in_path (espp_fs.get_root_path (), true , true );
307+ for (const auto &f : files) {
308+ logger.info (" \t {}" , f);
267309 }
268310 // ! [file_system std filesystem example]
269311 }
0 commit comments