cephfs: add cephfs commands, expand logging

* CephError uses syscall for string version of error
* Add error logging for every function
* Add RemoveDir() function
* Add Unmount() function
* Add Release() function
* Add Chown() function
* Add Chmod() function
This commit is contained in:
Lincoln Thurlow 2018-10-08 21:26:51 -07:00
parent 768c5507d1
commit f3df337fab
2 changed files with 164 additions and 8 deletions

View File

@ -7,17 +7,26 @@ package cephfs
#include <cephfs/libcephfs.h> #include <cephfs/libcephfs.h>
*/ */
import "C" import "C"
import "fmt"
import "unsafe"
// import (
"fmt"
log "github.com/sirupsen/logrus"
"math"
"syscall"
"unsafe"
)
type CephError int type CephError int
func (e CephError) Error() string { func (e CephError) Error() string {
return fmt.Sprintf("cephfs: ret=%d", e) if e == 0 {
return fmt.Sprintf("")
} else {
err := syscall.Errno(uint(math.Abs(float64(e))))
return fmt.Sprintf("cephfs: ret=(%d) %v", e, err)
}
} }
//
type MountInfo struct { type MountInfo struct {
mount *C.struct_ceph_mount_info mount *C.struct_ceph_mount_info
} }
@ -28,15 +37,50 @@ func CreateMount() (*MountInfo, error) {
if ret == 0 { if ret == 0 {
return mount, nil return mount, nil
} else { } else {
log.Errorf("CreateMount: Failed to create mount")
return nil, CephError(ret) return nil, CephError(ret)
} }
} }
func (mount *MountInfo) RemoveDir(path string) error {
c_path := C.CString(path)
defer C.free(unsafe.Pointer(c_path))
ret := C.ceph_rmdir(mount.mount, c_path)
if ret == 0 {
return nil
} else {
log.Errorf("RemoveDir: Failed to remove directory")
return CephError(ret)
}
}
func (mount *MountInfo) Unmount() error {
ret := C.ceph_unmount(mount.mount)
if ret == 0 {
return nil
} else {
log.Errorf("Unmount: Failed to unmount")
return CephError(ret)
}
}
func (mount *MountInfo) Release() error {
ret := C.ceph_release(mount.mount)
if ret == 0 {
return nil
} else {
log.Errorf("Release: Failed to release mount")
return CephError(ret)
}
}
func (mount *MountInfo) ReadDefaultConfigFile() error { func (mount *MountInfo) ReadDefaultConfigFile() error {
ret := C.ceph_conf_read_file(mount.mount, nil) ret := C.ceph_conf_read_file(mount.mount, nil)
if ret == 0 { if ret == 0 {
return nil return nil
} else { } else {
log.Errorf("ReadDefaultConfigFile: Failed to read ceph config")
return CephError(ret) return CephError(ret)
} }
} }
@ -46,6 +90,7 @@ func (mount *MountInfo) Mount() error {
if ret == 0 { if ret == 0 {
return nil return nil
} else { } else {
log.Errorf("Mount: Failed to mount")
return CephError(ret) return CephError(ret)
} }
} }
@ -55,6 +100,7 @@ func (mount *MountInfo) SyncFs() error {
if ret == 0 { if ret == 0 {
return nil return nil
} else { } else {
log.Errorf("Mount: Failed to sync filesystem")
return CephError(ret) return CephError(ret)
} }
} }
@ -72,6 +118,7 @@ func (mount *MountInfo) ChangeDir(path string) error {
if ret == 0 { if ret == 0 {
return nil return nil
} else { } else {
log.Errorf("ChangeDir: Failed to change directory")
return CephError(ret) return CephError(ret)
} }
} }
@ -84,6 +131,46 @@ func (mount *MountInfo) MakeDir(path string, mode uint32) error {
if ret == 0 { if ret == 0 {
return nil return nil
} else { } else {
log.Errorf("MakeDir: Failed to make directory %s", path)
return CephError(ret) return CephError(ret)
} }
} }
func (mount *MountInfo) Chmod(path string, mode uint32) error {
c_path := C.CString(path)
defer C.free(unsafe.Pointer(c_path))
ret := C.ceph_chmod(mount.mount, c_path, C.mode_t(mode))
if ret == 0 {
return nil
} else {
log.Errorf("Chmod: Failed to chmod :%s", path)
return CephError(ret)
}
}
func (mount *MountInfo) Chown(path string, user uint32, group uint32) error {
c_path := C.CString(path)
defer C.free(unsafe.Pointer(c_path))
ret := C.ceph_chown(mount.mount, c_path, C.int(user), C.int(group))
if ret == 0 {
return nil
} else {
log.Errorf("Chown: Failed to chown :%s", path)
return CephError(ret)
}
}
/*
* Helper functions
*/
func (mount *MountInfo) IsMounted() bool {
ret := C.ceph_is_mounted(mount.mount)
return ret == 0
}
func (mount *MountInfo) GetMount() *C.struct_ceph_mount_info {
return mount.mount
}

