getting freespace returns null instead of blowing up.

pull/3113/head
kay.one 11 years ago
parent 561e78df8b
commit 3b240e0dd7

@ -8,7 +8,7 @@ namespace NzbDrone.Api.RootFolders
public class RootFolderResource : RestResource public class RootFolderResource : RestResource
{ {
public String Path { get; set; } public String Path { get; set; }
public Int64 FreeSpace { get; set; } public Int64? FreeSpace { get; set; }
public List<UnmappedFolder> UnmappedFolders { get; set; } public List<UnmappedFolder> UnmappedFolders { get; set; }
} }

@ -1,8 +1,4 @@
using System; using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using FluentAssertions; using FluentAssertions;
using NUnit.Framework; using NUnit.Framework;
using NzbDrone.Test.Common; using NzbDrone.Test.Common;
@ -17,7 +13,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
{ {
var path = @"C:\".AsOsAgnostic(); var path = @"C:\".AsOsAgnostic();
Subject.GetAvilableSpace(path).Should().NotBe(0); Subject.GetAvailableSpace(path).Should().NotBe(0);
} }
[Test] [Test]
@ -25,7 +21,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
{ {
var path = @"C:\".AsOsAgnostic(); 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 @@ namespace NzbDrone.Common.Test.DiskProviderTests
{ {
WindowsOnly(); WindowsOnly();
Assert.Throws<DirectoryNotFoundException>(() => Subject.GetAvilableSpace("J:\\").Should().NotBe(0)); Assert.Throws<DirectoryNotFoundException>(() => Subject.GetAvailableSpace("J:\\").Should().NotBe(0));
}
[Test]
public void should_return_null_when_cant_get_free_space()
{
LinuxOnly();
Subject.GetAvailableSpace("/run/").Should().NotBe(null);
} }
} }
} }

@ -17,7 +17,6 @@ namespace NzbDrone.Common
DateTime GetLastFolderWrite(string path); DateTime GetLastFolderWrite(string path);
DateTime GetLastFileWrite(string path); DateTime GetLastFileWrite(string path);
void EnsureFolder(string path); void EnsureFolder(string path);
bool FolderExists(string path, bool caseSensitive);
bool FolderExists(string path); bool FolderExists(string path);
bool FileExists(string path); bool FileExists(string path);
bool FileExists(string path, bool caseSensitive); bool FileExists(string path, bool caseSensitive);
@ -32,7 +31,7 @@ namespace NzbDrone.Common
void MoveFile(string source, string destination); void MoveFile(string source, string destination);
void DeleteFolder(string path, bool recursive); void DeleteFolder(string path, bool recursive);
void InheritFolderPermissions(string filename); void InheritFolderPermissions(string filename);
long GetAvilableSpace(string path); long? GetAvailableSpace(string path);
string ReadAllText(string filePath); string ReadAllText(string filePath);
void WriteAllText(string filename, string contents); void WriteAllText(string filename, string contents);
void FileSetLastWriteTimeUtc(string path, DateTime dateTime); void FileSetLastWriteTimeUtc(string path, DateTime dateTime);
@ -113,16 +112,6 @@ namespace NzbDrone.Common
return Directory.Exists(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) public bool FileExists(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(() => path).IsValidPath();
@ -289,26 +278,37 @@ namespace NzbDrone.Common
File.SetAccessControl(filename, fs); File.SetAccessControl(filename, fs);
} }
public long GetAvilableSpace(string path) public long? GetAvailableSpace(string path)
{ {
Ensure.That(() => path).IsValidPath(); Ensure.That(() => path).IsValidPath();
var root = GetPathRoot(path);
if (!FolderExists(root))
throw new DirectoryNotFoundException(root);
if (OsInfo.IsLinux) if (OsInfo.IsLinux)
{ {
var driveInfo = DriveInfo.GetDrives().SingleOrDefault(c => c.IsReady && path.StartsWith(c.Name, StringComparison.CurrentCultureIgnoreCase)); var drives = DriveInfo.GetDrives();
if (driveInfo == null) foreach (var drive in drives)
{ {
throw new DirectoryNotFoundException(path); 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 driveInfo.AvailableFreeSpace;
} }
var root = GetPathRoot(path); return null;
}
if (!FolderExists(root))
throw new DirectoryNotFoundException(root);
return DriveFreeSpaceEx(root); return DriveFreeSpaceEx(root);
} }

@ -54,7 +54,7 @@ namespace NzbDrone.Core.Test.MediaFileTests.EpisodeImportTests
private void GivenFreeSpace(long size) private void GivenFreeSpace(long size)
{ {
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(s => s.GetAvilableSpace(It.IsAny<String>())) .Setup(s => s.GetAvailableSpace(It.IsAny<String>()))
.Returns(size); .Returns(size);
} }
@ -96,7 +96,7 @@ namespace NzbDrone.Core.Test.MediaFileTests.EpisodeImportTests
Subject.IsSatisfiedBy(_localEpisode).Should().BeTrue(); Subject.IsSatisfiedBy(_localEpisode).Should().BeTrue();
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Verify(v => v.GetAvilableSpace(_rootFolder), Times.Once()); .Verify(v => v.GetAvailableSpace(_rootFolder), Times.Once());
} }
} }
} }

