cephfs: begin cephfs wrappers

Signed-off-by: Noah Watkins <noahwatkins@gmail.com>
This commit is contained in:
Noah Watkins 2015-05-01 12:35:40 -07:00
parent 95fa549c14
commit 1efdf980f0
2 changed files with 145 additions and 0 deletions

77
cephfs/cephfs.go Normal file
View File

@ -0,0 +1,77 @@
package cephfs
/*
#cgo LDFLAGS: -lcephfs
#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64
#include <stdlib.h>
#include <cephfs/libcephfs.h>
*/
import "C"
import "fmt"
import "unsafe"
//
type CephError int
func (e CephError) Error() string {
return fmt.Sprintf("cephfs: ret=%d", e)
}
//
type MountInfo struct {
mount *C.struct_ceph_mount_info
}
func CreateMount() (*MountInfo, error) {
mount := &MountInfo{}
ret := C.ceph_create(&mount.mount, nil)
if ret == 0 {
return mount, nil
} else {
return nil, CephError(ret)
}
}
func (mount *MountInfo) ReadDefaultConfigFile() error {
ret := C.ceph_conf_read_file(mount.mount, nil)
if ret == 0 {
return nil
} else {
return CephError(ret)
}
}
func (mount *MountInfo) Mount() error {
ret := C.ceph_mount(mount.mount, nil)
if ret == 0 {
return nil
} else {
return CephError(ret)
}
}
func (mount *MountInfo) SyncFs() error {
ret := C.ceph_sync_fs(mount.mount)
if ret == 0 {
return nil
} else {
return CephError(ret)
}
}
func (mount *MountInfo) CurrentDir() string {
c_dir := C.ceph_getcwd(mount.mount)
return C.GoString(c_dir)
}
func (mount *MountInfo) ChangeDir(path string) error {
c_path := C.CString(path)
defer C.free(unsafe.Pointer(c_path))
ret := C.ceph_chdir(mount.mount, c_path)
if ret == 0 {
return nil
} else {
return CephError(ret)
}
}

68
cephfs/cephfs_test.go Normal file
View File

@ -0,0 +1,68 @@
package cephfs_test
import "testing"
import "github.com/noahdesu/go-ceph/cephfs"
import "github.com/stretchr/testify/assert"
func TestCreateMount(t *testing.T) {
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
}
func TestMountRoot(t *testing.T) {
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
err = mount.ReadDefaultConfigFile()
assert.NoError(t, err)
err = mount.Mount()
assert.NoError(t, err)
}
func TestSyncFs(t *testing.T) {
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
err = mount.ReadDefaultConfigFile()
assert.NoError(t, err)
err = mount.Mount()
assert.NoError(t, err)
err = mount.SyncFs()
assert.NoError(t, err)
}
func TestChangeDir(t *testing.T) {
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
err = mount.ReadDefaultConfigFile()
assert.NoError(t, err)
err = mount.Mount()
assert.NoError(t, err)
err = mount.ChangeDir("/")
assert.NoError(t, err)
}
func TestCurrentDir(t *testing.T) {
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
err = mount.ReadDefaultConfigFile()
assert.NoError(t, err)
err = mount.Mount()
assert.NoError(t, err)
dir := mount.CurrentDir()
assert.NotNil(t, dir)
}