From 3b240e0dd71c690c22b8483ea7c9328624d51b34 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 31 Aug 2013 21:38:06 -0700 Subject: [PATCH] getting freespace returns null instead of blowing up. --- .../RootFolders/RootFolderResource.cs | 2 +- .../DiskProviderTests/IsParentFixture.cs | 20 +++++--- NzbDrone.Common/DiskProvider.cs | 50 +++++++++---------- .../FreeSpaceSpecificationFixture.cs | 4 +- .../DiskProviderTests/FreeDiskSpaceFixture.cs | 8 +-- .../FreeSpaceOnDrivesFixture.cs | 8 +-- .../Specifications/FreeSpaceSpecification.cs | 2 +- NzbDrone.Core/RootFolders/RootFolder.cs | 2 +- .../RootFolders/RootFolderService.cs | 14 +++--- 9 files changed, 57 insertions(+), 53 deletions(-) diff --git a/NzbDrone.Api/RootFolders/RootFolderResource.cs b/NzbDrone.Api/RootFolders/RootFolderResource.cs index 05af828c1..43fcbe667 100644 --- a/NzbDrone.Api/RootFolders/RootFolderResource.cs +++ b/NzbDrone.Api/RootFolders/RootFolderResource.cs @@ -8,7 +8,7 @@ namespace NzbDrone.Api.RootFolders public class RootFolderResource : RestResource { public String Path { get; set; } - public Int64 FreeSpace { get; set; } + public Int64? FreeSpace { get; set; } public List UnmappedFolders { get; set; } } diff --git a/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs b/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs index bc52f482b..e18720eb3 100644 --- a/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs +++ b/NzbDrone.Common.Test/DiskProviderTests/IsParentFixture.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; +using System.IO; using FluentAssertions; using NUnit.Framework; using NzbDrone.Test.Common; @@ -17,7 +13,7 @@ public void should_get_free_space_for_folder() { var path = @"C:\".AsOsAgnostic(); - Subject.GetAvilableSpace(path).Should().NotBe(0); + Subject.GetAvailableSpace(path).Should().NotBe(0); } [Test] @@ -25,7 +21,7 @@ public void should_get_free_space_for_folder_that_doesnt_exist() { var path = @"C:\".AsOsAgnostic(); - Subject.GetAvilableSpace(Path.Combine(path, "invalidFolder")).Should().NotBe(0); + Subject.GetAvailableSpace(Path.Combine(path, "invalidFolder")).Should().NotBe(0); } @@ -34,7 +30,15 @@ public void should_get_free_space_for_drive_that_doesnt_exist() { WindowsOnly(); - Assert.Throws(() => Subject.GetAvilableSpace("J:\\").Should().NotBe(0)); + Assert.Throws(() => Subject.GetAvailableSpace("J:\\").Should().NotBe(0)); + } + + [Test] + public void should_return_null_when_cant_get_free_space() + { + LinuxOnly(); + + Subject.GetAvailableSpace("/run/").Should().NotBe(null); } } } diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index 062cd79d7..851ba4550 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -17,7 +17,6 @@ public interface IDiskProvider DateTime GetLastFolderWrite(string path); DateTime GetLastFileWrite(string path); void EnsureFolder(string path); - bool FolderExists(string path, bool caseSensitive); bool FolderExists(string path); bool FileExists(string path); bool FileExists(string path, bool caseSensitive); @@ -32,7 +31,7 @@ public interface IDiskProvider void MoveFile(string source, string destination); void DeleteFolder(string path, bool recursive); void InheritFolderPermissions(string filename); - long GetAvilableSpace(string path); + long? GetAvailableSpace(string path); string ReadAllText(string filePath); void WriteAllText(string filename, string contents); void FileSetLastWriteTimeUtc(string path, DateTime dateTime); @@ -113,16 +112,6 @@ public bool FolderExists(string path) return Directory.Exists(path); } - public bool FolderExists(string path, bool caseSensitive) - { - if (caseSensitive) - { - return FolderExists(path) && path == path.GetActualCasing(); - } - - return FolderExists(path); - } - public bool FileExists(string path) { Ensure.That(() => path).IsValidPath(); @@ -289,27 +278,38 @@ public void InheritFolderPermissions(string filename) File.SetAccessControl(filename, fs); } - public long GetAvilableSpace(string path) + public long? GetAvailableSpace(string path) { Ensure.That(() => path).IsValidPath(); - if (OsInfo.IsLinux) - { - var driveInfo = DriveInfo.GetDrives().SingleOrDefault(c => c.IsReady && path.StartsWith(c.Name, StringComparison.CurrentCultureIgnoreCase)); - - if (driveInfo == null) - { - throw new DirectoryNotFoundException(path); - } - - return driveInfo.AvailableFreeSpace; - } - var root = GetPathRoot(path); if (!FolderExists(root)) throw new DirectoryNotFoundException(root); + if (OsInfo.IsLinux) + { + var drives = DriveInfo.GetDrives(); + + foreach (var drive in drives) + { + try + { + if (drive.IsReady && path.StartsWith(drive.Name, StringComparison.CurrentCultureIgnoreCase)) + { + return drive.AvailableFreeSpace; + } + } + catch (InvalidOperationException e) + { + Logger.ErrorException("Couldn't get free space for " + path, e); + } + } + + return null; + } + + return DriveFreeSpaceEx(root); } diff --git a/NzbDrone.Core.Test/MediaFileTests/EpisodeImportTests/FreeSpaceSpecificationFixture.cs b/NzbDrone.Core.Test/MediaFileTests/EpisodeImportTests/FreeSpaceSpecificationFixture.cs index 7d2948ff8..47804bde0 100644 --- a/NzbDrone.Core.Test/MediaFileTests/EpisodeImportTests/FreeSpaceSpecificationFixture.cs +++ b/NzbDrone.Core.Test/MediaFileTests/EpisodeImportTests/FreeSpaceSpecificationFixture.cs @@ -54,7 +54,7 @@ private void GivenFileSize(long size) private void GivenFreeSpace(long size) { Mocker.GetMock() - .Setup(s => s.GetAvilableSpace(It.IsAny())) + .Setup(s => s.GetAvailableSpace(It.IsAny())) .Returns(size); } @@ -96,7 +96,7 @@ public void should_use_series_paths_parent_for_free_space_check() Subject.IsSatisfiedBy(_localEpisode).Should().BeTrue(); Mocker.GetMock() - .Verify(v => v.GetAvilableSpace(_rootFolder), Times.Once()); + .Verify(v => v.GetAvailableSpace(_rootFolder), Times.Once()); } } } diff --git a/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceFixture.cs b/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceFixture.cs index 4584770ca..849cf820a 100644 --- a/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceFixture.cs +++ b/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceFixture.cs @@ -14,7 +14,7 @@ public class FreeDiskSpaceFixture : CoreTest [Test] public void should_return_free_disk_space() { - var result = Subject.GetAvilableSpace(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); + var result = Subject.GetAvailableSpace(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); result.Should().BeGreaterThan(0); } @@ -23,7 +23,7 @@ public void should_be_able_to_get_space_on_unc() { WindowsOnly(); - var result = Subject.GetAvilableSpace(@"\\localhost\c$\Windows"); + var result = Subject.GetAvailableSpace(@"\\localhost\c$\Windows"); result.Should().BeGreaterThan(0); } @@ -32,13 +32,13 @@ public void should_throw_if_drive_doesnt_exist() { WindowsOnly(); - Assert.Throws(() => Subject.GetAvilableSpace(@"Z:\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic())); + Assert.Throws(() => Subject.GetAvailableSpace(@"Z:\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic())); } [Test] public void should_be_able_to_get_space_on_folder_that_doesnt_exist() { - var result = Subject.GetAvilableSpace(@"C:\I_DO_NOT_EXIST".AsOsAgnostic()); + var result = Subject.GetAvailableSpace(@"C:\I_DO_NOT_EXIST".AsOsAgnostic()); result.Should().BeGreaterThan(0); } } diff --git a/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs b/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs index ff3458ab1..ec5ade721 100644 --- a/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs +++ b/NzbDrone.Core.Test/RootFolderTests/FreeSpaceOnDrivesFixture.cs @@ -30,7 +30,7 @@ public void should_return_one_drive_when_only_one_root_dir_exists() .Returns(@"C:\"); Mocker.GetMock() - .Setup(s => s.GetAvilableSpace(@"C:\")) + .Setup(s => s.GetAvailableSpace(@"C:\")) .Returns(123456); var result = Subject.FreeSpaceOnDrives(); @@ -51,7 +51,7 @@ public void should_return_one_drive_when_two_rootDirs_on_the_same_drive_exist() .Returns(@"C:\"); Mocker.GetMock() - .Setup(s => s.GetAvilableSpace(@"C:\")) + .Setup(s => s.GetAvailableSpace(@"C:\")) .Returns(123456); var result = Subject.FreeSpaceOnDrives(); @@ -76,7 +76,7 @@ public void should_return_two_drives_when_two_rootDirs_on_the_different_drive_ex .Returns(@"D:\"); Mocker.GetMock() - .Setup(s => s.GetAvilableSpace(It.IsAny())) + .Setup(s => s.GetAvailableSpace(It.IsAny())) .Returns(123456); var result = Subject.FreeSpaceOnDrives(); @@ -96,7 +96,7 @@ public void should_skip_rootDir_if_not_found_on_disk() .Returns(@"C:\"); Mocker.GetMock() - .Setup(s => s.GetAvilableSpace(It.IsAny())) + .Setup(s => s.GetAvailableSpace(It.IsAny())) .Throws(new DirectoryNotFoundException()); var result = Subject.FreeSpaceOnDrives(); diff --git a/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/FreeSpaceSpecification.cs b/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/FreeSpaceSpecification.cs index 833e368a3..9c499d8fb 100644 --- a/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/FreeSpaceSpecification.cs +++ b/NzbDrone.Core/MediaFiles/EpisodeImport/Specifications/FreeSpaceSpecification.cs @@ -24,7 +24,7 @@ public bool IsSatisfiedBy(LocalEpisode localEpisode) try { var path = Directory.GetParent(localEpisode.Series.Path); - var freeSpace = _diskProvider.GetAvilableSpace(path.FullName); + var freeSpace = _diskProvider.GetAvailableSpace(path.FullName); if (freeSpace < localEpisode.Size + 100.Megabytes()) { diff --git a/NzbDrone.Core/RootFolders/RootFolder.cs b/NzbDrone.Core/RootFolders/RootFolder.cs index e19725193..823265323 100644 --- a/NzbDrone.Core/RootFolders/RootFolder.cs +++ b/NzbDrone.Core/RootFolders/RootFolder.cs @@ -8,7 +8,7 @@ public class RootFolder : ModelBase { public string Path { get; set; } - public long FreeSpace { get; set; } + public long? FreeSpace { get; set; } public List UnmappedFolders { get; set; } } diff --git a/NzbDrone.Core/RootFolders/RootFolderService.cs b/NzbDrone.Core/RootFolders/RootFolderService.cs index 00e1a9cf0..160cd3c9f 100644 --- a/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -17,7 +17,7 @@ public interface IRootFolderService RootFolder Add(RootFolder rootDir); void Remove(int id); List GetUnmappedFolders(string path); - Dictionary FreeSpaceOnDrives(); + Dictionary FreeSpaceOnDrives(); RootFolder Get(int id); } @@ -55,7 +55,7 @@ public List AllWithUnmappedFolders() { if (_diskProvider.FolderExists(folder.Path)) { - folder.FreeSpace = _diskProvider.GetAvilableSpace(folder.Path); + folder.FreeSpace = _diskProvider.GetAvailableSpace(folder.Path); folder.UnmappedFolders = GetUnmappedFolders(folder.Path); } }); @@ -82,7 +82,7 @@ public RootFolder Add(RootFolder rootFolder) _rootFolderRepository.Insert(rootFolder); - rootFolder.FreeSpace = _diskProvider.GetAvilableSpace(rootFolder.Path); + rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path); rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path); return rootFolder; } @@ -126,9 +126,9 @@ public List GetUnmappedFolders(string path) return results; } - public Dictionary FreeSpaceOnDrives() + public Dictionary FreeSpaceOnDrives() { - var freeSpace = new Dictionary(); + var freeSpace = new Dictionary(); var rootDirs = All(); @@ -140,7 +140,7 @@ public Dictionary FreeSpaceOnDrives() { try { - freeSpace.Add(pathRoot, _diskProvider.GetAvilableSpace(rootDir.Path)); + freeSpace.Add(pathRoot, _diskProvider.GetAvailableSpace(rootDir.Path)); } catch (Exception ex) { @@ -155,7 +155,7 @@ public Dictionary FreeSpaceOnDrives() public RootFolder Get(int id) { var rootFolder = _rootFolderRepository.Get(id); - rootFolder.FreeSpace = _diskProvider.GetAvilableSpace(rootFolder.Path); + rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path); rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path); return rootFolder; }