public void Truncate(ulong newLengthInBytes) { long clustersToKeep = (long)Math.Ceiling((double)newLengthInBytes / m_volume.BytesPerCluster); if (clustersToKeep < ClusterCount) { KeyValuePairList <long, long> clustersToDeallocate = m_attributeRecord.DataRunSequence.TranslateToLCN(clustersToKeep, ClusterCount - clustersToKeep); m_attributeRecord.DataRunSequence.Truncate(clustersToKeep); m_attributeRecord.HighestVCN = clustersToKeep - 1; m_attributeRecord.AllocatedLength = (ulong)(clustersToKeep * m_volume.BytesPerCluster); foreach (KeyValuePair <long, long> runToDeallocate in clustersToDeallocate) { m_volume.DeallocateClusters(runToDeallocate.Key, runToDeallocate.Value); } } m_attributeRecord.FileSize = newLengthInBytes; if (m_attributeRecord.ValidDataLength > newLengthInBytes) { m_attributeRecord.ValidDataLength = newLengthInBytes; } if (m_fileRecord != null) { m_volume.UpdateFileRecord(m_fileRecord); } }
public void Truncate(ulong newLengthInBytes) { long clustersToKeep = (long)Math.Ceiling((double)newLengthInBytes / m_volume.BytesPerCluster); if (clustersToKeep < ClusterCount) { KeyValuePairList <long, long> clustersToDeallocate = m_attributeRecord.DataRunSequence.TranslateToLCN(clustersToKeep, ClusterCount - clustersToKeep); m_attributeRecord.DataRunSequence.Truncate(clustersToKeep); m_attributeRecord.HighestVCN = clustersToKeep - 1; m_attributeRecord.AllocatedLength = (ulong)(clustersToKeep * m_volume.BytesPerCluster); foreach (KeyValuePair <long, long> runToDeallocate in clustersToDeallocate) { m_volume.DeallocateClusters(runToDeallocate.Key, runToDeallocate.Value); } } m_attributeRecord.FileSize = newLengthInBytes; if (m_attributeRecord.ValidDataLength > newLengthInBytes) { m_attributeRecord.ValidDataLength = newLengthInBytes; } if (m_fileRecord != null) { if (m_attributeRecord.AttributeType == AttributeType.Data && m_attributeRecord.Name == String.Empty) { // Windows NTFS v5.1 driver updates the value of the AllocatedLength field but does not usually update the value of // the FileSize field belonging to the FileNameRecords that are stored in the FileRecord, which is likely to be 0. List <FileNameRecord> fileNameRecords = m_fileRecord.FileNameRecords; foreach (FileNameRecord fileNameRecord in fileNameRecords) { fileNameRecord.AllocatedLength = m_attributeRecord.AllocatedLength; } } m_volume.UpdateFileRecord(m_fileRecord); } }