-
Notifications
You must be signed in to change notification settings - Fork 5
/
darwin.go
43 lines (35 loc) · 915 Bytes
/
darwin.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
package gomounts
/*
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
*/
import "C"
import (
"errors"
"unsafe"
"sync"
"reflect"
)
var mtx sync.Mutex = sync.Mutex{}
func getMountedVolumes() ([]Volume, error) {
// getmntinfo is non-reentrant
mtx.Lock()
defer mtx.Unlock()
result := make([]Volume, 0)
var mntbuf *C.struct_statfs
count := int(C.getmntinfo(&mntbuf, C.MNT_NOWAIT))
if count == -1 {
return result, errors.New("Failure calling getmntinfo")
}
// Convert to go slice per https://code.google.com/p/go-wiki/wiki/cgo
var mntSlice []C.struct_statfs
sliceHeader := (*reflect.SliceHeader)(unsafe.Pointer(&mntSlice))
sliceHeader.Cap = count
sliceHeader.Len = count
sliceHeader.Data = uintptr(unsafe.Pointer(mntbuf))
for _, v := range mntSlice {
result = append(result, Volume{C.GoString(&v.f_mntonname[0]), C.GoString(&v.f_fstypename[0])})
}
return result, nil
}