From 950b2f0965d9d4127c2ffb634f77fa7ad918f740 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 17 Nov 2011 23:16:05 -0800 Subject: [PATCH 1/2] DiskProvider.MoveFile now overwrites existing file. --- NzbDrone.Common.Test/DiskProviderTests.cs | 15 +++++++++++++++ NzbDrone.Common/DiskProvider.cs | 17 +++++++++-------- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/NzbDrone.Common.Test/DiskProviderTests.cs b/NzbDrone.Common.Test/DiskProviderTests.cs index c842fbf08..4e841281d 100644 --- a/NzbDrone.Common.Test/DiskProviderTests.cs +++ b/NzbDrone.Common.Test/DiskProviderTests.cs @@ -34,6 +34,20 @@ namespace NzbDrone.Common.Test } } + [Test] + public void moveFile_should_overwrite_existing_file() + { + var diskProvider = new DiskProvider(); + diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName); + + var targetPath = Path.Combine(BinFolderCopy.FullName, "file.move"); + + diskProvider.MoveFile(BinFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName, targetPath); + diskProvider.MoveFile(BinFolderCopy.GetFiles("*.dll", SearchOption.AllDirectories).First().FullName, targetPath); + + File.Exists(targetPath).Should().BeTrue(); + } + [Test] public void CopyFolder_should_copy_folder() { @@ -45,6 +59,7 @@ namespace NzbDrone.Common.Test VerifyCopy(); } + [Test] public void CopyFolder_should_overright_existing_folder() { diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index 2099bb7f6..4db625e94 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -98,24 +98,20 @@ namespace NzbDrone.Common TransferDirectory(subDir.FullName, Path.Combine(target, subDir.Name), transferAction); } - foreach (var file in sourceFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly)) + foreach (var sourceFile in sourceFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { - var destFile = Path.Combine(target, file.Name); + var destFile = Path.Combine(target, sourceFile.Name); switch (transferAction) { case TransferAction.Copy: { - file.CopyTo(destFile, true); + sourceFile.CopyTo(destFile, true); break; } case TransferAction.Move: { - if (FileExists(destFile)) - { - File.Delete(destFile); - } - file.MoveTo(destFile); + MoveFile(sourceFile.FullName, destFile); break; } } @@ -130,6 +126,11 @@ namespace NzbDrone.Common public virtual void MoveFile(string sourcePath, string destinationPath) { + if (FileExists(destinationPath)) + { + DeleteFile(destinationPath); + } + File.Move(sourcePath, destinationPath); } From 373b3ab0750766ade879ea8e84e2aa828b448513 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 17 Nov 2011 23:31:40 -0800 Subject: [PATCH 2/2] Update now moves the update package to target folder, should make updates faster. --- NzbDrone.Update.Test/UpdateProviderStartFixture.cs | 8 ++++---- NzbDrone.Update/Providers/UpdateProvider.cs | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/NzbDrone.Update.Test/UpdateProviderStartFixture.cs b/NzbDrone.Update.Test/UpdateProviderStartFixture.cs index 02853de30..7cfd0d73b 100644 --- a/NzbDrone.Update.Test/UpdateProviderStartFixture.cs +++ b/NzbDrone.Update.Test/UpdateProviderStartFixture.cs @@ -130,7 +130,7 @@ namespace NzbDrone.Update.Test public void should_copy_update_package_to_target() { Mocker.GetMock() - .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)); + .Setup(c => c.MoveDirectory(UPDATE_FOLDER, TARGET_FOLDER)); Mocker.Resolve().Start(TARGET_FOLDER); } @@ -139,7 +139,7 @@ namespace NzbDrone.Update.Test public void should_restore_if_update_fails() { Mocker.GetMock() - .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)) + .Setup(c => c.MoveDirectory(UPDATE_FOLDER, TARGET_FOLDER)) .Throws(new IOException()); //Act @@ -184,7 +184,7 @@ namespace NzbDrone.Update.Test WithServiceRunning(true); Mocker.GetMock() - .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)) + .Setup(c => c.MoveDirectory(UPDATE_FOLDER, TARGET_FOLDER)) .Throws(new IOException()); //Act @@ -202,7 +202,7 @@ namespace NzbDrone.Update.Test WithServiceRunning(false); Mocker.GetMock() - .Setup(c => c.CopyDirectory(UPDATE_FOLDER, TARGET_FOLDER)) + .Setup(c => c.MoveDirectory(UPDATE_FOLDER, TARGET_FOLDER)) .Throws(new IOException()); //Act diff --git a/NzbDrone.Update/Providers/UpdateProvider.cs b/NzbDrone.Update/Providers/UpdateProvider.cs index 05bcc76d1..969879d77 100644 --- a/NzbDrone.Update/Providers/UpdateProvider.cs +++ b/NzbDrone.Update/Providers/UpdateProvider.cs @@ -68,11 +68,11 @@ namespace NzbDrone.Update.Providers _diskProvider.CopyDirectory(targetFolder, _enviromentProvider.GetUpdateBackUpFolder()); - logger.Info("Copying update package to target"); + logger.Info("Moving update package to target"); try { - _diskProvider.CopyDirectory(_enviromentProvider.GetUpdatePackageFolder(), targetFolder); + _diskProvider.MoveDirectory(_enviromentProvider.GetUpdatePackageFolder(), targetFolder); } catch (Exception e) {