1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-09-17 15:02:34 +02:00

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
parent d6cc0b40fb
commit e03ab2ebea
5 changed files with 53 additions and 17 deletions

View File

@ -200,21 +200,49 @@ public virtual void InheritFolderPermissions(string filename)
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)

View File

@ -31,7 +31,7 @@ public static string CleanPath(this string path)
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 @@ static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo)
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;

View File

@ -13,10 +13,18 @@ public class FreeDiskSpaceTest : CoreTest<DiskProvider>
public void should_return_free_disk_space()
{
var result = Subject.GetAvilableSpace(Directory.GetCurrentDirectory());
//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);
}
[Test]
public void should_be_able_to_get_space_on_unc()
{
WindowsOnly();
var result = Subject.GetAvilableSpace(@"\\localhost\c$\Windows");
result.Should().BeGreaterThan(0);
}
[Test]
public void should_throw_if_drive_doesnt_exist()
{

View File

@ -8,7 +8,7 @@ public class RootFolder : ModelBase
{
public string Path { get; set; }
public ulong FreeSpace { get; set; }
public long FreeSpace { get; set; }
public List<UnmappedFolder> UnmappedFolders { get; set; }
}

View File

@ -15,7 +15,7 @@ public interface IRootFolderService
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 @@ public virtual List<UnmappedFolder> GetUnmappedFolders(string path)
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();