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
4 changes: 2 additions & 2 deletions SFTPSyncLib/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ private static void Log(string message)
switch (_mode)
{
case LoggerMode.Console:
Console.WriteLine($"{DateTime.Now.ToString()} {message}");
Console.WriteLine($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} {message}");
break;

case LoggerMode.RaiseEvent:
LogUpdated?.Invoke($"{DateTime.Now.ToString()} {message}");
LogUpdated?.Invoke($"{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")} {message}");
break;
}
}
Expand Down
83 changes: 50 additions & 33 deletions SFTPSyncLib/RemoteSync.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,25 +33,25 @@ public RemoteSync(string host, string username, string password,
_username = username;
_password = password;
_searchPattern = searchPattern;
_localRootDirectory = localRootDirectory;
_remoteRootDirectory = remoteRootDirectory;
_localRootDirectory = Path.TrimEndingDirectorySeparator(Path.GetFullPath(localRootDirectory));
_remoteRootDirectory = remoteRootDirectory.TrimEnd('/', '\\');
_director = director;
_excludedFolders = excludedFolders ?? new List<string>();
_sftp = new SftpClient(host, username, password);
_sftp.Connect();

//Our first instance is responsible for creating all of the the directories
//Subsequent instances will not be created until this is done
DoneMakingFolders = createFolders ? CreateDirectories(_localRootDirectory, _remoteRootDirectory) : Task.CompletedTask;
//The first instance is responsible for creating ALL of the the directories.
//Subsequent instances will not be created until this one completes.

DoneMakingFolders = createFolders
? CreateDirectories(_localRootDirectory, _remoteRootDirectory)
: Task.CompletedTask;

//Now perform the initial sync for the pattern this instance is responsible for

DoneInitialSync = InitialSync(_localRootDirectory, _remoteRootDirectory);

//Once the initial sync is done, we can start watching the file system for changes
DoneInitialSync.ContinueWith((tmp) =>
{
_director.AddCallback(searchPattern, (args) => Fsw_Changed(null, args));
});
// Register callbacks immediately; handler will ignore events until initial sync completes.
_director.AddCallback(searchPattern, (args) => Fsw_Changed(null, args));
}

