Skip to content

Commit 3cbac79

Browse files
committed
NTFS: NTFSVolume: Added ContentType enum to help identify the content of volume clusters being accessed
1 parent 04caa5f commit 3cbac79

File tree

6 files changed

+66
-14
lines changed

6 files changed

+66
-14
lines changed

DiskAccessLibrary/DiskAccessLibrary.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="FileSystems\NTFS\Enums\AttributeForm.cs" />
8686
<Compile Include="FileSystems\NTFS\Enums\AttributeType.cs" />
8787
<Compile Include="FileSystems\NTFS\Enums\CollationRule.cs" />
88+
<Compile Include="FileSystems\NTFS\Enums\ContentType.cs" />
8889
<Compile Include="FileSystems\NTFS\Enums\FileAttributes.cs" />
8990
<Compile Include="FileSystems\NTFS\Enums\FileNameFlags.cs" />
9091
<Compile Include="FileSystems\NTFS\Enums\FileRecordFlags.cs" />

DiskAccessLibrary/FileSystems/NTFS/AttributeData/NonResidentAttributeData.cs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ public class NonResidentAttributeData
1212
private NTFSVolume m_volume;
1313
private FileRecord m_fileRecord;
1414
private NonResidentAttributeRecord m_attributeRecord;
15+
private ContentType m_contentType;
1516

1617
public NonResidentAttributeData(NTFSVolume volume, FileRecord fileRecord, NonResidentAttributeRecord attributeRecord)
1718
{
1819
m_volume = volume;
1920
m_fileRecord = fileRecord;
2021
m_attributeRecord = attributeRecord;
22+
m_contentType = GetContentType(fileRecord, attributeRecord.AttributeType);
2123
}
2224

2325
/// <param name="clusterVCN">Cluster index</param>
@@ -68,7 +70,7 @@ public byte[] ReadSectors(long firstSectorIndex, int count)
6870
long bytesRead = 0;
6971
foreach (KeyValuePair<long, int> run in sequence)
7072
{
71-
byte[] data = m_volume.ReadSectors(run.Key, run.Value);
73+
byte[] data = m_volume.ReadSectors(run.Key, run.Value, m_contentType);
7274
Array.Copy(data, 0, result, bytesRead, data.Length);
7375
bytesRead += data.Length;
7476
}
@@ -164,7 +166,7 @@ public void WriteSectors(long firstSectorIndex, byte[] data)
164166
{
165167
byte[] sectors = new byte[run.Value * bytesPerSector];
166168
Array.Copy(data, bytesWritten, sectors, 0, sectors.Length);
167-
m_volume.WriteSectors(run.Key, sectors);
169+
m_volume.WriteSectors(run.Key, sectors, m_contentType);
168170
bytesWritten += sectors.Length;
169171
}
170172

@@ -324,5 +326,42 @@ public NonResidentAttributeRecord AttributeRecord
324326
return m_attributeRecord;
325327
}
326328
}
329+
330+
public static ContentType GetContentType(FileRecord fileRecord, AttributeType attributeType)
331+
{
332+
if (fileRecord != null)
333+
{
334+
long baseSegmentNumber = fileRecord.BaseSegmentNumber;
335+
if (baseSegmentNumber == MasterFileTable.MasterFileTableSegmentNumber || baseSegmentNumber == MasterFileTable.MftMirrorSegmentNumber)
336+
{
337+
return (attributeType == AttributeType.Data) ? ContentType.MftData : ContentType.MftBitmap;
338+
}
339+
else if (baseSegmentNumber == MasterFileTable.VolumeSegmentNumber)
340+
{
341+
return ContentType.VolumeBitmap;
342+
}
343+
}
344+
return GetContentType(attributeType);
345+
}
346+
347+
public static ContentType GetContentType(AttributeType attributeType)
348+
{
349+
if (attributeType == AttributeType.AttributeList)
350+
{
351+
return ContentType.MftData;
352+
}
353+
else if (attributeType == AttributeType.IndexAllocation)
354+
{
355+
return ContentType.IndexData;
356+
}
357+
else if (attributeType == AttributeType.Bitmap)
358+
{
359+
return ContentType.IndexBitmap;
360+
}
361+
else
362+
{
363+
return ContentType.FileData;
364+
}
365+
}
327366
}
328367
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
namespace DiskAccessLibrary.FileSystems.NTFS
3+
{
4+
public enum ContentType
5+
{
6+
FileData,
7+
IndexBitmap,
8+
IndexData,
9+
MftBitmap,
10+
MftData,
11+
VolumeBitmap,
12+
}
13+
}

