Skip to content

Code Quality: Removed Vanara from DriveHelpers #16091

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

Merged
merged 3 commits into from
Sep 29, 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
1 change: 1 addition & 0 deletions src/Files.App.CsWin32/NativeMethods.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ IFileOperation
IShellItem2
PSGetPropertyKeyFromName
ShellExecuteEx
QueryDosDevice
50 changes: 36 additions & 14 deletions src/Files.App/Utils/Storage/Helpers/DriveHelpers.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// Copyright (c) 2024 Files Community
// Licensed under the MIT License. See the LICENSE.

using DiscUtils.Udf;
using Microsoft.Management.Infrastructure;
using System.IO;
using Windows.Devices.Enumeration;
using Windows.Devices.Portable;
using Windows.Storage;
using Windows.Storage.FileProperties;
using DiscUtils.Udf;
using Windows.Win32;
using Windows.Win32.Foundation;

namespace Files.App.Utils.Storage
{
Expand Down Expand Up @@ -52,12 +53,12 @@ public static async Task<bool> CheckEmptyDrive(string? drivePath)

public static async Task<StorageFolderWithPath> GetRootFromPathAsync(string devicePath)
{
if (!Path.IsPathRooted(devicePath))
if (!SystemIO.Path.IsPathRooted(devicePath))
return null;

var drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();

var rootPath = Path.GetPathRoot(devicePath);
var rootPath = SystemIO.Path.GetPathRoot(devicePath);
if (devicePath.StartsWith(@"\\?\", StringComparison.Ordinal)) // USB device
{
// Check among already discovered drives
Expand Down Expand Up @@ -114,31 +115,52 @@ public static Data.Items.DriveType GetDriveType(System.IO.DriveInfo drive)

return drive.DriveType switch
{
System.IO.DriveType.CDRom => Data.Items.DriveType.CDRom,
System.IO.DriveType.Fixed => Data.Items.DriveType.Fixed,
System.IO.DriveType.Network => Data.Items.DriveType.Network,
System.IO.DriveType.NoRootDirectory => Data.Items.DriveType.NoRootDirectory,
System.IO.DriveType.Ram => Data.Items.DriveType.Ram,
System.IO.DriveType.Removable => Data.Items.DriveType.Removable,
SystemIO.DriveType.CDRom => Data.Items.DriveType.CDRom,
SystemIO.DriveType.Fixed => Data.Items.DriveType.Fixed,
SystemIO.DriveType.Network => Data.Items.DriveType.Network,
SystemIO.DriveType.NoRootDirectory => Data.Items.DriveType.NoRootDirectory,
SystemIO.DriveType.Ram => Data.Items.DriveType.Ram,
SystemIO.DriveType.Removable => Data.Items.DriveType.Removable,
_ => Data.Items.DriveType.Unknown,
};
}

public static string GetExtendedDriveLabel(DriveInfo drive)
public static unsafe string GetExtendedDriveLabel(SystemIO.DriveInfo drive)
{
return SafetyExtensions.IgnoreExceptions(() =>
{
if (drive.DriveType is not System.IO.DriveType.CDRom || drive.DriveFormat is not "UDF")
if (drive.DriveType is not SystemIO.DriveType.CDRom || drive.DriveFormat is not "UDF")
return drive.VolumeLabel;

return SafetyExtensions.IgnoreExceptions(() =>
{
var dosDevicePath = Vanara.PInvoke.Kernel32.QueryDosDevice(drive.Name).FirstOrDefault();
string dosDevicePath = "";

fixed (char* cDeviceName = drive.Name)
{
var cch = PInvoke.QueryDosDevice(cDeviceName, null, 0u);

fixed (char* cTargetPath = new char[cch])
{
PWSTR pszTargetPath = new(cTargetPath);
PInvoke.QueryDosDevice(cDeviceName, pszTargetPath, 0u);
dosDevicePath = pszTargetPath.ToString();
}
}

if (string.IsNullOrEmpty(dosDevicePath))
return drive.VolumeLabel;
using var driveStream = new FileStream(dosDevicePath.Replace(@"\Device\", @"\\.\"), FileMode.Open, FileAccess.Read);

using var driveStream = new SystemIO.FileStream(
dosDevicePath.Replace(@"\Device\", @"\\.\"),
SystemIO.FileMode.Open,
SystemIO.FileAccess.Read);

using var udf = new UdfReader(driveStream);

return udf.VolumeLabel;
}) ?? drive.VolumeLabel;

}) ?? "";
}

Expand Down