Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use DriveInfo constructor to get drive from path #4125

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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