/// <summary> /// Rename the file that is in the way of the node with the name conflict. /// </summary> /// <param name="newName">The new name of the file.</param> public void RenameConflictingFile(string newName) { if (!SyncFile.IsNameValid(newName)) { throw new MalformedException(newName); } // Now get the conflicting node. Node cfn = collection.GetNodeByID(node.Properties.GetSingleProperty(ConflictLinkProperty).Value.ToString()); FileNode fn = cfn as FileNode; if (fn != null) { // Now rename the file and the node. string parentPath = Path.GetDirectoryName(fn.GetFullPath(collection)); string tmpName = Path.Combine(parentPath, TempFilePrefix + fn.ID); string newFName = Path.Combine(parentPath, newName); File.Move(fn.GetFullPath(collection), tmpName); File.Move(tmpName, newFName); string relativePath = fn.GetRelativePath(); relativePath = relativePath.Remove(relativePath.Length - fn.Name.Length, fn.Name.Length) + newName; fn.Properties.ModifyNodeProperty(new Property(PropertyTags.FileSystemPath, Syntax.String, relativePath)); fn.Name = newName; fn = RemoveNameConflict(collection, fn) as FileNode; collection.Commit(fn); } else { DirNode dn = cfn as DirNode; if (dn != null) { new Conflict(collection, dn).Resolve(newName); } } }
//--------------------------------------------------------------------------- /// <summary> /// resolve file name conflict and commit /// </summary> public void Resolve(string newNodeName) { if (!SyncFile.IsNameValid(newNodeName)) { throw new MalformedException(newNodeName); } FileNode fn = node as FileNode; if (fn != null) { DirNode parent = fn.GetParent(collection); if (newNodeName == node.Name) { Log.log.Debug("Resolving the name conflict >>>>> name: {0} newName: {1}", node.Name, newNodeName); // We are resolving to the same name. if (Path.GetDirectoryName(FileNameConflictPath) != Path.GetDirectoryName(NonconflictedPath)) { // This file is in the conflict bin and has been sync-ed from the server. We do not need // To push it back up. Set internal. node.Properties.State = PropertyList.PropertyListState.Internal; try { File.Move(FileNameConflictPath, NonconflictedPath); } catch (IOException) { // The file exists it must have been modified locally create a node conflict. File.Move(FileNameConflictPath, GetUpdateConflictPath(collection, fn)); fn = (FileNode)RemoveNameConflict(collection, fn); collection.Commit(fn); Node serverNode = collection.GetNodeByID(fn.ID); // Now commit the node to update the file (size and dates); fn.UpdateFileInfo(collection); collection.Commit(fn); // Create the node conflict. fn = (FileNode)collection.CreateCollision(serverNode, false); collection.Commit(fn); return; } } } else { Log.log.Debug("Resolving the name conflict >>>>> name: {0} newName: {1}", node.Name, newNodeName); if (SyncFile.DoesNodeExist(collection, parent, newNodeName)) { throw new ExistsException(newNodeName); } //TODO: what if move succeeds but node rename or commit fails? File.Move(FileNameConflictPath, Path.Combine(Path.GetDirectoryName(NonconflictedPath), newNodeName)); string relativePath = fn.GetRelativePath(); relativePath = relativePath.Remove(relativePath.Length - node.Name.Length, node.Name.Length) + newNodeName; node.Properties.ModifyNodeProperty(new Property(PropertyTags.FileSystemPath, Syntax.String, relativePath)); node.Name = newNodeName; } node = RemoveNameConflict(collection, node); collection.Commit(node); } else { DirNode dn = node as DirNode; if (dn != null) { DirNode parent = dn.GetParent(collection); string oldname, newname; oldname = FileNameConflictPath; newname = Path.Combine(Path.GetDirectoryName(dn.GetFullPath(collection)), newNodeName); if (newNodeName != node.Name) { if (SyncFile.DoesNodeExist(collection, parent, newNodeName)) { throw new ExistsException(newNodeName); } if (Directory.Exists(oldname)) { Directory.Move(oldname, newname); } else { Directory.CreateDirectory(newname); } string oldRelativePath = dn.GetRelativePath(); string relativePath = oldRelativePath.Remove(oldRelativePath.Length - node.Name.Length, node.Name.Length) + newNodeName; node.Properties.ModifyNodeProperty(new Property(PropertyTags.FileSystemPath, Syntax.String, relativePath)); node.Name = newNodeName; node = RemoveNameConflict(collection, node); collection.Commit(node); FileWatcher.RenameDirsChildren(collection, dn, oldRelativePath); } else { // The name did not change. if (Directory.Exists(oldname)) { Directory.Move(oldname, newname); } else { Directory.CreateDirectory(newname); } node = RemoveNameConflict(collection, node); collection.Commit(node); } } } }