-
Notifications
You must be signed in to change notification settings - Fork 0
/
rados_xattr.go
65 lines (56 loc) · 1.43 KB
/
rados_xattr.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
package gorados
/*
#cgo LDFLAGS: -lrados
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rados/librados.h"
*/
import "C"
import (
"errors"
"unsafe"
)
const MAX_XATTR_LEN = 255
func (r *RadosIoCtx) GetXattr(oid, name string) (string, error) {
var buf [MAX_XATTR_LEN]C.char
cname := C.CString(name)
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(cname))
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_getxattr(*r.ctx, coid, cname, &buf[0], MAX_XATTR_LEN)
if cerr < 0 {
return "", errors.New("get xattr failed")
}
return C.GoString(&buf[0]), nil
}
func (r *RadosIoCtx) SetXattr(oid, name, value string) error {
cname := C.CString(name)
coid := C.CString(oid)
cvalue := C.CString(value)
defer func(){
C.free(unsafe.Pointer(cname))
C.free(unsafe.Pointer(coid))
C.free(unsafe.Pointer(cvalue))
}()
cerr := C.rados_setxattr(*r.ctx, coid, cname, cvalue, C.size_t(len(value)))
if cerr < 0 {
return errors.New("set xattr failed")
}
return nil
}
func (r *RadosIoCtx) RmXattr(oid, name string) error {
cname := C.CString(name)
coid := C.CString(oid)
defer func(){
C.free(unsafe.Pointer(cname))
C.free(unsafe.Pointer(coid))
}()
cerr := C.rados_rmxattr(*r.ctx, coid, cname)
if cerr < 0 {
return errors.New("delete xattr failed")
}
return nil
}