Moved FreeDiskSpace to DiskProvider.

This commit is contained in:
Mark McDowall 2011-11-12 11:53:36 -08:00
parent 401b3b13e2
commit 95cdc4b78c
6 changed files with 59 additions and 29 deletions

View File

@ -80,13 +80,5 @@ namespace NzbDrone.Core.Test
Console.WriteLine(dateTime.DayOfWeek); Console.WriteLine(dateTime.DayOfWeek);
dateTime.ToBestDateString().Should().Be(dateTime.ToShortDateString()); dateTime.ToBestDateString().Should().Be(dateTime.ToShortDateString());
} }
[Test]
public void FreeDiskSpace()
{
//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 di = new DirectoryInfo(Directory.GetCurrentDirectory());
di.FreeDiskSpace().Should().BeGreaterThan(0);
}
} }
} }

View File

@ -85,6 +85,7 @@
<Compile Include="JobTests\BacklogSearchJobTest.cs" /> <Compile Include="JobTests\BacklogSearchJobTest.cs" />
<Compile Include="JobTests\BannerDownloadJobTest.cs" /> <Compile Include="JobTests\BannerDownloadJobTest.cs" />
<Compile Include="ProviderTests\ConfigFileProviderTest.cs" /> <Compile Include="ProviderTests\ConfigFileProviderTest.cs" />
<Compile Include="ProviderTests\DiskProviderTests\FreeDiskSpaceTest.cs" />
<Compile Include="ProviderTests\ProwlProviderTest.cs" /> <Compile Include="ProviderTests\ProwlProviderTest.cs" />
<Compile Include="ProviderTests\GrowlProviderTest.cs" /> <Compile Include="ProviderTests\GrowlProviderTest.cs" />
<Compile Include="ProviderTests\DiskProviderTests\ExtractArchiveFixture.cs" /> <Compile Include="ProviderTests\DiskProviderTests\ExtractArchiveFixture.cs" />

View File

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using AutoMoq;
using FluentAssertions;
using Moq;
using NUnit.Framework;
using NzbDrone.Core.Providers.Core;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.ProviderTests.DiskProviderTests
{
[TestFixture]
public class FreeDiskSpaceTest : TestBase
{
[Test]
public void FreeDiskSpace()
{
//Setup
var mocker = new AutoMoqer();
//Act
var di = new DirectoryInfo(Directory.GetCurrentDirectory());
var result = mocker.Resolve<DiskProvider>().FreeDiskSpace(di);
//Asert
//Checks to ensure that the free space on the first is greater than 0 (It should be in 99.99999999999999% of cases... I hope)
result.Should().BeGreaterThan(0);
}
}
}

View File

@ -8,13 +8,6 @@ namespace NzbDrone.Core
{ {
public static class Fluent public static class Fluent
{ {
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
public static string WithDefault(this string actual, object defaultValue) public static string WithDefault(this string actual, object defaultValue)
{ {
if (defaultValue == null) if (defaultValue == null)
@ -54,20 +47,9 @@ namespace NzbDrone.Core
return dateTime.ToShortDateString(); return dateTime.ToShortDateString();
} }
public static string ParentUriString(this Uri uri)
//TODO: this should be moved to DiskProvider
public static ulong FreeDiskSpace(this DirectoryInfo directoryInfo)
{ {
ulong freeBytesAvailable; return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - String.Join("", uri.Segments).Length);
ulong totalNumberOfBytes;
ulong totalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx(directoryInfo.FullName, out freeBytesAvailable, out totalNumberOfBytes,
out totalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
return freeBytesAvailable;
} }
} }
} }

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.InteropServices;
using Ionic.Zip; using Ionic.Zip;
using NLog; using NLog;
@ -9,6 +10,13 @@ namespace NzbDrone.Core.Providers.Core
{ {
public class DiskProvider public class DiskProvider
{ {
[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool GetDiskFreeSpaceEx(string lpDirectoryName,
out ulong lpFreeBytesAvailable,
out ulong lpTotalNumberOfBytes,
out ulong lpTotalNumberOfFreeBytes);
private static readonly Logger Logger = LogManager.GetCurrentClassLogger(); private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
public virtual bool FolderExists(string path) public virtual bool FolderExists(string path)
@ -96,5 +104,19 @@ namespace NzbDrone.Core.Providers.Core
fs.SetAccessRuleProtection(false, false); fs.SetAccessRuleProtection(false, false);
File.SetAccessControl(filename, fs); File.SetAccessControl(filename, fs);
} }
public virtual ulong FreeDiskSpace(DirectoryInfo directoryInfo)
{
ulong freeBytesAvailable;
ulong totalNumberOfBytes;
ulong totalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx(directoryInfo.FullName, out freeBytesAvailable, out totalNumberOfBytes,
out totalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
return freeBytesAvailable;
}
} }
} }

View File

@ -92,7 +92,7 @@ namespace NzbDrone.Core.Providers
foreach (var rootDir in rootDirs) foreach (var rootDir in rootDirs)
{ {
rootDir.FreeSpace = new DirectoryInfo(rootDir.Path).FreeDiskSpace(); rootDir.FreeSpace = _diskProvider.FreeDiskSpace(new DirectoryInfo(rootDir.Path));
if (rootDir.FreeSpace > maxSize) if (rootDir.FreeSpace > maxSize)
{ {
maxPath = rootDir.Path; maxPath = rootDir.Path;