From d6fba6f33ec989cf89050ca7c9f3859fa5a21c41 Mon Sep 17 00:00:00 2001 From: Steve Ives Date: Fri, 20 Nov 2026 12:56:30 -0800 Subject: [PATCH 1/2] Ignore real-time file events in excluded directories. Only the initial sync honoured excluded directories. The whole tree is watched, so files that changed or were deleted in an excluded directory (or one of its subdirectories, or a built-in excluded one such as .git or obj) were uploaded or deleted remotely, producing 'not found' warnings. File events are now checked the same way the initial sync chooses directories. A file renamed into or out of an excluded directory is treated as a delete or an upload respectively. Co-Authored-By: Claude Sonnet 5.5 --- SFTPSyncLib/RemoteSync.cs | 42 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/SFTPSyncLib/RemoteSync.cs b/SFTPSyncLib/RemoteSync.cs index 0557d13..dd8f1fe 100644 --- a/SFTPSyncLib/RemoteSync.cs +++ b/SFTPSyncLib/RemoteSync.cs @@ -134,9 +134,12 @@ private void OnFileEvent(FileSystemEventArgs arg) try { + // The whole tree is watched, so events arrive for excluded directories too. Files there are never synced. + var excluded = IsInExcludedDirectory(arg.FullPath); + if (arg.ChangeType == WatcherChangeTypes.Deleted) { - if (_deleteEnabled) + if (_deleteEnabled && !excluded) { Enqueue(arg.FullPath, new PendingChange(ChangeKind.Delete, arg.FullPath, null)); } @@ -145,7 +148,21 @@ private void OnFileEvent(FileSystemEventArgs arg) || arg.ChangeType == WatcherChangeTypes.Renamed) { var oldPath = (arg as RenamedEventArgs)?.OldFullPath; - Enqueue(arg.FullPath, new PendingChange(ChangeKind.Upload, arg.FullPath, oldPath)); + var oldExcluded = oldPath != null && IsInExcludedDirectory(oldPath); + + if (excluded) + { + // Moved into an excluded directory: it is no longer synced, so remove the remote copy + if (oldPath != null && !oldExcluded && _deleteEnabled) + { + Enqueue(oldPath, new PendingChange(ChangeKind.Delete, oldPath, null)); + } + } + else + { + // Moved out of an excluded directory: there is no remote copy of the old name to clean up + Enqueue(arg.FullPath, new PendingChange(ChangeKind.Upload, arg.FullPath, oldExcluded ? null : oldPath)); + } } } catch (Exception ex) @@ -1080,6 +1097,27 @@ private static string AppendRemotePath(string remotePath, string segment) return remotePath + "/" + normalizedSegment; } + /// + /// True if a file is in an excluded directory, or in a subdirectory of one. The directories are checked from the + /// local root down, the same way the initial sync decides which directories to visit. + /// + private bool IsInExcludedDirectory(string localFilePath) + { + var directory = Path.GetDirectoryName(Path.GetFullPath(localFilePath)); + + while (!string.IsNullOrEmpty(directory) + && directory.Length > _localRootDirectory.Length + && directory.StartsWith(_localRootDirectory, StringComparison.OrdinalIgnoreCase)) + { + if (IsExcludedDirectoryPath(directory)) + return true; + + directory = Path.GetDirectoryName(directory); + } + + return false; + } + private bool IsExcludedDirectoryPath(string localPath) { var fullPath = Path.GetFullPath(localPath).TrimEnd(Path.DirectorySeparatorChar); From 24bb3c167b6443f032f0fbaf27b898b37c9c8d03 Mon Sep 17 00:00:00 2001 From: Steve Ives Date: Fri, 20 Nov 2026 13:08:39 -0800 Subject: [PATCH 2/2] Sync genuinely empty files in real time instead of timing out. A file was only treated as ready to upload once it was unlocked AND non-empty, so a real zero-byte file waited 30 seconds and was then skipped with a 'Timeout waiting for file ready' warning. A newly created empty file now gets a 2 second grace period for a writer to fill it, then is synced as an empty file. The 30 second limit still applies to locked files. Co-Authored-By: Claude Sonnet 5.5 --- SFTPSyncLib/RemoteSync.cs | 49 ++++++++++++++++++++++++++------------- 1 file changed, 33 insertions(+), 16 deletions(-) diff --git a/SFTPSyncLib/RemoteSync.cs b/SFTPSyncLib/RemoteSync.cs index dd8f1fe..659c080 100644 --- a/SFTPSyncLib/RemoteSync.cs +++ b/SFTPSyncLib/RemoteSync.cs @@ -18,6 +18,9 @@ public sealed class RemoteSync : IDisposable { private static readonly TimeSpan IdleConnectionTimeout = TimeSpan.FromSeconds(60); + // How long a newly created empty file is given to be written to before it is synced as an empty file + private static readonly TimeSpan EmptyFileGracePeriod = TimeSpan.FromSeconds(2); + // Bounds the number of changed files being prepared (waiting for a connection or for the file to be writable) private const int MaxPendingFileTasks = 64; @@ -298,10 +301,28 @@ private async Task ProcessFileChangeAsync(string key, PendingChange change) // Wait for whatever is writing the file to finish, without holding on to a connection var waitStart = DateTime.UtcNow; - while (!IsFileReady(change.Path)) + DateTime? emptySince = null; + + while (true) { - if (!File.Exists(change.Path)) - return; + if (TryGetUnlockedFileLength(change.Path, out var length)) + { + if (length > 0) + break; + + // A new file is often created empty and then written to, so give a writer a moment. If it is + // still empty after that, it really is an empty file and is synced as one. + emptySince ??= DateTime.UtcNow; + if (DateTime.UtcNow - emptySince.Value > EmptyFileGracePeriod) + break; + } + else + { + emptySince = null; + + if (!File.Exists(change.Path)) + return; + } if (DateTime.UtcNow - waitStart > TimeSpan.FromSeconds(30)) { @@ -1033,27 +1054,23 @@ public static Task> ListDirectoryAsync(SftpClient sftp, s } - public static bool IsFileReady(String sFilename) + /// + /// If the file can be opened for exclusive access it means that the file is no longer locked by another + /// process. Returns false if it is locked (or missing), otherwise true with the file's length. + /// + public static bool TryGetUnlockedFileLength(string fileName, out long length) { - // If the file can be opened for exclusive access it means that the file - // is no longer locked by another process. try { - using (FileStream inputStream = File.Open(sFilename, FileMode.Open, FileAccess.Read, FileShare.None)) + using (var inputStream = File.Open(fileName, FileMode.Open, FileAccess.Read, FileShare.None)) { - if (inputStream.Length > 0) - { - return true; - } - else - { - return false; - } - + length = inputStream.Length; + return true; } } catch (Exception) { + length = 0; return false; } }