mirror of
https://github.com/lidarr/lidarr.git
synced 2025-08-20 13:33:34 -07:00
getting free space tries to get the space safely, if doesn't work and windows
the tries interop.
This commit is contained in:
parent
d6cc0b40fb
commit
e03ab2ebea
5 changed files with 53 additions and 17 deletions
|
@ -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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue