|
1 | 1 | // Copyright (c) 2024 Files Community
|
2 | 2 | // Licensed under the MIT License. See the LICENSE.
|
3 | 3 |
|
| 4 | +using DiscUtils.Udf; |
4 | 5 | using Microsoft.Management.Infrastructure;
|
5 |
| -using System.IO; |
6 | 6 | using Windows.Devices.Enumeration;
|
7 | 7 | using Windows.Devices.Portable;
|
8 | 8 | using Windows.Storage;
|
9 | 9 | using Windows.Storage.FileProperties;
|
10 |
| -using DiscUtils.Udf; |
| 10 | +using Windows.Win32; |
| 11 | +using Windows.Win32.Foundation; |
11 | 12 |
|
12 | 13 | namespace Files.App.Utils.Storage
|
13 | 14 | {
|
@@ -52,12 +53,12 @@ public static async Task<bool> CheckEmptyDrive(string? drivePath)
|
52 | 53 |
|
53 | 54 | public static async Task<StorageFolderWithPath> GetRootFromPathAsync(string devicePath)
|
54 | 55 | {
|
55 |
| - if (!Path.IsPathRooted(devicePath)) |
| 56 | + if (!SystemIO.Path.IsPathRooted(devicePath)) |
56 | 57 | return null;
|
57 | 58 |
|
58 | 59 | var drivesViewModel = Ioc.Default.GetRequiredService<DrivesViewModel>();
|
59 | 60 |
|
60 |
| - var rootPath = Path.GetPathRoot(devicePath); |
| 61 | + var rootPath = SystemIO.Path.GetPathRoot(devicePath); |
61 | 62 | if (devicePath.StartsWith(@"\\?\", StringComparison.Ordinal)) // USB device
|
62 | 63 | {
|
63 | 64 | // Check among already discovered drives
|
@@ -102,41 +103,61 @@ public static async Task<StorageFolderWithPath> GetRootFromPathAsync(string devi
|
102 | 103 | return null;
|
103 | 104 | }
|
104 | 105 |
|
105 |
| - public static Data.Items.DriveType GetDriveType(System.IO.DriveInfo drive) |
| 106 | + public static DriveType GetDriveType(SystemIO.DriveInfo drive) |
106 | 107 | {
|
107 |
| - if (drive.DriveType is System.IO.DriveType.Unknown) |
| 108 | + if (drive.DriveType is SystemIO.DriveType.Unknown) |
108 | 109 | {
|
109 | 110 | string path = PathNormalization.NormalizePath(drive.Name);
|
110 | 111 |
|
111 | 112 | if (path is "A:" or "B:")
|
112 |
| - return Data.Items.DriveType.FloppyDisk; |
| 113 | + return DriveType.FloppyDisk; |
113 | 114 | }
|
114 | 115 |
|
115 | 116 | return drive.DriveType switch
|
116 | 117 | {
|
117 |
| - System.IO.DriveType.CDRom => Data.Items.DriveType.CDRom, |
118 |
| - System.IO.DriveType.Fixed => Data.Items.DriveType.Fixed, |
119 |
| - System.IO.DriveType.Network => Data.Items.DriveType.Network, |
120 |
| - System.IO.DriveType.NoRootDirectory => Data.Items.DriveType.NoRootDirectory, |
121 |
| - System.IO.DriveType.Ram => Data.Items.DriveType.Ram, |
122 |
| - System.IO.DriveType.Removable => Data.Items.DriveType.Removable, |
123 |
| - _ => Data.Items.DriveType.Unknown, |
| 118 | + SystemIO.DriveType.CDRom => DriveType.CDRom, |
| 119 | + SystemIO.DriveType.Fixed => DriveType.Fixed, |
| 120 | + SystemIO.DriveType.Network => DriveType.Network, |
| 121 | + SystemIO.DriveType.NoRootDirectory => DriveType.NoRootDirectory, |
| 122 | + SystemIO.DriveType.Ram => DriveType.Ram, |
| 123 | + SystemIO.DriveType.Removable => DriveType.Removable, |
| 124 | + _ => DriveType.Unknown, |
124 | 125 | };
|
125 | 126 | }
|
126 | 127 |
|
127 |
| - public static string GetExtendedDriveLabel(DriveInfo drive) |
| 128 | + public static unsafe string GetExtendedDriveLabel(SystemIO.DriveInfo drive) |
128 | 129 | {
|
129 | 130 | return SafetyExtensions.IgnoreExceptions(() =>
|
130 | 131 | {
|
131 |
| - if (drive.DriveType is not System.IO.DriveType.CDRom || drive.DriveFormat is not "UDF") |
| 132 | + if (drive.DriveType is not SystemIO.DriveType.CDRom || drive.DriveFormat is not "UDF") |
132 | 133 | return drive.VolumeLabel;
|
| 134 | + |
133 | 135 | return SafetyExtensions.IgnoreExceptions(() =>
|
134 | 136 | {
|
135 |
| - var dosDevicePath = Vanara.PInvoke.Kernel32.QueryDosDevice(drive.Name).FirstOrDefault(); |
| 137 | + string dosDevicePath = ""; |
| 138 | + |
| 139 | + fixed (char* cDeviceName = drive.Name) |
| 140 | + { |
| 141 | + var cch = PInvoke.QueryDosDevice(cDeviceName, null, 0u); |
| 142 | + |
| 143 | + fixed (char* cTargetPath = new char[cch]) |
| 144 | + { |
| 145 | + PWSTR pszTargetPath = new(cTargetPath); |
| 146 | + PInvoke.QueryDosDevice(cDeviceName, pszTargetPath, 0u); |
| 147 | + dosDevicePath = pszTargetPath.ToString(); |
| 148 | + } |
| 149 | + } |
| 150 | + |
136 | 151 | if (string.IsNullOrEmpty(dosDevicePath))
|
137 | 152 | return drive.VolumeLabel;
|
138 |
| - using var driveStream = new FileStream(dosDevicePath.Replace(@"\Device\", @"\\.\"), FileMode.Open, FileAccess.Read); |
| 153 | + |
| 154 | + using var driveStream = new SystemIO.FileStream( |
| 155 | + dosDevicePath.Replace(@"\Device\", @"\\.\"), |
| 156 | + SystemIO.FileMode.Open, |
| 157 | + SystemIO.FileAccess.Read); |
| 158 | + |
139 | 159 | using var udf = new UdfReader(driveStream);
|
| 160 | + |
140 | 161 | return udf.VolumeLabel;
|
141 | 162 | }) ?? drive.VolumeLabel;
|
142 | 163 | }) ?? "";
|
|
0 commit comments