github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/block/bitmap/factory.go (about)

     1  package bitmap
     2  
     3  import (
     4  	"github.com/Microsoft/azure-vhd-utils/vhdcore/bat"
     5  	"github.com/Microsoft/azure-vhd-utils/vhdcore/reader"
     6  )
     7  
     8  // Factory type is used to create BitMap instance by reading 'bitmap section' of a block.
     9  //
    10  type Factory struct {
    11  	vhdReader            *reader.VhdReader
    12  	blockAllocationTable *bat.BlockAllocationTable
    13  }
    14  
    15  // NewFactory creates a new instance of Factory, which can be used to create a BitMap instance by reading
    16  // the 'bitmap section' of a block. vhdReader is the reader to read the disk, blockAllocationTable wraps
    17  // the disk's BAT table, which has one entry per block, this is used to retrieve the absolute offset to
    18  // the beginning of the 'bitmap section' of a block and the size of the 'bitmap section'.
    19  //
    20  func NewFactory(vhdReader *reader.VhdReader, blockAllocationTable *bat.BlockAllocationTable) *Factory {
    21  	return &Factory{vhdReader: vhdReader, blockAllocationTable: blockAllocationTable}
    22  }
    23  
    24  // Create creates a BitMap instance by reading block's 'bitmap section', block is the index of the
    25  // block entry in the BAT whose 'bitmap section' needs to be read.
    26  // This function return error if any error occurs while reading or parsing the block's bitmap.
    27  //
    28  func (f *Factory) Create(blockIndex uint32) (*BitMap, error) {
    29  	bitmapAbsoluteByteOffset := f.blockAllocationTable.GetBitmapAddress(blockIndex)
    30  	bitmapSizeInBytes := f.blockAllocationTable.GetBitmapSizeInBytes()
    31  	bitmapBytes := make([]byte, bitmapSizeInBytes)
    32  	if _, err := f.vhdReader.ReadBytes(bitmapAbsoluteByteOffset, bitmapBytes); err != nil {
    33  		return nil, NewParseError(blockIndex, err)
    34  	}
    35  	return NewBitMapFromByteSlice(bitmapBytes), nil
    36  }