-
Notifications
You must be signed in to change notification settings - Fork 10
/
nvme_darwin.go
67 lines (54 loc) · 1.71 KB
/
nvme_darwin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package smart
// #cgo darwin LDFLAGS: -framework IOKit -framework CoreFoundation
// #include "nvme_darwin.h"
import "C"
import (
"bytes"
"encoding/binary"
"fmt"
"unsafe"
)
type NVMeDevice struct {
ptr unsafe.Pointer // opaque pointer to underlying macosx nvme struct
}
func OpenNVMe(name string) (*NVMeDevice, error) {
dev := NVMeDevice{}
if res := C.smart_nvme_open_darwin(C.CString(name), &dev.ptr); res != 0 {
return nil, fmt.Errorf("open darwin device error: 0x%x", res)
}
return &dev, nil
}
func (d *NVMeDevice) Close() error {
C.smart_nvme_close_darwin(d.ptr)
d.ptr = unsafe.Pointer(nil)
return nil
}
func (d *NVMeDevice) ReadSMART() (*NvmeSMARTLog, error) {
buf := make([]byte, 512)
if err := C.smart_nvme_readsmart_darwin(d.ptr, unsafe.Pointer(&buf[0])); err != 0 {
return nil, fmt.Errorf("smart_nvme_readsmart_darwin: %v", err)
}
var sl NvmeSMARTLog
if err := binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &sl); err != nil {
return nil, err
}
return &sl, nil
}
func (d *NVMeDevice) readControllerIdentifyData() (*NvmeIdentController, error) {
buf := make([]byte, 4096)
if err := C.smart_nvme_identify_darwin(d.ptr, unsafe.Pointer(&buf[0]), 0); err != 0 {
return nil, fmt.Errorf("smart_nvme_identify_darwin: %v", err)
}
var controller NvmeIdentController
if err := binary.Read(bytes.NewBuffer(buf), binary.LittleEndian, &controller); err != nil {
return nil, err
}
return &controller, nil
}
func (d *NVMeDevice) readNamespaceIdentifyData(nsid int) (*NvmeIdentNamespace, error) {
var n NvmeIdentNamespace
if err := C.smart_nvme_identify_darwin(d.ptr, unsafe.Pointer(&n), C.uint(nsid)); err != 0 {
return nil, fmt.Errorf("smart_nvme_identify_darwin: %X", err)
}
return &n, nil
}