Skip to content

Commit 3490b8b

Browse files
committed
Attempt to fix chkdsk validations
1 parent ce4c1f8 commit 3490b8b

File tree

3 files changed

+23
-18
lines changed

3 files changed

+23
-18
lines changed

Tests/LibraryTests/ExFat/Environment/StreamTestEnvironment.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.IO;
77
using System.IO.Compression;
88
using DiscUtils;
9+
using DiscUtils.Partitions;
910
using DiscUtils.Streams;
1011
using DiscUtils.Vhdx;
1112

@@ -41,6 +42,7 @@ private void ExtractVhdx(bool allowDebugKeep)
4142

4243
disk = new Disk(vhdxStream, Ownership.Dispose);
4344
var volume = VolumeManager.GetPhysicalVolumes(disk)[1];
45+
volumeId = ((GuidPartitionInfo)volume.Partition!).Identity;
4446
PartitionStream = volume.Open();
4547
}
4648
}

Tests/LibraryTests/ExFat/Environment/TestEnvironment.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,14 @@
99
using System.Runtime.InteropServices;
1010
using System.Runtime.Versioning;
1111
using System.Security.Principal;
12+
using System.Threading;
1213
using DiscUtils.Vhdx;
1314

1415
namespace LibraryTests.ExFat.Environment;
1516
internal class TestEnvironment : IDisposable
1617
{
1718
protected string vhdxPath = null!;
19+
protected Guid volumeId;
1820
protected Disk? disk;
1921

2022
protected TestEnvironment()
@@ -69,26 +71,27 @@ public virtual void Dispose()
6971
}
7072
}
7173

74+
#if NET9_0_OR_GREATER
75+
private static readonly Lock _lock = new();
76+
#else
77+
private static readonly object _lock = new();
78+
#endif
79+
7280
#if NETCOREAPP
7381
[SupportedOSPlatform("windows")]
7482
#endif
75-
private Tuple<bool, string?> CheckDisk()
83+
private (bool success, string? checkResult) CheckDisk()
7684
{
77-
var previousDrives = DriveInfo.GetDrives();
78-
RunDiskPart("attach", vhdxPath);
79-
var newDrives = DriveInfo.GetDrives();
80-
var mountedDrive = newDrives.FirstOrDefault(d => previousDrives.All(p => p.Name != d.Name));
81-
var success = true;
82-
string? checkResult = null;
83-
if (mountedDrive != null)
85+
lock (_lock)
8486
{
85-
var result = ProcessUtility.Run("chkdsk", mountedDrive.Name.TrimEnd('\\'));
86-
success = result.Item1 == 0;
87-
checkResult = result.Item2;
88-
}
87+
RunDiskPart("attach", vhdxPath);
88+
var result = ProcessUtility.Run("chkdsk", @$"\\?\Volume{{{volumeId}}} /x");
89+
var success = result.Item1 == 0;
90+
var checkResult = result.Item3;
8991

90-
RunDiskPart("detach", vhdxPath);
91-
return Tuple.Create(success, checkResult);
92+
RunDiskPart("detach", vhdxPath);
93+
return (success, checkResult);
94+
}
9295
}
9396

9497
#if NETCOREAPP

Tests/LibraryTests/ExFat/ProcessUtility.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static class ProcessUtility
2121
/// <param name="waitForExit">if set to <c>true</c> [wait for exit].</param>
2222
/// <returns>Either an exit code or a PID (when waitForExit is false)</returns>
2323
/// <exception cref="ProcessStartInfo.FileName">The file specified in the command parameter <see cref="T:System.IO.FileNotFoundException" /> property could not be found.</exception>
24-
public static Tuple<int, string?> Run(string command, string arguments, string? input = null,
24+
public static (int pid, int exitCode, string? result) Run(string command, string arguments, string? input = null,
2525
bool waitForExit = true)
2626
{
2727
var process = new Process
@@ -50,7 +50,7 @@ public static class ProcessUtility
5050
}
5151
catch
5252
{
53-
return Tuple.Create(-1, (string?)null);
53+
return (-1, -1, null);
5454
}
5555

5656
if (input != null)
@@ -61,11 +61,11 @@ public static class ProcessUtility
6161

6262
if (!waitForExit)
6363
{
64-
return Tuple.Create(process.Id, (string?)null);
64+
return (process.Id, -1, null);
6565
}
6666

6767
process.BeginOutputReadLine();
6868
process.WaitForExit();
69-
return Tuple.Create(process.ExitCode, (string?)resultBuilder.ToString());
69+
return (-1, process.ExitCode, (string?)resultBuilder.ToString());
7070
}
7171
}

0 commit comments

Comments
 (0)