From 11662db1b968207130832556e1504c271d8b3b2a Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Fri, 13 Jun 2014 19:03:45 +0200 Subject: [PATCH] fix #418: File rename can break project file. The SVN AddIn should check if the rename operation is legal before attempting to rename a file. --- .../Src/Commands/AutostartCommands.cs | 4 ++ .../Project/ICSharpCode.SharpDevelop.csproj | 1 + .../Project/Workbench/File/FileHelpers.cs | 49 +++++++++++++++++++ .../SharpDevelop/Workbench/FileService.cs | 36 ++++---------- 4 files changed, 64 insertions(+), 26 deletions(-) create mode 100644 src/Main/Base/Project/Workbench/File/FileHelpers.cs diff --git a/src/AddIns/VersionControl/SubversionAddIn/Src/Commands/AutostartCommands.cs b/src/AddIns/VersionControl/SubversionAddIn/Src/Commands/AutostartCommands.cs index 639719169e..20e32b2a72 100644 --- a/src/AddIns/VersionControl/SubversionAddIn/Src/Commands/AutostartCommands.cs +++ b/src/AddIns/VersionControl/SubversionAddIn/Src/Commands/AutostartCommands.cs @@ -389,6 +389,10 @@ namespace ICSharpCode.Svn.Commands if (!AddInOptions.AutomaticallyRenameFiles) return; string fullSource = Path.GetFullPath(e.SourceFile); if (!CanBeVersionControlledFile(fullSource)) return; + if (!FileHelpers.CheckRenameOrReplacePossible(e)) { + e.Cancel = true; + return; + } try { using (SvnClientWrapper client = new SvnClientWrapper()) { SvnMessageView.HandleNotifications(client); diff --git a/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj b/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj index b5d00fc3bb..07b470e716 100644 --- a/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj +++ b/src/Main/Base/Project/ICSharpCode.SharpDevelop.csproj @@ -366,6 +366,7 @@ + diff --git a/src/Main/Base/Project/Workbench/File/FileHelpers.cs b/src/Main/Base/Project/Workbench/File/FileHelpers.cs new file mode 100644 index 0000000000..d2f27371d5 --- /dev/null +++ b/src/Main/Base/Project/Workbench/File/FileHelpers.cs @@ -0,0 +1,49 @@ +// Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team +// +// Permission is hereby granted, free of charge, to any person obtaining a copy of this +// software and associated documentation files (the "Software"), to deal in the Software +// without restriction, including without limitation the rights to use, copy, modify, merge, +// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons +// to whom the Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in all copies or +// substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, +// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR +// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE +// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +using System; +using System.IO; +using ICSharpCode.Core; + +namespace ICSharpCode.SharpDevelop.Workbench +{ + /// + /// Utility/helper methods for IFileService to avoid changing the IFileService interface. + /// + public static class FileHelpers + { + /// + /// Checks that the rename/overwrite operation is possible. + /// + public static bool CheckRenameOrReplacePossible(FileRenameEventArgs e, bool replaceAllowed = false) + { + if (e.IsDirectory && Directory.Exists(e.SourceFile)) { + if (!replaceAllowed && Directory.Exists(e.TargetFile)) { + SD.MessageService.ShowMessage(StringParser.Parse("${res:Gui.ProjectBrowser.FileInUseError}")); + return false; + } + } else if (File.Exists(e.SourceFile)) { + if (!replaceAllowed && File.Exists(e.TargetFile)) { + SD.MessageService.ShowMessage(StringParser.Parse("${res:Gui.ProjectBrowser.FileInUseError}")); + return false; + } + } + return true; + } + } +} diff --git a/src/Main/SharpDevelop/Workbench/FileService.cs b/src/Main/SharpDevelop/Workbench/FileService.cs index 41a18fb0ff..36816e36a4 100644 --- a/src/Main/SharpDevelop/Workbench/FileService.cs +++ b/src/Main/SharpDevelop/Workbench/FileService.cs @@ -580,20 +580,12 @@ namespace ICSharpCode.SharpDevelop.Workbench return false; if (!eargs.OperationAlreadyDone) { try { - if (isDirectory && Directory.Exists(oldName)) { - - if (Directory.Exists(newName)) { - MessageService.ShowMessage(StringParser.Parse("${res:Gui.ProjectBrowser.FileInUseError}")); - return false; + if (FileHelpers.CheckRenameOrReplacePossible(eargs)) { + if (isDirectory) { + Directory.Move(oldName, newName); + } else { + File.Move(oldName, newName); } - Directory.Move(oldName, newName); - - } else if (File.Exists(oldName)) { - if (File.Exists(newName)) { - MessageService.ShowMessage(StringParser.Parse("${res:Gui.ProjectBrowser.FileInUseError}")); - return false; - } - File.Move(oldName, newName); } } catch (Exception e) { if (isDirectory) { @@ -624,20 +616,12 @@ namespace ICSharpCode.SharpDevelop.Workbench return false; if (!eargs.OperationAlreadyDone) { try { - if (isDirectory && Directory.Exists(oldName)) { - - if (!overwrite && Directory.Exists(newName)) { - MessageService.ShowMessage(StringParser.Parse("${res:Gui.ProjectBrowser.FileInUseError}")); - return false; - } - FileUtility.DeepCopy(oldName, newName, overwrite); - - } else if (File.Exists(oldName)) { - if (!overwrite && File.Exists(newName)) { - MessageService.ShowMessage(StringParser.Parse("${res:Gui.ProjectBrowser.FileInUseError}")); - return false; + if (FileHelpers.CheckRenameOrReplacePossible(eargs, overwrite)) { + if (isDirectory) { + FileUtility.DeepCopy(oldName, newName, overwrite); + } else { + File.Copy(oldName, newName, overwrite); } - File.Copy(oldName, newName, overwrite); } } catch (Exception e) { if (isDirectory) {