...
1
2
3 package disk
4
5 import (
6 "context"
7
8 "github.com/shirou/gopsutil/internal/common"
9 "golang.org/x/sys/unix"
10 )
11
12
13
14 func PartitionsWithContext(ctx context.Context, all bool) ([]PartitionStat, error) {
15 var ret []PartitionStat
16
17 count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
18 if err != nil {
19 return ret, err
20 }
21 fs := make([]unix.Statfs_t, count)
22 if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
23 return ret, err
24 }
25 for _, stat := range fs {
26 opts := "rw"
27 if stat.Flags&unix.MNT_RDONLY != 0 {
28 opts = "ro"
29 }
30 if stat.Flags&unix.MNT_SYNCHRONOUS != 0 {
31 opts += ",sync"
32 }
33 if stat.Flags&unix.MNT_NOEXEC != 0 {
34 opts += ",noexec"
35 }
36 if stat.Flags&unix.MNT_NOSUID != 0 {
37 opts += ",nosuid"
38 }
39 if stat.Flags&unix.MNT_UNION != 0 {
40 opts += ",union"
41 }
42 if stat.Flags&unix.MNT_ASYNC != 0 {
43 opts += ",async"
44 }
45 if stat.Flags&unix.MNT_DONTBROWSE != 0 {
46 opts += ",nobrowse"
47 }
48 if stat.Flags&unix.MNT_AUTOMOUNTED != 0 {
49 opts += ",automounted"
50 }
51 if stat.Flags&unix.MNT_JOURNALED != 0 {
52 opts += ",journaled"
53 }
54 if stat.Flags&unix.MNT_MULTILABEL != 0 {
55 opts += ",multilabel"
56 }
57 if stat.Flags&unix.MNT_NOATIME != 0 {
58 opts += ",noatime"
59 }
60 if stat.Flags&unix.MNT_NODEV != 0 {
61 opts += ",nodev"
62 }
63 d := PartitionStat{
64 Device: common.ByteToString(stat.Mntfromname[:]),
65 Mountpoint: common.ByteToString(stat.Mntonname[:]),
66 Fstype: common.ByteToString(stat.Fstypename[:]),
67 Opts: opts,
68 }
69
70 ret = append(ret, d)
71 }
72
73 return ret, nil
74 }
75
76 func getFsType(stat unix.Statfs_t) string {
77 return common.ByteToString(stat.Fstypename[:])
78 }
79
View as plain text