Skip to content
Merged
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
91 changes: 73 additions & 18 deletions SFTPSyncLib/RemoteSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -134,9 +137,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));
}
Expand All @@ -145,7 +151,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)
Expand Down Expand Up @@ -281,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))
{
Expand Down Expand Up @@ -1016,27 +1054,23 @@ public static Task<IEnumerable<ISftpFile>> ListDirectoryAsync(SftpClient sftp, s
}


public static bool IsFileReady(String sFilename)
/// <summary>
/// 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.
/// </summary>
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;
}
}
Expand Down Expand Up @@ -1080,6 +1114,27 @@ private static string AppendRemotePath(string remotePath, string segment)
return remotePath + "/" + normalizedSegment;
}

/// <summary>
/// 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.
/// </summary>
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);
Expand Down