@@ -24,6 +24,7 @@ namespace DiskAccessLibrary.FileSystems.NTFS
2424 public partial class NTFSVolume : IExtendableFileSystem
2525 {
2626 private Volume m_volume ;
27+ private bool m_isReadOnly ;
2728 private NTFSBootRecord m_bootRecord ; // Partition's boot record
2829 private MasterFileTable m_mft ;
2930 private LogFile m_logFile ;
@@ -39,9 +40,14 @@ public NTFSVolume(Volume volume) : this(volume, false)
3940 {
4041 }
4142
42- public NTFSVolume ( Volume volume , bool useMftMirror )
43+ public NTFSVolume ( Volume volume , bool isReadOnly ) : this ( volume , isReadOnly , false )
44+ {
45+ }
46+
47+ public NTFSVolume ( Volume volume , bool isReadOnly , bool useMftMirror )
4348 {
4449 m_volume = volume ;
50+ m_isReadOnly = volume . IsReadOnly || isReadOnly ;
4551
4652 byte [ ] bootSector = m_volume . ReadSector ( 0 ) ;
4753 m_bootRecord = NTFSBootRecord . ReadRecord ( bootSector ) ;
@@ -358,13 +364,16 @@ protected internal virtual byte[] ReadClusters(long clusterLCN, int count, Conte
358364 long firstSectorIndex = clusterLCN * m_bootRecord . SectorsPerCluster ;
359365 int sectorsToRead = m_bootRecord . SectorsPerCluster * count ;
360366
361- byte [ ] result = m_volume . ReadSectors ( firstSectorIndex , sectorsToRead ) ;
362-
363- return result ;
367+ return m_volume . ReadSectors ( firstSectorIndex , sectorsToRead ) ;
364368 }
365369
366370 protected internal virtual void WriteClusters ( long clusterLCN , byte [ ] data , ContentType contentType )
367371 {
372+ if ( IsReadOnly )
373+ {
374+ throw new UnauthorizedAccessException ( "Attempted to perform write on a filesystem mounted for readonly access" ) ;
375+ }
376+
368377 long firstSectorIndex = clusterLCN * m_bootRecord . SectorsPerCluster ;
369378 m_volume . WriteSectors ( firstSectorIndex , data ) ;
370379 }
@@ -376,6 +385,11 @@ protected internal virtual byte[] ReadSectors(long sectorIndex, int sectorCount,
376385
377386 protected internal virtual void WriteSectors ( long sectorIndex , byte [ ] data , ContentType contentType )
378387 {
388+ if ( IsReadOnly )
389+ {
390+ throw new UnauthorizedAccessException ( "Attempted to perform write on a filesystem mounted for readonly access" ) ;
391+ }
392+
379393 m_volume . WriteSectors ( sectorIndex , data ) ;
380394 }
381395
@@ -492,7 +506,7 @@ public bool IsReadOnly
492506 {
493507 get
494508 {
495- return m_volume . IsReadOnly ;
509+ return m_isReadOnly ;
496510 }
497511 }
498512
0 commit comments