View File

@ -1,19 +1,26 @@
package cephfs_test package cephfs_test
import "testing" import (
import "github.com/ceph/go-ceph/cephfs" "fmt"
import "github.com/stretchr/testify/assert" "github.com/ceph/go-ceph/cephfs"
"github.com/stretchr/testify/assert"
"io/ioutil"
"os"
"testing"
)
func TestCreateMount(t *testing.T) { func TestCreateMount(t *testing.T) {
mount, err := cephfs.CreateMount() mount, err := cephfs.CreateMount()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, mount) assert.NotNil(t, mount)
assert.True(t, mount.IsMounted())
} }
func TestMountRoot(t *testing.T) { func TestMountRoot(t *testing.T) {
mount, err := cephfs.CreateMount() mount, err := cephfs.CreateMount()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, mount) assert.NotNil(t, mount)
assert.True(t, mount.IsMounted())
err = mount.ReadDefaultConfigFile() err = mount.ReadDefaultConfigFile()
assert.NoError(t, err) assert.NoError(t, err)
@ -26,6 +33,7 @@ func TestSyncFs(t *testing.T) {
mount, err := cephfs.CreateMount() mount, err := cephfs.CreateMount()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, mount) assert.NotNil(t, mount)
assert.True(t, mount.IsMounted())
err = mount.ReadDefaultConfigFile() err = mount.ReadDefaultConfigFile()
assert.NoError(t, err) assert.NoError(t, err)
@ -41,6 +49,7 @@ func TestChangeDir(t *testing.T) {
mount, err := cephfs.CreateMount() mount, err := cephfs.CreateMount()
assert.NoError(t, err) assert.NoError(t, err)
assert.NotNil(t, mount) assert.NotNil(t, mount)
assert.True(t, mount.IsMounted())
err = mount.ReadDefaultConfigFile() err = mount.ReadDefaultConfigFile()
assert.NoError(t, err) assert.NoError(t, err)
@ -64,3 +73,63 @@ func TestChangeDir(t *testing.T) {
assert.Equal(t, dir1, "/") assert.Equal(t, dir1, "/")
assert.Equal(t, dir2, "/asdf") assert.Equal(t, dir2, "/asdf")
} }
func TestRemoveDir(t *testing.T) {
dirname := "/one"
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
assert.True(t, mount.IsMounted())
err = mount.ReadDefaultConfigFile()
assert.NoError(t, err)
err = mount.Mount()
assert.NoError(t, err)
dir1 := mount.CurrentDir()
assert.NotNil(t, dir1)
fmt.Printf("path: %v\n", dir1)
err = mount.MakeDir(dirname, 0755)
assert.NoError(t, err)
err = mount.SyncFs()
assert.NoError(t, err)
files, _ := ioutil.ReadDir("./")
for _, f := range files {
fmt.Println(f.Name())
}
_, err = os.Stat(dirname)
assert.NoError(t, err)
err = mount.RemoveDir(dirname)
assert.NoError(t, err)
_, err = os.Stat(dirname)
assert.EqualError(t, err, fmt.Sprintf("stat %s: no such file or directory", dirname))
}
func TestUnmountMount(t *testing.T) {
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
assert.True(t, mount.IsMounted())
err = mount.Unmount()
assert.NoError(t, err)
assert.False(t, mount.IsMounted())
}
func TestReleaseMount(t *testing.T) {
mount, err := cephfs.CreateMount()
assert.NoError(t, err)
assert.NotNil(t, mount)
assert.True(t, mount.IsMounted())
err = mount.Release()
assert.NoError(t, err)
assert.Nil(t, mount.GetMount())
}