1
0
mirror of https://github.com/shirou/gopsutil.git synced 2025-04-26 13:48:59 +08:00
shirou_gopsutil/disk/disk_darwin.go
Tobias Klauser 422c4f61a1 Use Getfsstat from golang.org/x/sys/unix on Darwin
Starting with Go 1.12, direct syscalls on darwin are no longer
supported. Instead, libSystem is used when making syscalls. See
https://golang.org/doc/go1.12#darwin

In order to still support Getfsstat, use the syscall wrapper and types
from golang.org/x/sys/unix which uses the correct syscall method
depending on the Go version.

Also use the correct MNT_* consts and their respective strings according
to the mount(8) manpage.

Follow-up for #810
2020-01-07 23:24:48 +01:00

87 lines
1.8 KiB
Go

// +build darwin
package disk
import (
"context"
"path"
"github.com/shirou/gopsutil/internal/common"
"golang.org/x/sys/unix"
)
func Partitions(all bool) ([]PartitionStat, error) {
return PartitionsWithContext(context.Background(), all)
}
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
var ret []PartitionStat
count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
if err != nil {
return ret, err
}
fs := make([]unix.Statfs_t, count)
if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
return ret, err
}
for _, stat := range fs {
opts := "rw"
if stat.Flags&unix.MNT_RDONLY != 0 {
opts = "ro"
}
if stat.Flags&unix.MNT_SYNCHRONOUS != 0 {
opts += ",sync"
}
if stat.Flags&unix.MNT_NOEXEC != 0 {
opts += ",noexec"
}
if stat.Flags&unix.MNT_NOSUID != 0 {
opts += ",nosuid"
}
if stat.Flags&unix.MNT_UNION != 0 {
opts += ",union"
}
if stat.Flags&unix.MNT_ASYNC != 0 {
opts += ",async"
}
if stat.Flags&unix.MNT_DONTBROWSE != 0 {
opts += ",nobrowse"
}
if stat.Flags&unix.MNT_AUTOMOUNTED != 0 {
opts += ",automounted"
}
if stat.Flags&unix.MNT_JOURNALED != 0 {
opts += ",journaled"
}
if stat.Flags&unix.MNT_MULTILABEL != 0 {
opts += ",multilabel"
}
if stat.Flags&unix.MNT_NOATIME != 0 {
opts += ",noatime"
}
if stat.Flags&unix.MNT_NODEV != 0 {
opts += ",nodev"
}
d := PartitionStat{
Device: common.IntToString(stat.Mntfromname[:]),
Mountpoint: common.IntToString(stat.Mntonname[:]),
Fstype: common.IntToString(stat.Fstypename[:]),
Opts: opts,
}
if all == false {
if !path.IsAbs(d.Device) || !common.PathExists(d.Device) {
continue
}
}
ret = append(ret, d)
}
return ret, nil
}
func getFsType(stat unix.Statfs_t) string {
return common.IntToString(stat.Fstypename[:])
}