public RemoteSync(string host, string username, string password,
Expand All @@ -62,31 +62,24 @@ public RemoteSync(string host, string username, string password,
_username = username;
_password = password;
_searchPattern = searchPattern;
_localRootDirectory = localRootDirectory;
_remoteRootDirectory = remoteRootDirectory;
_localRootDirectory = Path.TrimEndingDirectorySeparator(Path.GetFullPath(localRootDirectory));
_remoteRootDirectory = remoteRootDirectory.TrimEnd('/', '\\');
_director = director;
_excludedFolders = excludedFolders ?? new List<string>();
_sftp = new SftpClient(host, username, password);
_sftp.Connect();

DoneMakingFolders = Task.CompletedTask;

DoneInitialSync = initialSyncTask;

DoneInitialSync.ContinueWith((tmp) =>
{
_director.AddCallback(searchPattern, (args) => Fsw_Changed(null, args));
});
// Register callbacks immediately; handler will ignore events until initial sync completes.
_director.AddCallback(searchPattern, (args) => Fsw_Changed(null, args));
}

public static async Task RunSharedInitialSyncAsync(
string host,
string username,
string password,
string localRootDirectory,
string remoteRootDirectory,
string[] searchPatterns,
List<string>? excludedFolders,
int workerCount)
string host, string username, string password,
string localRootDirectory, string remoteRootDirectory,
string[] searchPatterns, List<string>? excludedFolders, int workerCount)
{
if (workerCount <= 0 || searchPatterns.Length == 0)
{
Expand All @@ -108,6 +101,7 @@ public static async Task RunSharedInitialSyncAsync(
}

var workQueue = new ConcurrentQueue<SyncWorkItem>();

foreach (var pair in EnumerateLocalDirectories(localRootDirectory, remoteRootDirectory, excludedFolders))
{
foreach (var pattern in searchPatterns)
Expand All @@ -117,6 +111,7 @@ public static async Task RunSharedInitialSyncAsync(
}

var workers = new List<Task>();

for (int i = 0; i < workerCount; i++)
{
workers.Add(Task.Run(async () =>
Expand Down Expand Up @@ -150,7 +145,7 @@ public static async Task RunSharedInitialSyncAsync(
/// <param name="destinationPath">Path to the destination file</param>
public static void SyncFile(SftpClient sftp, string sourcePath, string destinationPath)
{
Logger.LogInfo($"Syncing {sourcePath} -> {destinationPath}");
Logger.LogInfo($"Syncing {sourcePath}");

int retryCount = 0;
const int maxRetries = 5;
Expand Down Expand Up @@ -195,7 +190,7 @@ public static Task<IEnumerable<FileInfo>> SyncDirectoryAsync(SftpClient sftp, st
{
if (new DirectoryInfo(sourcePath).EnumerateFiles(searchPattern, SearchOption.TopDirectoryOnly).Any())
{
Logger.LogInfo($"Sync started for {sourcePath}\\{searchPattern} -> {destinationPath}");
Logger.LogInfo($"Sync started for {sourcePath}\\{searchPattern}");

return Task<IEnumerable<FileInfo>>.Factory.FromAsync(sftp.BeginSynchronizeDirectories,
sftp.EndSynchronizeDirectories, sourcePath,
Expand All @@ -214,7 +209,7 @@ private string[] FilteredDirectories(string localPath)

public async Task CreateDirectories(string localPath, string remotePath)
{
Logger.LogInfo($"Creating directory {localPath} -> {remotePath}");
Logger.LogInfo($"Creating directory {remotePath}");

try
{
Expand Down Expand Up @@ -337,6 +332,7 @@ private static async Task CreateDirectoriesInternal(
var directoryName = item.Split(Path.DirectorySeparatorChar).Last();
if (!remoteDirectories.ContainsKey(directoryName))
{
Logger.LogInfo($"Creating remote directory {remotePath}{directoryName}");
sftp.CreateDirectory(remotePath + "/" + directoryName);
}
await CreateDirectoriesInternal(sftp, localRootDirectory, localPath + "\\" + directoryName, remotePath + "/" + directoryName, excludedFolders);
Expand Down Expand Up @@ -383,6 +379,19 @@ public static bool IsFileReady(String sFilename)
}
}

private string GetRemotePathForLocal(string localPath)
{
var relativePath = Path.GetRelativePath(_localRootDirectory, localPath);
if (relativePath == "." || string.IsNullOrEmpty(relativePath))
return _remoteRootDirectory;

relativePath = relativePath.Replace('\\', '/').TrimStart('/');
if (relativePath.Length == 0)
return _remoteRootDirectory;

return _remoteRootDirectory + "/" + relativePath;
}

private void EnsureConnected()
{
if (_disposed)
Expand All @@ -408,17 +417,25 @@ private bool EnsureConnectedSafe()
}
}

public Task<bool> ConnectAsync()
{
return Task.Run(() => EnsureConnectedSafe());
}


private async void Fsw_Changed(object? sender, FileSystemEventArgs arg)
{
try
{
if (!DoneInitialSync.IsCompleted)
return;

if (arg.ChangeType == WatcherChangeTypes.Changed || arg.ChangeType == WatcherChangeTypes.Created
|| arg.ChangeType == WatcherChangeTypes.Renamed)
{
var changedPath = Path.GetDirectoryName(arg.FullPath);
var relativePath = _localRootDirectory == changedPath ? "" : changedPath?.Substring(_localRootDirectory.Length).Replace('\\', '/');
var fullRemotePath = _remoteRootDirectory + relativePath;
var fullRemotePath = GetRemotePathForLocal(changedPath ?? _localRootDirectory);
var fullRemoteFilePath = GetRemotePathForLocal(arg.FullPath);
await Task.Yield();
bool makeDirectory = true;
lock (_activeDirSync)
Expand All @@ -439,7 +456,7 @@ private async void Fsw_Changed(object? sender, FileSystemEventArgs arg)
if (connectionOk)
{
//check if we're a new directory
if (makeDirectory && Directory.Exists(arg.FullPath) && !_sftp.Exists(fullRemotePath))
if (makeDirectory && changedPath != null && Directory.Exists(changedPath) && !_sftp.Exists(fullRemotePath))
{
_sftp.CreateDirectory(fullRemotePath);
}
Expand Down Expand Up @@ -501,7 +518,7 @@ private async void Fsw_Changed(object? sender, FileSystemEventArgs arg)
fileConnectionOk = EnsureConnectedSafe();
if (fileConnectionOk)
{
SyncFile(_sftp, arg.FullPath, fullRemotePath + "/" + Path.GetFileName(arg.FullPath));
SyncFile(_sftp, arg.FullPath, fullRemoteFilePath);
}
}
finally
Expand Down
31 changes: 18 additions & 13 deletions SFTPSyncLib/SyncDirector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,47 +18,52 @@ public SyncDirector(string rootFolder)
InternalBufferSize = 64 * 1024
};

_fsw.Changed += Fsw_Changed;
_fsw.Created += Fsw_Created;
_fsw.Changed += Fsw_Changed;
_fsw.Renamed += Fsw_Renamed;
_fsw.Error += Fsw_Error;

_fsw.EnableRaisingEvents = true;
}

private void Fsw_Renamed(object sender, RenamedEventArgs e)
public void AddCallback(string match, Action<FileSystemEventArgs> handler)
{
string regexPattern = "^" + Regex.Escape(match)
.Replace("\\*", ".*")
.Replace("\\?", ".") + "$";
callbacks.Add((new Regex(regexPattern, RegexOptions.IgnoreCase), handler));
}

private void Fsw_Created(object sender, FileSystemEventArgs e)
{
var name = Path.GetFileName(e.FullPath);
foreach (var (regex, callback) in callbacks)
{
if (regex.IsMatch(e.FullPath))
if (regex.IsMatch(name))
{
callback(e);
}
}
}

private void Fsw_Created(object sender, FileSystemEventArgs e)
private void Fsw_Changed(object sender, FileSystemEventArgs e)
{
var name = Path.GetFileName(e.FullPath);
foreach (var (regex, callback) in callbacks)
{
if (regex.IsMatch(e.FullPath))
if (regex.IsMatch(name))
{
callback(e);
}
}
}

public void AddCallback(string match, Action<FileSystemEventArgs> handler)
{
string regexPattern = "^" + Regex.Escape(match).Replace("\\*", ".*") + "$";
callbacks.Add((new Regex(regexPattern), handler));
}

private void Fsw_Changed(object sender, FileSystemEventArgs e)
private void Fsw_Renamed(object sender, RenamedEventArgs e)
{
var name = Path.GetFileName(e.FullPath);
foreach (var (regex, callback) in callbacks)
{
if (regex.IsMatch(e.FullPath))
if (regex.IsMatch(name))
{
callback(e);
}
Expand Down
2 changes: 1 addition & 1 deletion SFTPSyncSetup/Product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
Id="*"
Name="SFTPSync"
Language="1033"
Version="1.5"
Version="1.6"
Manufacturer="Synergex International Corporation"
UpgradeCode="6000f870-b811-4e22-b80b-5b8956317d09">

Expand Down
2 changes: 1 addition & 1 deletion SFTPSyncSetup/SFTPSyncSetup.wixproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ProductVersion>3.10</ProductVersion>
<ProjectGuid>5074cbcb-641b-4a9c-b3bc-8dd0b78810a6</ProjectGuid>
<SchemaVersion>2.0</SchemaVersion>
<OutputName>SFTPSync-1.5</OutputName>
<OutputName>SFTPSync-1.6</OutputName>
<OutputType>Package</OutputType>
<Name>SFTPSyncSetup</Name>
</PropertyGroup>
Expand Down
8 changes: 4 additions & 4 deletions SFTPSyncUI/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ private void AppendLog(string message)

private void AddMessage(string message)
{
listBoxMessages.Items.Add(message);
listBoxMessages.Items.Insert(0, message);

// Optional: auto-scroll to bottom
listBoxMessages.TopIndex = listBoxMessages.Items.Count - 1;
// Keep newest at top
listBoxMessages.TopIndex = 0;

// Optional: trim excess from UI if _log did a dequeue
if (listBoxMessages.Items.Count > 1000)
{
listBoxMessages.Items.RemoveAt(0);
listBoxMessages.Items.RemoveAt(listBoxMessages.Items.Count - 1);
}
}

Expand Down
Loading