From b19678f6fbc142735b52f7ce5ee8e89151d01537 Mon Sep 17 00:00:00 2001 From: William Emfinger Date: Thu, 21 Aug 2025 10:12:54 -0500 Subject: [PATCH] feat(file_system): Return early from remove() if path does not exist --- .../file_system/example/main/file_system_example.cpp | 10 ++++++++++ components/file_system/src/file_system.cpp | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/components/file_system/example/main/file_system_example.cpp b/components/file_system/example/main/file_system_example.cpp index 450d1fe52..9f72c8e5c 100644 --- a/components/file_system/example/main/file_system_example.cpp +++ b/components/file_system/example/main/file_system_example.cpp @@ -294,6 +294,16 @@ extern "C" void app_main(void) { logger.info("\t{}", f); } + // attempt to delete an etry that doesn't exist + fs::path nonexistent = sandbox / "does_not_exist.txt"; + if (!espp_fs.remove(nonexistent, ec)) { + logger.info("Properly returned error when trying to remove nonexistent file {} - {}", + nonexistent.string(), ec.message()); + ec.clear(); + } else { + logger.error("Removed nonexistent file {} - this should not happen!", nonexistent.string()); + } + // cleanup, use convenience functions // NOTE: cannot use fs::remove since it seems POSIX remove() doesn't work // We'll use espp::FileSystem::remove, which works for both files and diff --git a/components/file_system/src/file_system.cpp b/components/file_system/src/file_system.cpp index e0f423e24..520b208cf 100644 --- a/components/file_system/src/file_system.cpp +++ b/components/file_system/src/file_system.cpp @@ -199,6 +199,10 @@ bool FileSystem::remove(const std::filesystem::path &path, std::error_code &ec) logger_.debug("Removing path: {}", path.string()); namespace fs = std::filesystem; auto file_status = fs::status(path, ec); + if (ec) { + logger_.info("Failed to get status for file: {}", path.string()); + return false; + } if (std::filesystem::is_directory(file_status)) { if (!remove_contents(path, ec)) { logger_.error("Failed to remove contents of directory: {}", path.string());