//--------------------------------------------------------------------------- /// <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); } } } }