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

     1  package validator
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/Microsoft/azure-vhd-utils/vhdcore/diskstream"
     7  	"github.com/Microsoft/azure-vhd-utils/vhdcore/vhdfile"
     8  )
     9  
    10  // oneTB is one TeraByte
    11  //
    12  const oneTB int64 = 1024 * 1024 * 1024 * 1024
    13  
    14  // ValidateVhd returns error if the vhdPath refer to invalid vhd.
    15  //
    16  func ValidateVhd(vhdPath string) error {
    17  	vFactory := &vhdfile.FileFactory{}
    18  	_, err := vFactory.Create(vhdPath)
    19  	if err != nil {
    20  		return fmt.Errorf("%s is not a valid VHD: %v", vhdPath, err)
    21  	}
    22  	return nil
    23  }
    24  
    25  // ValidateVhdSize returns error if size of the vhd referenced by vhdPath is more than
    26  // the maximum allowed size (1TB)
    27  //
    28  func ValidateVhdSize(vhdPath string) error {
    29  	stream, _ := diskstream.CreateNewDiskStream(vhdPath)
    30  	if stream.GetSize() > oneTB {
    31  		return fmt.Errorf("VHD size is too large ('%d'), maximum allowed size is '%d'", stream.GetSize(), oneTB)
    32  	}
    33  	return nil
    34  }