forked from muesli/duf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmounts_freebsd.go
130 lines (117 loc) · 2.58 KB
/
mounts_freebsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// +build freebsd
package main
import (
"golang.org/x/sys/unix"
)
func (m *Mount) Stat() unix.Statfs_t {
return m.Metadata.(unix.Statfs_t)
}
func mounts() ([]Mount, []string, error) {
var ret []Mount
var warnings []string
count, err := unix.Getfsstat(nil, unix.MNT_WAIT)
if err != nil {
return nil, nil, err
}
fs := make([]unix.Statfs_t, count)
if _, err = unix.Getfsstat(fs, unix.MNT_WAIT); err != nil {
return nil, nil, 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_SUIDDIR != 0 {
opts += ",suiddir"
}
if stat.Flags&unix.MNT_SOFTDEP != 0 {
opts += ",softdep"
}
if stat.Flags&unix.MNT_NOSYMFOLLOW != 0 {
opts += ",nosymfollow"
}
if stat.Flags&unix.MNT_GJOURNAL != 0 {
opts += ",gjournal"
}
if stat.Flags&unix.MNT_MULTILABEL != 0 {
opts += ",multilabel"
}
if stat.Flags&unix.MNT_ACLS != 0 {
opts += ",acls"
}
if stat.Flags&unix.MNT_NOATIME != 0 {
opts += ",noatime"
}
if stat.Flags&unix.MNT_NOCLUSTERR != 0 {
opts += ",noclusterr"
}
if stat.Flags&unix.MNT_NOCLUSTERW != 0 {
opts += ",noclusterw"
}
if stat.Flags&unix.MNT_NFS4ACLS != 0 {
opts += ",nfsv4acls"
}
device := byteToString(stat.Mntfromname[:])
mountPoint := byteToString(stat.Mntonname[:])
fsType := byteToString(stat.Fstypename[:])
if len(device) == 0 {
continue
}
d := Mount{
Device: device,
Mountpoint: mountPoint,
Fstype: fsType,
Type: fsType,
Opts: opts,
Metadata: stat,
Total: (uint64(stat.Blocks) * uint64(stat.Bsize)),
Free: (uint64(stat.Bavail) * uint64(stat.Bsize)),
Used: (uint64(stat.Blocks) - uint64(stat.Bfree)) * uint64(stat.Bsize),
Inodes: stat.Files,
InodesFree: uint64(stat.Ffree),
InodesUsed: stat.Files - uint64(stat.Ffree),
Blocks: uint64(stat.Blocks),
BlockSize: uint64(stat.Bsize),
}
d.DeviceType = deviceType(d)
ret = append(ret, d)
}
return ret, warnings, nil
}
func byteToString(orig []byte) string {
n := -1
l := -1
for i, b := range orig {
// skip left side null
if l == -1 && b == 0 {
continue
}
if l == -1 {
l = i
}
if b == 0 {
break
}
n = i + 1
}
if n == -1 {
return string(orig)
}
return string(orig[l:n])
}