Moved FreeDiskSpace to DiskProvider.

pull/4/head
Mark McDowall 13 years ago
parent 401b3b13e2
commit 95cdc4b78c

@ -80,13 +80,5 @@ namespace NzbDrone.Core.Test
Console.WriteLine(dateTime.DayOfWeek);
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);
}
}
}

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

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

@ -8,13 +8,6 @@ namespace NzbDrone.Core
{
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)
{
if (defaultValue == null)
@ -54,20 +47,9 @@ namespace NzbDrone.Core
return dateTime.ToShortDateString();
}
//TODO: this should be moved to DiskProvider
public static ulong FreeDiskSpace(this DirectoryInfo directoryInfo)
public static string ParentUriString(this Uri uri)
{
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;
return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - String.Join("", uri.Segments).Length);
}
}
}

@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using Ionic.Zip;
using NLog;
@ -9,6 +10,13 @@ namespace NzbDrone.Core.Providers.Core
{
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();
public virtual bool FolderExists(string path)
@ -96,5 +104,19 @@ namespace NzbDrone.Core.Providers.Core
fs.SetAccessRuleProtection(false, false);
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;
}
}
}

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

Loading…
Cancel
Save