Ejemplo n.º 1
0
        /// <summary>
        /// Copies a source file to a target location, but only if it does not exist yet, with the same MD5 checksum
        /// as the source
        /// </summary>
        /// <param name="sourceDirectory">Source root directory</param>
        /// <param name="sourceFileName">Source file's relative path</param>
        /// <param name="targetRoot">Target root directory</param>
        /// <param name="targetRelativePath">Target file's relative path</param>
        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);
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Copies a source file to a target location, but only if it does not exist yet, with the same MD5 checksum
        /// as the source
        /// </summary>
        /// <param name="sourceDirectory">Source root directory</param>
        /// <param name="sourceFileName">Source file's relative path</param>
        /// <param name="targetRoot">Target root directory</param>
        /// <param name="targetRelativePath">Target file's relative path</param>
        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)
                {
                    byte[] sourceChecksum = ComputeChecksum(sourceDirectory, sourceFileName);
                    byte[] targetChecksum = ComputeChecksum(targetRoot, targetRelativePath);

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

            if (copy)
            {
                using (var source = sourceDirectory.ReadBinaryFile(sourceFileName))
                    using (var target = targetRoot.CreateBinaryFileWithDirectories(targetRelativePath))
                        StreamOperations.Copy(source, target);
            }
            else
            {
                log.DebugFormat("File {0} is the same as the cached one", targetRelativePath);
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Copy a file to a target directory
        /// </summary>
        /// <param name="name">Name of the file</param>
        /// <param name="target">Target file system directory</param>
        /// <param name="targetName">Name (relative path) in the target directory</param>
        public void CopyFile(string name, IFileSystemDirectory target, string targetName)
        {
            var localTarget = target as LocalFileSystemDirectory;

            if (localTarget != null)
            {
                var targetSubdir = Path.GetDirectoryName(targetName);

                if (!String.IsNullOrWhiteSpace(targetSubdir))
                {
                    var absoluteTargetDir = Path.Combine(localTarget.AbsolutePath, targetSubdir);
                    if (!Directory.Exists(absoluteTargetDir))
                    {
                        Directory.CreateDirectory(absoluteTargetDir);
                    }
                }

                var copy = true;
                if (target.Exists(targetName))
                {
                    var sourceSize = GetFileSize(name);
                    var targetSize = target.GetFileSize(targetName);
                    var sourceDate = GetLastModifiedDate(name);
                    var targetDate = target.GetLastModifiedDate(targetName);

                    copy = sourceSize != targetSize || !sourceDate.Equals(targetDate);
                }

                if (copy)
                {
                    File.Copy(Path.Combine(path, name), Path.Combine(localTarget.AbsolutePath, targetName), overwrite: true);
                }
            }
            else
            {
                using (var sourceStream = ReadBinaryFile(name))
                    using (var targetStream = target.CreateBinaryFileWithDirectories(targetName))
                        StreamOperations.Copy(sourceStream, targetStream);
            }
        }
Ejemplo n.º 4
0
        public SourceSetFingerprint([SuiteRoot] IFileSystemDirectory root, IEnumerable <SuiteRelativePath> files, Func <string, bool> exclusions, bool fullDependency)
        {
            Contract.Requires(root != null);
            Contract.Requires(files != null);

            this.fullDependency = fullDependency;
            fileNames           = new SortedSet <SuiteRelativePath>();
            lastModifiedDates   = new Dictionary <SuiteRelativePath, DateTime>();
            lastSizes           = new Dictionary <SuiteRelativePath, long>();

            foreach (var file in files)
            {
                if (exclusions == null || !exclusions(file))
                {
                    fileNames.Add(file);

                    if (fullDependency)
                    {
                        lastModifiedDates.Add(file, root.GetLastModifiedDate(file));
                        lastSizes.Add(file, root.GetFileSize(file));
                    }
                }
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Copies a source file to a target location, but only if it does not exist yet, with the same MD5 checksum
        /// as the source
        /// </summary>
        /// <param name="sourceDirectory">Source root directory</param>
        /// <param name="sourceFileName">Source file's relative path</param>
        /// <param name="targetRoot">Target root directory</param>
        /// <param name="targetRelativePath">Target file's relative path</param>
        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);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Copies a source file to a target location, but only if it does not exist yet, with the same MD5 checksum
        /// as the source
        /// </summary>
        /// <param name="sourceDirectory">Source root directory</param>
        /// <param name="sourceFileName">Source file's relative path</param>
        /// <param name="targetRoot">Target root directory</param>
        /// <param name="targetRelativePath">Target file's relative path</param>
        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)
                {
                    byte[] sourceChecksum = ComputeChecksum(sourceDirectory, sourceFileName);
                    byte[] targetChecksum = ComputeChecksum(targetRoot, targetRelativePath);

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

            if (copy)
            {
                using (var source = sourceDirectory.ReadBinaryFile(sourceFileName))
                using (var target = targetRoot.CreateBinaryFileWithDirectories(targetRelativePath))
                    StreamOperations.Copy(source, target);
            }
            else
            {
                log.DebugFormat("File {0} is the same as the cached one", targetRelativePath);
            }
        }