diff --git a/cephfs/command.go b/cephfs/command.go index 0c4b3b3..3ef780c 100644 --- a/cephfs/command.go +++ b/cephfs/command.go @@ -10,6 +10,8 @@ import "C" import ( "unsafe" + + "github.com/ceph/go-ceph/internal/cutil" ) // MdsCommand sends commands to the specified MDS. @@ -36,51 +38,22 @@ func (mount *MountInfo) MdsCommandWithInputBuffer(mdsSpec string, args [][]byte, func (mount *MountInfo) mdsCommand(mdsSpec string, args [][]byte, inputBuffer []byte) (buffer []byte, info string, err error) { spec := C.CString(mdsSpec) defer C.free(unsafe.Pointer(spec)) - - argc := len(args) - argv := make([]*C.char, argc) - - for i, arg := range args { - argv[i] = C.CString(string(arg)) - } - // free all array elements in a single defer - defer func() { - for i := range argv { - C.free(unsafe.Pointer(argv[i])) - } - }() - - var ( - outs, outbuf *C.char - outslen, outbuflen C.size_t - ) - inbuf := C.CString(string(inputBuffer)) - inbufLen := len(inputBuffer) - defer C.free(unsafe.Pointer(inbuf)) + ci := cutil.NewCommandInput(args, inputBuffer) + defer ci.Free() + co := cutil.NewCommandOutput() + defer co.Free() ret := C.ceph_mds_command( - mount.mount, // cephfs mount ref - spec, // mds spec - &argv[0], // cmd array - C.size_t(argc), // cmd array length - inbuf, // bulk input - C.size_t(inbufLen), // length inbuf - &outbuf, // buffer - &outbuflen, // buffer length - &outs, // status string - &outslen) - - if outslen > 0 { - info = C.GoStringN(outs, C.int(outslen)) - C.free(unsafe.Pointer(outs)) - } - if outbuflen > 0 { - buffer = C.GoBytes(unsafe.Pointer(outbuf), C.int(outbuflen)) - C.free(unsafe.Pointer(outbuf)) - } - if ret != 0 { - return nil, info, getError(ret) - } - - return buffer, info, nil + mount.mount, // cephfs mount ref + spec, // mds spec + (**C.char)(ci.Cmd()), + C.size_t(ci.CmdLen()), + (*C.char)(ci.InBuf()), + C.size_t(ci.InBufLen()), + (**C.char)(co.OutBuf()), + (*C.size_t)(co.OutBufLen()), + (**C.char)(co.Outs()), + (*C.size_t)(co.OutsLen())) + buf, status := co.GoValues() + return buf, status, getError(ret) }