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);
}
}