Skip to content

Commit d34a769

Browse files
committed
NTFSVolume: Added ability to mount as readonly
1 parent 16dcc80 commit d34a769

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

DiskAccessLibrary/FileSystems/FileSystemHelper.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* Copyright (C) 2014-2016 Tal Aloni <[email protected]>. All rights reserved.
1+
/* Copyright (C) 2014-2019 Tal Aloni <[email protected]>. All rights reserved.
22
*
33
* You can redistribute this program and/or modify it under the terms of
44
* the GNU Lesser Public License as published by the Free Software Foundation,
@@ -15,10 +15,15 @@ namespace DiskAccessLibrary.FileSystems
1515
public class FileSystemHelper
1616
{
1717
public static FileSystem ReadFileSystem(Volume volume)
18+
{
19+
return ReadFileSystem(volume, false);
20+
}
21+
22+
public static FileSystem ReadFileSystem(Volume volume, bool isReadOnly)
1823
{
1924
try
2025
{
21-
return new NTFSFileSystem(volume);
26+
return new NTFSFileSystem(volume, isReadOnly);
2227
}
2328
catch (InvalidDataException)
2429
{

DiskAccessLibrary/FileSystems/NTFS/Adapters/NTFSFileSystem.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ public class NTFSFileSystem : FileSystem, IExtendableFileSystem
1919
private NTFSVolume m_volume;
2020
private Dictionary<long, List<NTFSFileStream>> m_openStreams = new Dictionary<long, List<NTFSFileStream>>();
2121

22-
public NTFSFileSystem(Volume volume)
22+
public NTFSFileSystem(Volume volume, bool isReadOnly)
2323
{
24-
m_volume = new NTFSVolume(volume);
24+
m_volume = new NTFSVolume(volume, isReadOnly);
2525
}
2626

2727
public NTFSFileSystem(NTFSVolume volume)

DiskAccessLibrary/FileSystems/NTFS/NTFSVolume.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)