123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- using System;
- using System.Collections.Generic;
- using System.Text;
- using Utilities;
- namespace DiskAccessLibrary.LogicalDiskManager
- {
-
-
-
-
-
-
- public class DynamicColumn
- {
- List<DynamicDiskExtent> m_extents = new List<DynamicDiskExtent>();
- public DynamicColumn(DynamicDiskExtent extent)
- {
- m_extents.Add(extent);
- }
- public DynamicColumn(List<DynamicDiskExtent> extents)
- {
- m_extents = extents;
- }
- private List<ArrayPosition> TranslateSectors(long startSectorIndex, int sectorCount)
- {
- List<ArrayPosition> result = new List<ArrayPosition>();
- int numberOfDisks = m_extents.Count;
- int sectorsLeft = sectorCount;
- long currentSectorIndex = startSectorIndex;
- while (sectorsLeft > 0)
- {
- long extentStartSectorInColumn = 0;
- long nextExtentStartSectorInColumn = 0;
- for (int index = 0; index < m_extents.Count; index++)
- {
- DynamicDiskExtent extent = m_extents[index];
- extentStartSectorInColumn = nextExtentStartSectorInColumn;
- nextExtentStartSectorInColumn += extent.TotalSectors;
- if (currentSectorIndex >= extentStartSectorInColumn && currentSectorIndex < nextExtentStartSectorInColumn)
- {
- long sectorIndexInExtent = currentSectorIndex - extentStartSectorInColumn;
- int sectorCountInExtent = (int)Math.Min(extent.TotalSectors - sectorIndexInExtent, sectorsLeft);
- ArrayPosition position = new ArrayPosition(index, sectorIndexInExtent, sectorCountInExtent);
- result.Add(position);
- currentSectorIndex += sectorCountInExtent;
- sectorsLeft -= sectorCountInExtent;
- }
- }
- }
- return result;
- }
- public byte[] ReadSectors(long sectorIndex, int sectorCount)
- {
- List<ArrayPosition> readPositions = TranslateSectors(sectorIndex, sectorCount);
- byte[] result = new byte[sectorCount * DynamicVolume.BytesPerDynamicDiskSector];
- int bytesRead = 0;
- foreach (ArrayPosition readPosition in readPositions)
- {
- DynamicDiskExtent extent = m_extents[readPosition.DiskIndex];
- byte[] extentBytes = extent.ReadSectors(readPosition.SectorIndex, readPosition.SectorCount);
- Array.Copy(extentBytes, 0, result, bytesRead, extentBytes.Length);
- bytesRead += extentBytes.Length;
- }
- return result;
- }
- public void WriteSectors(long sectorIndex, byte[] data)
- {
- int sectorCount = data.Length / DynamicVolume.BytesPerDynamicDiskSector;
- List<ArrayPosition> writePositions = TranslateSectors(sectorIndex, sectorCount);
- int bytesWritten = 0;
- foreach (ArrayPosition writePosition in writePositions)
- {
- DynamicDiskExtent extent = m_extents[writePosition.DiskIndex];
- byte[] extentBytes = new byte[writePosition.SectorCount * DynamicVolume.BytesPerDynamicDiskSector];
- Array.Copy(data, bytesWritten, extentBytes, 0, extentBytes.Length);
- extent.WriteSectors(writePosition.SectorIndex, extentBytes);
-
- bytesWritten += extentBytes.Length;
- }
- }
- public List<DynamicDiskExtent> Extents
- {
- get
- {
- return m_extents;
- }
- }
- public long Size
- {
- get
- {
- long result = 0;
- foreach (DynamicDiskExtent extent in m_extents)
- {
- result += extent.Size;
- }
- return result;
- }
- }
- public bool IsOperational
- {
- get
- {
- foreach (DynamicDiskExtent extent in m_extents)
- {
- if (extent.Disk == null)
- {
- return false;
- }
- }
- return true;
- }
- }
- }
- }
|