private void SyncNamesAndDates(IDirectoryInfo localFolder, IFolder remoteFolder, IMappedObject mappedObject) { DateTime?oldRemoteModificationDate = remoteFolder.LastModificationDate; DateTime oldLocalModificationDate = localFolder.LastWriteTimeUtc; // Sync Names if (mappedObject.Name != localFolder.Name && mappedObject.Name == remoteFolder.Name) { // local folder has been renamed => rename remote folder remoteFolder.Rename(localFolder.Name, true); mappedObject.Name = localFolder.Name; } else if (mappedObject.Name == localFolder.Name && mappedObject.Name != remoteFolder.Name) { // remote folder has been renamed => rename local folder localFolder.MoveTo(Path.Combine(localFolder.Parent.FullName, remoteFolder.Name)); mappedObject.Name = remoteFolder.Name; } else if (mappedObject.Name != localFolder.Name && mappedObject.Name != remoteFolder.Name) { // both folders are renamed => rename to the latest change DateTime localModification = localFolder.LastWriteTimeUtc; DateTime remoteModification = (DateTime)remoteFolder.LastModificationDate; if (localModification > remoteModification) { // local modification is newer remoteFolder.Rename(localFolder.Name, true); mappedObject.Name = localFolder.Name; } else { // remote modification is newer localFolder.MoveTo(Path.Combine(localFolder.Parent.FullName, remoteFolder.Name)); localFolder.LastWriteTimeUtc = (DateTime)remoteFolder.LastModificationDate; mappedObject.Name = remoteFolder.Name; } } // Sync modification dates if (oldRemoteModificationDate != null) { if (oldLocalModificationDate > oldRemoteModificationDate && this.ServerCanModifyDateTimes) { remoteFolder.UpdateLastWriteTimeUtc(oldLocalModificationDate); localFolder.LastWriteTimeUtc = oldLocalModificationDate; } else if (oldLocalModificationDate < (DateTime)oldRemoteModificationDate) { localFolder.LastWriteTimeUtc = (DateTime)oldRemoteModificationDate; } } mappedObject.LastLocalWriteTimeUtc = localFolder.LastWriteTimeUtc; mappedObject.LastRemoteWriteTimeUtc = (DateTime)remoteFolder.LastModificationDate; mappedObject.ParentId = remoteFolder.ParentId; mappedObject.LastChangeToken = remoteFolder.ChangeToken; this.Storage.SaveMappedObject(mappedObject); }
private void When_moving_directory_it_must_update_properties() { // Arrange const string sourcePath = @"c:\some\folder"; const string destinationPath = @"c:\other\folder"; IFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingDirectory(sourcePath) .IncludingDirectory(@"c:\other") .Build(); IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(sourcePath); string beforeDirectoryName = dirInfo.Parent.ShouldNotBeNull().FullName; // Act dirInfo.MoveTo(destinationPath); // Assert beforeDirectoryName.Should().Be(@"c:\some"); dirInfo.Exists.Should().BeTrue(); dirInfo.Name.Should().Be("folder"); dirInfo.Extension.Should().BeEmpty(); dirInfo.FullName.Should().Be(destinationPath); dirInfo.ToString().Should().Be(destinationPath); IDirectoryInfo parentInfo = dirInfo.Parent.ShouldNotBeNull(); parentInfo.FullName.Should().Be(@"c:\other"); IDirectoryInfo rootInfo = dirInfo.Root.ShouldNotBeNull(); rootInfo.FullName.Should().Be(@"c:\"); }
/// <summary> /// Solve the specified situation by using the session, storage, localFile and remoteId. /// Moves the local file/folder to the new location. /// </summary> /// <param name="localFile">Old local file/folder.</param> /// <param name="remoteId">Remote identifier.</param> /// <param name="localContent">Hint if the local content has been changed.</param> /// <param name="remoteContent">Information if the remote content has been changed.</param> public override void Solve( IFileSystemInfo localFile, IObjectId remoteId, ContentChangeType localContent = ContentChangeType.NONE, ContentChangeType remoteContent = ContentChangeType.NONE) { // Move local object var savedObject = this.Storage.GetObjectByRemoteId(remoteId.Id); string newPath = remoteId is IFolder?this.Storage.Matcher.CreateLocalPath(remoteId as IFolder) : this.Storage.Matcher.CreateLocalPath(remoteId as IDocument); if (remoteId is IFolder) { IDirectoryInfo dirInfo = localFile as IDirectoryInfo; string oldPath = dirInfo.FullName; if (!dirInfo.FullName.Equals(newPath)) { dirInfo.MoveTo(newPath); dirInfo.LastWriteTimeUtc = (remoteId as IFolder).LastModificationDate != null ? (DateTime)(remoteId as IFolder).LastModificationDate : dirInfo.LastWriteTimeUtc; OperationsLogger.Info(string.Format("Moved local folder {0} to {1}", oldPath, newPath)); } else { return; } } else if (remoteId is IDocument) { IFileInfo fileInfo = localFile as IFileInfo; string oldPath = fileInfo.FullName; fileInfo.MoveTo(newPath); fileInfo.LastWriteTimeUtc = (remoteId as IDocument).LastModificationDate != null ? (DateTime)(remoteId as IDocument).LastModificationDate : fileInfo.LastWriteTimeUtc; OperationsLogger.Info(string.Format("Moved local file {0} to {1}", oldPath, newPath)); } savedObject.Name = (remoteId as ICmisObject).Name; savedObject.ParentId = remoteId is IFolder ? (remoteId as IFolder).ParentId : (remoteId as IDocument).Parents[0].Id; savedObject.LastChangeToken = (remoteId is IDocument && remoteContent != ContentChangeType.NONE) ? savedObject.LastChangeToken : remoteId is ICmisObject ? (remoteId as ICmisObject).ChangeToken : null; savedObject.LastLocalWriteTimeUtc = localFile.LastWriteTimeUtc; savedObject.LastRemoteWriteTimeUtc = (remoteId is IDocument && remoteContent != ContentChangeType.NONE) ? savedObject.LastRemoteWriteTimeUtc : (remoteId as ICmisObject).LastModificationDate; savedObject.Ignored = (remoteId as ICmisObject).AreAllChildrenIgnored(); this.Storage.SaveMappedObject(savedObject); if (remoteId is IDocument && remoteContent != ContentChangeType.NONE) { throw new ArgumentException("Remote content has also been changed => force crawl sync."); } }
public void CreateDirectory_MoveTo() { //arange IDirectory directory = new DirectoryWrap(); // should create this somewhere and inject into you method for later mocking var source = Path.GetRandomFileName(); var target = Path.GetRandomFileName(); //act IDirectoryInfo info = directory.CreateDirectory(source); info.MoveTo(target); //assert Assert.IsTrue(info.Exists); // clean info.Delete(); }
private void When_moving_directory_it_must_succeed() { // Arrange const string sourcePath = @"c:\some\folder"; const string destinationPath = @"c:\other\renamed"; IFileSystem fileSystem = new FakeFileSystemBuilder() .IncludingTextFile(@"c:\some\folder\file.txt", DefaultContents, attributes: FileAttributes.ReadOnly) .IncludingDirectory(@"c:\other") .Build(); IDirectoryInfo dirInfo = fileSystem.ConstructDirectoryInfo(sourcePath); // Act dirInfo.MoveTo(destinationPath); // Assert fileSystem.Directory.Exists(sourcePath).Should().BeFalse(); fileSystem.Directory.Exists(destinationPath).Should().BeTrue(); }
public override void Solve( IFileSystemInfo localFileSystemInfo, IObjectId remoteId, ContentChangeType localContent, ContentChangeType remoteContent) { var savedObject = this.Storage.GetObjectByRemoteId(remoteId.Id); string newPath = remoteId is IFolder?this.Storage.Matcher.CreateLocalPath(remoteId as IFolder) : this.Storage.Matcher.CreateLocalPath(remoteId as IDocument); if (remoteId is IFolder) { IDirectoryInfo dirInfo = localFileSystemInfo as IDirectoryInfo; string oldPath = dirInfo.FullName; if (!dirInfo.FullName.Equals(newPath)) { dirInfo.MoveTo(newPath); OperationsLogger.Info(string.Format("Moved local folder {0} to {1}", oldPath, newPath)); } else { return; } } else if (remoteId is IDocument) { IFileInfo fileInfo = localFileSystemInfo as IFileInfo; string oldPath = fileInfo.FullName; fileInfo.MoveTo(newPath); OperationsLogger.Info(string.Format("Moved local file {0} to {1}", oldPath, newPath)); } savedObject.Name = (remoteId as ICmisObject).Name; savedObject.Ignored = (remoteId as ICmisObject).AreAllChildrenIgnored(); savedObject.ParentId = remoteId is IFolder ? (remoteId as IFolder).ParentId : (remoteId as IDocument).Parents[0].Id; this.Storage.SaveMappedObject(savedObject); this.changeChangeSolver.Solve(localFileSystemInfo, remoteId, localContent, remoteContent); }
private void SyncNamesAndDates(IFileSystemInfo local, IFileableCmisObject remote, IMappedObject mappedObject) { DateTime?oldRemoteModificationDate = remote.LastModificationDate; DateTime oldLocalModificationDate = local.LastWriteTimeUtc; // Sync Names if (mappedObject.Name != local.Name && mappedObject.Name == remote.Name) { // local has been renamed => rename remote remote.Rename(local.Name, true); mappedObject.Name = local.Name; } else if (mappedObject.Name == local.Name && mappedObject.Name != remote.Name) { // remote has been renamed => rename local if (local is IFileInfo) { IFileInfo localFile = local as IFileInfo; localFile.MoveTo(Path.Combine(localFile.Directory.FullName, remote.Name)); } else if (local is IDirectoryInfo) { IDirectoryInfo localFolder = local as IDirectoryInfo; localFolder.MoveTo(Path.Combine(localFolder.Parent.FullName, remote.Name)); } else { throw new ArgumentException("Solved move conflict => invoke crawl sync to detect other changes"); } mappedObject.Name = remote.Name; } else if (mappedObject.Name != local.Name && mappedObject.Name != remote.Name) { // both are renamed => rename to the latest change DateTime localModification = local.LastWriteTimeUtc; DateTime remoteModification = (DateTime)remote.LastModificationDate; if (localModification > remoteModification) { // local modification is newer remote.Rename(local.Name, true); mappedObject.Name = local.Name; } else { // remote modification is newer if (local is IFileInfo) { IFileInfo localFile = local as IFileInfo; localFile.MoveTo(Path.Combine(localFile.Directory.FullName, remote.Name)); } else if (local is IDirectoryInfo) { IDirectoryInfo localFolder = local as IDirectoryInfo; localFolder.MoveTo(Path.Combine(localFolder.Parent.FullName, remote.Name)); } else { throw new ArgumentException("Solved move conflict => invoke crawl sync to detect other changes"); } local.LastWriteTimeUtc = (DateTime)remote.LastModificationDate; mappedObject.Name = remote.Name; } } // Sync modification dates if (oldRemoteModificationDate != null) { if (oldLocalModificationDate > oldRemoteModificationDate && this.ServerCanModifyDateTimes) { remote.UpdateLastWriteTimeUtc(oldLocalModificationDate); local.LastWriteTimeUtc = oldLocalModificationDate; } else if (oldLocalModificationDate < (DateTime)oldRemoteModificationDate) { local.LastWriteTimeUtc = (DateTime)oldRemoteModificationDate; } } mappedObject.LastLocalWriteTimeUtc = local.LastWriteTimeUtc; mappedObject.LastRemoteWriteTimeUtc = (DateTime)remote.LastModificationDate; mappedObject.ParentId = remote.Parents[0].Id; mappedObject.LastChangeToken = remote.ChangeToken; mappedObject.Ignored = remote.AreAllChildrenIgnored(); this.Storage.SaveMappedObject(mappedObject); }
/// <summary> /// Renames the specified localFile to the name of the given remoteId object by using the storage, localFile and remoteId. /// </summary> /// <param name="localFile">Local file or folder. It is the source file/folder reference, which should be renamed.</param> /// <param name="remoteId">Remote identifier. Should be an instance of IFolder or IDocument.</param> /// <param name="localContent">Hint if the local content has been changed.</param> /// <param name="remoteContent">Information if the remote content has been changed.</param> public override void Solve( IFileSystemInfo localFile, IObjectId remoteId, ContentChangeType localContent = ContentChangeType.NONE, ContentChangeType remoteContent = ContentChangeType.NONE) { IMappedObject obj = this.Storage.GetObjectByRemoteId(remoteId.Id); if (remoteId is IFolder) { // Rename local folder IFolder remoteFolder = remoteId as IFolder; IDirectoryInfo dirInfo = localFile as IDirectoryInfo; string oldPath = dirInfo.FullName; try { dirInfo.MoveTo(Path.Combine(dirInfo.Parent.FullName, remoteFolder.Name)); obj.Name = remoteFolder.Name; } catch (IOException) { if (dirInfo.Name.Equals(remoteFolder.Name, StringComparison.OrdinalIgnoreCase)) { obj.Name = dirInfo.Name; } else { throw; } } if (remoteFolder.LastModificationDate != null) { dirInfo.LastWriteTimeUtc = (DateTime)remoteFolder.LastModificationDate; } obj.LastChangeToken = remoteFolder.ChangeToken; obj.LastRemoteWriteTimeUtc = remoteFolder.LastModificationDate; obj.LastLocalWriteTimeUtc = dirInfo.LastWriteTimeUtc; this.Storage.SaveMappedObject(obj); OperationsLogger.Info(string.Format("Renamed local folder {0} to {1}", oldPath, remoteFolder.Name)); } else if (remoteId is IDocument) { // Rename local file IDocument remoteDocument = remoteId as IDocument; IFileInfo fileInfo = localFile as IFileInfo; string oldPath = fileInfo.FullName; fileInfo.MoveTo(Path.Combine(fileInfo.Directory.FullName, remoteDocument.Name)); if (remoteDocument.LastModificationDate != null) { fileInfo.LastWriteTimeUtc = (DateTime)remoteDocument.LastModificationDate; } obj.Name = remoteDocument.Name; obj.LastChangeToken = remoteContent == ContentChangeType.NONE ? remoteDocument.ChangeToken : obj.LastChangeToken; obj.LastRemoteWriteTimeUtc = remoteContent == ContentChangeType.NONE ? remoteDocument.LastModificationDate : obj.LastRemoteWriteTimeUtc; obj.LastLocalWriteTimeUtc = fileInfo.LastWriteTimeUtc; this.Storage.SaveMappedObject(obj); OperationsLogger.Info(string.Format("Renamed local file {0} to {1}", oldPath, remoteDocument.Name)); if (remoteContent != ContentChangeType.NONE) { throw new ArgumentException("Remote documents content is also changed => force crawl sync."); } } else { throw new ArgumentException("Given remote Id is not an IFolder nor an IDocument instance"); } }