|
@@ -27,20 +27,17 @@ namespace DiskAccessLibrary
|
|
public BlockDifferencingDiskImage(string diskImagePath, bool isReadOnly) : base(diskImagePath, isReadOnly)
|
|
public BlockDifferencingDiskImage(string diskImagePath, bool isReadOnly) : base(diskImagePath, isReadOnly)
|
|
{
|
|
{
|
|
BddInfo = new BddInfo(diskImagePath);
|
|
BddInfo = new BddInfo(diskImagePath);
|
|
|
|
+ IsBlockAllocated = new IReadOnlyIndexer<int, bool>(i => BddInfo.Allocated[i]);
|
|
|
|
+ }
|
|
|
|
|
|
- if (isReadOnly)
|
|
|
|
- {
|
|
|
|
- if (null != BddInfo.BasedImagePath) _basedFileStream = File.OpenRead(BddInfo.BasedImagePath);
|
|
|
|
- _snapshotFileStream = File.OpenRead(Path);
|
|
|
|
|
|
+ public void OpenForRead()
|
|
|
|
+ {
|
|
|
|
+ _snapshotFileStream = File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
|
+ }
|
|
|
|
|
|
- IsBlockAllocated = new IReadOnlyIndexer<int, bool>(i => BddInfo.Allocated[i]);
|
|
|
|
- }
|
|
|
|
- else
|
|
|
|
- {
|
|
|
|
- if (null != BddInfo.BasedImagePath) _basedFileStream = File.OpenRead(BddInfo.BasedImagePath);
|
|
|
|
- _snapshotFileStream = File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
|
|
|
|
- _isExclusiveLock = true;
|
|
|
|
- }
|
|
|
|
|
|
+ public void CloseForRead()
|
|
|
|
+ {
|
|
|
|
+ _snapshotFileStream?.Close();
|
|
}
|
|
}
|
|
|
|
|
|
public void ReadBlock(byte[] buffer, int offset, int count, int blockIndex, int blockOffset)
|
|
public void ReadBlock(byte[] buffer, int offset, int count, int blockIndex, int blockOffset)
|
|
@@ -199,14 +196,32 @@ namespace DiskAccessLibrary
|
|
_basedFileStream?.Close();
|
|
_basedFileStream?.Close();
|
|
_snapshotFileStream?.Close();
|
|
_snapshotFileStream?.Close();
|
|
|
|
|
|
- _basedFileStream = File.OpenRead(BddInfo.BasedImagePath);
|
|
|
|
- _snapshotFileStream = File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.None);
|
|
|
|
-
|
|
|
|
|
|
+ if (null != BddInfo.BasedImagePath) _basedFileStream = new FileStream(BddInfo.BasedImagePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
|
|
|
|
+ _snapshotFileStream = IsReadOnly
|
|
|
|
+ ? File.Open(Path, FileMode.Open, FileAccess.Read, FileShare.Read)
|
|
|
|
+ : File.Open(Path, FileMode.Open, FileAccess.ReadWrite, FileShare.Read);
|
|
_isExclusiveLock = true;
|
|
_isExclusiveLock = true;
|
|
|
|
|
|
return true;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ public void Clean()
|
|
|
|
+ {
|
|
|
|
+ if (IsReadOnly) return;
|
|
|
|
+ for (var blockIndex = 0; blockIndex < BddInfo.NumberOfBlocks; blockIndex++)
|
|
|
|
+ {
|
|
|
|
+ BddInfo.Allocated[blockIndex] = false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for (var blockIndex = 0; blockIndex < BddInfo.NumberOfBlocks; blockIndex++)
|
|
|
|
+ {
|
|
|
|
+ _snapshotFileStream.Position = BddInfo.EntryTableOffset + BddInfo.BlockIndexEntrySize * blockIndex;
|
|
|
|
+ var bufEntry = BitConverter.GetBytes(BddInfo.Entries[blockIndex]);
|
|
|
|
+ _snapshotFileStream.Write(bufEntry, 0, bufEntry.Length);
|
|
|
|
+ }
|
|
|
|
+ _snapshotFileStream.SetLength(BddInfo.EntryTableOffset + BddInfo.NumberOfBlocks * BddInfo.BlockIndexEntrySize);
|
|
|
|
+ }
|
|
|
|
+
|
|
public override bool ReleaseLock()
|
|
public override bool ReleaseLock()
|
|
{
|
|
{
|
|
_basedFileStream?.Close();
|
|
_basedFileStream?.Close();
|
|
@@ -281,4 +296,4 @@ namespace DiskAccessLibrary
|
|
throw new System.NotImplementedException();
|
|
throw new System.NotImplementedException();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-}
|
|
|
|
|
|
+}
|