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

the tries interop.
This commit is contained in:
kay.one 2013-04-29 23:11:49 -07:00
commit e03ab2ebea
5 changed files with 53 additions and 17 deletions

View file

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