getting freespace returns null instead of blowing up.

This commit is contained in:
kay.one 2013-08-31 21:38:06 -07:00
parent 561e78df8b
commit 3b240e0dd7
9 changed files with 57 additions and 53 deletions

View File

@ -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<UnmappedFolder> UnmappedFolders { get; set; }
}

View File

@ -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 @@ namespace NzbDrone.Common.Test.DiskProviderTests
{
var path = @"C:\".AsOsAgnostic();
Subject.GetAvilableSpace(path).Should().NotBe(0);
Subject.GetAvailableSpace(path).Should().NotBe(0);
}
[Test]
@ -25,7 +21,7 @@ namespace NzbDrone.Common.Test.DiskProviderTests
{
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();
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);
}
}
}

View File

@ -17,7 +17,6 @@ namespace NzbDrone.Common
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 @@ namespace NzbDrone.Common
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 @@ namespace NzbDrone.Common
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 @@ namespace NzbDrone.Common
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);
}

View File

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

View File

@ -14,7 +14,7 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
[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 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
{
WindowsOnly();
var result = Subject.GetAvilableSpace(@"\\localhost\c$\Windows");
var result = Subject.GetAvailableSpace(@"\\localhost\c$\Windows");
result.Should().BeGreaterThan(0);
}
@ -32,13 +32,13 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
{
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]
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);
}
}

View File

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

View File

@ -24,7 +24,7 @@ namespace NzbDrone.Core.MediaFiles.EpisodeImport.Specifications
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())
{

View File

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

View File

@ -17,7 +17,7 @@ namespace NzbDrone.Core.RootFolders
RootFolder Add(RootFolder rootDir);
void Remove(int id);
List<UnmappedFolder> GetUnmappedFolders(string path);
Dictionary<string, long> FreeSpaceOnDrives();
Dictionary<string, long?> FreeSpaceOnDrives();
RootFolder Get(int id);
}
@ -55,7 +55,7 @@ namespace NzbDrone.Core.RootFolders
{
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 @@ namespace NzbDrone.Core.RootFolders
_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 @@ namespace NzbDrone.Core.RootFolders
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();
@ -140,7 +140,7 @@ namespace NzbDrone.Core.RootFolders
{
try
{
freeSpace.Add(pathRoot, _diskProvider.GetAvilableSpace(rootDir.Path));
freeSpace.Add(pathRoot, _diskProvider.GetAvailableSpace(rootDir.Path));
}
catch (Exception ex)
{
@ -155,7 +155,7 @@ namespace NzbDrone.Core.RootFolders
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;
}