Skip to content

Commit a222a2c

Browse files
committed
NTFS: NTFSVolume: Improved free space checking before creating / moving a file
1 parent d30f70b commit a222a2c

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

DiskAccessLibrary/FileSystems/NTFS/NTFSVolume.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public partial class NTFSVolume : IExtendableFileSystem
3333
private object m_bitmapLock = new object();
3434
private VolumeInformationRecord m_volumeInformation;
3535
private readonly bool GenerateDosNames = false;
36+
private readonly int NumberOfClustersRequiredToExtendIndex;
3637

3738
public NTFSVolume(Volume volume) : this(volume, false)
3839
{
@@ -63,6 +64,7 @@ public NTFSVolume(Volume volume, bool useMftMirror)
6364
m_logFile = new LogFile(this);
6465
m_logClient = new NTFSLogClient(m_logFile);
6566
m_bitmap = new VolumeBitmap(this);
67+
NumberOfClustersRequiredToExtendIndex = (int)Math.Ceiling((double)(IndexData.ExtendGranularity * m_bootRecord.BytesPerIndexRecord) / m_bootRecord.BytesPerCluster);
6668
}
6769

6870
public virtual FileRecord GetFileRecord(string path)
@@ -137,8 +139,9 @@ public virtual FileRecord CreateFile(MftSegmentReference parentDirectory, string
137139
throw new InvalidNameException();
138140
}
139141

140-
// Worst case scenrario: the MFT might be full and the parent directory index requires multiple splits
141-
if (NumberOfFreeClusters < m_mft.NumberOfClustersRequiredToExtend + 8)
142+
// Worst case scenrario: the MFT might be full and the parent directory index requires multiple splits.
143+
// We assume IndexData.ExtendGranularity is bigger than or equal to the number of splits.
144+
if (NumberOfFreeClusters < m_mft.NumberOfClustersRequiredToExtend + NumberOfClustersRequiredToExtendIndex)
142145
{
143146
throw new DiskFullException();
144147
}
@@ -196,8 +199,9 @@ protected internal void UpdateFileRecord(FileRecord fileRecord, uint? transactio
196199

197200
public virtual void MoveFile(FileRecord fileRecord, MftSegmentReference newParentDirectory, string newFileName)
198201
{
199-
// Worst case scenrario: the new parent directory index requires multiple splits
200-
if (NumberOfFreeClusters < 8)
202+
// Worst case scenrario: the new parent directory index requires multiple splits.
203+
// We assume IndexData.ExtendGranularity is bigger than or equal to the number of splits.
204+
if (NumberOfFreeClusters < NumberOfClustersRequiredToExtendIndex)
201205
{
202206
throw new DiskFullException();
203207
}

0 commit comments

Comments
 (0)