namespace LlamaApp.Common;
///
/// Free-disk-space probes used to preflight multi-GB model downloads before
/// they start. All methods are total: an unreadable path or drive returns
/// "unknown" rather than throwing, and callers treat "unknown" as "allow" —
/// a failed probe must never block a download.
///
public static class DiskSpace
{
///
/// Returns the free bytes on the drive hosting
/// (which need not exist yet — the probe resolves the path's root).
/// false when the drive can't be determined or queried.
///
public static bool TryGetAvailableFreeBytes(string? forPath, out long freeBytes)
{
freeBytes = 0;
if (string.IsNullOrWhiteSpace(forPath)) return false;
try
{
var root = Path.GetPathRoot(Path.GetFullPath(forPath));
if (string.IsNullOrEmpty(root)) return false;
freeBytes = new DriveInfo(root).AvailableFreeSpace;
return true;
}
catch
{
// Invalid path syntax, unready drive, security — all "unknown".
return false;
}
}
///
/// True when the drive hosting has at least
/// free — or when the free space can't be
/// determined (a failed probe never blocks).
/// receives the probe result for messaging; 0 when unknown.
///
public static bool HasEnoughSpace(string? forPath, ulong neededBytes, out long freeBytes)
{
if (!TryGetAvailableFreeBytes(forPath, out freeBytes)) return true;
return freeBytes >= 0 && (ulong)freeBytes >= neededBytes;
}
}