-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathfs_infos.go
85 lines (77 loc) · 1.82 KB
/
fs_infos.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
package statgo
// #cgo LDFLAGS: -lstatgrab
// #include <statgrab.h>
import "C"
import (
"fmt"
"unsafe"
)
// FSInfos contains filesystem & mountpoints informations
type FSInfos struct {
DeviceName string
FSType string
MountPoint string
// Size, Used, Free, Available are expressed in bytes
Size int
Used int
Free int
Available int
// Inodes count
TotalInodes int
UsedInodes int
FreeInodes int
AvailableInodes int
}
// FSInfos return an FSInfo struct per mounted filesystem
// Go equivalent to sg_get_fs_stats
func (s *Stat) FSInfos() []*FSInfos {
s.Lock()
defer s.Unlock()
var fsSize C.size_t
var cArray *C.sg_fs_stats = C.sg_get_fs_stats(&fsSize)
length := int(fsSize)
slice := (*[1 << 16]C.sg_fs_stats)(unsafe.Pointer(cArray))[:length:length]
var res []*FSInfos
for _, v := range slice {
f := &FSInfos{
DeviceName: C.GoString(v.device_name),
FSType: C.GoString(v.fs_type),
MountPoint: C.GoString(v.mnt_point),
Size: int(v.size),
Used: int(v.used),
Free: int(v.free),
Available: int(v.avail),
TotalInodes: int(v.total_inodes),
UsedInodes: int(v.used_inodes),
FreeInodes: int(v.free_inodes),
AvailableInodes: int(v.avail_inodes),
}
res = append(res, f)
}
return res
}
func (fs *FSInfos) String() string {
return fmt.Sprintf(
"DeviceName:\t\t\t%s\n"+
"FSType:\t\t\t\t%s\n"+
"MountPoint:\t\t\t%s\n"+
"Size:\t\t\t\t%d\n"+
"Used:\t\t\t\t%d\n"+
"Free:\t\t\t\t%d\n"+
"Available:\t\t\t%d\n"+
"TotalInodes:\t\t%d\n"+
"UsedInodes:\t\t\t%d\n"+
"FreeInodes:\t\t\t%d\n"+
"AvailableInodes:\t%d\n",
fs.DeviceName,
fs.FSType,
fs.MountPoint,
fs.Size,
fs.Used,
fs.Free,
fs.Available,
fs.TotalInodes,
fs.UsedInodes,
fs.FreeInodes,
fs.AvailableInodes)
}