private static bool CopyFile(USNJournalSyncLog syncLog, SyncMountpoint syncFrom) { logger.Info($"[{syncLog.Id}] [C] {syncLog.Action.RelativePath}"); var copyAction = syncLog.Action as UpdateAction; var publicPath = syncFrom.Mountpoint.Reference.PublicPath; var relativePath = copyAction.RelativePath; if (publicPath == null) { throw new NullReferenceException("publicPath"); } if (relativePath == null) { throw new NullReferenceException("relativePath"); } var destination = Path.Get(syncFrom.Path, relativePath); string tempPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toCopy", syncLog.Id).FullPath; var source = Path.Get(publicPath, relativePath); if (source.IsDirectory) { DirectoryInfo sourceInfo = new DirectoryInfo(source.FullPath); DirectoryInfo tempInfo = new DirectoryInfo(tempPath); DirectoryInfo destInfo = new DirectoryInfo(destination.FullPath); sourceInfo.CopyTo(tempInfo.FullName); tempInfo.MoveTo(destInfo.FullName); } else { source.Copy(tempPath); FileInfo tempInfo = new FileInfo(tempPath); tempInfo.MoveTo(destination.FullPath, MoveOptions.ReplaceExisting); } return(true); }
private void DumpGetXxxTime(bool isLocal) { #region Setup Console.WriteLine("\n=== TEST {0} ===", isLocal ? Local : Network); string path = isLocal ? SysRoot32 : Path.LocalToUnc(SysRoot32); Console.WriteLine("\nInput Directory Path: [{0}]\n", path); #endregion // Setup StopWatcher(true); #region GetCreationTimeXxx DateTime actual = Directory.GetCreationTime(path); DateTime expected = System.IO.Directory.GetCreationTime(path); Console.WriteLine("\tGetCreationTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "GetCreationTime()"); actual = Directory.GetCreationTimeUtc(path); expected = System.IO.Directory.GetCreationTimeUtc(path); Console.WriteLine("\tGetCreationTimeUtc() : [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "GetCreationTimeUtc()"); #endregion // GetCreationTimeXxx #region GetLastAccessTimeXxx actual = Directory.GetLastAccessTime(path); expected = System.IO.Directory.GetLastAccessTime(path); Console.WriteLine("\tGetLastAccessTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "GetLastAccessTime()"); actual = Directory.GetLastAccessTimeUtc(path); expected = System.IO.Directory.GetLastAccessTimeUtc(path); Console.WriteLine("\tGetLastAccessTimeUtc(): [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "GetLastAccessTimeUtc()"); #endregion // GetLastAccessTimeXxx #region GetLastWriteTimeXxx actual = Directory.GetLastWriteTime(path); expected = System.IO.Directory.GetLastWriteTime(path); Console.WriteLine("\tGetLastWriteTime() : [{0}] System.IO: [{1}]", actual, expected); Assert.AreEqual(expected, actual, "GetLastWriteTime()"); actual = Directory.GetLastWriteTimeUtc(path); expected = System.IO.Directory.GetLastWriteTimeUtc(path); Console.WriteLine("\tGetLastWriteTimeUtc() : [{0}] System.IO: [{1}]\n", actual, expected); Assert.AreEqual(expected, actual, "GetLastWriteTimeUtc()"); #endregion // GetLastWriteTimeXxx #region GetChangeTimeXxx Console.WriteLine("\tGetChangeTime() : [{0}] System.IO: [N/A]", Directory.GetChangeTime(path)); Console.WriteLine("\tGetChangeTimeUtc() : [{0}] System.IO: [N/A]", Directory.GetChangeTimeUtc(path)); #endregion GetChangeTimeXxx Console.WriteLine(); Console.WriteLine(Reporter()); Console.WriteLine(); #region Trigger GetChangeTimeXxx // We can not compare ChangeTime against .NET because it does not exist. // Creating a directory and renaming it triggers ChangeTime, so test for that. path = Path.GetTempPath("Directory-GetChangeTimeXxx()-directory-" + Path.GetRandomFileName()); if (!isLocal) path = Path.LocalToUnc(path); DirectoryInfo di = new DirectoryInfo(path); di.Create(); string fileName = di.Name; DateTime lastAccessTimeActual = Directory.GetLastAccessTime(path); DateTime lastAccessTimeUtcActual = Directory.GetLastAccessTimeUtc(path); DateTime changeTimeActual = Directory.GetChangeTime(path); DateTime changeTimeUtcActual = Directory.GetChangeTimeUtc(path); Console.WriteLine("\nTesting ChangeTime on a temp directory."); Console.WriteLine("\nInput Directory Path: [{0}]\n", path); Console.WriteLine("\tGetChangeTime() : [{0}]\t", changeTimeActual); Console.WriteLine("\tGetChangeTimeUtc() : [{0}]\t", changeTimeUtcActual); di.MoveTo(di.FullName.Replace(fileName, fileName + "-Renamed")); // Pause for at least a second so that the difference in time can be seen. int sleep = new Random().Next(2000, 4000); Thread.Sleep(sleep); di.MoveTo(di.FullName.Replace(fileName + "-Renamed", fileName)); DateTime lastAccessTimeExpected = Directory.GetLastAccessTime(path); DateTime lastAccessTimeUtcExpected = Directory.GetLastAccessTimeUtc(path); DateTime changeTimeExpected = Directory.GetChangeTime(path); DateTime changeTimeUtcExpected = Directory.GetChangeTimeUtc(path); Console.WriteLine("\nTrigger ChangeTime by renaming the directory."); Console.WriteLine("For Unit Test, ChangeTime should differ approximately: [{0}] seconds.\n", sleep / 1000); Console.WriteLine("\tGetChangeTime() : [{0}]\t", changeTimeExpected); Console.WriteLine("\tGetChangeTimeUtc() : [{0}]\t\n", changeTimeUtcExpected); Assert.AreNotEqual(changeTimeActual, changeTimeExpected); Assert.AreNotEqual(changeTimeUtcActual, changeTimeUtcExpected); Assert.AreEqual(lastAccessTimeExpected, lastAccessTimeActual); Assert.AreEqual(lastAccessTimeUtcExpected, lastAccessTimeUtcActual); #endregion // Trigger GetChangeTimeXxx di.Delete(); di.Refresh(); // Must Refresh() to get actual state. Assert.IsFalse(di.Exists, "Cleanup failed: Directory should have been removed."); Console.WriteLine(); }
private void TransferItem(USNJournalSyncLog syncLog, SyncMountpoint syncFrom) { try { syncLog.ActionStartDate = DateTime.Now; syncLog.ActionFinishDate = null; Singleton.Instance.Repository.Update(syncLog); bool successfull = false; if (syncLog.Action.GetType() == typeof(DeleteAction)) { var path = Path.Get(syncFrom.Path, syncLog.Action.RelativePath); logger.Info($"[{syncLog.Id}] [D] " + path); if (path.Exists) { var destinationPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toDelete", syncLog.Id).FullPath; if (syncLog.Action.IsDirectory) { DirectoryInfo info = new DirectoryInfo(path.FullPath); info.MoveTo(destinationPath); info.Delete(true, true); } else { FileInfo info = new FileInfo(path.FullPath); info.MoveTo(destinationPath); info.Delete(); } } successfull = true; } else if (syncLog.Action.GetType() == typeof(RenameAction)) { var renameAction = syncLog.Action as RenameAction; logger.Info($"[{syncLog.Id}] [R] {renameAction.RenameFrom} to {renameAction.RelativePath}"); if (String.IsNullOrWhiteSpace(renameAction.RenameFrom)) { CopyFile(syncLog, syncFrom); } string pathFrom = Path.Get(syncFrom.Path, renameAction.RenameFrom).FullPath; string tempPath = Path.Get(syncFrom.Path, ".proximaTemp.", "toRename", syncLog.Id).FullPath; string pathTo = Path.Get(syncFrom.Path, renameAction.RelativePath).FullPath; if (syncLog.Action.IsDirectory) { new DirectoryInfo(pathFrom).MoveTo(tempPath, MoveOptions.None); new DirectoryInfo(tempPath).MoveTo(pathTo, MoveOptions.None); } else { Alphaleonis.Win32.Filesystem.File.Move(pathFrom, tempPath, MoveOptions.None); Alphaleonis.Win32.Filesystem.File.Move(tempPath, pathTo, MoveOptions.None); //new FileInfo(pathFrom).MoveTo(tempPath, MoveOptions.WriteThrough); //new FileInfo(tempPath).MoveTo(pathTo, MoveOptions.WriteThrough); } successfull = true; } else { successfull = CopyFile(syncLog, syncFrom); } syncLog.ActionFinishDate = DateTime.Now; syncLog.Successfull = successfull; Singleton.Instance.Repository.Update(syncLog); foreach (var error in Singleton.Instance.Repository.Many <Error>(f => f.SyncLog.Id == syncLog.Id)) { Singleton.Instance.Repository.Delete(error); } } catch (FileNotFoundException ex) { syncLog.RequiresManualIntervention = true; syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(ex, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = ex; error.ItemId = syncLog.Id; error.Message = ex.Message; Singleton.Instance.Repository.Add(error); } catch (Exception e) { syncLog.ActionFinishDate = DateTime.Now; Singleton.Instance.Repository.Update(syncLog); logger.Error(e, "Error on item " + syncLog.Id); Error error = new Error(); error.SyncLog = syncLog; error.Exception = e; error.ItemId = syncLog.Id; error.Message = e.Message; Singleton.Instance.Repository.Add(error); } }