@@ -5,14 +5,78 @@ package disk
5
5
6
6
import (
7
7
"context"
8
+ "regexp"
9
+ "strings"
8
10
11
+ "golang.org/x/sys/unix"
9
12
"github.com/shirou/gopsutil/v3/internal/common"
10
13
)
11
14
15
+ var whiteSpaces = regexp .MustCompile (`\s+` )
16
+ var startBlank = regexp .MustCompile (`^\s+` )
17
+
18
+ var ignoreFSType = map [string ]bool {"procfs" : true }
19
+ var FSType = map [int ]string {
20
+ 0 : "jfs2" , 1 : "namefs" , 2 : "nfs" , 3 : "jfs" , 5 : "cdrom" , 6 : "proc" ,
21
+ 16 : "special-fs" , 17 : "cache-fs" , 18 : "nfs3" , 19 : "automount-fs" , 20 : "pool-fs" , 32 : "vxfs" ,
22
+ 33 : "veritas-fs" , 34 : "udfs" , 35 : "nfs4" , 36 : "nfs4-pseudo" , 37 : "smbfs" , 38 : "mcr-pseudofs" ,
23
+ 39 : "ahafs" , 40 : "sterm-nfs" , 41 : "asmfs" ,
24
+ }
25
+
12
26
func PartitionsWithContext (ctx context.Context , all bool ) ([]PartitionStat , error ) {
13
- return []PartitionStat {}, common .ErrNotImplementedError
27
+ var ret []PartitionStat
28
+
29
+ out , err := invoke .CommandWithContext (ctx , "mount" )
30
+ if err != nil {
31
+ return nil , err
32
+ }
33
+
34
+ // parse head lines for column names
35
+ colidx := make (map [string ]int )
36
+ lines := strings .Split (string (out ), "\n " )
37
+ if len (lines ) < 3 {
38
+ return nil , common .ErrNotImplementedError
39
+ }
40
+
41
+ idx := 0
42
+ start := 0
43
+ finished := false
44
+ for pos , ch := range lines [1 ] {
45
+ if ch == ' ' && ! finished {
46
+ name := strings .TrimSpace (lines [0 ][start :pos ])
47
+ colidx [name ] = idx
48
+ finished = true
49
+ } else if ch == '-' && finished {
50
+ idx ++
51
+ start = pos
52
+ finished = false
53
+ }
54
+ }
55
+ name := strings .TrimSpace (lines [0 ][start :len (lines [1 ])])
56
+ colidx [name ] = idx
57
+
58
+ for idx := 2 ; idx < len (lines ); idx ++ {
59
+ line := lines [idx ]
60
+ if startBlank .MatchString (line ) {
61
+ line = "localhost" + line
62
+ }
63
+ p := whiteSpaces .Split (lines [idx ], 6 )
64
+ if len (p ) < 5 || ignoreFSType [p [colidx ["vfs" ]]] {
65
+ continue
66
+ }
67
+ d := PartitionStat {
68
+ Device : p [colidx ["mounted" ]],
69
+ Mountpoint : p [colidx ["mounted over" ]],
70
+ Fstype : p [colidx ["vfs" ]],
71
+ Opts : strings .Split (p [colidx ["options" ]], "," ),
72
+ }
73
+
74
+ ret = append (ret , d )
75
+ }
76
+
77
+ return ret , nil
14
78
}
15
79
16
- func UsageWithContext ( ctx context. Context , path string ) ( * UsageStat , error ) {
17
- return nil , common . ErrNotImplementedError
80
+ func getFsType ( stat unix. Statfs_t ) string {
81
+ return FSType [ int ( stat . Vfstype )]
18
82
}
0 commit comments