private void inititalizeSectors(VirtualDriveParameters parameters) { lock (_sectorsLock) { var fileTableSector = new SectorInfo { StartPosition = FixedPositions.SectorsInformation + parameters.SectorsInformationRegionLength, Length = parameters.EntriesTableSectorLength, Mark = ServiceMarks.EntriesSector, Id = 0 }; var contentSector = new SectorInfo { StartPosition = fileTableSector.StartPosition + fileTableSector.Length, Length = 0, Mark = ServiceMarks.ContentSector, Id = 1 }; var fileTableSectorWriteOperation = writeSectorInfo(fileTableSector); var contentSectorWriteOperation = writeSectorInfo(contentSector); fileTableSectorWriteOperation.Task.Wait(); contentSectorWriteOperation.Task.Wait(); finalizeSectorRegion().Task.Wait(); } }
private void init() { try { _parameters = readParameters(); _sectorInfoWriter = new SectorInfoRawWriter(_parameters, _synchronizer); var sectors = readSectors(); foreach (var sectorInfo in sectors) { switch (sectorInfo.Mark) { case ServiceMarks.EntriesSector: _currentFileEntriesSector = sectorInfo; break; case ServiceMarks.ContentSector: _currentContentSector = sectorInfo; break; } } if (_currentFileEntriesSector == null || _currentContentSector == null) { throw new Exception("Sectors read error"); } var entriesStartPosition = _currentFileEntriesSector.StartPosition; _entryWriter = new EntryRawWriter(_parameters, _synchronizer, entriesStartPosition); var contentStartPosition = _currentContentSector.StartPosition; _contentWriter = new ContentRawWriter(_parameters, _synchronizer, contentStartPosition); _contentWriter.SetLength(_currentContentSector.Length); var availableContentBlocks = checkIfDriveIsFinalized() ? readAvailableContentBlocks() : restoreAvailableContentBlocks(); foreach (var availableContentBlock in availableContentBlocks) { _contentWriter.AddAvailableBlock(availableContentBlock); } } catch (Exception e) { throw new IOException("Virtual drive is corrupted", e); } }
/// <summary> /// Finalize current content sector, create new sector for file table and create new content sector /// </summary> private void handleEntryTableSector() { lock (_sectorsLock) lock (_contentLock) { var currentContentSectorInfoPosition = _sectorInfosStartPositions[_currentContentSector]; _sectorInfoWriter.SetContentSectorLength(currentContentSectorInfoPosition, _contentWriter.Length); var fileTableSector = new SectorInfo { StartPosition = _contentWriter.InitialPosition + _contentWriter.Length, Length = _parameters.EntriesTableSectorLength, Mark = ServiceMarks.EntriesSector, Id = _sectorNextId++ }; var contentSector = new SectorInfo { StartPosition = fileTableSector.StartPosition + fileTableSector.Length, Length = 0, Mark = ServiceMarks.ContentSector, Id = _sectorNextId++, }; var fileTableSectorWriteOperation = writeSectorInfo(fileTableSector); var contentSectorWriteOperation = writeSectorInfo(contentSector); _sectorInfosStartPositions[_currentFileEntriesSector] = fileTableSectorWriteOperation.Position; _sectorInfosStartPositions[_currentContentSector] = contentSectorWriteOperation.Position; _currentFileEntriesSector = fileTableSector; _currentContentSector = contentSector; fileTableSectorWriteOperation.Task.Wait(); contentSectorWriteOperation.Task.Wait(); finalizeSectorRegion().Task.Wait(); _entryWriter.SetInitialPosition(_currentFileEntriesSector.StartPosition); _contentWriter.SetInitialPosition(_currentContentSector.StartPosition); _contentWriter.SetLength(contentSector.Length); } }
private IEnumerable <SectorInfo> readSectors() { lock (_sectorsLock) { var buffer = new byte[4096]; var reader = new SectorInfoRawReader(_synchronizer, _parameters); var lengthBytesLength = ByteHelper.GetLength <int>(); while (reader.CheckCanRead(lengthBytesLength)) { reader.Read(buffer, 0, lengthBytesLength).Task.Wait(); var sectorInfoEntryLength = BitConverter.ToInt32(buffer, 0); if (sectorInfoEntryLength == (int)ServiceBytes.End) { yield break; } buffer = new byte[sectorInfoEntryLength]; var position = reader.CurrentPosition; reader.Read(buffer, 0, sectorInfoEntryLength).Task.Wait(); var sector = SectorInfo.Read(buffer); _sectorInfosStartPositions[sector] = position; _sectorInfoWriter.SetCurrentPostion(reader.CurrentPosition); if (_sectorNextId <= sector.Id) { _sectorNextId = sector.Id + 1; } yield return(sector); if (sector.Length == 0) { yield break; } } } }
private WriteOperation writeSectorInfo(SectorInfo sectorInfo) { var bytes = sectorInfo.GetBytes(); lock (_sectorsLock) { if (!_sectorInfoWriter.CheckCanWrite(bytes.Length)) { throw new InvalidOperationException("Unable to write sector info"); } if (sectorInfo.Mark != ServiceMarks.EntriesSector) { return(_sectorInfoWriter.Write(bytes)); } var position = sectorInfo.StartPosition + sectorInfo.Length - ByteHelper.GetLength <int>(); var op = new FileTableOperation(drive => drive.Write(position, ServiceBytes.End), position); _synchronizer.EnqueueOperation(op); return(_sectorInfoWriter.Write(bytes)); } }