From 422c4f61a1d0c8a742bd0ac3a97152bbb2cc70fa Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Tue, 7 Jan 2020 23:24:48 +0100 Subject: [PATCH] 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 --- disk/disk_darwin.go | 72 +++++++++++++---------------------------------- disk/disk_darwin_386.go | 59 -------------------------------------- disk/disk_darwin_amd64.go | 58 -------------------------------------- disk/disk_darwin_arm64.go | 58 -------------------------------------- 4 files changed, 20 insertions(+), 227 deletions(-) delete mode 100644 disk/disk_darwin_386.go delete mode 100644 disk/disk_darwin_amd64.go delete mode 100644 disk/disk_darwin_arm64.go diff --git a/disk/disk_darwin.go b/disk/disk_darwin.go index 2b1d000..64c41d7 100644 --- a/disk/disk_darwin.go +++ b/disk/disk_darwin.go @@ -5,7 +5,6 @@ package disk import ( "context" "path" - "unsafe" "github.com/shirou/gopsutil/internal/common" "golang.org/x/sys/unix" @@ -18,63 +17,51 @@ func Partitions(all bool) ([]PartitionStat, error) { func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { var ret []PartitionStat - count, err := Getfsstat(nil, MntWait) + count, err := unix.Getfsstat(nil, unix.MNT_WAIT) if err != nil { return ret, err } - fs := make([]Statfs, count) - if _, err = Getfsstat(fs, MntWait); err != nil { + 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&MntReadOnly != 0 { + if stat.Flags&unix.MNT_RDONLY != 0 { opts = "ro" } - if stat.Flags&MntSynchronous != 0 { + if stat.Flags&unix.MNT_SYNCHRONOUS != 0 { opts += ",sync" } - if stat.Flags&MntNoExec != 0 { + if stat.Flags&unix.MNT_NOEXEC != 0 { opts += ",noexec" } - if stat.Flags&MntNoSuid != 0 { + if stat.Flags&unix.MNT_NOSUID != 0 { opts += ",nosuid" } - if stat.Flags&MntUnion != 0 { + if stat.Flags&unix.MNT_UNION != 0 { opts += ",union" } - if stat.Flags&MntAsync != 0 { + if stat.Flags&unix.MNT_ASYNC != 0 { opts += ",async" } - if stat.Flags&MntSuidDir != 0 { - opts += ",suiddir" + if stat.Flags&unix.MNT_DONTBROWSE != 0 { + opts += ",nobrowse" } - if stat.Flags&MntSoftDep != 0 { - opts += ",softdep" + if stat.Flags&unix.MNT_AUTOMOUNTED != 0 { + opts += ",automounted" } - if stat.Flags&MntNoSymFollow != 0 { - opts += ",nosymfollow" + if stat.Flags&unix.MNT_JOURNALED != 0 { + opts += ",journaled" } - if stat.Flags&MntGEOMJournal != 0 { - opts += ",gjounalc" - } - if stat.Flags&MntMultilabel != 0 { + if stat.Flags&unix.MNT_MULTILABEL != 0 { opts += ",multilabel" } - if stat.Flags&MntACLs != 0 { - opts += ",acls" - } - if stat.Flags&MntNoATime != 0 { - opts += ",noattime" - } - if stat.Flags&MntClusterRead != 0 { - opts += ",nocluster" + if stat.Flags&unix.MNT_NOATIME != 0 { + opts += ",noatime" } - if stat.Flags&MntClusterWrite != 0 { - opts += ",noclusterw" - } - if stat.Flags&MntNFS4ACLs != 0 { - opts += ",nfs4acls" + if stat.Flags&unix.MNT_NODEV != 0 { + opts += ",nodev" } d := PartitionStat{ Device: common.IntToString(stat.Mntfromname[:]), @@ -94,25 +81,6 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro return ret, nil } -func Getfsstat(buf []Statfs, flags int) (n int, err error) { - return GetfsstatWithContext(context.Background(), buf, flags) -} - -func GetfsstatWithContext(ctx context.Context, buf []Statfs, flags int) (n int, err error) { - var _p0 unsafe.Pointer - var bufsize uintptr - if len(buf) > 0 { - _p0 = unsafe.Pointer(&buf[0]) - bufsize = unsafe.Sizeof(Statfs{}) * uintptr(len(buf)) - } - r0, _, e1 := unix.Syscall(SYS_GETFSSTAT64, uintptr(_p0), bufsize, uintptr(flags)) - n = int(r0) - if e1 != 0 { - err = e1 - } - return -} - func getFsType(stat unix.Statfs_t) string { return common.IntToString(stat.Fstypename[:]) } diff --git a/disk/disk_darwin_386.go b/disk/disk_darwin_386.go deleted file mode 100644 index bd83a4a..0000000 --- a/disk/disk_darwin_386.go +++ /dev/null @@ -1,59 +0,0 @@ -// +build darwin -// +build 386 - -package disk - -const ( - MntWait = 1 - MfsNameLen = 15 /* length of fs type name, not inc. nul */ - MNameLen = 90 /* length of buffer for returned name */ - - MFSTYPENAMELEN = 16 /* length of fs type name including null */ - MAXPATHLEN = 1024 - MNAMELEN = MAXPATHLEN - - SYS_GETFSSTAT64 = 347 -) - -type Fsid struct{ val [2]int32 } /* file system id type */ -type uid_t int32 - -// sys/mount.h -const ( - MntReadOnly = 0x00000001 /* read only filesystem */ - MntSynchronous = 0x00000002 /* filesystem written synchronously */ - MntNoExec = 0x00000004 /* can't exec from filesystem */ - MntNoSuid = 0x00000008 /* don't honor setuid bits on fs */ - MntUnion = 0x00000020 /* union with underlying filesystem */ - MntAsync = 0x00000040 /* filesystem written asynchronously */ - MntSuidDir = 0x00100000 /* special handling of SUID on dirs */ - MntSoftDep = 0x00200000 /* soft updates being done */ - MntNoSymFollow = 0x00400000 /* do not follow symlinks */ - MntGEOMJournal = 0x02000000 /* GEOM journal support enabled */ - MntMultilabel = 0x04000000 /* MAC support for individual objects */ - MntACLs = 0x08000000 /* ACL support enabled */ - MntNoATime = 0x10000000 /* disable update of file access time */ - MntClusterRead = 0x40000000 /* disable cluster read */ - MntClusterWrite = 0x80000000 /* disable cluster write */ - MntNFS4ACLs = 0x00000010 -) - -// https://github.com/golang/go/blob/master/src/syscall/ztypes_darwin_386.go#L82 -type Statfs struct { - Bsize uint32 - Iosize int32 - Blocks uint64 - Bfree uint64 - Bavail uint64 - Files uint64 - Ffree uint64 - Fsid Fsid - Owner uint32 - Type uint32 - Flags uint32 - Fssubtype uint32 - Fstypename [16]int8 - Mntonname [1024]int8 - Mntfromname [1024]int8 - Reserved [8]uint32 -} diff --git a/disk/disk_darwin_amd64.go b/disk/disk_darwin_amd64.go deleted file mode 100644 index ec40a75..0000000 --- a/disk/disk_darwin_amd64.go +++ /dev/null @@ -1,58 +0,0 @@ -// +build darwin -// +build amd64 - -package disk - -const ( - MntWait = 1 - MfsNameLen = 15 /* length of fs type name, not inc. nul */ - MNameLen = 90 /* length of buffer for returned name */ - - MFSTYPENAMELEN = 16 /* length of fs type name including null */ - MAXPATHLEN = 1024 - MNAMELEN = MAXPATHLEN - - SYS_GETFSSTAT64 = 347 -) - -type Fsid struct{ val [2]int32 } /* file system id type */ -type uid_t int32 - -// sys/mount.h -const ( - MntReadOnly = 0x00000001 /* read only filesystem */ - MntSynchronous = 0x00000002 /* filesystem written synchronously */ - MntNoExec = 0x00000004 /* can't exec from filesystem */ - MntNoSuid = 0x00000008 /* don't honor setuid bits on fs */ - MntUnion = 0x00000020 /* union with underlying filesystem */ - MntAsync = 0x00000040 /* filesystem written asynchronously */ - MntSuidDir = 0x00100000 /* special handling of SUID on dirs */ - MntSoftDep = 0x00200000 /* soft updates being done */ - MntNoSymFollow = 0x00400000 /* do not follow symlinks */ - MntGEOMJournal = 0x02000000 /* GEOM journal support enabled */ - MntMultilabel = 0x04000000 /* MAC support for individual objects */ - MntACLs = 0x08000000 /* ACL support enabled */ - MntNoATime = 0x10000000 /* disable update of file access time */ - MntClusterRead = 0x40000000 /* disable cluster read */ - MntClusterWrite = 0x80000000 /* disable cluster write */ - MntNFS4ACLs = 0x00000010 -) - -type Statfs struct { - Bsize uint32 - Iosize int32 - Blocks uint64 - Bfree uint64 - Bavail uint64 - Files uint64 - Ffree uint64 - Fsid Fsid - Owner uint32 - Type uint32 - Flags uint32 - Fssubtype uint32 - Fstypename [16]int8 - Mntonname [1024]int8 - Mntfromname [1024]int8 - Reserved [8]uint32 -} diff --git a/disk/disk_darwin_arm64.go b/disk/disk_darwin_arm64.go deleted file mode 100644 index 0e3f670..0000000 --- a/disk/disk_darwin_arm64.go +++ /dev/null @@ -1,58 +0,0 @@ -// +build darwin -// +build arm64 - -package disk - -const ( - MntWait = 1 - MfsNameLen = 15 /* length of fs type name, not inc. nul */ - MNameLen = 90 /* length of buffer for returned name */ - - MFSTYPENAMELEN = 16 /* length of fs type name including null */ - MAXPATHLEN = 1024 - MNAMELEN = MAXPATHLEN - - SYS_GETFSSTAT64 = 347 -) - -type Fsid struct{ val [2]int32 } /* file system id type */ -type uid_t int32 - -// sys/mount.h -const ( - MntReadOnly = 0x00000001 /* read only filesystem */ - MntSynchronous = 0x00000002 /* filesystem written synchronously */ - MntNoExec = 0x00000004 /* can't exec from filesystem */ - MntNoSuid = 0x00000008 /* don't honor setuid bits on fs */ - MntUnion = 0x00000020 /* union with underlying filesystem */ - MntAsync = 0x00000040 /* filesystem written asynchronously */ - MntSuidDir = 0x00100000 /* special handling of SUID on dirs */ - MntSoftDep = 0x00200000 /* soft updates being done */ - MntNoSymFollow = 0x00400000 /* do not follow symlinks */ - MntGEOMJournal = 0x02000000 /* GEOM journal support enabled */ - MntMultilabel = 0x04000000 /* MAC support for individual objects */ - MntACLs = 0x08000000 /* ACL support enabled */ - MntNoATime = 0x10000000 /* disable update of file access time */ - MntClusterRead = 0x40000000 /* disable cluster read */ - MntClusterWrite = 0x80000000 /* disable cluster write */ - MntNFS4ACLs = 0x00000010 -) - -type Statfs struct { - Bsize uint32 - Iosize int32 - Blocks uint64 - Bfree uint64 - Bavail uint64 - Files uint64 - Ffree uint64 - Fsid Fsid - Owner uint32 - Type uint32 - Flags uint32 - Fssubtype uint32 - Fstypename [16]int8 - Mntonname [1024]int8 - Mntfromname [1024]int8 - Reserved [8]uint32 -}