private void InnerCopy([NotNull] string sourceFileName, [NotNull] string destFileName, bool overwrite, bool isCopyAfterMoveFailed) { FileCopyResult copyResult = null; try { container.FileSystemLock.ExecuteInLock(() => { AbsolutePath sourcePath = owner.ToAbsolutePath(sourceFileName); AbsolutePath destinationPath = owner.ToAbsolutePath(destFileName); var handler = new FileCopyHandler(container); var arguments = new FileCopyArguments(sourcePath, destinationPath, overwrite, isCopyAfterMoveFailed); copyResult = handler.Handle(arguments); }); WaitOnIndicator(owner.CopyWaitIndicator); copyResult.SourceStream.CopyTo(copyResult.DestinationStream); } finally { copyResult?.DestinationStream.Dispose(); copyResult?.SourceStream.Dispose(); } container.FileSystemLock.ExecuteInLock(() => { copyResult.DestinationFile.LastWriteTimeUtc = copyResult.ExistingDestinationLastWriteTimeUtc ?? copyResult.SourceFile.LastWriteTimeUtc; }); }
public void Copy(string sourceFileName, string destFileName, bool overwrite = false) { Guard.NotNull(sourceFileName, nameof(sourceFileName)); Guard.NotNull(destFileName, nameof(destFileName)); AssertFileNameIsNotEmpty(sourceFileName); AssertFileNameIsNotEmpty(destFileName); FileCopyResult copyResult = null; try { lock (owner.TreeLock) { AbsolutePath sourcePath = owner.ToAbsolutePath(sourceFileName); AbsolutePath destinationPath = owner.ToAbsolutePath(destFileName); var handler = new FileCopyHandler(root); var arguments = new FileCopyArguments(sourcePath, destinationPath, overwrite); copyResult = handler.Handle(arguments); } WaitOnIndicator(owner.CopyWaitIndicator); copyResult.SourceStream.CopyTo(copyResult.DestinationStream); } finally { copyResult?.DestinationStream.Dispose(); copyResult?.SourceStream.Dispose(); } lock (owner.TreeLock) { copyResult.DestinationFile.LastWriteTimeUtc = copyResult.SourceFile.LastWriteTimeUtc; } }
public FileCopyResult CopyFile(string Source, string Target) { _sourceFile = Source; _targetFile = Target; bool resume = false; _EventArgs.Action = FileCopyEventAction.Continue; FileCopyResult result = FileCopyResult.Success; using (FileStream source = new FileStream(Source, FileMode.Open, FileAccess.Read)) { long total = source.Length; long left = total; if (File.Exists(Target)) { switch (Options.DefaultFileExistsAction) { case FileExistsAction.Overwrite: { File.Delete(Target); break; } case FileExistsAction.OverwriteOlder: { if (File.GetLastWriteTime(Target) < File.GetLastWriteTime(Source)) { File.Delete(Target); } break; } case FileExistsAction.Resume: { resume = true; break; } } } using (FileStream target = new FileStream(Target, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.Read)) { long bytesTransferred = 0; if (resume) { target.Seek(0, SeekOrigin.End); left -= target.Length; bytesTransferred += target.Length; } while (left > 0) { byte[] buffer; if (left > Options.BufferSize) { buffer = new byte[Options.BufferSize]; } else { buffer = new byte[left]; } left -= buffer.Length; bytesTransferred += buffer.Length; source.Read(buffer, 0, buffer.Length); target.Write(buffer, 0, buffer.Length); _EventArgs.BytesLeft = left; _EventArgs.BytesTransferred = bytesTransferred; if (OnFileProgress != null) { OnFileProgress(this, _EventArgs); } if (_EventArgs.Action == FileCopyEventAction.Abort) { result = FileCopyResult.Aborted; break; } } } if (result != FileCopyResult.Aborted) { File.SetCreationTime(Target, File.GetCreationTime(Source)); File.SetAttributes(Target, File.GetAttributes(Source)); } } return(result); }