func showVhdBlocksInfo()

in vhdInspectCmdHandler.go [303:360]


func showVhdBlocksInfo(c *cli.Context) error {
	vhdPath := c.String("path")
	if vhdPath == "" {
		return errors.New("Missing required argument --path")
	}

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

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

	if vFile.GetDiskType() == footer.DiskTypeFixed {
		info := &FixedDiskBlocksInfo{
			BlockSize:  vBlockFactory.GetBlockSize(),
			BlockCount: vBlockFactory.GetBlockCount(),
		}
		// Note: Identifying empty and used blocks of a FixedDisk requires reading each
		// block and checking it contains all zeros, which is time consuming so we don't
		// show those information.
		t, err := template.New("root").
			Parse(fixedDiskBlockInfoTempl)
		if err != nil {
			return err
		}
		t.Execute(os.Stdout, info)
	} else {
		info := &ExpandableDiskBlocksInfo{
			BlockDataSize:         vBlockFactory.GetBlockSize(),
			BlockBitmapSize:       vFile.BlockAllocationTable.GetBitmapSizeInBytes(),
			BlockBitmapPaddedSize: vFile.BlockAllocationTable.GetSectorPaddedBitmapSizeInBytes(),
			BlockCount:            vBlockFactory.GetBlockCount(),
		}

		for _, v := range vFile.BlockAllocationTable.BAT {
			if v == vhdcore.VhdNoDataInt {
				info.EmptyBlockCount++
			} else {
				info.UsedBlockCount++
			}
		}

		t, err := template.New("root").
			Parse(expandableDiskBlockInfoTempl)
		if err != nil {
			return err
		}
		t.Execute(os.Stdout, info)
	}

	return nil
}