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

     1  package upload
     2  
     3  import (
     4  	"github.com/Microsoft/azure-vhd-utils/vhdcore/common"
     5  	"github.com/Microsoft/azure-vhd-utils/vhdcore/diskstream"
     6  )
     7  
     8  // LocateUploadableRanges detects the uploadable ranges in a VHD stream, size of each range is at most pageSizeInBytes.
     9  //
    10  // This method reads the existing ranges A from the disk stream, creates a new set of ranges B from A by removing the
    11  // ranges identified by the parameter rangesToSkip, returns new set of ranges C (with each range of size at most
    12  // pageSizeInBytes) by merging adjacent ranges in B or splitting ranges in B.
    13  //
    14  // Note that this method will not check whether ranges of a fixed disk contains zeros, hence inorder to filter out such
    15  // ranges from the uploadable ranges, caller must use LocateNonEmptyRangeIndices method.
    16  //
    17  func LocateUploadableRanges(stream *diskstream.DiskStream, rangesToSkip []*common.IndexRange, pageSizeInBytes int64) ([]*common.IndexRange, error) {
    18  	var err error
    19  	var diskRanges = make([]*common.IndexRange, 0)
    20  	stream.EnumerateExtents(func(ext *diskstream.StreamExtent, extErr error) bool {
    21  		if extErr != nil {
    22  			err = extErr
    23  			return false
    24  		}
    25  
    26  		diskRanges = append(diskRanges, ext.Range)
    27  		return true
    28  	})
    29  
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  
    34  	diskRanges = common.SubtractRanges(diskRanges, rangesToSkip)
    35  	diskRanges = common.ChunkRangesBySize(diskRanges, pageSizeInBytes)
    36  	return diskRanges, nil
    37  }