diff --git a/cmd/admin-handlers.go b/cmd/admin-handlers.go index 6246df087..77105c49c 100644 --- a/cmd/admin-handlers.go +++ b/cmd/admin-handlers.go @@ -478,8 +478,9 @@ func toAdminAPIErrCode(err error) APIErrorCode { switch err { case errXLWriteQuorum: return ErrAdminConfigNoQuorum + default: + return toAPIErrorCode(err) } - return toAPIErrorCode(err) } // SetConfigResult - represents detailed results of a set-config diff --git a/cmd/admin-rpc-client.go b/cmd/admin-rpc-client.go index c732d4bf5..d1cec675a 100644 --- a/cmd/admin-rpc-client.go +++ b/cmd/admin-rpc-client.go @@ -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. diff --git a/cmd/bool-flag.go b/cmd/bool-flag.go index 62016c24f..604f15c67 100644 --- a/cmd/bool-flag.go +++ b/cmd/bool-flag.go @@ -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) } diff --git a/cmd/common-main.go b/cmd/common-main.go index 3583ee036..d64886df1 100644 --- a/cmd/common-main.go +++ b/cmd/common-main.go @@ -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() diff --git a/cmd/fs-v1-helpers.go b/cmd/fs-v1-helpers.go index d2cfb2f14..b7ca81c17 100644 --- a/cmd/fs-v1-helpers.go +++ b/cmd/fs-v1-helpers.go @@ -121,23 +121,25 @@ 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 } - logger.LogIf(ctx, err) - return err } return nil diff --git a/cmd/fs-v1-rwpool.go b/cmd/fs-v1-rwpool.go index 38a7eb3a1..e71a49f2a 100644 --- a/cmd/fs-v1-rwpool.go +++ b/cmd/fs-v1-rwpool.go @@ -93,18 +93,20 @@ 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 } - return nil, err } /// Save new reader on the map. @@ -148,14 +150,16 @@ 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 nil, err } return wlk, nil } @@ -175,14 +179,16 @@ 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 } - return nil, err } // Success. diff --git a/cmd/httprange.go b/cmd/httprange.go index 45e5e133e..bed49eba8 100644 --- a/cmd/httprange.go +++ b/cmd/httprange.go @@ -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) } diff --git a/cmd/logger/logger.go b/cmd/logger/logger.go index 5768f3316..6350d2d0d 100644 --- a/cmd/logger/logger.go +++ b/cmd/logger/logger.go @@ -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...) } } diff --git a/cmd/object-api-errors.go b/cmd/object-api-errors.go index 241c98c32..b8fc53fc5 100644 --- a/cmd/object-api-errors.go +++ b/cmd/object-api-errors.go @@ -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. diff --git a/cmd/os-reliable.go b/cmd/os-reliable.go index cb3b39f24..f95cea06e 100644 --- a/cmd/os-reliable.go +++ b/cmd/os-reliable.go @@ -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 diff --git a/cmd/posix-errors.go b/cmd/posix-errors.go index 01e9bb25b..4ea2070fb 100644 --- a/cmd/posix-errors.go +++ b/cmd/posix-errors.go @@ -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 } diff --git a/cmd/posix.go b/cmd/posix.go index 18556b19b..a23d2cdf4 100644 --- a/cmd/posix.go +++ b/cmd/posix.go @@ -329,14 +329,16 @@ 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 err } return nil } @@ -597,16 +599,18 @@ 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 err } return nil } @@ -771,16 +775,18 @@ 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 } - return 0, err } // Close the file descriptor. @@ -880,14 +886,16 @@ 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 nil, err } return w, nil diff --git a/cmd/postpolicyform.go b/cmd/postpolicyform.go index cb71cd385..d4356433b 100644 --- a/cmd/postpolicyform.go +++ b/cmd/postpolicyform.go @@ -57,8 +57,9 @@ func toString(val interface{}) string { switch v := val.(type) { case string: return v + default: + return "" } - 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") } - - 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. diff --git a/cmd/xl-sets.go b/cmd/xl-sets.go index 3b2eb0b1c..93220f421 100644 --- a/cmd/xl-sets.go +++ b/cmd/xl-sets.go @@ -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 } - // Unknown algorithm returns -1, also if cardinality is lesser than 0. - return -1 } // Returns always a same erasure coded set for a given input.