private DirectoryInfo DifferencePartialParallelCopy(DirectoryInfo source, DirectoryInfo target, LogOptions _logOptions, ParallelOptions _po) { DirectoryInfo newDirectoryInfo = null; try { newDirectoryInfo = target; OnAtomicCurrent?.Invoke(this, new StringEventArgs(source.FullName)); } catch (Exception Ex) { Logger.log(newDirectoryInfo.FullName, Ex.Message, _logOptions); } try { Parallel.ForEach(source.GetFiles(), _po, (fileInfo, state) => { OnAtomicCurrent?.Invoke(this, new StringEventArgs(fileInfo.FullName)); _po.CancellationToken.ThrowIfCancellationRequested(); if (_po.CancellationToken.IsCancellationRequested) { state.Break(); } try { if (fileInfo.Exists && ((File.GetAttributes(fileInfo.FullName) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) && AccessViolationHelper.IsNotLocked(fileInfo.FullName, _logOptions)) { if (AccessValidator.CanRead(fileInfo.FullName)) { var _targetpath = Path.Combine(newDirectoryInfo.FullName, fileInfo.Name); if (File.Exists(_targetpath)) { if (IsSourceNewer(fileInfo.FullName, _targetpath)) { fileInfo.CopyTo(_targetpath, true); File.SetAttributes(_targetpath, FileAttributes.Normal); } } else { fileInfo.CopyTo(_targetpath, true); File.SetAttributes(_targetpath, FileAttributes.Normal); } } else { Logger.log(fileInfo.FullName, "Access Denied", _logOptions); } } } catch (Exception Ex) { try { Logger.log(fileInfo.FullName, Ex.Message, _logOptions); } catch (Exception) { throw; } } }); } catch (OperationCanceledException) { throw; } foreach (DirectoryInfo childDirectoryInfo in source.GetDirectories()) { if (_po.CancellationToken.IsCancellationRequested) { _po.CancellationToken.ThrowIfCancellationRequested(); } try { if (childDirectoryInfo.Exists && (childDirectoryInfo.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) { newDirectoryInfo = target.CreateSubdirectory(childDirectoryInfo.Name); DifferencePartialParallelCopy(childDirectoryInfo, newDirectoryInfo, _logOptions, _po); } else { Logger.log(childDirectoryInfo.FullName, "Reparse Point / Hard Link", _logOptions); } } catch (Exception Ex) { try { if (!Ex.Message.Contains("The operation was canceled.")) { Logger.log(childDirectoryInfo.FullName, Ex.Message, _logOptions); } continue; } catch (Exception) { throw; } } } return(newDirectoryInfo); }
private DirectoryInfo CopyRecursive(DirectoryInfo source, DirectoryInfo target, LogOptions _logOptions, CancellationToken token) { DirectoryInfo newDirectoryInfo = null; if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } try { newDirectoryInfo = target; OnAtomicCurrent?.Invoke(this, new StringEventArgs(source.FullName)); } catch (Exception Ex) { Logger.log(newDirectoryInfo.FullName, Ex.Message, _logOptions); } foreach (var fileInfo in source.GetFiles()) { OnAtomicCurrent?.Invoke(this, new StringEventArgs(fileInfo.FullName)); if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } try { if (fileInfo.Exists && ((File.GetAttributes(fileInfo.FullName) & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) && AccessViolationHelper.IsNotLocked(fileInfo.FullName, _logOptions)) { if (AccessValidator.CanRead(fileInfo.FullName)) { var _targetpath = Path.Combine(newDirectoryInfo.FullName, fileInfo.Name); fileInfo.CopyTo(_targetpath, true); File.SetAttributes(_targetpath, FileAttributes.Normal); } else { Logger.log(fileInfo.FullName, "Access Denied", _logOptions); } } } catch (OperationCanceledException) { throw; } catch (Exception Ex) { try { if (!Ex.Message.Contains("The operation was canceled.")) { Logger.log(fileInfo.FullName, Ex.Message, _logOptions); } else { break; } continue; } catch (Exception) { throw; } } } foreach (var childDirectoryInfo in source.GetDirectories()) { if (token.IsCancellationRequested) { token.ThrowIfCancellationRequested(); } try { if (childDirectoryInfo.Exists && (childDirectoryInfo.Attributes & FileAttributes.ReparsePoint) != FileAttributes.ReparsePoint) { newDirectoryInfo = target.CreateSubdirectory(childDirectoryInfo.Name); CopyRecursive(childDirectoryInfo, newDirectoryInfo, _logOptions, token); } else { Logger.log(childDirectoryInfo.FullName, "Reparse Point / Hard Link", _logOptions); } } catch (OperationCanceledException) { throw; } catch (Exception Ex) { try { if (!Ex.Message.Contains("The operation was canceled.")) { Logger.log(childDirectoryInfo.FullName, Ex.Message, _logOptions); } else { break; } continue; } catch (Exception) { throw; } } } return(newDirectoryInfo); }