Check and return error first in IsSymlinkExist and IsBindMountExist

This commit is contained in:
Masaki Kimura 2019-11-04 21:07:48 +00:00
parent 8a159d7253
commit 5a351e3014

View File

@ -231,20 +231,20 @@ func (v VolumePathHandler) RemoveMapPath(mapPath string) error {
// On other cases, return false with error from Lstat(). // On other cases, return false with error from Lstat().
func (v VolumePathHandler) IsSymlinkExist(mapPath string) (bool, error) { func (v VolumePathHandler) IsSymlinkExist(mapPath string) (bool, error) {
fi, err := os.Lstat(mapPath) fi, err := os.Lstat(mapPath)
if err == nil { if err != nil {
// If file exits and it's symbolic link, return true and no error // If file doesn't exist, return false and no error
if fi.Mode()&os.ModeSymlink == os.ModeSymlink { if os.IsNotExist(err) {
return true, nil return false, nil
} }
// If file exits but it's not symbolic link, return fale and no error // Return error from Lstat()
return false, nil return false, err
} }
// If file doesn't exist, return false and no error // If file exits and it's symbolic link, return true and no error
if os.IsNotExist(err) { if fi.Mode()&os.ModeSymlink == os.ModeSymlink {
return false, nil return true, nil
} }
// Return error from Lstat() // If file exits but it's not symbolic link, return fale and no error
return false, err return false, nil
} }
// IsBindMountExist returns true if specified file exists and the type is device. // IsBindMountExist returns true if specified file exists and the type is device.
@ -252,20 +252,21 @@ func (v VolumePathHandler) IsSymlinkExist(mapPath string) (bool, error) {
// On other cases, return false with error from Lstat(). // On other cases, return false with error from Lstat().
func (v VolumePathHandler) IsBindMountExist(mapPath string) (bool, error) { func (v VolumePathHandler) IsBindMountExist(mapPath string) (bool, error) {
fi, err := os.Lstat(mapPath) fi, err := os.Lstat(mapPath)
if err == nil { if err != nil {
// If file exits and it's device, return true and no error // If file doesn't exist, return false and no error
if fi.Mode()&os.ModeDevice == os.ModeDevice { if os.IsNotExist(err) {
return true, nil return false, nil
} }
// If file exits but it's not device, return fale and no error
return false, nil // Return error from Lstat()
return false, err
} }
// If file doesn't exist, return false and no error // If file exits and it's device, return true and no error
if os.IsNotExist(err) { if fi.Mode()&os.ModeDevice == os.ModeDevice {
return false, nil return true, nil
} }
// Return error from Lstat() // If file exits but it's not device, return fale and no error
return false, err return false, nil
} }
// GetDeviceBindMountRefs searches bind mounts under global map path // GetDeviceBindMountRefs searches bind mounts under global map path