From 1e3f76a2fdaee5bc1a9277d8298374d5ea3bfa8e Mon Sep 17 00:00:00 2001 From: Cryolithic Date: Fri, 18 Oct 2024 14:53:41 -0700 Subject: [PATCH] Update Filesystem.cs Made disk space check more reliable. --- Bloxstrap/Utility/Filesystem.cs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/Bloxstrap/Utility/Filesystem.cs b/Bloxstrap/Utility/Filesystem.cs index 77bd2845..192c8f4c 100644 --- a/Bloxstrap/Utility/Filesystem.cs +++ b/Bloxstrap/Utility/Filesystem.cs @@ -11,14 +11,21 @@ internal static class Filesystem { internal static long GetFreeDiskSpace(string path) { - foreach (var drive in DriveInfo.GetDrives()) + try { - // https://github.com/bloxstraplabs/bloxstrap/issues/1648#issuecomment-2192571030 - if (path.ToUpperInvariant().StartsWith(drive.Name)) - return drive.AvailableFreeSpace; + var isUri = Uri.TryCreate(p, UriKind.RelativeOrAbsolute, out var u); + if (!Path.IsPathRooted(p) || !Path.IsPathFullyQualified(p) || (isUri && (u?.IsUnc??false))) + { + return -1; + } + var drive = new DriveInfo(p); + return drive.AvailableFreeSpace; } - - return -1; + catch (ArgumentException e) + { + App.Logger.WriteLine("Filesystem::BadPath", $"The path: {p} does not contain a valid drive info."); + return -1 + } } internal static void AssertReadOnly(string filePath)