github.com/Microsoft/azure-vhd-utils@v0.0.0-20230613175315-7c30a3748a1b/vhdcore/bat/blockAllocationTableFactory.go (about) 1 package bat 2 3 import ( 4 "github.com/Microsoft/azure-vhd-utils/vhdcore/header" 5 "github.com/Microsoft/azure-vhd-utils/vhdcore/reader" 6 ) 7 8 // BlockAllocationTableFactory type is used to create BlockAllocationTable instance by reading BAT 9 // section of the disk which follows the header 10 // 11 type BlockAllocationTableFactory struct { 12 vhdReader *reader.VhdReader 13 vhdHeader *header.Header 14 } 15 16 // NewBlockAllocationFactory creates a new instance of BlockAllocationTableFactory, which can be used 17 // to create BlockAllocationTable instance by reading BAT section of the Vhd. 18 // vhdReader is the reader to be used to read the entry, vhdHeader is the header structure representing 19 // the disk header. 20 // 21 func NewBlockAllocationFactory(vhdReader *reader.VhdReader, vhdHeader *header.Header) *BlockAllocationTableFactory { 22 return &BlockAllocationTableFactory{ 23 vhdReader: vhdReader, 24 vhdHeader: vhdHeader, 25 } 26 } 27 28 // Create creates a BlockAllocationTable instance by reading the BAT section of the disk. 29 // This function return error if any error occurs while reading or parsing the BAT entries. 30 // 31 func (f *BlockAllocationTableFactory) Create() (*BlockAllocationTable, error) { 32 var err error 33 batEntriesCount := f.vhdHeader.MaxTableEntries 34 batEntryOffset := f.vhdHeader.TableOffset 35 bat := make([]uint32, batEntriesCount) 36 for i := uint32(0); i < batEntriesCount; i++ { 37 bat[i], err = f.vhdReader.ReadUInt32(batEntryOffset) 38 if err != nil { 39 return nil, NewBlockAllocationTableParseError(i, err) 40 } 41 42 batEntryOffset += 4 43 } 44 return NewBlockAllocationTable(f.vhdHeader.BlockSize, bat), nil 45 }