xl/utils: getPartSizeFromIdx should return error. (#3669)

This commit is contained in:
Harshavardhana
2017-01-31 15:34:49 -08:00
committed by GitHub
parent f7f103725b
commit 1b30a3be2b
3 changed files with 56 additions and 11 deletions

View File

@@ -17,6 +17,7 @@
package cmd
import (
"errors"
"hash/crc32"
"path"
"sync"
@@ -344,29 +345,35 @@ func getOrderedDisks(distribution []int, disks []StorageAPI) (orderedDisks []Sto
return orderedDisks
}
// Errors specifically generated by getPartSizeFromIdx function.
var (
errPartSizeZero = errors.New("Part size cannot be zero")
errPartSizeIndex = errors.New("Part index cannot be smaller than 1")
)
// getPartSizeFromIdx predicts the part size according to its index. It also
// returns -1 when totalSize is also -1.
func getPartSizeFromIdx(totalSize int64, partSize int64, partIndex int) int64 {
func getPartSizeFromIdx(totalSize int64, partSize int64, partIndex int) (int64, error) {
if partSize == 0 {
panic("Part size cannot be nil.")
return 0, traceError(errPartSizeZero)
}
if partIndex < 1 {
panic("Part index should be greater than 1.")
return 0, traceError(errPartSizeIndex)
}
switch totalSize {
case -1, 0:
return totalSize
return totalSize, nil
}
// Compute the total count of parts
partsCount := totalSize/partSize + 1
// Return the part's size
switch {
case int64(partIndex) < partsCount:
return partSize
return partSize, nil
case int64(partIndex) == partsCount:
// Size of last part
return totalSize % partSize
return totalSize % partSize, nil
default:
return 0
return 0, nil
}
}