Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ Changelog

**Other Changes**

- [windows] Changing only the case of a name no longer emits a deletion event on top of the move event. (`#752 <https://github.com/gorakhargosh/watchdog/issues/752>`__)
- [bsd] Do not let a ``PermissionError`` raised while registering a kevent kill the emitter thread. (`#1115 <https://github.com/gorakhargosh/watchdog/issues/1115>`__)
- [core] Add context manager support to ``Observer`` class. The observer can now be used with a ``with`` statement for automatic start/stop management. (`#1090 <https://github.com/gorakhargosh/watchdog/pull/1149>`__)
- [freebsd] Supports ``inotify`` on FreeBSD 15+. (`#1147 <https://github.com/gorakhargosh/watchdog/pull/1147>`__)
Expand Down
24 changes: 23 additions & 1 deletion src/watchdog/observers/winapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,28 @@ def is_removed_self(self) -> bool:
return self.action == FILE_ACTION_REMOVED_SELF


def _drop_case_only_rename_deletions(events: list[WinAPINativeEvent]) -> list[WinAPINativeEvent]:
"""Drop the spurious deletion reported ahead of a case-only rename.

Renaming ``file`` to ``FILE`` makes ReadDirectoryChangesW report a
``FILE_ACTION_REMOVED`` for the old name on top of the usual
``FILE_ACTION_RENAMED_OLD_NAME``/``FILE_ACTION_RENAMED_NEW_NAME`` pair, so such a
rename would surface as a deletion followed by a move. A rename that also changes
the name reports the pair alone, hence the deletion is dropped only when the very
next record renames that same path: nothing can rename a genuinely deleted path.
"""
return [
event
for index, event in enumerate(events)
if not (
event.is_removed
and index + 1 < len(events)
and events[index + 1].is_renamed_old
and events[index + 1].src_path == event.src_path
)
]


class DirectoryChangeReader:
"""Uses ReadDirectoryChangesW() to detect file system changes. A separate
thread is used to make that call in order to reduce the time window
Expand Down Expand Up @@ -465,4 +487,4 @@ def get_events(self, timeout: float) -> list[WinAPINativeEvent]:
except queue.Empty:
break
events.extend(_parse_event_buffer(buf))
return [WinAPINativeEvent(action, src_path) for action, src_path in events]
return _drop_case_only_rename_deletions([WinAPINativeEvent(action, src_path) for action, src_path in events])
37 changes: 37 additions & 0 deletions tests/test_emitter.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,15 @@ def test_case_change(
) -> None:
mkdir(p("dir1"))
mkdir(p("dir2"))
mkdir(p("dir1", "sub"))
mkdir(p("subdir"))
mkfile(p("dir1", "file"))
mkfile(p("other"))
start_watching()

# Across directories. Windows reports this as a deletion and a creation
# carrying no rename records at all, whether or not the case differs, so
# there is nothing to coalesce here.
mv(p("dir1", "file"), p("dir2", "FILE"))

with events_checker() as ec:
Expand All @@ -197,6 +203,37 @@ def test_case_change(
ec.add(FileCreatedEvent, "dir2/FILE")
ec.add(DirModifiedEvent, "dir2")

# In place, a file. Windows prepends a deletion of the old name here, which
# is the case being coalesced away.
mv(p("other"), p("OTHER"))

with events_checker() as ec:
ec.add(FileMovedEvent, "other", dest_path="OTHER")
if platform.is_linux():
ec.add(DirModifiedEvent, ".")
ec.add(DirModifiedEvent, ".")

# In place, a directory.
mv(p("subdir"), p("SUBDIR"))

with events_checker() as ec:
ec.add(DirMovedEvent, "subdir", dest_path="SUBDIR")
if platform.is_linux():
ec.add(DirModifiedEvent, ".")
ec.add(DirModifiedEvent, ".")

# In place, a subfolder one level down.
mv(p("dir1", "sub"), p("dir1", "SUB"))

with events_checker() as ec:
if platform.is_windows():
ec.add(DirModifiedEvent, "dir1")
ec.add(DirMovedEvent, "dir1/sub", dest_path="dir1/SUB")
if platform.is_windows() or platform.is_linux():
ec.add(DirModifiedEvent, "dir1")
if platform.is_linux():
ec.add(DirModifiedEvent, "dir1")


def test_move_to(
p: P, event_queue: TestEventQueue, start_watching: StartWatching, events_checker: EventsChecker
Expand Down
44 changes: 44 additions & 0 deletions tests/test_observers_winapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,14 @@
pytest.skip("Windows only.", allow_module_level=True)

from watchdog.observers.read_directory_changes import WindowsApiEmitter
from watchdog.observers.winapi import (
FILE_ACTION_CREATED,
FILE_ACTION_DELETED,
FILE_ACTION_RENAMED_NEW_NAME,
FILE_ACTION_RENAMED_OLD_NAME,
WinAPINativeEvent,
_drop_case_only_rename_deletions,
)

SLEEP_TIME = 2

Expand Down Expand Up @@ -127,3 +135,39 @@ def test_root_deleted(event_queue, emitter):

# The emitter is automatically stopped, with no error
assert not emitter.should_keep_running()


def test_drop_case_only_rename_deletions():
"""A case-only rename is reported with an extra deletion of the old name."""
events = [
WinAPINativeEvent(FILE_ACTION_DELETED, "file"),
WinAPINativeEvent(FILE_ACTION_RENAMED_OLD_NAME, "file"),
WinAPINativeEvent(FILE_ACTION_RENAMED_NEW_NAME, "FILE"),
]

assert _drop_case_only_rename_deletions(events) == events[1:]


@pytest.mark.parametrize(
"events",
[
# A deletion is only spurious when the very next record renames that same
# path, so a path deleted, recreated and only then renamed is left alone.
[
WinAPINativeEvent(FILE_ACTION_DELETED, "file"),
WinAPINativeEvent(FILE_ACTION_CREATED, "file"),
WinAPINativeEvent(FILE_ACTION_RENAMED_OLD_NAME, "file"),
WinAPINativeEvent(FILE_ACTION_RENAMED_NEW_NAME, "other"),
],
# A deletion followed by the rename of a different path.
[
WinAPINativeEvent(FILE_ACTION_DELETED, "file"),
WinAPINativeEvent(FILE_ACTION_RENAMED_OLD_NAME, "other"),
WinAPINativeEvent(FILE_ACTION_RENAMED_NEW_NAME, "OTHER"),
],
# A deletion closing the batch, which is how a plain deletion is reported.
[WinAPINativeEvent(FILE_ACTION_DELETED, "file")],
],
)
def test_drop_case_only_rename_deletions_keeps_real_deletions(events):
assert _drop_case_only_rename_deletions(events) == events
Loading