public void TestCompare_When_Directories_Are_Same() { var tempDir = Path.GetTempPath(); var testDir = Path.Combine(tempDir, Guid.NewGuid().ToString("N")); Directory.CreateDirectory(testDir); try { // Arrange var dir1Path = Path.Combine(testDir, "1"); Directory.CreateDirectory(dir1Path); File.WriteAllText(Path.Combine(dir1Path, "both_same.txt"), "1234567890"); Directory.CreateDirectory(Path.Combine(dir1Path, "both")); File.WriteAllText(Path.Combine(dir1Path, "both", "both_same.txt"), "1234567890"); var dir2Path = Path.Combine(testDir, "2"); Directory.CreateDirectory(dir2Path); File.WriteAllText(Path.Combine(dir2Path, "both_same.txt"), "1234567890"); Directory.CreateDirectory(Path.Combine(dir2Path, "both")); File.WriteAllText(Path.Combine(dir2Path, "both", "both_same.txt"), "1234567890"); // Act var results = DirectoryComparer.Compare(dir1Path, dir2Path); // Assert var fileResults = results.OfType <FileCompareResult>().ToArray(); Assert.AreEqual(1, fileResults.Length); var sameFileResult = fileResults.Single(r => r.Name == "both_same.txt"); Assert.IsTrue(sameFileResult.AreSame); Assert.AreEqual(Path.Combine(dir1Path, "both_same.txt"), sameFileResult.FullPath1); Assert.AreEqual(Path.Combine(dir2Path, "both_same.txt"), sameFileResult.FullPath2); var dirResults = results.OfType <DirectoryCompareResult>().ToArray(); Assert.AreEqual(1, dirResults.Length); var bothDirResult = dirResults.Single(r => r.Name == "both"); Assert.IsTrue(bothDirResult.AreSame); Assert.AreEqual(Path.Combine(dir1Path, "both"), bothDirResult.FullPath1); Assert.AreEqual(Path.Combine(dir2Path, "both"), bothDirResult.FullPath2); Assert.AreEqual(1, bothDirResult.Items.Count); } finally { Directory.Delete(testDir, recursive: true); } }
private IReadOnlyDictionary <string, DirectoryDifference> CreateComparisonsForInvocationUnit(IReadOnlyDictionary <string, string> commandAndPathMap) { Dictionary <string, DirectoryDifference> comparisonResults = new Dictionary <string, DirectoryDifference>(); foreach (KeyValuePair <string, string> commandAndRelativePath in commandAndPathMap) { string command = commandAndRelativePath.Key; string relativePath = commandAndRelativePath.Value; DirectoryComparer comparer = new DirectoryComparer(_masterDataBasePath, _secondaryDataBasePath, relativePath); comparisonResults[command] = comparer.Compare(); } return(comparisonResults); }
/// <summary> /// Expands a node /// </summary> /// <param name="node">the node to expand</param> void ExpandNode(TreeNode node) { NodeTag tag = (NodeTag)node.Tag; if (tag._hasBeenExpandedOnce) { return; } DirectoryComparer comparer = new DirectoryComparer(); foreach (TreeNode childnode in node.Nodes) { try { string path = ""; GetNodePath(childnode, ref path); childnode.Nodes.Clear(); System.IO.DirectoryInfo dirInfo = new System.IO.DirectoryInfo(path); // floppy or removable drive not mounted ? if (dirInfo.Exists) { System.IO.DirectoryInfo[] subDirInfos = dirInfo.GetDirectories(); Array.Sort(subDirInfos, comparer); foreach (System.IO.DirectoryInfo subDirInfo in subDirInfos) { if ((subDirInfo.Attributes & System.IO.FileAttributes.Hidden) == 0) { TreeNode nnode = new TreeNode(subDirInfo.Name); nnode.Tag = new NodeTag(); nnode.ImageIndex = 4; nnode.SelectedImageIndex = 5; childnode.Nodes.Add(nnode); } } } } catch { // floppy or removable drive not mounted } } tag._hasBeenExpandedOnce = true; }
private static void TaskAction(object state) { var tuple = (TaskTuple)state; var task = tuple.Item1; var cancelTokenSrc = tuple.Item2; try { task.Progress = new CompareProgress(task); App.Current .BeginInvoke((a, appState) => { appState.Task.Results.Clear(); }, actionState: new { Task = task, }); task.IsRunning = true; task.RaiseEventHandler(task.Started); var comparer = new DirectoryComparer(src: task.Source, dest: task.Destination); var ctx = comparer.CreateContext(recursive: task.Recursive, state: task); ctx.ComparingItems += (sender, e) => { Action <Exception> addError = (err) => { try { task.Results.Add(new CompareError(e, err)); } catch (Exception ex) { task.OnError(new AggregateException(ex, err)); } }; try { e.Handled = true; task.Progress.Source = e.Source; task.Progress.Destination = e.Destination; task.Progress.StatusText = null; task.Progress.ContentState = null; task.Progress.SizeState = null; task.Progress.TimestampState = null; if (e.Source is DirectoryInfo) { var srcDir = (DirectoryInfo)e.Source; var destDir = (DirectoryInfo)e.Destination; if (srcDir.Exists) { if (destDir.Exists) { } else { e.Differences |= FileSystemItemDifferences.IsMissing; } } else { if (destDir.Exists) { e.Differences |= FileSystemItemDifferences.IsExtra; } else { } } } else if (e.Source is FileInfo) { var srcFile = (FileInfo)e.Source; var destFile = (FileInfo)e.Destination; if (srcFile.Exists) { if (destFile.Exists) { var doHash = true; // timestamp task.Progress.StatusText = "Checking last write time..."; try { task.Progress.TimestampState = CompareState.InProgress; if (NormalizeTimestamp(srcFile.LastWriteTimeUtc) != NormalizeTimestamp(destFile.LastWriteTimeUtc)) { e.Differences |= FileSystemItemDifferences.LastWriteTime; task.Progress.TimestampState = CompareState.Different; doHash = true; } else { task.Progress.TimestampState = CompareState.Match; } } catch (Exception ex) { addError(ex); task.Progress.TimestampState = CompareState.Failed; } // size task.Progress.StatusText = "Checking file size..."; try { task.Progress.SizeState = CompareState.InProgress; if (srcFile.Length != destFile.Length) { e.Differences |= FileSystemItemDifferences.Size; task.Progress.SizeState = CompareState.Different; task.Progress.ContentState = CompareState.Different; doHash = false; } else { task.Progress.SizeState = CompareState.Match; } } catch (Exception ex) { addError(ex); task.Progress.SizeState = CompareState.Failed; } if (doHash && task.Hash != null) { task.Progress.StatusText = "Hashing files..."; // hash content try { task.Progress.ContentState = CompareState.InProgress; task.Progress.StatusText = "Hashing source file..."; byte[] hashSrc; using (var algo = (HashAlgorithm)Activator.CreateInstance(task.Hash)) { using (var stream = srcFile.OpenRead()) { hashSrc = algo.ComputeHash(stream); } } task.Progress.StatusText = "Hashing destination file..."; byte[] hashDest; using (var algo = (HashAlgorithm)Activator.CreateInstance(task.Hash)) { using (var stream = destFile.OpenRead()) { hashDest = algo.ComputeHash(stream); } } if (CollectionHelper.SequenceEqual(hashSrc, hashDest)) { task.Progress.ContentState = CompareState.Match; } else { task.Progress.ContentState = CompareState.Different; e.Differences |= FileSystemItemDifferences.Content; } } catch (Exception ex) { addError(ex); task.Progress.TimestampState = CompareState.Failed; } } } else { e.Differences |= FileSystemItemDifferences.IsMissing; } } else { if (destFile.Exists) { e.Differences |= FileSystemItemDifferences.IsExtra; } } } } catch (Exception ex) { addError(ex); } }; ctx.DifferentItemsFound += (sender, e) => { try { App.Current .BeginInvoke((a, appState) => { appState.Task .Results .Add(appState.Difference); }, actionState: new { Difference = new CompareDifference(e), Task = task, }); } catch (Exception ex) { task.OnError(ex); } }; var t = Task.Factory.StartNew(() => { try { ctx.Start(); } catch (Exception ex) { task.OnError(ex); } }); while (t.Status == TaskStatus.WaitingToRun) { } while (t.Status == TaskStatus.Running) { if (cancelTokenSrc.Token.IsCancellationRequested) { ctx.Cancel(); break; } } } catch (Exception ex) { task.OnError(ex); } finally { task.IsRunning = false; task.RaiseEventHandler(task.Stopped); task.Progress = null; } }