Skip to content

Commit 3ff87ff

Browse files
Clear clusters when directory is created (#56)
1 parent a983efd commit 3ff87ff

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

Library/DiscUtils.Fat/Directory.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,15 @@ private void PopulateNewChildDirectory(DirectoryEntry newEntry)
532532
// Populate new directory with initial (special) entries. First one is easy, just change the name!
533533
using var stream = new ClusterStream(FileSystem, FileAccess.Write, newEntry.FirstCluster,
534534
uint.MaxValue);
535+
536+
// Clear cluster with zeroes in case it contains existing data from quick formatting
537+
var blankCluster = new byte[FileSystem.ClusterSize];
538+
for (var cluster = 0; cluster < stream.Length / FileSystem.ClusterSize; cluster++)
539+
{
540+
stream.Write(blankCluster);
541+
}
542+
stream.Position = 0;
543+
535544
// First is the self-referencing entry...
536545
var selfEntry = new DirectoryEntry(newEntry, FatFileName.SelfEntryName);
537546
selfEntry.WriteTo(stream, FileSystem.FatOptions.FileNameEncodingTable);

Tests/LibraryTests/Fat/FatFileSystemTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,4 +582,31 @@ public void TestFindPosition()
582582

583583
Assert.True(pattern.SequenceEqual(buffer));
584584
}
585+
586+
[Fact]
587+
public void CreateDirectoryWithExistingData()
588+
{
589+
const int highDensitySize = 1474560;
590+
using var diskStream = new SparseMemoryStream();
591+
592+
byte[] existingData = [
593+
0x00, 0x00, 0x00, 0x4E, 0x00, 0x0A, 0x7B, 0x9B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
594+
0x00, 0x0A, 0x7B, 0xE9, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0A, 0x7B, 0xEB,
595+
];
596+
597+
for (var i = 0; i < 1474560 / existingData.Length; i++)
598+
{
599+
diskStream.Seek(i * existingData.Length, SeekOrigin.Begin);
600+
diskStream.Write(existingData, 0, existingData.Length);
601+
}
602+
603+
diskStream.Position = 0;
604+
using var fs = FatFileSystem.FormatFloppy(diskStream, FloppyDiskType.HighDensity, "FLOPPY_IMG ");
605+
606+
fs.CreateDirectory("dir");
607+
608+
var entries = fs.GetFileSystemEntries("dir").ToList();
609+
610+
Assert.Empty(entries);
611+
}
585612
}

0 commit comments

Comments
 (0)