From 1bf68e86f5430730fa9792b8ccd675c5ef2eb677 Mon Sep 17 00:00:00 2001 From: Utkarsh Dubey Date: Sun, 1 Oct 2023 20:12:11 +0530 Subject: [PATCH] Adds error checks for generous os errors --- service/mount.go | 37 ++++++++++++++++++++++++------------- service/node.go | 6 +++--- 2 files changed, 27 insertions(+), 16 deletions(-) diff --git a/service/mount.go b/service/mount.go index 9ddc30c4..35e1e0f0 100644 --- a/service/mount.go +++ b/service/mount.go @@ -404,16 +404,22 @@ func contains(list []string, item string) bool { // return pair is a bool flag of whether file was created, and an error func mkfile(path string) (bool, error) { st, err := os.Stat(path) - if os.IsNotExist(err) { - file, err := os.OpenFile(path, os.O_CREATE, 0600) // #nosec G304 - if err != nil { + if err != nil { + if os.IsNotExist(err) { + file, err := os.OpenFile(path, os.O_CREATE, 0600) // #nosec G304 + if err != nil { + log.WithField("path", path).WithError( + err).Error("Unable to create file") + } else { + file.Close() // #nosec G20 + log.WithField("path", path).Debug("created file") + return true, nil + } + } else { log.WithField("path", path).WithError( err).Error("Unable to create file") - return false, err } - file.Close() // #nosec G20 - log.WithField("path", path).Debug("created file") - return true, nil + return false, err } if st.IsDir() { return false, fmt.Errorf("existing path is a directory") @@ -425,14 +431,19 @@ func mkfile(path string) (bool, error) { // return pair is a bool flag of whether dir was created, and an error func mkdir(path string) (bool, error) { st, err := os.Stat(path) - if os.IsNotExist(err) { - if err := os.Mkdir(path, 0750); err != nil { - log.WithField("dir", path).WithError( + if err != nil { + if os.IsNotExist(err) { + if err := os.Mkdir(path, 0750); err != nil { + log.WithField("dir", path).WithError( + err).Error("Unable to create dir") + } else { + log.WithField("path", path).Debug("created directory") + } + } else { + log.WithField("path", path).WithError( err).Error("Unable to create dir") - return false, err } - log.WithField("path", path).Debug("created directory") - return true, nil + return false, err } if !st.IsDir() { return false, fmt.Errorf("existing path is not a directory") diff --git a/service/node.go b/service/node.go index 7b99d253..943d0ce1 100644 --- a/service/node.go +++ b/service/node.go @@ -560,11 +560,11 @@ func (s *service) disconnectVolume(reqID, symID, devID, volumeWWN string) error nodeUnstageCtx = setLogFields(nodeUnstageCtx, f) switch s.arrayTransportProtocolMap[symID] { case Vsphere: - s.fcConnector.DisconnectVolumeByDeviceName(nodeUnstageCtx, deviceName) // #nosec G20 + _ = s.fcConnector.DisconnectVolumeByDeviceName(nodeUnstageCtx, deviceName) case FcTransportProtocol: - s.fcConnector.DisconnectVolumeByDeviceName(nodeUnstageCtx, deviceName) // #nosec G20 + _ = s.fcConnector.DisconnectVolumeByDeviceName(nodeUnstageCtx, deviceName) case IscsiTransportProtocol: - s.iscsiConnector.DisconnectVolumeByDeviceName(nodeUnstageCtx, deviceName) // #nosec G20 + _ = s.iscsiConnector.DisconnectVolumeByDeviceName(nodeUnstageCtx, deviceName) } cancel() time.Sleep(disconnectVolumeRetryTime)