Bari.Core.Build.Cache.FileBuildCache.CopyIfDifferent C# (CSharp) Method

CopyIfDifferent() private method

Copies a source file to a target location, but only if it does not exist yet, with the same MD5 checksum as the source
private CopyIfDifferent ( IFileSystemDirectory sourceDirectory, string sourceFileName, IFileSystemDirectory targetRoot, string targetRelativePath ) : void
sourceDirectory IFileSystemDirectory Source root directory
sourceFileName string Source file's relative path
targetRoot IFileSystemDirectory Target root directory
targetRelativePath string Target file's relative path
return void
        private void CopyIfDifferent(IFileSystemDirectory sourceDirectory, string sourceFileName, IFileSystemDirectory targetRoot, string targetRelativePath)
        {
            bool copy = true;
            long sourceSize = sourceDirectory.GetFileSize(sourceFileName);
            if (targetRoot.Exists(targetRelativePath))
            {
                long targetSize = targetRoot.GetFileSize(targetRelativePath);
                if (sourceSize == targetSize)
                {
                    var sourceChecksum = Task.Factory.StartNew(() => ComputeChecksum(md5a, sourceDirectory, sourceFileName));
                    var targetChecksum = Task.Factory.StartNew(() => ComputeChecksum(md5b, targetRoot, targetRelativePath));

                    copy = !sourceChecksum.Result.SequenceEqual(targetChecksum.Result);
                }
            }

            if (copy)
            {
                sourceDirectory.CopyFile(sourceFileName, targetRoot, targetRelativePath);
            }
            else
            {
                log.DebugFormat("File {0} is the same as the cached one", targetRelativePath);
            }
        }