github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/block/fixedDiskBlockReader.go (about) 1 package block 2 3 import ( 4 "io" 5 6 "github.com/Microsoft/azure-vhd-utils/vhdcore/footer" 7 "github.com/Microsoft/azure-vhd-utils/vhdcore/reader" 8 ) 9 10 // FixedDiskBlockReader type satisfies BlockDataReader interface, 11 // implementation of BlockDataReader::Read by this type can read the data from a block 12 // of a fixed disk. 13 // 14 type FixedDiskBlockReader struct { 15 vhdReader *reader.VhdReader 16 blockSizeInBytes uint32 17 } 18 19 // NewFixedDiskBlockReader create a new instance of FixedDiskBlockReader which can read data from 20 // a fixed disk block. 21 // The parameter vhdReader is the reader to read the disk 22 // The parameter blockSizeInBytes is the size of the fixed disk block 23 // 24 func NewFixedDiskBlockReader(vhdReader *reader.VhdReader, blockSizeInBytes uint32) *FixedDiskBlockReader { 25 return &FixedDiskBlockReader{ 26 vhdReader: vhdReader, 27 blockSizeInBytes: blockSizeInBytes, 28 } 29 } 30 31 // Read reads the data in a block of a fixed disk 32 // The parameter block represents the block to read 33 // 34 func (r *FixedDiskBlockReader) Read(block *Block) ([]byte, error) { 35 blockIndex := block.BlockIndex 36 blockByteOffset := int64(blockIndex) * int64(r.blockSizeInBytes) 37 blockDataBuffer := make([]byte, block.LogicalRange.Length()) 38 n, err := r.vhdReader.ReadBytes(blockByteOffset, blockDataBuffer) 39 if err == io.ErrUnexpectedEOF { 40 return blockDataBuffer[:n], nil 41 } 42 43 if err != nil { 44 return nil, NewDataReadError(blockIndex, footer.DiskTypeFixed, err) 45 } 46 return blockDataBuffer, nil 47 }