-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs.go
132 lines (107 loc) · 3.21 KB
/
zfs.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
131
132
package zfs
// #cgo CFLAGS: -I/usr/include/libzfs
// #cgo CFLAGS: -I/usr/include/libspl
// #cgo CFLAGS: -I/usr/include/x86_64-linux-gnu
// #cgo CFLAGS: -DHAVE_IOCTL_IN_SYS_IOCTL_H=1
// #cgo LDFLAGS: -lzfs
// #include <libzfs.h>
import "C"
import (
"errors"
"fmt"
"strconv"
)
var libzfs *C.libzfs_handle_t
// Zfs is a pointer to libzfs handler
type Zfs struct {
zfs *C.zfs_handle_t
}
func init() {
libzfs = C.libzfs_init()
if libzfs == nil {
panic("Can't initialize libzfs")
}
}
// OpenDataset opens dataset for further operations; Returns Zfs handle
func OpenDataset(dataset string) (handle *Zfs, err error) {
handle = nil
h := new(Zfs)
zfs := C.zfs_open(libzfs, C.CString(dataset), C.ZFS_TYPE_FILESYSTEM)
if zfs == nil {
err = fmt.Errorf("Can't dataset %v", dataset)
} else {
h.zfs = zfs
handle = h
}
return
}
// GetUserQuota returns user quota is bytes
func (z *Zfs) GetUserQuota(uid uint64) (quota uint64, err error) {
prop := C.CString(fmt.Sprintf("userquota@%v", uid))
var buf *C.char
buf = (*C.char)(C.calloc(C.ZFS_MAXPROPLEN, 1))
ret := C.zfs_prop_get_userquota(z.zfs, prop, buf, C.ZFS_MAXPROPLEN, C.B_TRUE)
if ret != 0 {
err = fmt.Errorf("Can't get user quota for uid %v", uid)
} else {
quota, _ = strconv.ParseUint(C.GoString(buf), 0, 64)
}
return
}
// GetUserUsed returns user usage
func (z *Zfs) GetUserUsed(uid uint64) (quota uint64, err error) {
prop := C.CString(fmt.Sprintf("userused@%v", uid))
var buf *C.char
buf = (*C.char)(C.calloc(C.ZFS_MAXPROPLEN, 1))
ret := C.zfs_prop_get_userquota(z.zfs, prop, buf, C.ZFS_MAXPROPLEN, C.B_TRUE)
if ret != 0 {
err = fmt.Errorf("Can't get user quota for uid %v", uid)
} else {
quota, _ = strconv.ParseUint(C.GoString(buf), 0, 64)
}
return
}
// GetGroupQuota returns group quota
func (z *Zfs) GetGroupQuota(gid uint64) (quota uint64, err error) {
prop := C.CString(fmt.Sprintf("groupquota@%v", gid))
var buf *C.char
buf = (*C.char)(C.calloc(C.ZFS_MAXPROPLEN, 1))
ret := C.zfs_prop_get_userquota(z.zfs, prop, buf, C.ZFS_MAXPROPLEN, C.B_TRUE)
if ret != 0 {
err = fmt.Errorf("Can't get group quota for gid %v", gid)
} else {
quota, _ = strconv.ParseUint(C.GoString(buf), 0, 64)
}
return
}
// GetGroupUsed returns group usage
func (z *Zfs) GetGroupUsed(gid uint64) (quota uint64, err error) {
prop := C.CString(fmt.Sprintf("groupused@%v", gid))
var buf *C.char
buf = (*C.char)(C.calloc(C.ZFS_MAXPROPLEN, 1))
ret := C.zfs_prop_get_userquota(z.zfs, prop, buf, C.ZFS_MAXPROPLEN, C.B_TRUE)
if ret != 0 {
err = fmt.Errof("Can't get group used for gid %v", gid)
} else {
quota, _ = strconv.ParseUint(C.GoString(buf), 0, 64)
}
return
}
// SetGroupQuota sets group quota
func (z *Zfs) SetGroupQuota(gid uint64, quota string) error {
prop := C.CString(fmt.Sprintf("groupquota@%v", gid))
ret := C.zfs_prop_set(z.zfs, prop, C.CString(quota))
if ret != 0 {
return fmt.Errorf("Unable to set quota '%v' for gid %v", quota, gid)
}
return nil
}
// SetUserQuota sets user quota
func (z *Zfs) SetUserQuota(uid uint64, quota string) error {
prop := C.CString(fmt.Sprintf("userquota@%v", uid))
ret := C.zfs_prop_set(z.zfs, prop, C.CString(quota))
if ret != 0 {
return fmt.Errorf("Unable to set quota '%v' for uid %v", quota, uid)
}
return nil
}