-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransferEngine.cs
More file actions
290 lines (254 loc) · 10.4 KB
/
Copy pathTransferEngine.cs
File metadata and controls
290 lines (254 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.IO.Enumeration;
using System.Threading.Tasks;
namespace RoboCopyApp
{
public class TransferEngine
{
public event EventHandler<TransferProgressEventArgs>? ProgressChanged;
public event EventHandler<string>? LogMessage;
private long _totalBytes;
private long _transferredBytes;
private int _totalFiles;
private int _transferredFiles;
private bool _isScanning;
private CancellationTokenSource? _cancellationTokenSource;
private ManualResetEventSlim _pauseEvent = new ManualResetEventSlim(true);
public void Pause()
{
_pauseEvent.Reset();
}
public void Resume()
{
_pauseEvent.Set();
}
public async Task StartTransferAsync(
string sourceDir,
string destDir,
TransferMode mode,
int maxThreads,
int bufferSize,
int maxRetries,
bool verifyChecksums,
List<string> excludePatterns)
{
_cancellationTokenSource = new CancellationTokenSource();
_pauseEvent.Set(); // Ensure running on start
var ct = _cancellationTokenSource.Token;
_totalBytes = 0;
_transferredBytes = 0;
_totalFiles = 0;
_transferredFiles = 0;
_isScanning = true;
var queue = new ConcurrentQueue<FileTransferTask>();
var sw = Stopwatch.StartNew();
LogMessage?.Invoke(this, $"Scanning directory: {sourceDir}");
// Fire and forget progress updater
var progressUpdater = Task.Run(async () =>
{
while (!ct.IsCancellationRequested)
{
ReportProgress(sw, string.Empty);
await Task.Delay(250, ct).ConfigureAwait(false);
}
});
// 1. Scan Directory
try
{
await ScanDirectoryAsync(sourceDir, destDir, queue, ct, excludePatterns);
}
catch (Exception ex)
{
LogMessage?.Invoke(this, $"Error during scan: {ex.Message}");
}
_isScanning = false;
LogMessage?.Invoke(this, $"Scan complete. Found {_totalFiles} files ({_totalBytes / (1024.0 * 1024.0):F2} MB). Starting transfer with {maxThreads} threads.");
if (_totalFiles == 0)
{
_cancellationTokenSource.Cancel();
LogMessage?.Invoke(this, "Transfer complete (no files found).");
return;
}
// 2. Process Queue in Parallel
var parallelOptions = new ParallelOptions
{
MaxDegreeOfParallelism = maxThreads,
CancellationToken = ct
};
try
{
await Parallel.ForEachAsync(queue, parallelOptions, async (task, token) =>
{
try
{
_pauseEvent.Wait(token); // Pause mid-queue, respects cancellation token
await FileCopier.CopyFileAsync(
task,
bufferSize,
maxRetries,
verifyChecksums,
(bytes) => Interlocked.Add(ref _transferredBytes, bytes),
token);
if (mode == TransferMode.Move)
{
File.Delete(task.SourcePath);
}
Interlocked.Increment(ref _transferredFiles);
LogMessage?.Invoke(this, $"Success: {task.SourcePath}");
}
catch (Exception ex)
{
LogMessage?.Invoke(this, $"Failed: {task.SourcePath}. Error: {ex.Message}");
}
});
}
catch (OperationCanceledException)
{
LogMessage?.Invoke(this, "Transfer canceled by user.");
}
catch (Exception ex)
{
LogMessage?.Invoke(this, $"Transfer engine error: {ex.Message}");
}
finally
{
sw.Stop();
_cancellationTokenSource.Cancel(); // Stop the progress updater loop
ReportProgress(sw, "Completed");
if (mode == TransferMode.Move)
{
try { Directory.Delete(sourceDir, true); } catch { /* Ignore */ }
}
LogMessage?.Invoke(this, "Transfer complete.");
}
}
public void StopTransfer()
{
_cancellationTokenSource?.Cancel();
_pauseEvent.Set(); // Release any waiting threads to let them cancel
}
private async Task ScanDirectoryAsync(string source, string dest, ConcurrentQueue<FileTransferTask> queue, CancellationToken ct, List<string> excludePatterns)
{
if (ct.IsCancellationRequested) return;
try
{
var dirInfo = new DirectoryInfo(source);
if (!dirInfo.Exists)
{
// It might be a single file
var fileInfo = new FileInfo(source);
if (fileInfo.Exists)
{
if (ShouldExclude(fileInfo.Name, excludePatterns))
{
LogMessage?.Invoke(this, $"Skipped (excluded): {fileInfo.FullName}");
return;
}
Interlocked.Add(ref _totalBytes, fileInfo.Length);
Interlocked.Increment(ref _totalFiles);
queue.Enqueue(new FileTransferTask
{
SourcePath = source,
DestinationPath = dest,
Size = fileInfo.Length
});
}
return;
}
// Create root dest dir
Directory.CreateDirectory(dest);
var dirQueue = new Queue<string>();
dirQueue.Enqueue(source);
while (dirQueue.Count > 0)
{
ct.ThrowIfCancellationRequested();
string currentDir = dirQueue.Dequeue();
try
{
var currentDirInfo = new DirectoryInfo(currentDir);
if (!currentDirInfo.Exists) continue;
// Exclude folders
foreach (var subdir in currentDirInfo.EnumerateDirectories())
{
if (ShouldExclude(subdir.Name, excludePatterns))
{
LogMessage?.Invoke(this, $"Skipped (excluded): {subdir.FullName}");
continue;
}
dirQueue.Enqueue(subdir.FullName);
}
// Exclude files
foreach (var file in currentDirInfo.EnumerateFiles())
{
if (ShouldExclude(file.Name, excludePatterns))
{
LogMessage?.Invoke(this, $"Skipped (excluded): {file.FullName}");
continue;
}
var relPath = Path.GetRelativePath(source, file.FullName);
var targetPath = Path.Combine(dest, relPath);
Interlocked.Add(ref _totalBytes, file.Length);
Interlocked.Increment(ref _totalFiles);
queue.Enqueue(new FileTransferTask
{
SourcePath = file.FullName,
DestinationPath = targetPath,
Size = file.Length
});
}
}
catch (UnauthorizedAccessException)
{
LogMessage?.Invoke(this, $"Access denied scanning: {currentDir}");
}
catch (DirectoryNotFoundException)
{
// Directory was deleted while scanning
}
}
}
catch (UnauthorizedAccessException)
{
LogMessage?.Invoke(this, $"Access denied scanning root: {source}");
}
}
private bool ShouldExclude(string name, List<string> excludePatterns)
{
if (excludePatterns == null || excludePatterns.Count == 0) return false;
foreach (var pattern in excludePatterns)
{
if (FileSystemName.MatchesSimpleExpression(pattern, name, ignoreCase: true))
return true;
}
return false;
}
private void ReportProgress(Stopwatch sw, string currentFile)
{
double elapsedSeconds = sw.Elapsed.TotalSeconds;
double speed = elapsedSeconds > 0 ? _transferredBytes / elapsedSeconds : 0;
TimeSpan eta = TimeSpan.Zero;
if (speed > 0 && _totalBytes > _transferredBytes)
{
eta = TimeSpan.FromSeconds((_totalBytes - _transferredBytes) / speed);
}
ProgressChanged?.Invoke(this, new TransferProgressEventArgs
{
TotalBytesTransferred = _transferredBytes,
TotalBytes = _totalBytes,
FilesCompleted = _transferredFiles,
TotalFiles = _totalFiles,
SpeedBytesPerSecond = speed,
EstimatedTimeRemaining = eta,
CurrentFile = currentFile,
IsScanning = _isScanning
});
}
}
}