From d97a1d068c773acb1ff746ff1ebed364f3db1eb4 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Thu, 17 Nov 2011 22:52:50 -0800 Subject: [PATCH] DiskProvider.Move now overwrites existing folder, Update some Diskprovider to use .NET 4 calls. --- NzbDrone.Common.Test/DiskProviderTests.cs | 37 ++++++++++ NzbDrone.Common/DiskProvider.cs | 74 +++++++++++++------ .../Controllers/DirectoryController.cs | 5 +- 3 files changed, 92 insertions(+), 24 deletions(-) diff --git a/NzbDrone.Common.Test/DiskProviderTests.cs b/NzbDrone.Common.Test/DiskProviderTests.cs index d2c2a950a..c842fbf08 100644 --- a/NzbDrone.Common.Test/DiskProviderTests.cs +++ b/NzbDrone.Common.Test/DiskProviderTests.cs @@ -13,6 +13,7 @@ public class DiskProviderTests : TestBase { DirectoryInfo BinFolder; DirectoryInfo BinFolderCopy; + DirectoryInfo BinFolderMove; [SetUp] public void Setup() @@ -20,11 +21,17 @@ public void Setup() var binRoot = new DirectoryInfo(Directory.GetCurrentDirectory()).Parent.Parent; BinFolder = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin")); BinFolderCopy = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin_copy")); + BinFolderMove = new DirectoryInfo(Path.Combine(binRoot.FullName, "bin_move")); if (BinFolderCopy.Exists) { BinFolderCopy.Delete(true); } + + if (BinFolderMove.Exists) + { + BinFolderMove.Delete(true); + } } [Test] @@ -56,6 +63,22 @@ public void CopyFolder_should_overright_existing_folder() VerifyCopy(); } + [Test] + public void MoveFolder_should_overright_existing_folder() + { + var diskProvider = new DiskProvider(); + + diskProvider.CopyDirectory(BinFolder.FullName, BinFolderCopy.FullName); + diskProvider.CopyDirectory(BinFolder.FullName, BinFolderMove.FullName); + VerifyCopy(); + + //Act + diskProvider.MoveDirectory(BinFolderCopy.FullName, BinFolderMove.FullName); + + //Assert + VerifyMove(); + } + private void VerifyCopy() { BinFolder.Refresh(); @@ -66,5 +89,19 @@ private void VerifyCopy() BinFolderCopy.GetDirectories().Should().HaveSameCount(BinFolder.GetDirectories()); } + + private void VerifyMove() + { + BinFolder.Refresh(); + BinFolderCopy.Refresh(); + BinFolderMove.Refresh(); + + BinFolderCopy.Exists.Should().BeFalse(); + + BinFolderMove.GetFiles("*.*", SearchOption.AllDirectories) + .Should().HaveSameCount(BinFolder.GetFiles("*.*", SearchOption.AllDirectories)); + + BinFolderMove.GetDirectories().Should().HaveSameCount(BinFolder.GetDirectories()); + } } } diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index 80fe1e3ac..2099bb7f6 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -9,6 +9,12 @@ namespace NzbDrone.Common { public class DiskProvider { + enum TransferAction + { + Copy, + Move + } + [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetDiskFreeSpaceEx(string lpDirectoryName, @@ -28,14 +34,14 @@ public virtual bool FileExists(string path) return File.Exists(path); } - public virtual string[] GetDirectories(string path) + public virtual IEnumerable GetDirectories(string path) { - return Directory.GetDirectories(path); + return Directory.EnumerateDirectories(path); } - public virtual string[] GetFiles(string path, SearchOption searchOption) + public virtual IEnumerable GetFiles(string path, SearchOption searchOption) { - return Directory.GetFiles(path, "*.*", searchOption); + return Directory.EnumerateFiles(path, "*.*", searchOption); } public virtual long GetDirectorySize(string path) @@ -57,7 +63,27 @@ public virtual String CreateDirectory(string path) public virtual void CopyDirectory(string source, string target) { - Logger.Trace("Copying {0} -> {1}", source, target); + TransferDirectory(source, target, TransferAction.Copy); + } + + public virtual void MoveDirectory(string source, string destination) + { + try + { + TransferDirectory(source, destination, TransferAction.Move); + Directory.Delete(source, true); + } + catch (Exception e) + { + e.Data.Add("Source", source); + e.Data.Add("Destination", destination); + throw; + } + } + + private void TransferDirectory(string source, string target, TransferAction transferAction) + { + Logger.Trace("{0} {1} -> {2}", transferAction, source, target); var sourceFolder = new DirectoryInfo(source); var targetFolder = new DirectoryInfo(target); @@ -69,13 +95,31 @@ public virtual void CopyDirectory(string source, string target) foreach (var subDir in sourceFolder.GetDirectories()) { - CopyDirectory(subDir.FullName, Path.Combine(target, subDir.Name)); + TransferDirectory(subDir.FullName, Path.Combine(target, subDir.Name), transferAction); } foreach (var file in sourceFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly)) { var destFile = Path.Combine(target, file.Name); - file.CopyTo(destFile, true); + + switch (transferAction) + { + case TransferAction.Copy: + { + file.CopyTo(destFile, true); + break; + } + case TransferAction.Move: + { + if (FileExists(destFile)) + { + File.Delete(destFile); + } + file.MoveTo(destFile); + break; + } + } + } } @@ -101,21 +145,7 @@ public virtual DateTime DirectoryDateCreated(string path) public virtual IEnumerable GetFileInfos(string path, string pattern, SearchOption searchOption) { - return new DirectoryInfo(path).GetFiles(pattern, searchOption); - } - - public virtual void MoveDirectory(string source, string destination) - { - try - { - Directory.Move(source, destination); - } - catch (Exception e) - { - e.Data.Add("Source", source); - e.Data.Add("Destination", destination); - throw; - } + return new DirectoryInfo(path).EnumerateFiles(pattern, searchOption); } public virtual void InheritFolderPermissions(string filename) diff --git a/NzbDrone.Web/Controllers/DirectoryController.cs b/NzbDrone.Web/Controllers/DirectoryController.cs index faae59850..c3dbc5140 100644 --- a/NzbDrone.Web/Controllers/DirectoryController.cs +++ b/NzbDrone.Web/Controllers/DirectoryController.cs @@ -1,4 +1,5 @@ -using System.Web.Mvc; +using System.Collections.Generic; +using System.Web.Mvc; using NzbDrone.Common; namespace NzbDrone.Web.Controllers @@ -27,7 +28,7 @@ public ActionResult _autoCompletePath(string text, int? filterMode) [HttpGet] public JsonResult GetDirectories(string term) { - string[] dirs = null; + IEnumerable dirs = null; try { //Windows (Including UNC)