Skip to content

Commit d557574

Browse files
prepare parallel copying in AbstractProvider - much faster for small files but may be problematic on controllers
1 parent 5bfe911 commit d557574

1 file changed

Lines changed: 84 additions & 25 deletions

File tree

FileSyncLibNet/SyncProviders/AbstractProvider.cs

Lines changed: 84 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Microsoft.Extensions.Logging;
66
using System;
77
using System.Diagnostics;
8+
using System.Threading.Tasks;
89

910
namespace FileSyncLibNet.SyncProviders
1011
{
@@ -130,48 +131,106 @@ public override void SyncSourceToDest()
130131
pattern: JobOptions.SearchPattern,
131132
recursive: JobOptions.Recursive,
132133
subfolders: JobOptions.Subfolders);
133-
foreach (var sourceFile in sourceFiles)
134+
135+
bool parallel = false;
136+
if (parallel)
134137
{
135-
var remoteFile = DestinationAccess.GetFileInfo(sourceFile.Name);
136-
bool copy = !remoteFile.Exists || remoteFile.Length != sourceFile.Length || remoteFile.LastWriteTime != sourceFile.LastWriteTime;
137-
if (copy)
138+
Parallel.ForEach(sourceFiles, (sourceFile) =>
138139
{
139-
if (createDestinationDir)
140+
var remoteFile = DestinationAccess.GetFileInfo(sourceFile.Name);
141+
bool copy = !remoteFile.Exists || remoteFile.Length != sourceFile.Length || remoteFile.LastWriteTime != sourceFile.LastWriteTime;
142+
if (copy)
140143
{
144+
if (createDestinationDir)
145+
{
146+
try
147+
{
148+
DestinationAccess.CreateDirectory("");
149+
}
150+
catch (Exception ex) { logger?.LogError(ex, "exception creating destination directory {A}", jobOptions.DestinationPath); }
151+
createDestinationDir = false;
152+
}
141153
try
142154
{
143-
DestinationAccess.CreateDirectory("");
155+
logger.LogDebug("Copy {A}", sourceFile.Name);
156+
using (var sourceStream = SourceAccess.GetStream(sourceFile))
157+
{
158+
DestinationAccess.WriteFile(sourceFile, sourceStream);
159+
}
160+
//file.copy
161+
copied++;
162+
if (jobOptions.DeleteSourceAfterBackup)
163+
{
164+
SourceAccess.Delete(sourceFile);
165+
}
166+
}
167+
catch (Exception exc)
168+
{
169+
error_occured = true;
170+
logger.LogError(exc, "Exception copying {A}", sourceFile);
144171
}
145-
catch (Exception ex) { logger?.LogError(ex, "exception creating destination directory {A}", jobOptions.DestinationPath); }
146-
createDestinationDir = false;
147172
}
148-
try
173+
else
149174
{
150-
logger.LogDebug("Copy {A}", sourceFile.Name);
151-
using (var sourceStream = SourceAccess.GetStream(sourceFile))
175+
176+
skipped++;
177+
if (skipped % 1000 == 0)
152178
{
153-
DestinationAccess.WriteFile(sourceFile, sourceStream);
179+
logger.LogTrace("Skip {A} {B}", skipped, sourceFile);
154180
}
155-
//file.copy
156-
copied++;
157-
if (jobOptions.DeleteSourceAfterBackup)
181+
//logger.LogTrace("Skip {A} {B}", skipped, sourceFile);
182+
}
183+
});
184+
}
185+
else
186+
{
187+
foreach (var sourceFile in sourceFiles)
188+
{
189+
var remoteFile = DestinationAccess.GetFileInfo(sourceFile.Name);
190+
bool copy = !remoteFile.Exists || remoteFile.Length != sourceFile.Length || remoteFile.LastWriteTime != sourceFile.LastWriteTime;
191+
if (copy)
192+
{
193+
if (createDestinationDir)
158194
{
159-
SourceAccess.Delete(sourceFile);
195+
try
196+
{
197+
DestinationAccess.CreateDirectory("");
198+
}
199+
catch (Exception ex) { logger?.LogError(ex, "exception creating destination directory {A}", jobOptions.DestinationPath); }
200+
createDestinationDir = false;
201+
}
202+
try
203+
{
204+
logger.LogDebug("Copy {A}", sourceFile.Name);
205+
using (var sourceStream = SourceAccess.GetStream(sourceFile))
206+
{
207+
DestinationAccess.WriteFile(sourceFile, sourceStream);
208+
}
209+
//file.copy
210+
copied++;
211+
if (jobOptions.DeleteSourceAfterBackup)
212+
{
213+
SourceAccess.Delete(sourceFile);
214+
}
215+
}
216+
catch (Exception exc)
217+
{
218+
error_occured = true;
219+
logger.LogError(exc, "Exception copying {A}", sourceFile);
160220
}
161221
}
162-
catch (Exception exc)
222+
else
163223
{
164-
error_occured = true;
165-
logger.LogError(exc, "Exception copying {A}", sourceFile);
224+
225+
skipped++;
226+
if (skipped % 1000 == 0)
227+
{
228+
logger.LogTrace("Skip {A} {B}", skipped, sourceFile);
229+
}
230+
//logger.LogTrace("Skip {A} {B}", skipped, sourceFile);
166231
}
167-
}
168-
else
169-
{
170232

171-
skipped++;
172-
logger.LogTrace("Skip {A}", sourceFile);
173233
}
174-
175234
}
176235
if (!error_occured)
177236
{

0 commit comments

Comments
 (0)