From e03ab2ebeaa307d028175fc992d7e2d6354e51d0 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Mon, 29 Apr 2013 23:11:49 -0700 Subject: [PATCH] getting free space tries to get the space safely, if doesn't work and windows the tries interop. --- NzbDrone.Common/DiskProvider.cs | 46 +++++++++++++++---- NzbDrone.Common/PathExtensions.cs | 4 +- .../DiskProviderTests/FreeDiskSpaceTest.cs | 10 +++- NzbDrone.Core/RootFolders/RootFolder.cs | 2 +- .../RootFolders/RootFolderService.cs | 6 +-- 5 files changed, 52 insertions(+), 16 deletions(-) diff --git a/NzbDrone.Common/DiskProvider.cs b/NzbDrone.Common/DiskProvider.cs index 315ea8cdd..13ed77c3b 100644 --- a/NzbDrone.Common/DiskProvider.cs +++ b/NzbDrone.Common/DiskProvider.cs @@ -200,21 +200,49 @@ namespace NzbDrone.Common File.SetAccessControl(filename, fs); } - public virtual ulong GetAvilableSpace(string path) + public virtual long GetAvilableSpace(string path) { if (!FolderExists(path)) throw new DirectoryNotFoundException(path); - ulong freeBytesAvailable; - ulong totalNumberOfBytes; - ulong totalNumberOfFreeBytes; - bool success = GetDiskFreeSpaceEx(path, out freeBytesAvailable, out totalNumberOfBytes, - out totalNumberOfFreeBytes); - if (!success) - throw new System.ComponentModel.Win32Exception(); + var driveInfo = DriveInfo.GetDrives().SingleOrDefault(c => c.IsReady && c.Name.Equals(Path.GetPathRoot(path), StringComparison.CurrentCultureIgnoreCase)); - return freeBytesAvailable; + if (driveInfo == null) + { + if (EnvironmentProvider.IsLinux) + { + return 0; + } + + return DriveFreeSpaceEx(path); + } + + return driveInfo.AvailableFreeSpace; + } + + private static long DriveFreeSpaceEx(string folderName) + { + if (string.IsNullOrEmpty(folderName)) + { + throw new ArgumentNullException("folderName"); + } + + if (!folderName.EndsWith("\\")) + { + folderName += '\\'; + } + + ulong free = 0; + ulong dummy1 = 0; + ulong dummy2 = 0; + + if (GetDiskFreeSpaceEx(folderName, out free, out dummy1, out dummy2)) + { + return (long)free; + } + + return 0; } public virtual string ReadAllText(string filePath) diff --git a/NzbDrone.Common/PathExtensions.cs b/NzbDrone.Common/PathExtensions.cs index 65079499c..58e419edf 100644 --- a/NzbDrone.Common/PathExtensions.cs +++ b/NzbDrone.Common/PathExtensions.cs @@ -31,7 +31,7 @@ namespace NzbDrone.Common return info.FullName.TrimEnd('/').Trim('\\', ' '); } - static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo) + private static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo) { var parentDirInfo = dirInfo.Parent; if (null == parentDirInfo) @@ -40,7 +40,7 @@ namespace NzbDrone.Common parentDirInfo.GetDirectories(dirInfo.Name)[0].Name); } - static string GetProperFilePathCapitalization(string filename) + public static string GetActualCasing(this string filename) { var fileInfo = new FileInfo(filename); DirectoryInfo dirInfo = fileInfo.Directory; diff --git a/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceTest.cs b/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceTest.cs index 94d59b132..cd796b0ac 100644 --- a/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceTest.cs +++ b/NzbDrone.Core.Test/ProviderTests/DiskProviderTests/FreeDiskSpaceTest.cs @@ -13,10 +13,18 @@ namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests public void should_return_free_disk_space() { var result = Subject.GetAvilableSpace(Directory.GetCurrentDirectory()); + result.Should().BeGreaterThan(0); + } + + [Test] + public void should_be_able_to_get_space_on_unc() + { + WindowsOnly(); - //Checks to ensure that the free space on the first is greater than 0 (It should be in 99.99999999999999% of cases... I hope) + var result = Subject.GetAvilableSpace(@"\\localhost\c$\Windows"); result.Should().BeGreaterThan(0); } + [Test] public void should_throw_if_drive_doesnt_exist() { diff --git a/NzbDrone.Core/RootFolders/RootFolder.cs b/NzbDrone.Core/RootFolders/RootFolder.cs index d78d29fa4..e19725193 100644 --- a/NzbDrone.Core/RootFolders/RootFolder.cs +++ b/NzbDrone.Core/RootFolders/RootFolder.cs @@ -8,7 +8,7 @@ namespace NzbDrone.Core.RootFolders { public string Path { get; set; } - public ulong 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 0c8964263..7a73d5a52 100644 --- a/NzbDrone.Core/RootFolders/RootFolderService.cs +++ b/NzbDrone.Core/RootFolders/RootFolderService.cs @@ -15,7 +15,7 @@ namespace NzbDrone.Core.RootFolders RootFolder Add(RootFolder rootDir); void Remove(int id); List GetUnmappedFolders(string path); - Dictionary FreeSpaceOnDrives(); + Dictionary FreeSpaceOnDrives(); RootFolder Get(int id); } @@ -99,9 +99,9 @@ namespace NzbDrone.Core.RootFolders return results; } - public virtual Dictionary FreeSpaceOnDrives() + public virtual Dictionary FreeSpaceOnDrives() { - var freeSpace = new Dictionary(); + var freeSpace = new Dictionary(); var rootDirs = All();