simplifying if-else chains to switches (#6208)

master
Oleg Kovalov 6 years ago committed by kannappanr
parent a82500f162
commit 37de2dbd3b
  1. 3
      cmd/admin-handlers.go
  2. 6
      cmd/admin-rpc-client.go
  3. 7
      cmd/bool-flag.go
  4. 7
      cmd/common-main.go
  5. 12
      cmd/fs-v1-helpers.go
  6. 34
      cmd/fs-v1-rwpool.go
  7. 9
      cmd/httprange.go
  8. 7
      cmd/logger/logger.go
  9. 14
      cmd/object-api-errors.go
  10. 13
      cmd/os-reliable.go
  11. 81
      cmd/posix-errors.go
  12. 44
      cmd/posix.go
  13. 14
      cmd/postpolicyform.go
  14. 3
      cmd/xl-sets.go

@ -478,8 +478,9 @@ func toAdminAPIErrCode(err error) APIErrorCode {
switch err {
case errXLWriteQuorum:
return ErrAdminConfigNoQuorum
}
default:
return toAPIErrorCode(err)
}
}
// SetConfigResult - represents detailed results of a set-config

@ -398,14 +398,16 @@ func getValidServerConfig(serverConfigs []serverConfig, errs []error) (scv serve
// seen. See example above for
// clarity.
continue
} else if j < i && serverConfigs[i].ConfigDiff(&serverConfigs[j]) == "" {
}
if j < i && serverConfigs[i].ConfigDiff(&serverConfigs[j]) == "" {
// serverConfigs[i] is equal to
// serverConfigs[j], update
// serverConfigs[j]'s counter since it
// is the lower index.
configCounter[j]++
break
} else if j == i {
}
if j == i {
// serverConfigs[i] is equal to no
// other value seen before. It is
// unique so far.

@ -56,11 +56,12 @@ func (bf *BoolFlag) UnmarshalJSON(data []byte) (err error) {
// ParseBoolFlag - parses string into BoolFlag.
func ParseBoolFlag(s string) (bf BoolFlag, err error) {
if s == "on" {
switch s {
case "on":
bf = true
} else if s == "off" {
case "off":
bf = false
} else {
default:
err = fmt.Errorf("invalid value ‘%s’ for BoolFlag", s)
}

@ -99,9 +99,10 @@ func handleCommonCmdArgs(ctx *cli.Context) {
var configDir string
if ctx.IsSet("config-dir") {
switch {
case ctx.IsSet("config-dir"):
configDir = ctx.String("config-dir")
} else if ctx.GlobalIsSet("config-dir") {
case ctx.GlobalIsSet("config-dir"):
configDir = ctx.GlobalString("config-dir")
// cli package does not expose parent's "config-dir" option. Below code is workaround.
if configDir == "" || configDir == getConfigDir() {
@ -109,7 +110,7 @@ func handleCommonCmdArgs(ctx *cli.Context) {
configDir = ctx.Parent().GlobalString("config-dir")
}
}
} else {
default:
// Neither local nor global config-dir option is provided. In this case, try to use
// default config directory.
configDir = getConfigDir()

@ -121,24 +121,26 @@ func fsMkdir(ctx context.Context, dirPath string) (err error) {
}
if err = os.Mkdir((dirPath), 0777); err != nil {
if os.IsExist(err) {
switch {
case os.IsExist(err):
return errVolumeExists
} else if os.IsPermission(err) {
case os.IsPermission(err):
logger.LogIf(ctx, errDiskAccessDenied)
return errDiskAccessDenied
} else if isSysErrNotDir(err) {
case isSysErrNotDir(err):
// File path cannot be verified since
// one of the parents is a file.
logger.LogIf(ctx, errDiskAccessDenied)
return errDiskAccessDenied
} else if isSysErrPathNotFound(err) {
case isSysErrPathNotFound(err):
// Add specific case for windows.
logger.LogIf(ctx, errDiskAccessDenied)
return errDiskAccessDenied
}
default:
logger.LogIf(ctx, err)
return err
}
}
return nil
}

@ -93,19 +93,21 @@ func (fsi *fsIOPool) Open(path string) (*lock.RLockedFile, error) {
// Open file for reading with read lock.
newRlkFile, err := lock.RLockedOpenFile(path)
if err != nil {
if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return nil, errFileNotFound
} else if os.IsPermission(err) {
case os.IsPermission(err):
return nil, errFileAccessDenied
} else if isSysErrIsDir(err) {
case isSysErrIsDir(err):
return nil, errIsNotRegular
} else if isSysErrNotDir(err) {
case isSysErrNotDir(err):
return nil, errFileAccessDenied
} else if isSysErrPathNotFound(err) {
case isSysErrPathNotFound(err):
return nil, errFileNotFound
}
default:
return nil, err
}
}
/// Save new reader on the map.
@ -148,15 +150,17 @@ func (fsi *fsIOPool) Write(path string) (wlk *lock.LockedFile, err error) {
wlk, err = lock.LockedOpenFile(path, os.O_RDWR, 0666)
if err != nil {
if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return nil, errFileNotFound
} else if os.IsPermission(err) {
case os.IsPermission(err):
return nil, errFileAccessDenied
} else if isSysErrIsDir(err) {
case isSysErrIsDir(err):
return nil, errIsNotRegular
}
default:
return nil, err
}
}
return wlk, nil
}
@ -175,15 +179,17 @@ func (fsi *fsIOPool) Create(path string) (wlk *lock.LockedFile, err error) {
// Attempt to create the file.
wlk, err = lock.LockedOpenFile(path, os.O_RDWR|os.O_CREATE, 0666)
if err != nil {
if os.IsPermission(err) {
switch {
case os.IsPermission(err):
return nil, errFileAccessDenied
} else if isSysErrIsDir(err) {
case isSysErrIsDir(err):
return nil, errIsNotRegular
} else if isSysErrPathNotFound(err) {
case isSysErrPathNotFound(err):
return nil, errFileAccessDenied
}
default:
return nil, err
}
}
// Success.
return wlk, nil

@ -89,7 +89,8 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
}
// rangeString contains first and last byte positions. eg. "bytes=2-5"
if offsetBegin > -1 && offsetEnd > -1 {
switch {
case offsetBegin > -1 && offsetEnd > -1:
if offsetBegin > offsetEnd {
// Last byte position is not greater than first byte position. eg. "bytes=5-2"
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
@ -103,7 +104,7 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
if offsetEnd >= resourceSize {
offsetEnd = resourceSize - 1
}
} else if offsetBegin > -1 {
case offsetBegin > -1:
// rangeString contains only first byte position. eg. "bytes=8-"
if offsetBegin >= resourceSize {
// First byte position should not be >= resourceSize.
@ -111,7 +112,7 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
}
offsetEnd = resourceSize - 1
} else if offsetEnd > -1 {
case offsetEnd > -1:
// rangeString contains only last byte position. eg. "bytes=-3"
if offsetEnd == 0 {
// Last byte position should not be zero eg. "bytes=-0"
@ -125,7 +126,7 @@ func parseRequestRange(rangeString string, resourceSize int64) (hrange *httpRang
}
offsetEnd = resourceSize - 1
} else {
default:
// rangeString contains first and last byte positions missing. eg. "bytes=-"
return nil, fmt.Errorf("'%s' does not have valid range value", rangeString)
}

@ -94,13 +94,14 @@ type Console interface {
}
func consoleLog(console Console, msg string, args ...interface{}) {
if jsonFlag {
switch {
case jsonFlag:
// Strip escape control characters from json message
msg = ansiRE.ReplaceAllLiteralString(msg, "")
console.json(msg, args...)
} else if quiet {
case quiet:
console.quiet(msg, args...)
} else {
default:
console.pretty(msg, args...)
}
}

@ -375,20 +375,14 @@ func (e BackendDown) Error() string {
// isErrIncompleteBody - Check if error type is IncompleteBody.
func isErrIncompleteBody(err error) bool {
switch err.(type) {
case IncompleteBody:
return true
}
return false
_, ok := err.(IncompleteBody)
return ok
}
// isErrObjectNotFound - Check if error type is ObjectNotFound.
func isErrObjectNotFound(err error) bool {
switch err.(type) {
case ObjectNotFound:
return true
}
return false
_, ok := err.(ObjectNotFound)
return ok
}
// isInsufficientReadQuorum - Check if error type is InsufficientReadQuorum.

@ -130,20 +130,23 @@ func renameAll(srcFilePath, dstFilePath string) (err error) {
}
if err = reliableRename(srcFilePath, dstFilePath); err != nil {
if isSysErrNotDir(err) {
switch {
case isSysErrNotDir(err):
return errFileAccessDenied
} else if isSysErrPathNotFound(err) {
case isSysErrPathNotFound(err):
// This is a special case should be handled only for
// windows, because windows API does not return "not a
// directory" error message. Handle this specifically here.
return errFileAccessDenied
} else if isSysErrCrossDevice(err) {
case isSysErrCrossDevice(err):
return fmt.Errorf("%s (%s)->(%s)", errCrossDeviceLink, srcFilePath, dstFilePath)
} else if os.IsNotExist(err) {
case os.IsNotExist(err):
return errFileNotFound
default:
return err
}
}
return err
return nil
}
// Reliably retries os.RenameAll if for some reason os.RenameAll returns

@ -27,13 +27,9 @@ func isSysErrNoSys(err error) bool {
if err == syscall.ENOSYS {
return true
}
if pathErr, ok := err.(*os.PathError); ok {
switch pathErr.Err {
case syscall.ENOSYS:
return true
}
}
return false
pathErr, ok := err.(*os.PathError)
return ok && pathErr.Err == syscall.ENOSYS
}
// Not supported error
@ -41,13 +37,9 @@ func isSysErrOpNotSupported(err error) bool {
if err == syscall.EOPNOTSUPP {
return true
}
if pathErr, ok := err.(*os.PathError); ok {
switch pathErr.Err {
case syscall.EOPNOTSUPP:
return true
}
}
return false
pathErr, ok := err.(*os.PathError)
return ok && pathErr.Err == syscall.EOPNOTSUPP
}
// No space left on device error
@ -55,13 +47,8 @@ func isSysErrNoSpace(err error) bool {
if err == syscall.ENOSPC {
return true
}
if pathErr, ok := err.(*os.PathError); ok {
switch pathErr.Err {
case syscall.ENOSPC:
return true
}
}
return false
pathErr, ok := err.(*os.PathError)
return ok && pathErr.Err == syscall.ENOSPC
}
// Input/output error
@ -69,13 +56,8 @@ func isSysErrIO(err error) bool {
if err == syscall.EIO {
return true
}
if pathErr, ok := err.(*os.PathError); ok {
switch pathErr.Err {
case syscall.EIO:
return true
}
}
return false
pathErr, ok := err.(*os.PathError)
return ok && pathErr.Err == syscall.EIO
}
// Check if the given error corresponds to EISDIR (is a directory).
@ -83,13 +65,9 @@ func isSysErrIsDir(err error) bool {
if err == syscall.EISDIR {
return true
}
if pathErr, ok := err.(*os.PathError); ok {
switch pathErr.Err {
case syscall.EISDIR:
return true
}
}
return false
pathErr, ok := err.(*os.PathError)
return ok && pathErr.Err == syscall.EISDIR
}
// Check if the given error corresponds to ENOTDIR (is not a directory).
@ -97,13 +75,8 @@ func isSysErrNotDir(err error) bool {
if err == syscall.ENOTDIR {
return true
}
if pathErr, ok := err.(*os.PathError); ok {
switch pathErr.Err {
case syscall.ENOTDIR:
return true
}
}
return false
pathErr, ok := err.(*os.PathError)
return ok && pathErr.Err == syscall.ENOTDIR
}
// Check if the given error corresponds to the ENAMETOOLONG (name too long).
@ -111,13 +84,8 @@ func isSysErrTooLong(err error) bool {
if err == syscall.ENAMETOOLONG {
return true
}
if pathErr, ok := err.(*os.PathError); ok {
switch pathErr.Err {
case syscall.ENAMETOOLONG:
return true
}
}
return false
pathErr, ok := err.(*os.PathError)
return ok && pathErr.Err == syscall.ENAMETOOLONG
}
// Check if the given error corresponds to ENOTEMPTY for unix
@ -133,8 +101,7 @@ func isSysErrNotEmpty(err error) bool {
return true
}
}
switch pathErr.Err {
case syscall.ENOTEMPTY:
if pathErr.Err == syscall.ENOTEMPTY {
return true
}
}
@ -161,15 +128,11 @@ func isSysErrHandleInvalid(err error) bool {
return false
}
// Check if err contains ERROR_INVALID_HANDLE errno
if errno, ok := err.(syscall.Errno); ok && errno == 0x6 {
return true
}
return false
errno, ok := err.(syscall.Errno)
return ok && errno == 0x6
}
func isSysErrCrossDevice(err error) bool {
if e, ok := err.(*os.LinkError); ok {
return e.Err == syscall.EXDEV
}
return false
e, ok := err.(*os.LinkError)
return ok && e.Err == syscall.EXDEV
}

@ -329,15 +329,17 @@ func (s *posix) checkDiskFound() (err error) {
}
_, err = os.Stat(s.diskPath)
if err != nil {
if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return errDiskNotFound
} else if isSysErrTooLong(err) {
case isSysErrTooLong(err):
return errFileNameTooLong
} else if isSysErrIO(err) {
case isSysErrIO(err):
return errFaultyDisk
}
default:
return err
}
}
return nil
}
@ -597,17 +599,19 @@ func (s *posix) DeleteVol(volume string) (err error) {
}
err = os.Remove((volumeDir))
if err != nil {
if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return errVolumeNotFound
} else if isSysErrNotEmpty(err) {
case isSysErrNotEmpty(err):
return errVolumeNotEmpty
} else if os.IsPermission(err) {
case os.IsPermission(err):
return errDiskAccessDenied
} else if isSysErrIO(err) {
case isSysErrIO(err):
return errFaultyDisk
}
default:
return err
}
}
return nil
}
@ -771,17 +775,19 @@ func (s *posix) ReadFile(volume, path string, offset int64, buffer []byte, verif
// Open the file for reading.
file, err := os.Open((filePath))
if err != nil {
if os.IsNotExist(err) {
switch {
case os.IsNotExist(err):
return 0, errFileNotFound
} else if os.IsPermission(err) {
case os.IsPermission(err):
return 0, errFileAccessDenied
} else if isSysErrNotDir(err) {
case isSysErrNotDir(err):
return 0, errFileAccessDenied
} else if isSysErrIO(err) {
case isSysErrIO(err):
return 0, errFaultyDisk
}
default:
return 0, err
}
}
// Close the file descriptor.
defer file.Close()
@ -880,15 +886,17 @@ func (s *posix) createFile(volume, path string) (f *os.File, err error) {
w, err := os.OpenFile(filePath, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
if err != nil {
// File path cannot be verified since one of the parents is a file.
if isSysErrNotDir(err) {
switch {
case isSysErrNotDir(err):
return nil, errFileAccessDenied
} else if os.IsPermission(err) {
case os.IsPermission(err):
return nil, errFileAccessDenied
} else if isSysErrIO(err) {
case isSysErrIO(err):
return nil, errFaultyDisk
}
default:
return nil, err
}
}
return w, nil
}

@ -57,8 +57,9 @@ func toString(val interface{}) string {
switch v := val.(type) {
case string:
return v
}
default:
return ""
}
}
// toLowerString - safely convert interface to lower string
@ -78,18 +79,15 @@ func toInteger(val interface{}) (int64, error) {
case string:
i, err := strconv.Atoi(v)
return int64(i), err
}
default:
return 0, errors.New("Invalid number format")
}
}
// isString - Safely check if val is of type string without causing panic.
func isString(val interface{}) bool {
switch val.(type) {
case string:
return true
}
return false
_, ok := val.(string)
return ok
}
// ContentLengthRange - policy content-length-range field.

@ -427,9 +427,10 @@ func hashKey(algo string, key string, cardinality int) int {
switch algo {
case formatXLVersionV2DistributionAlgo:
return crcHashMod(key, cardinality)
}
default:
// Unknown algorithm returns -1, also if cardinality is lesser than 0.
return -1
}
}
// Returns always a same erasure coded set for a given input.

Loading…
Cancel
Save