From 371056cd18c339aafb83bc0740025dde887f6c91 Mon Sep 17 00:00:00 2001 From: Anatol Pomozov Date: Sun, 12 Jun 2022 19:29:01 -0700 Subject: [PATCH] Clarify error codepath in smart.Open() function SATA/SCSI codepath does not need to close as OpenSata/OpenScsi either returns a valid device (and it then passed up to the user) or returns error and the device is nil. The only case that requires a special handling is OpenNVMe() that just really opening a file. We later use Identify() to make sure it support NVME SMART interface. If file opened succesfully but IDENTIFY is not supoprted then it considered a error codepath and device should be closed. Fixes #6 --- smart.go | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/smart.go b/smart.go index 02d3b22..433f377 100644 --- a/smart.go +++ b/smart.go @@ -3,7 +3,6 @@ package smart import ( "errors" "fmt" - "os" ) // ErrOSUnsupported is returned on unsupported operating systems. @@ -21,33 +20,18 @@ func Open(path string) (Device, error) { if err == nil { return n, nil } - } - if err != nil && n != nil { n.Close() } - if os.IsPermission(err) || errors.Is(err, ErrOSUnsupported) { - return nil, err - } - a, err := OpenSata(path) if err == nil { return a, nil } - if err != nil && a != nil { - a.Close() - } s, err := OpenScsi(path) if err == nil { return s, nil } - if err != nil && s != nil { - s.Close() - } - if errors.Is(err, ErrOSUnsupported) { - return nil, err - } return nil, fmt.Errorf("unknown drive type") }