Skip to content

Commit

Permalink
Fixed PathToExtents to FatFileSystem
Browse files Browse the repository at this point in the history
  • Loading branch information
LTRData committed Nov 20, 2024
1 parent 1e35f07 commit eb1def3
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 5 deletions.
5 changes: 5 additions & 0 deletions Library/DiscUtils.Fat/ClusterStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -638,5 +638,10 @@ public IEnumerable<Range<long, long>> EnumerateAllocatedClusters()
firstCluster = null;
lastCluster = null;
}

if (firstCluster.HasValue && lastCluster.HasValue)
{
yield return new(firstCluster.Value, lastCluster.Value - firstCluster.Value + 1);
}
}
}
3 changes: 2 additions & 1 deletion Library/DiscUtils.Fat/FatFileStream.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,6 @@ private void FirstClusterAllocatedHandler(uint cluster)
_dir.UpdateEntry(_dirId, dirEntry);
}

public IEnumerable<Range<long, long>> EnumerateAllocatedClusters() => _stream.EnumerateAllocatedClusters();
public IEnumerable<Range<long, long>> EnumerateAllocatedClusters()
=> _stream.EnumerateAllocatedClusters();
}
8 changes: 7 additions & 1 deletion Library/DiscUtils.Fat/FatFileSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,13 @@ public override SparseStream OpenFile(string path, FileMode mode, FileAccess acc
}

public IEnumerable<StreamExtent> 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<Range<long, long>> PathToClusters(string path)
{
Expand Down
8 changes: 7 additions & 1 deletion Tests/LibraryTests/Fat/FatFileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> buffer = stackalloc byte[pattern.Length];
Expand All @@ -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();
Expand Down
16 changes: 14 additions & 2 deletions Tests/LibraryTests/Ntfs/NtfsFileSystemTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<byte> buffer = stackalloc byte[pattern.Length];
Expand All @@ -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();
Expand Down Expand Up @@ -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];
Expand All @@ -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();
Expand Down

0 comments on commit eb1def3

Please sign in to comment.