Ejemplo n.º 1
0
        public Operation(FileOperationItem fileOperationItem, OperationError operationError)
        {
            if (fileOperationItem == null)
            {
                throw new ArgumentNullException(nameof(fileOperationItem));
            }
            if (operationError == null)
            {
                throw new ArgumentNullException(nameof(operationError));
            }

            this.FileOperationItem = fileOperationItem;
            this.OperationError    = operationError;
        }
Ejemplo n.º 2
0
        public static void CalculateCopyFileOperations
        (
            DifferenceCalculationResults results,
            DirectoryInfo sourceDirectory,
            DirectoryInfo destDirectory,
            DirectoryInfo parentDirectory
        )
        {
#if DEBUG
            IEnumerable <FileSystemInfo> fileSystemInfos = parentDirectory.GetFileSystemInfos();
#else
            IEnumerable <FileSystemInfo> fileSystemInfos = parentDirectory.EnumerateFileSystemInfos();
#endif
            foreach (FileSystemInfo fileSystemInfo in fileSystemInfos)
            {
                string matchingFilePath = FileSystem.GetOtherFolderMatchingFileSystemPath
                                          (
                    fileSystemInfo,
                    sourceDirectory,
                    destDirectory
                                          );
                FileSystemInfo matchingFileSystemInfo = FileSystem.GetFileSystemInfo
                                                        (
                    matchingFilePath
                                                        );
#if DEBUG
                bool isFolder             = fileSystemInfo.IsFolder();
                bool?matchingFileIsFolder = matchingFileSystemInfo?.IsFolder();
#endif

                if (matchingFileSystemInfo == null)
                {
                    // The matching file in the destination directory does not exists,
                    // we can safely copy this file (or folder) there.

                    results.AddCopyOperation
                    (
                        fileSystemInfo.FullName,
                        matchingFilePath,
                        OperationErrors.DestFileDoesNotExists
                    );
                }
                else
                {
#if !DEBUG
                    bool isFolder = fileSystemInfo.IsFolder();
#endif
                    if (isFolder != matchingFileSystemInfo.IsFolder())
                    {
                        /*
                         * If the matching file's "kind" (is file or is folder) is different
                         * from the "kind" of this file, we have to delete it first in order to override it
                         * and then copy this file there once it can be done with no errors.
                         */

                        results.AddCopyOperation
                        (
                            fileSystemInfo.FullName,
                            matchingFileSystemInfo.FullName,
                            OperationErrors.DestFileDifferentDirAttr
                        );
                    }
                    else
                    {
                        if (isFolder)
                        {
                            // If both are directories, then we need to keep calculating the differences.
                            CalculateCopyFileOperations
                            (
                                results,
                                sourceDirectory,
                                destDirectory,
                                (DirectoryInfo)fileSystemInfo
                            );
                        }
                        else
                        {
                            // If the file exists, then we need to compare them.
                            OperationError error            = null;
                            var            fileInfo         = (FileInfo)fileSystemInfo;
                            var            matchingFileInfo = (FileInfo)matchingFileSystemInfo;

                            if (fileInfo.LastWriteTime > matchingFileInfo.LastWriteTime)
                            {
                                error = OperationErrors.DestFileLaterLastWriteTime;
                            }
                            else if (fileInfo.LastWriteTime == matchingFileInfo.LastWriteTime)
                            {
                                if (fileInfo.Length != matchingFileInfo.Length)
                                {
                                    error = OperationErrors.DestFileSameWriteTimeDifferentLength;
                                }
                            }
                            else // fileInfo.LastWriteTime < matchingFileInfo.LastWriteTime
                            {
                                error = OperationErrors.DestFileBeforeWriteTime;
                            }

                            if (error != null)
                            {
                                results.AddCopyOperation
                                (
                                    fileSystemInfo.FullName,
                                    matchingFileSystemInfo.FullName,
                                    error
                                );
                            }
                        }
                    }
                }
            }
        }
 public void AddCopyOperation(string sourceFilePath, string destionationFilePath, OperationError error)
 {
     this.Operations.Add(new Operation
                         (
                             CreateCopyOperation(sourceFilePath, destionationFilePath),
                             error
                         ));
 }