Skip to content

Commit 3e6ba07

Browse files
committed
Code cleanup
1 parent 8b2c28d commit 3e6ba07

2 files changed

Lines changed: 28 additions & 27 deletions

File tree

SFTPSyncLib/RemoteSync.cs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,25 @@ public RemoteSync(string host, string username, string password,
3333
_username = username;
3434
_password = password;
3535
_searchPattern = searchPattern;
36-
_localRootDirectory = Path.TrimEndingDirectorySeparator(
37-
Path.GetFullPath(localRootDirectory));
36+
_localRootDirectory = Path.TrimEndingDirectorySeparator(Path.GetFullPath(localRootDirectory));
3837
_remoteRootDirectory = remoteRootDirectory.TrimEnd('/', '\\');
3938
_director = director;
4039
_excludedFolders = excludedFolders ?? new List<string>();
4140
_sftp = new SftpClient(host, username, password);
4241

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

4749
//Now perform the initial sync for the pattern this instance is responsible for
50+
4851
DoneInitialSync = InitialSync(_localRootDirectory, _remoteRootDirectory);
4952

5053
//Once the initial sync is done, we can start watching the file system for changes
54+
5155
DoneInitialSync.ContinueWith((tmp) =>
5256
{
5357
_director.AddCallback(searchPattern, (args) => Fsw_Changed(null, args));
@@ -62,14 +66,14 @@ public RemoteSync(string host, string username, string password,
6266
_username = username;
6367
_password = password;
6468
_searchPattern = searchPattern;
65-
_localRootDirectory = Path.TrimEndingDirectorySeparator(
66-
Path.GetFullPath(localRootDirectory));
69+
_localRootDirectory = Path.TrimEndingDirectorySeparator(Path.GetFullPath(localRootDirectory));
6770
_remoteRootDirectory = remoteRootDirectory.TrimEnd('/', '\\');
6871
_director = director;
6972
_excludedFolders = excludedFolders ?? new List<string>();
7073
_sftp = new SftpClient(host, username, password);
7174

7275
DoneMakingFolders = Task.CompletedTask;
76+
7377
DoneInitialSync = initialSyncTask;
7478

7579
DoneInitialSync.ContinueWith((tmp) =>
@@ -79,14 +83,9 @@ public RemoteSync(string host, string username, string password,
7983
}
8084

8185
public static async Task RunSharedInitialSyncAsync(
82-
string host,
83-
string username,
84-
string password,
85-
string localRootDirectory,
86-
string remoteRootDirectory,
87-
string[] searchPatterns,
88-
List<string>? excludedFolders,
89-
int workerCount)
86+
string host, string username, string password,
87+
string localRootDirectory, string remoteRootDirectory,
88+
string[] searchPatterns, List<string>? excludedFolders, int workerCount)
9089
{
9190
if (workerCount <= 0 || searchPatterns.Length == 0)
9291
{
@@ -108,6 +107,7 @@ public static async Task RunSharedInitialSyncAsync(
108107
}
109108

110109
var workQueue = new ConcurrentQueue<SyncWorkItem>();
110+
111111
foreach (var pair in EnumerateLocalDirectories(localRootDirectory, remoteRootDirectory, excludedFolders))
112112
{
113113
foreach (var pattern in searchPatterns)
@@ -117,6 +117,7 @@ public static async Task RunSharedInitialSyncAsync(
117117
}
118118

119119
var workers = new List<Task>();
120+
120121
for (int i = 0; i < workerCount; i++)
121122
{
122123
workers.Add(Task.Run(async () =>

SFTPSyncLib/SyncDirector.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,23 @@ public SyncDirector(string rootFolder)
1818
InternalBufferSize = 64 * 1024
1919
};
2020

21-
_fsw.Changed += Fsw_Changed;
2221
_fsw.Created += Fsw_Created;
22+
_fsw.Changed += Fsw_Changed;
2323
_fsw.Renamed += Fsw_Renamed;
2424
_fsw.Error += Fsw_Error;
2525

2626
_fsw.EnableRaisingEvents = true;
2727
}
2828

29-
private void Fsw_Renamed(object sender, RenamedEventArgs e)
29+
public void AddCallback(string match, Action<FileSystemEventArgs> handler)
30+
{
31+
string regexPattern = "^" + Regex.Escape(match)
32+
.Replace("\\*", ".*")
33+
.Replace("\\?", ".") + "$";
34+
callbacks.Add((new Regex(regexPattern, RegexOptions.IgnoreCase), handler));
35+
}
36+
37+
private void Fsw_Created(object sender, FileSystemEventArgs e)
3038
{
3139
var name = Path.GetFileName(e.FullPath);
3240
foreach (var (regex, callback) in callbacks)
@@ -38,7 +46,7 @@ private void Fsw_Renamed(object sender, RenamedEventArgs e)
3846
}
3947
}
4048

41-
private void Fsw_Created(object sender, FileSystemEventArgs e)
49+
private void Fsw_Changed(object sender, FileSystemEventArgs e)
4250
{
4351
var name = Path.GetFileName(e.FullPath);
4452
foreach (var (regex, callback) in callbacks)
@@ -50,15 +58,7 @@ private void Fsw_Created(object sender, FileSystemEventArgs e)
5058
}
5159
}
5260

53-
public void AddCallback(string match, Action<FileSystemEventArgs> handler)
54-
{
55-
string regexPattern = "^" + Regex.Escape(match)
56-
.Replace("\\*", ".*")
57-
.Replace("\\?", ".") + "$";
58-
callbacks.Add((new Regex(regexPattern, RegexOptions.IgnoreCase), handler));
59-
}
60-
61-
private void Fsw_Changed(object sender, FileSystemEventArgs e)
61+
private void Fsw_Renamed(object sender, RenamedEventArgs e)
6262
{
6363
var name = Path.GetFileName(e.FullPath);
6464
foreach (var (regex, callback) in callbacks)

0 commit comments

Comments
 (0)