From eb1def3f3a221a8fda80c5c09667f3d9c2c1bf22 Mon Sep 17 00:00:00 2001 From: LTRData Date: Wed, 20 Nov 2024 20:40:57 +0100 Subject: [PATCH] Fixed PathToExtents to FatFileSystem --- Library/DiscUtils.Fat/ClusterStream.cs | 5 +++++ Library/DiscUtils.Fat/FatFileStream.cs | 3 ++- Library/DiscUtils.Fat/FatFileSystem.cs | 8 +++++++- Tests/LibraryTests/Fat/FatFileSystemTest.cs | 8 +++++++- Tests/LibraryTests/Ntfs/NtfsFileSystemTest.cs | 16 ++++++++++++++-- 5 files changed, 35 insertions(+), 5 deletions(-) diff --git a/Library/DiscUtils.Fat/ClusterStream.cs b/Library/DiscUtils.Fat/ClusterStream.cs index 0113e090..a50c94e1 100644 --- a/Library/DiscUtils.Fat/ClusterStream.cs +++ b/Library/DiscUtils.Fat/ClusterStream.cs @@ -638,5 +638,10 @@ public IEnumerable> EnumerateAllocatedClusters() firstCluster = null; lastCluster = null; } + + if (firstCluster.HasValue && lastCluster.HasValue) + { + yield return new(firstCluster.Value, lastCluster.Value - firstCluster.Value + 1); + } } } \ No newline at end of file diff --git a/Library/DiscUtils.Fat/FatFileStream.cs b/Library/DiscUtils.Fat/FatFileStream.cs index 870f26d0..42719e74 100644 --- a/Library/DiscUtils.Fat/FatFileStream.cs +++ b/Library/DiscUtils.Fat/FatFileStream.cs @@ -177,5 +177,6 @@ private void FirstClusterAllocatedHandler(uint cluster) _dir.UpdateEntry(_dirId, dirEntry); } - public IEnumerable> EnumerateAllocatedClusters() => _stream.EnumerateAllocatedClusters(); + public IEnumerable> EnumerateAllocatedClusters() + => _stream.EnumerateAllocatedClusters(); } \ No newline at end of file diff --git a/Library/DiscUtils.Fat/FatFileSystem.cs b/Library/DiscUtils.Fat/FatFileSystem.cs index 455c4909..9db82127 100644 --- a/Library/DiscUtils.Fat/FatFileSystem.cs +++ b/Library/DiscUtils.Fat/FatFileSystem.cs @@ -410,7 +410,13 @@ public override SparseStream OpenFile(string path, FileMode mode, FileAccess acc } public IEnumerable PathToExtents(string path) - => PathToClusters(path).Select(range => new StreamExtent(range.Offset * ClusterSize, range.Count * ClusterSize)); + { + var stream = (FatFileStream)OpenFile(path, FileMode.Open, FileAccess.Read); + + return stream + .EnumerateAllocatedClusters() + .Select(range => new StreamExtent(ClusterReader.GetBaseStreamPositionForCluster((uint)range.Offset), ClusterSize * range.Count)); + } public IEnumerable> PathToClusters(string path) { diff --git a/Tests/LibraryTests/Fat/FatFileSystemTest.cs b/Tests/LibraryTests/Fat/FatFileSystemTest.cs index 0a5d02b2..af6753c9 100644 --- a/Tests/LibraryTests/Fat/FatFileSystemTest.cs +++ b/Tests/LibraryTests/Fat/FatFileSystemTest.cs @@ -477,6 +477,12 @@ public void TestFindPosition() file.Write(pattern); } + var firstExt = fs + .PathToExtents("Test.txt") + .FirstOrDefault(); + + Assert.NotEqual(0, firstExt.Start); + long? locationOnDisk; Span buffer = stackalloc byte[pattern.Length]; @@ -492,7 +498,7 @@ public void TestFindPosition() } Assert.NotNull(locationOnDisk); - Assert.NotEqual(0, locationOnDisk.Value); + Assert.Equal(firstExt.Start, locationOnDisk.Value); diskStream.Position = locationOnDisk.Value; buffer.Clear(); diff --git a/Tests/LibraryTests/Ntfs/NtfsFileSystemTest.cs b/Tests/LibraryTests/Ntfs/NtfsFileSystemTest.cs index 76c329ad..e14ebee6 100644 --- a/Tests/LibraryTests/Ntfs/NtfsFileSystemTest.cs +++ b/Tests/LibraryTests/Ntfs/NtfsFileSystemTest.cs @@ -877,6 +877,12 @@ public void TestFindPositionSmallFile() file.Write(pattern); } + var firstExt = fs + .PathToExtents("Test.txt") + .FirstOrDefault(); + + Assert.NotEqual(0, firstExt.Start); + long? locationOnDisk; Span buffer = stackalloc byte[pattern.Length]; @@ -892,7 +898,7 @@ public void TestFindPositionSmallFile() } Assert.NotNull(locationOnDisk); - Assert.NotEqual(0, locationOnDisk.Value); + Assert.Equal(firstExt.Start, locationOnDisk.Value); diskStream.Position = locationOnDisk.Value; buffer.Clear(); @@ -920,6 +926,12 @@ public void TestFindPositionBigFile() file.Write(pattern); } + var firstExt = fs + .PathToExtents("Test.txt") + .FirstOrDefault(); + + Assert.NotEqual(0, firstExt.Start); + long? locationOnDisk; var buffer = new byte[pattern.Length]; @@ -935,7 +947,7 @@ public void TestFindPositionBigFile() } Assert.NotNull(locationOnDisk); - Assert.NotEqual(0, locationOnDisk.Value); + Assert.Equal(firstExt.Start, locationOnDisk.Value); diskStream.Position = locationOnDisk.Value; buffer.AsSpan().Clear();