diff --git a/firmware/application/apps/ui_fileman.cpp b/firmware/application/apps/ui_fileman.cpp index 029fc895..323110b6 100644 --- a/firmware/application/apps/ui_fileman.cpp +++ b/firmware/application/apps/ui_fileman.cpp @@ -440,6 +440,11 @@ void FileManagerView::on_rename(std::string_view hint) { } void FileManagerView::on_delete() { + if (is_directory(get_selected_full_path()) && !is_empty_directory(get_selected_full_path())) { + nav_.display_modal("Delete", "Directory not empty!"); + return; + } + auto name = get_selected_entry().path.filename().string(); nav_.push( "Delete", "Delete " + name + "\nAre you sure?", YESNO, diff --git a/firmware/application/file.cpp b/firmware/application/file.cpp index edc81885..95f1966b 100644 --- a/firmware/application/file.cpp +++ b/firmware/application/file.cpp @@ -568,6 +568,17 @@ bool is_directory(const path& file_path) { return fr == FR_OK && is_directory(static_cast(filinfo.fattrib)); } +bool is_empty_directory(const path& file_path) { + DIR dir; + FILINFO filinfo; + + if (!is_directory(file_path)) + return false; + + auto result = f_findfirst(&dir, &filinfo, reinterpret_cast(file_path.c_str()), (const TCHAR*)u"*"); + return !((result == FR_OK) && (filinfo.fname[0] != (TCHAR)'\0')); +} + space_info space(const path& p) { DWORD free_clusters{0}; FATFS* fs; diff --git a/firmware/application/file.hpp b/firmware/application/file.hpp index 6d321442..afe6db1b 100644 --- a/firmware/application/file.hpp +++ b/firmware/application/file.hpp @@ -248,6 +248,7 @@ bool is_directory(const file_status s); bool is_regular_file(const file_status s); bool file_exists(const path& file_path); bool is_directory(const path& file_path); +bool is_empty_directory(const path& file_path); space_info space(const path& p);