Skip to content

Commit ee9f706

Browse files
committed
NTFS: Clone attributes when assembled from and splitted to FileRecordSegments
1 parent a8fb6af commit ee9f706

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

DiskAccessLibrary/FileSystems/NTFS/FileRecord/FileRecord.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void UpdateSegments(int bytesPerFileRecordSegment, byte minorNTFSVersion)
5555
// A single record segment is needed
5656
foreach (AttributeRecord attribute in attributes)
5757
{
58-
m_segments[0].AddAttributeRecord(attribute);
58+
m_segments[0].AddAttributeRecord(attribute.Clone());
5959
}
6060

6161
// Free the rest of the segments, if there are any
@@ -79,7 +79,12 @@ private List<AttributeRecord> GetAttributes()
7979
}
8080
else
8181
{
82-
return new List<AttributeRecord>(m_segments[0].ImmediateAttributes);
82+
List<AttributeRecord> result = new List<AttributeRecord>();
83+
foreach (AttributeRecord attribute in m_segments[0].ImmediateAttributes)
84+
{
85+
result.Add(attribute.Clone());
86+
}
87+
return result;
8388
}
8489
}
8590

DiskAccessLibrary/FileSystems/NTFS/FileRecord/FileRecordHelper.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public static List<AttributeRecord> GetAssembledAttributes(List<FileRecordSegmen
4848

4949
if (attribute is ResidentAttributeRecord)
5050
{
51-
result.Add(attribute);
51+
result.Add(attribute.Clone());
5252
}
5353
else
5454
{
@@ -102,7 +102,7 @@ private static NonResidentAttributeRecord AssembleFragments(List<NonResidentAttr
102102
long relativeOffset = absoluteOffset - previousLCN;
103103

104104
int runIndex = attribute.DataRunSequence.Count;
105-
attribute.DataRunSequence.AddRange(attributeFragment.DataRunSequence);
105+
attribute.DataRunSequence.AddRange(attributeFragment.DataRunSequence.Clone());
106106
attribute.DataRunSequence[runIndex] = new DataRun(runLength, relativeOffset);
107107
attribute.HighestVCN = attributeFragment.HighestVCN;
108108
}
@@ -127,7 +127,7 @@ public static void SliceAttributes(List<FileRecordSegment> segments, List<Attrib
127127
if (attribute.AttributeType == AttributeType.StandardInformation ||
128128
attribute.AttributeType == AttributeType.FileName)
129129
{
130-
baseFileRecordSegment.AddAttributeRecord(attribute);
130+
baseFileRecordSegment.AddAttributeRecord(attribute.Clone());
131131
}
132132
else if (isMftFileRecord && attribute.AttributeType == AttributeType.Data)
133133
{
@@ -163,6 +163,10 @@ public static void SliceAttributes(List<FileRecordSegment> segments, List<Attrib
163163
if (attribute.RecordLength <= remainingLengthInCurrentSegment)
164164
{
165165
remainingLengthInCurrentSegment -= (int)attribute.RecordLength;
166+
if (attribute is ResidentAttributeRecord)
167+
{
168+
attribute = attribute.Clone();
169+
}
166170
segments[segmentIndex].AddAttributeRecord(attribute);
167171
remainingAttributes.RemoveFirst();
168172
}
@@ -226,7 +230,7 @@ private static NonResidentAttributeRecord FitMaxNumberOfRuns(NonResidentAttribut
226230
slice.AllocatedLength = record.AllocatedLength;
227231
slice.FileSize = record.FileSize;
228232
slice.ValidDataLength = record.ValidDataLength;
229-
slice.DataRunSequence.Add(dataRuns[runIndex]);
233+
slice.DataRunSequence.Add(dataRuns[runIndex].Clone());
230234
}
231235
else
232236
{
@@ -246,7 +250,7 @@ private static NonResidentAttributeRecord FitMaxNumberOfRuns(NonResidentAttribut
246250
runIndex++;
247251
while (runIndex < dataRuns.Count && sliceRecordLength + dataRuns[runIndex].RecordLength <= availableLength)
248252
{
249-
slice.DataRunSequence.Add(record.DataRunSequence[runIndex]);
253+
slice.DataRunSequence.Add(record.DataRunSequence[runIndex].Clone());
250254
sliceRecordLength += dataRuns[runIndex].RecordLength;
251255
clusterCount += dataRuns[runIndex].RunLength;
252256
runIndex++;

0 commit comments

Comments
 (0)