1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

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 @@ 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<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 @@ 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);
}

View File

@ -54,7 +54,7 @@ private void GivenFileSize(long size)
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 @@ public void should_use_series_paths_parent_for_free_space_check()
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 @@ public class FreeDiskSpaceFixture : CoreTest<DiskProvider>
[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<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 @@ public void should_return_one_drive_when_only_one_root_dir_exists()
.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 @@ public void should_return_one_drive_when_two_rootDirs_on_the_same_drive_exist()
.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 @@ public void should_return_two_drives_when_two_rootDirs_on_the_different_drive_ex
.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 @@ public void should_skip_rootDir_if_not_found_on_disk()
.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 @@ 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())
{

View File

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

View File

@ -17,7 +17,7 @@ public interface IRootFolderService
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 @@ public List<RootFolder> 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<UnmappedFolder> GetUnmappedFolders(string path)
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 @@ public Dictionary<string, long> 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<string, long> 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;
}