disk: code cleanup (not finish yet)

pull/1530/head
Justin Yang 2 years ago
parent caafa49ed1
commit ec817bfa68

@ -1,5 +1,5 @@
//go:build !darwin && !linux && !freebsd && !openbsd && !windows && !solaris && !aix //go:build !darwin && !linux && !freebsd && !openbsd && !netbsd && !windows && !solaris && !aix
// +build !darwin,!linux,!freebsd,!openbsd,!windows,!solaris,!aix // +build !darwin,!linux,!freebsd,!openbsd,!netbsd,!windows,!solaris,!aix
package disk package disk

@ -1,5 +1,5 @@
//go:build openbsd //go:build netbsd
// +build openbsd // +build netbsd
package disk package disk
@ -7,24 +7,37 @@ import (
"bytes" "bytes"
"context" "context"
"encoding/binary" "encoding/binary"
"unsafe"
"github.com/shirou/gopsutil/v3/internal/common" "github.com/shirou/gopsutil/v3/internal/common"
"golang.org/x/sys/unix" "golang.org/x/sys/unix"
) )
const (
// see sys/fstypes.h and `man 5 statvfs`
MNT_RDONLY = 0x00000001 /* read only filesystem */
MNT_SYNCHRONOUS = 0x00000002 /* file system written synchronously */
MNT_NOEXEC = 0x00000004 /* can't exec from filesystem */
MNT_NOSUID = 0x00000008 /* don't honor setuid bits on fs */
MNT_NODEV = 0x00000010 /* don't interpret special files */
MNT_ASYNC = 0x00000040 /* file system written asynchronously */
MNT_NOATIME = 0x04000000 /* Never update access times in fs */
MNT_SOFTDEP = 0x80000000 /* Use soft dependencies */
)
func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) { func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
var ret []PartitionStat var ret []PartitionStat
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h
// get required buffer size // get required buffer size
r, _, err = unix.Syscall6( r, _, err := unix.Syscall(
483, // SYS___getvfsstat90 syscall 483, // SYS___getvfsstat90 syscall
nil, 0,
0, 0,
uintptr(unsafe.Pointer(&flag)), uintptr(unsafe.Pointer(&flag)),
) )
if err != nil { if err != 0 {
return ret, err return ret, err
} }
mountedFsCount := uint64(r) mountedFsCount := uint64(r)
@ -34,17 +47,43 @@ func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, erro
buf := make([]Statvfs, bufSize) buf := make([]Statvfs, bufSize)
// request agian to get desired mount data // request agian to get desired mount data
_, _, err = unix.Syscall6( _, _, err = unix.Syscall(
483, 483,
uintptr(unsafe.Pointer(&buf[0])), uintptr(unsafe.Pointer(&buf[0])),
uintptr(unsafe.Pointer(&bufSize)), uintptr(unsafe.Pointer(&bufSize)),
uintptr(unsafe.Pointer(&flag)), uintptr(unsafe.Pointer(&flag)),
) )
if err != nil { if err != 0 {
return ret, err return ret, err
} }
for _, stat := range buf { for _, stat := range buf {
opts := []string{"rw"}
if stat.Flag&MNT_RDONLY != 0 {
opts = []string{"rw"}
}
if stat.Flag&MNT_SYNCHRONOUS != 0 {
opts = append(opts, "sync")
}
if stat.Flag&MNT_NOEXEC != 0 {
opts = append(opts, "noexec")
}
if stat.Flag&MNT_NOSUID != 0 {
opts = append(opts, "nosuid")
}
if stat.Flag&MNT_NODEV != 0 {
opts = append(opts, "nodev")
}
if stat.Flag&MNT_ASYNC != 0 {
opts = append(opts, "async")
}
if stat.Flag&MNT_SOFTDEP != 0 {
opts = append(opts, "softdep")
}
if stat.Flag&MNT_NOATIME != 0 {
opts = append(opts, "noatime")
}
d := PartitionStat{ d := PartitionStat{
Device: common.ByteToString([]byte(stat.Mntfromname[:])), Device: common.ByteToString([]byte(stat.Mntfromname[:])),
Mountpoint: common.ByteToString([]byte(stat.Mntonname[:])), Mountpoint: common.ByteToString([]byte(stat.Mntonname[:])),
@ -68,13 +107,13 @@ func UsageWithContext(ctx context.Context, path string) (*UsageStat, error) {
flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h flag := uint64(1) // ST_WAIT/MNT_WAIT, see sys/fstypes.h
// request agian to get desired mount data // request agian to get desired mount data
ret, _, err = unix.Syscall6( ret, _, err := unix.Syscall(
485, // SYS___fstatvfs190, see sys/syscall.h 485, // SYS___fstatvfs190, see sys/syscall.h
uintptr(unsafe.Pointer(&path)), uintptr(unsafe.Pointer(&path)),
uintptr(unsafe.Pointer(&stat)), uintptr(unsafe.Pointer(&stat)),
uintptr(unsafe.Pointer(&flag)), uintptr(unsafe.Pointer(&flag)),
) )
if err != nil { if err != 0 {
return ret, err return ret, err
} }

@ -1,3 +1,6 @@
//go:build netbsd && arm64
// +build netbsd,arm64
// Code generated by cmd/cgo -godefs; DO NOT EDIT. // Code generated by cmd/cgo -godefs; DO NOT EDIT.
// cgo -godefs types_netbsd.go // cgo -godefs types_netbsd.go
@ -36,3 +39,7 @@ type (
Mntfromlabel [1024]uint8 Mntfromlabel [1024]uint8
} }
) )
type _Ctype_struct___0 struct {
FsidVal [32]int32
}

@ -1,7 +1,7 @@
//go:build ignore //go:build ignore
// +build ignore // +build ignore
// Hand writing: _Ctype_struct_statvfs // Hand writing: _Ctype_struct___0
/* /*
Input to cgo -godefs. Input to cgo -godefs.
@ -17,7 +17,7 @@ package disk
#include <sys/stdint.h> #include <sys/stdint.h>
#include <machine/ansi.h> #include <machine/ansi.h>
#include <sys/ansi.h> #include <sys/ansi.h>
#include <sys/fstypes.h>
*/ */
import "C" import "C"

Loading…
Cancel
Save