From ead5f37921efe4722b71264424c00fc25994ca06 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sun, 22 Jan 2012 20:59:23 -0800 Subject: [PATCH] Directory write time is now calculated based on the most recent file write to any file inside of that directory. --- NzbDrone.Common.Test/DiskProviderFixture.cs | 24 ++++++++++++++ NzbDrone.Common/DiskProvider.cs | 10 +++++- .../ProcessDownloadProviderFixture.cs | 31 ++++++++++++++++--- .../Providers/PostDownloadProvider.cs | 2 +- 4 files changed, 60 insertions(+), 7 deletions(-) diff --git a/NzbDrone.Common.Test/DiskProviderFixture.cs b/NzbDrone.Common.Test/DiskProviderFixture.cs index e604f20cb..b66c01753 100644 --- a/NzbDrone.Common.Test/DiskProviderFixture.cs +++ b/NzbDrone.Common.Test/DiskProviderFixture.cs @@ -1,5 +1,6 @@ // ReSharper disable InconsistentNaming +using System; using System.IO; using System.Linq; using FluentAssertions; @@ -151,6 +152,29 @@ namespace NzbDrone.Common.Test DiskProvider.PathEquals(first, second).Should().BeFalse(); } + [Test] + public void empty_folder_should_return_folder_modified_date() + { + var tempfolder = new DirectoryInfo(TempFolder); + Mocker.Resolve().GetLastDirectoryWrite(TempFolder).Should().Be(tempfolder.LastWriteTimeUtc); + } + + [Test] + public void folder_should_return_correct_value_for_last_write() + { + var appPath = new EnviromentProvider().ApplicationPath; + Mocker.Resolve().GetLastDirectoryWrite(appPath).Should().BeOnOrAfter(DateTime.UtcNow.AddMinutes(-10)); + Mocker.Resolve().GetLastDirectoryWrite(appPath).Should().BeBefore(DateTime.UtcNow); + } + + [Test] + [Explicit] + public void check_last_write() + { + Console.WriteLine(Mocker.Resolve().GetLastDirectoryWrite(@"C:\DRIVERS")); + Console.WriteLine(new DirectoryInfo(@"C:\DRIVERS").LastWriteTimeUtc); + } + private void VerifyCopy() { BinFolder.Refresh(); diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index 7c3fac8b6..545d2ecea 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -31,7 +31,15 @@ namespace NzbDrone.Common throw new DirectoryNotFoundException("Directory doesn't exist. " + path); } - GetFiles(path, SearchOption.AllDirectories); + var dirFiles = GetFiles(path, SearchOption.AllDirectories).ToList(); + + if (!dirFiles.Any()) + { + return new DirectoryInfo(path).LastWriteTimeUtc; + } + + return dirFiles.Select(f => new FileInfo(f)) + .Max(c => c.LastWriteTimeUtc); } public virtual bool FolderExists(string path) diff --git a/NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/ProcessDownloadProviderFixture.cs b/NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/ProcessDownloadProviderFixture.cs index e7fbd45e7..9c2b465dc 100644 --- a/NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/ProcessDownloadProviderFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/PostDownloadProviderTests/ProcessDownloadProviderFixture.cs @@ -20,10 +20,26 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests [TestFixture] public class ProcessDownloadProviderFixture : CoreTest { + + private void WithOldWrite() + { + Mocker.GetMock() + .Setup(c => c.GetLastDirectoryWrite(It.IsAny())) + .Returns(DateTime.Now.AddDays(-5)); + } + + private void WithRecentWrite() + { + Mocker.GetMock() + .Setup(c => c.GetLastDirectoryWrite(It.IsAny())) + .Returns(DateTime.UtcNow); + } + [Test] public void should_skip_if_folder_is_tagged_and_too_fresh() { WithStrictMocker(); + WithRecentWrite(); var droppedFolder = new DirectoryInfo(TempFolder + "\\_test\\"); droppedFolder.Create(); @@ -34,11 +50,11 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests [Test] public void should_continue_processing_if_folder_is_tagged_and_not_fresh() { + WithOldWrite(); + var droppedFolder = new DirectoryInfo(TempFolder + "\\_test\\"); droppedFolder.Create(); - droppedFolder.LastWriteTime = DateTime.Now.AddMinutes(-2); - //Act Mocker.GetMock().Setup(s => s.FindSeries(It.IsAny())).Returns(null).Verifiable(); Mocker.Resolve().ProcessDownload(droppedFolder); @@ -52,6 +68,8 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests [Test] public void should_search_for_series_using_title_without_status() { + WithOldWrite(); + var droppedFolder = new DirectoryInfo(@"C:\Test\Unsorted TV\_unpack_The Office - S01E01 - Episode Title"); Mocker.GetMock().Setup(s => s.FindSeries("office")).Returns(null).Verifiable(); @@ -70,6 +88,7 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests { //Setup WithStrictMocker(); + WithOldWrite(); var droppedFolder = new DirectoryInfo(@"C:\Test\Unsorted TV\The Office - S01E01 - Episode Title"); var taggedFolder = @"C:\Test\Unsorted TV\_UnknownSeries_The Office - S01E01 - Episode Title"; @@ -91,6 +110,7 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests { //Setup WithStrictMocker(); + WithOldWrite(); var droppedFolder = new DirectoryInfo(@"C:\Test\Unsorted TV\The Office - S01E01 - Episode Title"); var taggedFolder = @"C:\Test\Unsorted TV\_ParseError_The Office - S01E01 - Episode Title"; @@ -120,6 +140,7 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests { //Setup WithStrictMocker(); + WithOldWrite(); var droppedFolder = new DirectoryInfo(@"C:\Test\Unsorted TV\The Office - Season 01"); var taggedFolder = PostDownloadProvider.GetTaggedFolderName(droppedFolder, PostDownloadStatusType.Unknown); @@ -154,10 +175,10 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests public void folder_shouldnt_be_tagged_with_same_tag_again(string path) { //Setup - + var droppedFolder = new DirectoryInfo(TempFolder + path); droppedFolder.Create(); - droppedFolder.LastWriteTime = DateTime.Now.AddHours(-1); + WithOldWrite(); //Act Mocker.GetMock().Setup(s => s.FindSeries(It.IsAny())).Returns(null); @@ -172,7 +193,7 @@ namespace NzbDrone.Core.Test.ProviderTests.PostDownloadProviderTests public void folder_should_not_be_tagged_if_existing_tag_is_diffrent() { //Setup - + WithOldWrite(); var droppedFolder = new DirectoryInfo(TempFolder + @"\_UnknownEpisode_The Office - S01E01 - Episode Title"); droppedFolder.Create(); droppedFolder.LastWriteTime = DateTime.Now.AddHours(-1); diff --git a/NzbDrone.Core/Providers/PostDownloadProvider.cs b/NzbDrone.Core/Providers/PostDownloadProvider.cs index 7ee62f6a4..68b46bee9 100644 --- a/NzbDrone.Core/Providers/PostDownloadProvider.cs +++ b/NzbDrone.Core/Providers/PostDownloadProvider.cs @@ -50,7 +50,7 @@ namespace NzbDrone.Core.Providers public virtual void ProcessDownload(DirectoryInfo subfolderInfo) { - if (subfolderInfo.Name.StartsWith("_") && subfolderInfo.LastWriteTimeUtc.AddMinutes(1) > DateTime.UtcNow) + if (subfolderInfo.Name.StartsWith("_") && _diskProvider.GetLastDirectoryWrite(subfolderInfo.FullName).AddMinutes(2) > DateTime.UtcNow) { Logger.Trace("[{0}] is too fresh. skipping", subfolderInfo.Name); return;