public void UpdateFileRecord(FileRecord fileRecord) { AttributeRecord oldAttributeList = fileRecord.BaseSegment.GetImmediateAttributeRecord(AttributeType.AttributeList, String.Empty); fileRecord.UpdateSegments(m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion); FileRecordSegment baseSegment = fileRecord.BaseSegment; for (int segmentIndex = 1; segmentIndex < fileRecord.Segments.Count; segmentIndex++) { FileRecordSegment segment = fileRecord.Segments[segmentIndex]; if (segment.SegmentReference == MftSegmentReference.NullReference) { // New segment, we must allocate space for it MftSegmentReference segmentReference; if (baseSegment.SegmentNumber == MasterFileTable.MasterFileTableSegmentNumber) { segmentReference = AllocateReservedFileRecordSegment(); } else { segmentReference = AllocateFileRecordSegment(); } FileRecordSegment newSegment = new FileRecordSegment(segmentReference.SegmentNumber, segmentReference.SequenceNumber, baseSegment.SegmentReference); newSegment.IsInUse = true; newSegment.IsDirectory = fileRecord.IsDirectory; newSegment.NextAttributeInstance = segment.NextAttributeInstance; newSegment.ImmediateAttributes.AddRange(segment.ImmediateAttributes); fileRecord.Segments[segmentIndex] = newSegment; } else if (segment.ImmediateAttributes.Count == 0) { DeallocateFileRecordSegment(segment); fileRecord.Segments.RemoveAt(segmentIndex); segmentIndex--; } } foreach (FileRecordSegment segment in fileRecord.Segments) { UpdateFileRecordSegment(segment); } if (oldAttributeList is NonResidentAttributeRecord) { new NonResidentAttributeData(m_volume, null, (NonResidentAttributeRecord)oldAttributeList).Truncate(0); } bool needsAttributeList = (fileRecord.Segments.Count > 1); if (needsAttributeList) { List <AttributeListEntry> entries = FileRecordHelper.BuildAttributeList(fileRecord.Segments, m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion); int dataLength = AttributeList.GetLength(entries); int attributeListRecordLength = ResidentAttributeRecord.GetRecordLength(0, dataLength); int numberOfBytesFreeInBaseSegment = baseSegment.GetNumberOfBytesFree(m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion); bool isResident = (attributeListRecordLength <= numberOfBytesFreeInBaseSegment); AttributeRecord attributeListRecord = baseSegment.CreateAttributeListRecord(isResident); AttributeList attributeList = new AttributeList(m_volume, attributeListRecord); attributeList.WriteEntries(entries); UpdateFileRecordSegment(baseSegment); } }
public void UpdateFileRecord(FileRecord fileRecord, uint transactionID) { Dictionary <MftSegmentReference, byte[]> undoDictionary = new Dictionary <MftSegmentReference, byte[]>(); foreach (FileRecordSegment segment in fileRecord.Segments) { byte[] segmentBytes = segment.GetBytes(m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion, false); undoDictionary.Add(segment.SegmentReference, segmentBytes); } AttributeRecord oldAttributeList = fileRecord.BaseSegment.GetImmediateAttributeRecord(AttributeType.AttributeList, String.Empty); fileRecord.UpdateSegments(m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion); FileRecordSegment baseSegment = fileRecord.BaseSegment; for (int segmentIndex = 1; segmentIndex < fileRecord.Segments.Count; segmentIndex++) { FileRecordSegment segment = fileRecord.Segments[segmentIndex]; if (segment.SegmentReference == MftSegmentReference.NullReference) { // New segment, we must allocate space for it MftSegmentReference segmentReference; if (baseSegment.SegmentNumber == MasterFileTable.MasterFileTableSegmentNumber) { segmentReference = AllocateReservedFileRecordSegment(transactionID); } else { segmentReference = AllocateFileRecordSegment(transactionID); } FileRecordSegment newSegment = new FileRecordSegment(segmentReference.SegmentNumber, segmentReference.SequenceNumber, baseSegment.SegmentReference); newSegment.IsInUse = true; newSegment.IsDirectory = fileRecord.IsDirectory; newSegment.NextAttributeInstance = segment.NextAttributeInstance; newSegment.ImmediateAttributes.AddRange(segment.ImmediateAttributes); fileRecord.Segments[segmentIndex] = newSegment; } else if (segment.ImmediateAttributes.Count == 0) { byte[] undoData = undoDictionary[segment.SegmentReference]; ulong streamOffset = (ulong)(segment.SegmentNumber * m_volume.BytesPerFileRecordSegment); m_volume.LogClient.WriteLogRecord(m_mftRecord.BaseSegmentReference, m_mftRecord.DataRecord, streamOffset, m_volume.BytesPerFileRecordSegment, NTFSLogOperation.DeallocateFileRecordSegment, new byte[0], NTFSLogOperation.InitializeFileRecordSegment, undoData, transactionID); DeallocateFileRecordSegment(segment, transactionID); fileRecord.Segments.RemoveAt(segmentIndex); segmentIndex--; } } for (int segmentIndex = 1; segmentIndex < fileRecord.Segments.Count; segmentIndex++) { FileRecordSegment segment = fileRecord.Segments[segmentIndex]; byte[] undoData; byte[] redoData = segment.GetBytes(m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion, false); ulong streamOffset = (ulong)(segment.SegmentNumber * m_volume.BytesPerFileRecordSegment); if (undoDictionary.TryGetValue(segment.SegmentReference, out undoData)) { m_volume.LogClient.WriteLogRecord(m_mftRecord.BaseSegmentReference, m_mftRecord.DataRecord, streamOffset, m_volume.BytesPerFileRecordSegment, NTFSLogOperation.InitializeFileRecordSegment, redoData, NTFSLogOperation.InitializeFileRecordSegment, undoData, transactionID); } else { // New segment m_volume.LogClient.WriteLogRecord(m_mftRecord.BaseSegmentReference, m_mftRecord.DataRecord, streamOffset, m_volume.BytesPerFileRecordSegment, NTFSLogOperation.InitializeFileRecordSegment, redoData, NTFSLogOperation.DeallocateFileRecordSegment, new byte[0], transactionID); } UpdateFileRecordSegment(segment); } if (oldAttributeList is NonResidentAttributeRecord) { new NonResidentAttributeData(m_volume, null, (NonResidentAttributeRecord)oldAttributeList).Truncate(0); } bool needsAttributeList = (fileRecord.Segments.Count > 1); if (needsAttributeList) { List <AttributeListEntry> entries = FileRecordHelper.BuildAttributeList(fileRecord.Segments, m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion); int dataLength = AttributeList.GetLength(entries); int attributeListRecordLength = ResidentAttributeRecord.GetRecordLength(0, dataLength); int numberOfBytesFreeInBaseSegment = baseSegment.GetNumberOfBytesFree(m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion); bool isResident = (attributeListRecordLength <= numberOfBytesFreeInBaseSegment); AttributeRecord attributeListRecord = baseSegment.CreateAttributeListRecord(isResident); AttributeList attributeList = new AttributeList(m_volume, attributeListRecord); attributeList.WriteEntries(entries); } byte[] baseRecordUndoData = undoDictionary[baseSegment.SegmentReference]; byte[] baseRecordRedoData = baseSegment.GetBytes(m_volume.BytesPerFileRecordSegment, m_volume.MinorVersion, false); ulong baseRecordStreamOffset = (ulong)(baseSegment.SegmentNumber * m_volume.BytesPerFileRecordSegment); m_volume.LogClient.WriteLogRecord(m_mftRecord.BaseSegmentReference, m_mftRecord.DataRecord, baseRecordStreamOffset, m_volume.BytesPerFileRecordSegment, NTFSLogOperation.InitializeFileRecordSegment, baseRecordRedoData, NTFSLogOperation.InitializeFileRecordSegment, baseRecordUndoData, transactionID); UpdateFileRecordSegment(baseSegment); }