private void CopyFiles(String sourceFile, String targetFile, UInt32 bufferLength, Boolean overwrite, IncrementalHash sourceHash) { if (this.IsAbort) { return; } // Ensure the target file exists as requested and initially takes the same length as the source file has. AccessHandler.Create(targetFile, overwrite, AccessHandler.GetLength(sourceFile)); using (AccessHandler reader = new AccessHandler(this.logger)) using (AccessHandler writer = new AccessHandler(this.logger)) { reader.OpenRead(sourceFile); writer.OpenWrite(targetFile); Byte[] buffer = new Byte[bufferLength]; Int32 count = 0; Int32 total = 0; while (!this.IsAbort && reader.ReadChunk(buffer, out count)) { if (sourceHash != null) { sourceHash.AppendData(buffer, 0, count); } total += count; if (this.IsAbort) { return; } if (!writer.WriteChunk(buffer, count, out Int32 written)) { this.IsError = true; this.logger.Error( MethodBase.GetCurrentMethod(), "Buffer processing failure.", this.GetDetail("source-length", count.ToSafeString(nameof(Byte))), this.GetDetail("target-length", written.ToSafeString(nameof(Byte)))); return; } } if (this.IsAbort) { return; } this.logger.Trace( MethodBase.GetCurrentMethod(), $"Processed file length: {total.ToSafeString(nameof(Byte))}.", this.GetSourceFileDetail(sourceFile)); } }
private void VerifyFiles(String targetFile, UInt32 bufferLength, IncrementalHash sourceHash, IncrementalHash targetHash) { if (this.IsAbort) { return; } using (AccessHandler reader = new AccessHandler(this.logger)) { reader.OpenRead(targetFile); Byte[] buffer = new Byte[bufferLength]; Int32 total = 0; while (!this.IsAbort && reader.ReadChunk(buffer, out Int32 length)) { targetHash.AppendData(buffer, 0, length); total += length; } if (this.IsAbort) { return; } String sourceResult = sourceHash.GetHashAndReset().ToSafeHexString(); String targetResult = targetHash.GetHashAndReset().ToSafeHexString(); if (String.Compare(sourceResult, targetResult) != 0) { this.IsError = true; this.logger.Error( MethodBase.GetCurrentMethod(), "File verification mismatch.", this.GetSourceHashDetail(sourceResult), this.GetTargetHashDetail(targetResult)); return; } if (this.IsAbort) { return; } this.logger.Trace( MethodBase.GetCurrentMethod(), $"Verified file length: {total.ToSafeString(nameof(Byte))}.", this.GetTargetHashDetail(targetResult), this.GetTargetFileDetail(targetFile)); } }