getting free space tries to get the space safely, if doesn't work and windows

the tries interop.
pull/3113/head
kay.one 12 years ago
parent d6cc0b40fb
commit e03ab2ebea

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

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

@ -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()
{

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

@ -15,7 +15,7 @@ namespace NzbDrone.Core.RootFolders
RootFolder Add(RootFolder rootDir);
void Remove(int id);
List<UnmappedFolder> GetUnmappedFolders(string path);
Dictionary<string, ulong> FreeSpaceOnDrives();
Dictionary<string, long> FreeSpaceOnDrives();
RootFolder Get(int id);
}
@ -99,9 +99,9 @@ namespace NzbDrone.Core.RootFolders
return results;
}
public virtual Dictionary<string, ulong> FreeSpaceOnDrives()
public virtual Dictionary<string, long> FreeSpaceOnDrives()
{
var freeSpace = new Dictionary<string, ulong>();
var freeSpace = new Dictionary<string, long>();
var rootDirs = All();

Loading…
Cancel
Save