From 190ad5d202c3e4408f7816907df3fe2cfb538dba Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Thu, 18 Jul 2024 21:03:31 -0500 Subject: [PATCH] Use DriveInfo constructor to get drive from path --- Core/CKANPathUtils.cs | 21 ++++++++------- Core/Extensions/IOExtensions.cs | 47 ++------------------------------- 2 files changed, 14 insertions(+), 54 deletions(-) diff --git a/Core/CKANPathUtils.cs b/Core/CKANPathUtils.cs index 1d431ba56..c1cce5eb3 100644 --- a/Core/CKANPathUtils.cs +++ b/Core/CKANPathUtils.cs @@ -127,16 +127,19 @@ public static string ToAbsolute(string path, string root) public static void CheckFreeSpace(DirectoryInfo where, long bytesToStore, string errorDescription) { - var bytesFree = where.GetDrive()?.AvailableFreeSpace; - if (bytesFree.HasValue && bytesToStore > bytesFree.Value) { - throw new NotEnoughSpaceKraken(errorDescription, where, - bytesFree.Value, bytesToStore); + if (bytesToStore > 0) + { + var bytesFree = where.GetDrive()?.AvailableFreeSpace; + if (bytesFree.HasValue && bytesToStore > bytesFree.Value) { + throw new NotEnoughSpaceKraken(errorDescription, where, + bytesFree.Value, bytesToStore); + } + log.DebugFormat("Storing {0} to {1} ({2} free)...", + CkanModule.FmtSize(bytesToStore), + where.FullName, + bytesFree.HasValue ? CkanModule.FmtSize(bytesFree.Value) + : "unknown bytes"); } - log.DebugFormat("Storing {0} to {1} ({2} free)...", - CkanModule.FmtSize(bytesToStore), - where.FullName, - bytesFree.HasValue ? CkanModule.FmtSize(bytesFree.Value) - : "unknown bytes"); } } diff --git a/Core/Extensions/IOExtensions.cs b/Core/Extensions/IOExtensions.cs index cd477e412..b55daffc8 100644 --- a/Core/Extensions/IOExtensions.cs +++ b/Core/Extensions/IOExtensions.cs @@ -1,6 +1,5 @@ using System; using System.IO; -using System.Linq; using System.Collections.Generic; using System.Threading; using Timer = System.Timers.Timer; @@ -9,55 +8,13 @@ namespace CKAN.Extensions { public static class IOExtensions { - private static bool StringArrayStartsWith(string[] child, string[] parent) - { - if (parent.Length > child.Length) - { - // Only child is allowed to have extra pieces - return false; - } - for (int i = 0; i < parent.Length; ++i) - { - if (!parent[i].Equals(child[i], Platform.PathComparison)) - { - return false; - } - } - return true; - } - - private static readonly char[] pathDelims = new char[] {Path.DirectorySeparatorChar}; - - /// - /// Check whether a given path is an ancestor of another - /// - /// The path to treat as potential ancestor - /// The path to treat as potential descendant - /// true if child is a descendant of parent, false otherwise - public static bool IsAncestorOf(this DirectoryInfo parent, DirectoryInfo child) - => StringArrayStartsWith( - child.FullName.Split(pathDelims, StringSplitOptions.RemoveEmptyEntries), - parent.FullName.Split(pathDelims, StringSplitOptions.RemoveEmptyEntries)); - /// - /// Extension method to fill in the gap of getting from a - /// directory to its drive in .NET. - /// Returns the drive with the longest RootDirectory.FullName - /// that's a prefix of the dir's FullName. + /// Extension method to get from a directory to its drive. /// /// Any DirectoryInfo object /// The DriveInfo associated with this directory, if any, else null public static DriveInfo GetDrive(this DirectoryInfo dir) - => Platform.IsMono - // Mono's DriveInfo.GetDrives doesn't return mounted filesystems, so we - // can't get the drive for a dir on Linux or Mac - ? null - : DriveInfo.GetDrives() - .Where(dr => dr.IsReady - && dr.DriveType != DriveType.NoRootDirectory - && dr.RootDirectory.IsAncestorOf(dir)) - .OrderByDescending(dr => dr.RootDirectory.FullName.Length) - .FirstOrDefault(); + => new DriveInfo(dir.FullName); /// /// A version of Stream.CopyTo with progress updates.