DiskAccessLibrary/FileSystems/NTFS/MasterFileTable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ private FileRecordSegment ReadMftRecordSegment(long mftStartLCN, MftSegmentRefer
153153
private FileRecordSegment ReadMftRecordSegment(long mftStartLCN, long segmentNumber)
154154
{
155155
long sectorIndex = mftStartLCN * m_volume.SectorsPerCluster + segmentNumber * m_volume.SectorsPerFileRecordSegment;
156-
byte[] segmentBytes = m_volume.ReadSectors(sectorIndex, m_volume.SectorsPerFileRecordSegment);
156+
byte[] segmentBytes = m_volume.ReadSectors(sectorIndex, m_volume.SectorsPerFileRecordSegment, ContentType.MftData);
157157
MultiSectorHelper.RevertUsaProtection(segmentBytes, 0);
158158
FileRecordSegment result = new FileRecordSegment(segmentBytes, 0, segmentNumber);
159159
return result;

DiskAccessLibrary/FileSystems/NTFS/NTFSVolume.Extend.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ public void Extend(long numberOfAdditionalSectors)
3030

3131
// Update boot sector
3232
byte[] bootRecordBytes = m_bootRecord.GetBytes();
33-
WriteSectors(0, bootRecordBytes);
33+
WriteSectors(0, bootRecordBytes, ContentType.FileData);
3434

3535
// Recreate the backup boot sector at the new end of the raw volume
3636
// Note: The backup boot sector does not count as part of the NTFS volume
3737
long backupBootSectorIndex = originalNumberOfSectors + numberOfAdditionalSectors;
38-
WriteSectors(backupBootSectorIndex, bootRecordBytes);
38+
WriteSectors(backupBootSectorIndex, bootRecordBytes, ContentType.FileData);
3939
}
4040
}
4141
}

DiskAccessLibrary/FileSystems/NTFS/NTFSVolume.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -332,13 +332,12 @@ internal void UpdateDirectoryIndex(MftSegmentReference parentDirectory, List<Fil
332332
}
333333
}
334334

335-
// logical cluster
336-
protected internal byte[] ReadCluster(long clusterLCN)
335+
protected internal byte[] ReadCluster(long clusterLCN, ContentType contentType)
337336
{
338-
return ReadClusters(clusterLCN, 1);
337+
return ReadClusters(clusterLCN, 1, contentType);
339338
}
340339

341-
protected internal virtual byte[] ReadClusters(long clusterLCN, int count)
340+
protected internal virtual byte[] ReadClusters(long clusterLCN, int count, ContentType contentType)
342341
{
343342
long firstSectorIndex = clusterLCN * m_bootRecord.SectorsPerCluster;
344343
int sectorsToRead = m_bootRecord.SectorsPerCluster * count;
@@ -348,18 +347,18 @@ protected internal virtual byte[] ReadClusters(long clusterLCN, int count)
348347
return result;
349348
}
350349

351-
protected internal virtual void WriteClusters(long clusterLCN, byte[] data)
350+
protected internal virtual void WriteClusters(long clusterLCN, byte[] data, ContentType contentType)
352351
{
353352
long firstSectorIndex = clusterLCN * m_bootRecord.SectorsPerCluster;
354353
m_volume.WriteSectors(firstSectorIndex, data);
355354
}
356355

357-
protected internal virtual byte[] ReadSectors(long sectorIndex, int sectorCount)
356+
protected internal virtual byte[] ReadSectors(long sectorIndex, int sectorCount, ContentType contentType)
358357
{
359358
return m_volume.ReadSectors(sectorIndex, sectorCount);
360359
}
361360

362-
protected internal virtual void WriteSectors(long sectorIndex, byte[] data)
361+
protected internal virtual void WriteSectors(long sectorIndex, byte[] data, ContentType contentType)
363362
{
364363
m_volume.WriteSectors(sectorIndex, data);
365364
}
@@ -416,9 +415,9 @@ public override string ToString()
416415
builder.AppendLine("Length of Volume Bitmap Attributes: " + volumeBitmapRecord.AttributesLengthOnDisk);
417416
}
418417

419-
byte[] bootRecord = ReadSectors(0, 1);
418+
byte[] bootRecord = ReadSectors(0, 1, ContentType.FileData);
420419
long backupBootSectorIndex = (long)m_bootRecord.TotalSectors;
421-
byte[] backupBootRecord = ReadSectors(backupBootSectorIndex, 1);
420+
byte[] backupBootRecord = ReadSectors(backupBootSectorIndex, 1, ContentType.FileData);
422421
builder.AppendLine();
423422
builder.AppendLine("Valid backup boot sector: " + ByteUtils.AreByteArraysEqual(bootRecord, backupBootRecord));
424423
builder.AppendLine("Free space: " + this.FreeSpace);

0 commit comments

Comments
 (0)