From 601cd31a1f75afd7996c293bb458c715ae1cf655 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Wed, 11 Sep 2013 23:52:37 -0700 Subject: [PATCH] Diskprovider cleanup. --- NzbDrone.Common/DiskProvider.cs | 27 ++++++++++--------- .../DownloadedEpisodesImportServiceFixture.cs | 2 +- .../NotInUseSpecificationFixture.cs | 6 ++--- .../DownloadedEpisodesImportService.cs | 2 +- .../Specifications/NotInUseSpecification.cs | 2 +- 5 files changed, 21 insertions(+), 18 deletions(-) diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index db49896fe..48a9e0602 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -25,8 +25,8 @@ namespace NzbDrone.Common string[] GetFiles(string path, SearchOption searchOption); long GetFolderSize(string path); long GetFileSize(string path); - String CreateFolder(string path); - void CopyFolder(string source, string target); + void CreateFolder(string path); + void CopyFolder(string source, string destination); void MoveFolder(string source, string destination); void DeleteFile(string path); void MoveFile(string source, string destination); @@ -37,7 +37,7 @@ namespace NzbDrone.Common void WriteAllText(string filename, string contents); void FileSetLastWriteTimeUtc(string path, DateTime dateTime); void FolderSetLastWriteTimeUtc(string path, DateTime dateTime); - bool IsFileLocked(FileInfo file); + bool IsFileLocked(string path); string GetPathRoot(string path); void SetPermissions(string filename, WellKnownSidType accountSid, FileSystemRights rights, AccessControlType controlType); bool IsParent(string parentPath, string childPath); @@ -60,7 +60,7 @@ namespace NzbDrone.Common out ulong lpTotalNumberOfBytes, out ulong lpTotalNumberOfFreeBytes); - private static readonly Logger Logger = NzbDroneLogger.GetLogger(); + private static readonly Logger Logger = NzbDroneLogger.GetLogger(); public HashSet SpecialFolders { @@ -162,19 +162,18 @@ namespace NzbDrone.Common return fi.Length; } - public String CreateFolder(string path) + public void CreateFolder(string path) { Ensure.That(() => path).IsValidPath(); - - return Directory.CreateDirectory(path).FullName; + Directory.CreateDirectory(path); } - public void CopyFolder(string source, string target) + public void CopyFolder(string source, string destination) { Ensure.That(() => source).IsValidPath(); - Ensure.That(() => target).IsValidPath(); + Ensure.That(() => destination).IsValidPath(); - TransferFolder(source, target, TransferAction.Copy); + TransferFolder(source, destination, TransferAction.Copy); } public void MoveFolder(string source, string destination) @@ -367,13 +366,17 @@ namespace NzbDrone.Common Directory.SetLastWriteTimeUtc(path, dateTime); } - public bool IsFileLocked(FileInfo file) + public bool IsFileLocked(string file) { + + //TOOD: Needs test + //TODO: move to using instead of trycatch + //TODO: prob should use OpenWrite to check for lock. FileStream stream = null; try { - stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None); + stream = File.OpenRead(file); } catch (IOException) { diff --git a/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs b/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs index 030b5b21d..09e51ac3a 100644 --- a/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs +++ b/NzbDrone.Core.Test/MediaFiles/DownloadedEpisodesImportServiceFixture.cs @@ -75,7 +75,7 @@ namespace NzbDrone.Core.Test.MediaFiles [Test] public void should_skip_if_file_is_in_use_by_another_process() { - Mocker.GetMock().Setup(c => c.IsFileLocked(It.IsAny())) + Mocker.GetMock().Setup(c => c.IsFileLocked(It.IsAny())) .Returns(true); Subject.Execute(new DownloadedEpisodesScanCommand()); diff --git a/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/NotInUseSpecificationFixture.cs b/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/NotInUseSpecificationFixture.cs index df23ee693..7b25b6b6c 100644 --- a/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/NotInUseSpecificationFixture.cs +++ b/NzbDrone.Core.Test/MediaFiles/EpisodeImport/Specifications/NotInUseSpecificationFixture.cs @@ -58,7 +58,7 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications Subject.IsSatisfiedBy(_localEpisode); Mocker.GetMock() - .Verify(v => v.IsFileLocked(It.IsAny()), Times.Never()); + .Verify(v => v.IsFileLocked(It.IsAny()), Times.Never()); } [Test] @@ -67,7 +67,7 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications GivenNewFile(); Mocker.GetMock() - .Setup(s => s.IsFileLocked(It.IsAny())) + .Setup(s => s.IsFileLocked(It.IsAny())) .Returns(true); Subject.IsSatisfiedBy(_localEpisode).Should().BeFalse(); @@ -79,7 +79,7 @@ namespace NzbDrone.Core.Test.MediaFiles.EpisodeImport.Specifications GivenNewFile(); Mocker.GetMock() - .Setup(s => s.IsFileLocked(It.IsAny())) + .Setup(s => s.IsFileLocked(It.IsAny())) .Returns(false); Subject.IsSatisfiedBy(_localEpisode).Should().BeTrue(); diff --git a/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs b/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs index 119379bba..7efc5dc34 100644 --- a/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs +++ b/NzbDrone.Core/MediaFiles/DownloadedEpisodesImportService.cs @@ -125,7 +125,7 @@ namespace NzbDrone.Core.MediaFiles return; } - if (_diskProvider.IsFileLocked(new FileInfo(videoFile))) + if (_diskProvider.IsFileLocked(videoFile)) { _logger.Debug("[{0}] is currently locked by another process, skipping", videoFile); return; diff --git a/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotInUseSpecification.cs b/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotInUseSpecification.cs index 5fd2f453b..48949760a 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotInUseSpecification.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/NotInUseSpecification.cs @@ -26,7 +26,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications return true; } - if (_diskProvider.IsFileLocked(new FileInfo(localEpisode.Path))) + if (_diskProvider.IsFileLocked(localEpisode.Path)) { _logger.Trace("{0} is in use"); return false;