From 19c087e50a18d5728370a0952797679ef4d20235 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Wed, 14 Dec 2011 00:06:54 -0800 Subject: [PATCH] Moving file from same source/destination no longer just deletes the file. --- NzbDrone.Common.Test/DiskProviderTests.cs | 14 ++++++++++++++ NzbDrone.Common/DiskProvider.cs | 14 ++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/NzbDrone.Common.Test/DiskProviderTests.cs b/NzbDrone.Common.Test/DiskProviderTests.cs index 978479444..54ce8977f 100644 --- a/NzbDrone.Common.Test/DiskProviderTests.cs +++ b/NzbDrone.Common.Test/DiskProviderTests.cs @@ -48,6 +48,20 @@ namespace NzbDrone.Common.Test File.Exists(targetPath).Should().BeTrue(); } + [Test] + public void moveFile_should_not_move_overwrite_itself() + { + var diskProvider = new DiskProvider(); + diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName); + + var targetPath = BinFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName; + + diskProvider.MoveFile(targetPath, targetPath); + + File.Exists(targetPath).Should().BeTrue(); + ExceptionVerification.ExcpectedWarns(1); + } + [Test] public void CopyFolder_should_copy_folder() { diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index d79d1c2e2..6b25cd676 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -125,14 +125,20 @@ namespace NzbDrone.Common File.Delete(path); } - public virtual void MoveFile(string sourcePath, string destinationPath) + public virtual void MoveFile(string source, string destination) { - if (FileExists(destinationPath)) + if (PathEquals(source, destination)) { - DeleteFile(destinationPath); + Logger.Warn("Source and destination can't be the same {0}", source); + return; } - File.Move(sourcePath, destinationPath); + if (FileExists(destination)) + { + DeleteFile(destination); + } + + File.Move(source, destination); } public virtual void DeleteFolder(string path, bool recursive)