Skip to content

Commit

Permalink
Use DriveInfo constructor to get drive from path
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Jul 19, 2024
1 parent 1b5948f commit 190ad5d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 54 deletions.
21 changes: 12 additions & 9 deletions Core/CKANPathUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

}
Expand Down
47 changes: 2 additions & 45 deletions Core/Extensions/IOExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using System.Threading;
using Timer = System.Timers.Timer;
Expand All @@ -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};

/// <summary>
/// Check whether a given path is an ancestor of another
/// </summary>
/// <param name="parent">The path to treat as potential ancestor</param>
/// <param name="child">The path to treat as potential descendant</param>
/// <returns>true if child is a descendant of parent, false otherwise</returns>
public static bool IsAncestorOf(this DirectoryInfo parent, DirectoryInfo child)
=> StringArrayStartsWith(
child.FullName.Split(pathDelims, StringSplitOptions.RemoveEmptyEntries),
parent.FullName.Split(pathDelims, StringSplitOptions.RemoveEmptyEntries));

/// <summary>
/// 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.
/// </summary>
/// <param name="dir">Any DirectoryInfo object</param>
/// <returns>The DriveInfo associated with this directory, if any, else null</returns>
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);

/// <summary>
/// A version of Stream.CopyTo with progress updates.
Expand Down

0 comments on commit 190ad5d

Please sign in to comment.