|
5 | 5 | using Microsoft.Extensions.Logging; |
6 | 6 | using System; |
7 | 7 | using System.Diagnostics; |
| 8 | +using System.Threading.Tasks; |
8 | 9 |
|
9 | 10 | namespace FileSyncLibNet.SyncProviders |
10 | 11 | { |
@@ -130,48 +131,106 @@ public override void SyncSourceToDest() |
130 | 131 | pattern: JobOptions.SearchPattern, |
131 | 132 | recursive: JobOptions.Recursive, |
132 | 133 | subfolders: JobOptions.Subfolders); |
133 | | - foreach (var sourceFile in sourceFiles) |
| 134 | + |
| 135 | + bool parallel = false; |
| 136 | + if (parallel) |
134 | 137 | { |
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) => |
138 | 139 | { |
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) |
140 | 143 | { |
| 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 | + } |
141 | 153 | try |
142 | 154 | { |
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); |
144 | 171 | } |
145 | | - catch (Exception ex) { logger?.LogError(ex, "exception creating destination directory {A}", jobOptions.DestinationPath); } |
146 | | - createDestinationDir = false; |
147 | 172 | } |
148 | | - try |
| 173 | + else |
149 | 174 | { |
150 | | - logger.LogDebug("Copy {A}", sourceFile.Name); |
151 | | - using (var sourceStream = SourceAccess.GetStream(sourceFile)) |
| 175 | + |
| 176 | + skipped++; |
| 177 | + if (skipped % 1000 == 0) |
152 | 178 | { |
153 | | - DestinationAccess.WriteFile(sourceFile, sourceStream); |
| 179 | + logger.LogTrace("Skip {A} {B}", skipped, sourceFile); |
154 | 180 | } |
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) |
158 | 194 | { |
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); |
160 | 220 | } |
161 | 221 | } |
162 | | - catch (Exception exc) |
| 222 | + else |
163 | 223 | { |
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); |
166 | 231 | } |
167 | | - } |
168 | | - else |
169 | | - { |
170 | 232 |
|
171 | | - skipped++; |
172 | | - logger.LogTrace("Skip {A}", sourceFile); |
173 | 233 | } |
174 | | - |
175 | 234 | } |
176 | 235 | if (!error_occured) |
177 | 236 | { |
|
0 commit comments