@ -14,7 +14,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
[Test] [Test]
public void should_return_free_disk_space() 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); result.Should().BeGreaterThan(0);
} }
@ -23,7 +23,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
{ {
WindowsOnly(); WindowsOnly();
var result = Subject.GetAvilableSpace(@"\\localhost\c$\Windows"); var result = Subject.GetAvailableSpace(@"\\localhost\c$\Windows");
result.Should().BeGreaterThan(0); result.Should().BeGreaterThan(0);
} }
@ -32,13 +32,13 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
{ {
WindowsOnly(); WindowsOnly();
Assert.Throws<DirectoryNotFoundException>(() => Subject.GetAvilableSpace(@"Z:\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic())); Assert.Throws<DirectoryNotFoundException>(() => Subject.GetAvailableSpace(@"Z:\NOT_A_REAL_PATH\DOES_NOT_EXIST".AsOsAgnostic()));
} }
[Test] [Test]
public void should_be_able_to_get_space_on_folder_that_doesnt_exist() 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); result.Should().BeGreaterThan(0);
} }
} }

@ -30,7 +30,7 @@ namespace NzbDrone.Core.Test.RootFolderTests
.Returns(@"C:\"); .Returns(@"C:\");
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(s => s.GetAvilableSpace(@"C:\")) .Setup(s => s.GetAvailableSpace(@"C:\"))
.Returns(123456); .Returns(123456);
var result = Subject.FreeSpaceOnDrives(); var result = Subject.FreeSpaceOnDrives();
@ -51,7 +51,7 @@ namespace NzbDrone.Core.Test.RootFolderTests
.Returns(@"C:\"); .Returns(@"C:\");
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(s => s.GetAvilableSpace(@"C:\")) .Setup(s => s.GetAvailableSpace(@"C:\"))
.Returns(123456); .Returns(123456);
var result = Subject.FreeSpaceOnDrives(); var result = Subject.FreeSpaceOnDrives();
@ -76,7 +76,7 @@ namespace NzbDrone.Core.Test.RootFolderTests
.Returns(@"D:\"); .Returns(@"D:\");
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(s => s.GetAvilableSpace(It.IsAny<string>())) .Setup(s => s.GetAvailableSpace(It.IsAny<string>()))
.Returns(123456); .Returns(123456);
var result = Subject.FreeSpaceOnDrives(); var result = Subject.FreeSpaceOnDrives();
@ -96,7 +96,7 @@ namespace NzbDrone.Core.Test.RootFolderTests
.Returns(@"C:\"); .Returns(@"C:\");
Mocker.GetMock<IDiskProvider>() Mocker.GetMock<IDiskProvider>()
.Setup(s => s.GetAvilableSpace(It.IsAny<string>())) .Setup(s => s.GetAvailableSpace(It.IsAny<string>()))
.Throws(new DirectoryNotFoundException()); .Throws(new DirectoryNotFoundException());
var result = Subject.FreeSpaceOnDrives(); var result = Subject.FreeSpaceOnDrives();

@ -24,7 +24,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
try try
{ {
var path = Directory.GetParent(localEpisode.Series.Path); 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()) if (freeSpace < localEpisode.Size + 100.Megabytes())
{ {

@ -8,7 +8,7 @@ namespace NzbDrone.Core.RootFolders
{ {
public string Path { get; set; } public string Path { get; set; }
public long FreeSpace { get; set; } public long? FreeSpace { get; set; }
public List<UnmappedFolder> UnmappedFolders { get; set; } public List<UnmappedFolder> UnmappedFolders { get; set; }
} }

@ -17,7 +17,7 @@ namespace NzbDrone.Core.RootFolders
RootFolder Add(RootFolder rootDir); RootFolder Add(RootFolder rootDir);
void Remove(int id); void Remove(int id);
List<UnmappedFolder> GetUnmappedFolders(string path); List<UnmappedFolder> GetUnmappedFolders(string path);
Dictionary<string, long> FreeSpaceOnDrives(); Dictionary<string, long?> FreeSpaceOnDrives();
RootFolder Get(int id); RootFolder Get(int id);
} }
@ -55,7 +55,7 @@ namespace NzbDrone.Core.RootFolders
{ {
if (_diskProvider.FolderExists(folder.Path)) if (_diskProvider.FolderExists(folder.Path))
{ {
folder.FreeSpace = _diskProvider.GetAvilableSpace(folder.Path); folder.FreeSpace = _diskProvider.GetAvailableSpace(folder.Path);
folder.UnmappedFolders = GetUnmappedFolders(folder.Path); folder.UnmappedFolders = GetUnmappedFolders(folder.Path);
} }
}); });
@ -82,7 +82,7 @@ namespace NzbDrone.Core.RootFolders
_rootFolderRepository.Insert(rootFolder); _rootFolderRepository.Insert(rootFolder);
rootFolder.FreeSpace = _diskProvider.GetAvilableSpace(rootFolder.Path); rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path);
rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path); rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path);
return rootFolder; return rootFolder;
} }
@ -126,9 +126,9 @@ namespace NzbDrone.Core.RootFolders
return results; return results;
} }
public Dictionary<string, long> FreeSpaceOnDrives() public Dictionary<string, long?> FreeSpaceOnDrives()
{ {
var freeSpace = new Dictionary<string, long>(); var freeSpace = new Dictionary<string, long?>();
var rootDirs = All(); var rootDirs = All();
@ -140,7 +140,7 @@ namespace NzbDrone.Core.RootFolders
{ {
try try
{ {
freeSpace.Add(pathRoot, _diskProvider.GetAvilableSpace(rootDir.Path)); freeSpace.Add(pathRoot, _diskProvider.GetAvailableSpace(rootDir.Path));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -155,7 +155,7 @@ namespace NzbDrone.Core.RootFolders
public RootFolder Get(int id) public RootFolder Get(int id)
{ {
var rootFolder = _rootFolderRepository.Get(id); var rootFolder = _rootFolderRepository.Get(id);
rootFolder.FreeSpace = _diskProvider.GetAvilableSpace(rootFolder.Path); rootFolder.FreeSpace = _diskProvider.GetAvailableSpace(rootFolder.Path);
rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path); rootFolder.UnmappedFolders = GetUnmappedFolders(rootFolder.Path);
return rootFolder; return rootFolder;
} }

Loading…
Cancel
Save