func showVhdBlockBitmap()

in vhdInspectCmdHandler.go [362:415]


func showVhdBlockBitmap(c *cli.Context) error {
	const bytesPerLine int32 = 8
	const bitsPerLine int32 = 8 * bytesPerLine

	vhdPath := c.String("path")
	if vhdPath == "" {
		return errors.New("missing required argument --path")
	}

	if !c.IsSet("block-index") {
		return errors.New("missing required argument --block-index")
	}

	blockIndex := uint32(0)
	id, err := strconv.ParseUint(c.String("block-index"), 10, 32)
	if err != nil {
		return fmt.Errorf("invalid index value --block-index: %s", err)
	}
	blockIndex = uint32(id)

	vFileFactory := &vhdfile.FileFactory{}
	vFile, err := vFileFactory.Create(vhdPath)
	if err != nil {
		return err
	}
	defer vFileFactory.Dispose(nil)

	if vFile.GetDiskType() == footer.DiskTypeFixed {
		return errors.New("warn: only expandable VHDs has bitmap associated with blocks, this is a fixed VHD")
	}

	vBlockFactory, err := vFile.GetBlockFactory()
	if err != nil {
		return err
	}

	if int64(blockIndex) > vBlockFactory.GetBlockCount()-1 {
		return fmt.Errorf("warn: given block index %d is out of boundary, block index range is [0 : %d]", blockIndex, vBlockFactory.GetBlockCount()-1)
	}

	vBlock, err := vBlockFactory.Create(blockIndex)
	if err != nil {
		return err
	}

	if vBlock.IsEmpty {
		fmt.Print("The block that this bitmap belongs to is marked as empty\n\n")
		fmt.Print(createEmptyBitmapString(bytesPerLine, bitsPerLine, vFile.BlockAllocationTable.GetBitmapSizeInBytes()))
		return nil
	}

	fmt.Print(createBitmapString(bitsPerLine, vBlock.BitMap))
	return nil
}