From 9396478caa87416e11a7831068ac5d4cb24db17a Mon Sep 17 00:00:00 2001 From: abraunegg Date: Fri, 12 Jan 2024 06:01:42 +1100 Subject: [PATCH] Update monitor.d * Update inotify event handling where files might be temporarily moved by applications. This scenario is common with certain text editors (like Vim with specific configurations), which can lead to misleading file deletion detections. --- src/monitor.d | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/monitor.d b/src/monitor.d index d5481dc3..d046829b 100644 --- a/src/monitor.d +++ b/src/monitor.d @@ -175,6 +175,9 @@ final class Monitor { void delegate(string path) onDelete; void delegate(string from, string to) onMove; + // List of paths that were moved, not deleted + bool[string] movedNotDeleted; + // Configure the class varaible to consume the application configuration including selective sync this(ApplicationConfig appConfig, ClientSideFiltering selectiveSync) { this.appConfig = appConfig; @@ -459,6 +462,7 @@ final class Monitor { if (event.mask & IN_MOVED_FROM) { addLogEntry("event IN_MOVED_FROM: " ~ path, ["debug"]); cookieToPath[event.cookie] = path; + movedNotDeleted[path] = true; // Mark as moved, not deleted } else if (event.mask & IN_MOVED_TO) { addLogEntry("event IN_MOVED_TO: " ~ path, ["debug"]); if (event.mask & IN_ISDIR) addRecursive(path); @@ -466,8 +470,9 @@ final class Monitor { if (from) { cookieToPath.remove(event.cookie); if (useCallbacks) onMove(*from, path); + movedNotDeleted.remove(*from); // Clear moved status } else { - // item moved from the outside + // Handle file moved in from outside if (event.mask & IN_ISDIR) { if (useCallbacks) onDirCreated(path); } else { @@ -481,8 +486,12 @@ final class Monitor { if (useCallbacks) onDirCreated(path); } } else if (event.mask & IN_DELETE) { - addLogEntry("event IN_DELETE: " ~ path, ["debug"]); - if (useCallbacks) onDelete(path); + if (path in movedNotDeleted) { + movedNotDeleted.remove(path); // Ignore delete for moved files + } else { + addLogEntry("event IN_DELETE: " ~ path, ["debug"]); + if (useCallbacks) onDelete(path); + } } else if ((event.mask & IN_CLOSE_WRITE) && !(event.mask & IN_ISDIR)) { addLogEntry("event IN_CLOSE_WRITE and not IN_ISDIR: " ~ path, ["debug"]); if (useCallbacks) onFileChanged(path);