Skip to content

Conversation

@xyhuang7215
Copy link
Contributor

Summary

Adds a new Windows-specific option WinPreventDirectoryDeletion that allows users to prevent the watched directory from being deleted while being monitored.

Motivation

By default, Windows file watcher uses FILE_SHARE_DELETE flag when opening directory handles, which has two issues:

  1. Allows unwanted deletion: The watched directory can be deleted while monitoring is active, which can lead to unexpected behavior in applications that need to ensure the watched directory remains accessible throughout the monitoring period.

  2. Missing delete events: When FILE_SHARE_DELETE is enabled and the watched directory itself is deleted, the watcher may not receive a delete event for the directory. This makes it impossible to detect when the monitored directory has been removed.

This option allows applications to:

  • Prevent accidental deletion of watched directories
  • Ensure the watched directory remains accessible during monitoring

Changes

  • Added new option Options::WinPreventDirectoryDeletion to control FILE_SHARE_DELETE behavior
  • When set to 1, prevents directory deletion by excluding FILE_SHARE_DELETE from share mode
  • When set to 0 (default), maintains original behavior (allows deletion)

Usage Example

UpdateListener* ul = new UpdateListener();
efsw::FileWatcher fileWatcher;

std::vector<efsw::WatcherOption> options;
options.push_back( { efsw::Options::WinPreventDirectoryDeletion, 1 } );

fileWatcher.addWatch( path, ul, true, options );

Copy link
Owner

@SpartanJ SpartanJ left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good but we also need to add the new flag to the C bindings in efsw.h. Let's add that and I'll merge it. Thanks!

@SpartanJ SpartanJ merged commit 04d1747 into SpartanJ:master Jan 6, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants