Skip to content

Commit

Permalink
Clarify error codepath in smart.Open() function
Browse files Browse the repository at this point in the history
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
  • Loading branch information
anatol committed Jun 15, 2022
1 parent 26585f2 commit 371056c
Showing 1 changed file with 0 additions and 16 deletions.
16 changes: 0 additions & 16 deletions smart.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package smart
import (
"errors"
"fmt"
"os"
)

// ErrOSUnsupported is returned on unsupported operating systems.
Expand All @@ -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")
}

0 comments on commit 371056c

Please sign in to comment.