1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-09 04:22:30 +01: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); File.SetAccessControl(filename, fs);
} }
public virtual ulong GetAvilableSpace(string path) public virtual long GetAvilableSpace(string path)
{ {
if (!FolderExists(path)) if (!FolderExists(path))
throw new DirectoryNotFoundException(path); throw new DirectoryNotFoundException(path);
ulong freeBytesAvailable;
ulong totalNumberOfBytes;
ulong totalNumberOfFreeBytes;
bool success = GetDiskFreeSpaceEx(path, out freeBytesAvailable, out totalNumberOfBytes, var driveInfo = DriveInfo.GetDrives().SingleOrDefault(c => c.IsReady && c.Name.Equals(Path.GetPathRoot(path), StringComparison.CurrentCultureIgnoreCase));
out totalNumberOfFreeBytes);
if (!success)
throw new System.ComponentModel.Win32Exception();
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) public virtual string ReadAllText(string filePath)

View File

@ -31,7 +31,7 @@ public static string CleanPath(this string path)
return info.FullName.TrimEnd('/').Trim('\\', ' '); return info.FullName.TrimEnd('/').Trim('\\', ' ');
} }
static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo) private static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo)
{ {
var parentDirInfo = dirInfo.Parent; var parentDirInfo = dirInfo.Parent;
if (null == parentDirInfo) if (null == parentDirInfo)
@ -40,7 +40,7 @@ static string GetProperDirectoryCapitalization(DirectoryInfo dirInfo)
parentDirInfo.GetDirectories(dirInfo.Name)[0].Name); parentDirInfo.GetDirectories(dirInfo.Name)[0].Name);
} }
static string GetProperFilePathCapitalization(string filename) public static string GetActualCasing(this string filename)
{ {
var fileInfo = new FileInfo(filename); var fileInfo = new FileInfo(filename);
DirectoryInfo dirInfo = fileInfo.Directory; DirectoryInfo dirInfo = fileInfo.Directory;

View File

@ -13,10 +13,18 @@ public class FreeDiskSpaceTest : CoreTest<DiskProvider>
public void should_return_free_disk_space() public void should_return_free_disk_space()
{ {
var result = Subject.GetAvilableSpace(Directory.GetCurrentDirectory()); 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); 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] [Test]
public void should_throw_if_drive_doesnt_exist() public void should_throw_if_drive_doesnt_exist()
{ {

View File

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

View File

@ -15,7 +15,7 @@ public interface IRootFolderService
RootFolder Add(RootFolder rootDir); RootFolder Add(RootFolder rootDir);
void Remove(int id); void Remove(int id);
List<UnmappedFolder> GetUnmappedFolders(string path); List<UnmappedFolder> GetUnmappedFolders(string path);
Dictionary<string, ulong> FreeSpaceOnDrives(); Dictionary<string, long> FreeSpaceOnDrives();
RootFolder Get(int id); RootFolder Get(int id);
} }
@ -99,9 +99,9 @@ public virtual List<UnmappedFolder> GetUnmappedFolders(string path)
return results; 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(); var rootDirs = All();