From d360ebe66fa7609a90dcf67d98683ea31064295d Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 19 Feb 2026 05:24:56 -0600 Subject: [PATCH] Enhance file and directory removal logic in VFS - Added a check for `watch_enabled` before calling `unwatch` in the `remove_file` and `remove_dir_all` methods to prevent unnecessary operations when watching is disabled. - Updated error handling in the `StdBackend` to include a new condition for `Generic` errors related to watch status, improving logging clarity. These changes aim to optimize the file system operations and enhance error handling in the virtual file system. --- crates/memofs/src/lib.rs | 8 ++++++-- crates/memofs/src/std_backend.rs | 8 ++++++-- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/crates/memofs/src/lib.rs b/crates/memofs/src/lib.rs index 35935f2f..2eaaba1c 100644 --- a/crates/memofs/src/lib.rs +++ b/crates/memofs/src/lib.rs @@ -252,13 +252,17 @@ impl VfsInner { fn remove_file>(&mut self, path: P) -> io::Result<()> { let path = path.as_ref(); - let _ = self.backend.unwatch(path); + if self.watch_enabled { + let _ = self.backend.unwatch(path); + } self.backend.remove_file(path) } fn remove_dir_all>(&mut self, path: P) -> io::Result<()> { let path = path.as_ref(); - let _ = self.backend.unwatch(path); + if self.watch_enabled { + let _ = self.backend.unwatch(path); + } self.backend.remove_dir_all(path) } diff --git a/crates/memofs/src/std_backend.rs b/crates/memofs/src/std_backend.rs index cd0c5411..67bc1651 100644 --- a/crates/memofs/src/std_backend.rs +++ b/crates/memofs/src/std_backend.rs @@ -373,10 +373,14 @@ impl VfsBackend for StdBackend { Ok(()) } Err(err) => { - if matches!( + let is_not_watched = matches!( err.kind, notify::ErrorKind::WatchNotFound | notify::ErrorKind::PathNotFound - ) { + ) || matches!( + &err.kind, + notify::ErrorKind::Generic(msg) if msg.contains("No watch was found") + ); + if is_not_watched { log::trace!( "Path was not directly watched (likely covered by parent): {}", path.display()