From bbf5fecf90ab3cd05114cf1d3f3c4fbf8f84ea72 Mon Sep 17 00:00:00 2001 From: John Mulligan Date: Wed, 25 Mar 2020 09:45:53 -0400 Subject: [PATCH] cephfs: split basic path management funcs into new files Continue organizing the cephfs functionality by creating new files for the most basic path management functions: MakeDir, RemoveDir, ChangeDir, CurrentDir Similarly, the dedicated test functions for those items are moved into a new file as well. Signed-off-by: John Mulligan --- cephfs/cephfs.go | 33 ----------------------- cephfs/cephfs_test.go | 52 ------------------------------------ cephfs/path.go | 46 ++++++++++++++++++++++++++++++++ cephfs/path_test.go | 62 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 85 deletions(-) create mode 100644 cephfs/path.go create mode 100644 cephfs/path_test.go diff --git a/cephfs/cephfs.go b/cephfs/cephfs.go index 25f3d30..25ecd50 100644 --- a/cephfs/cephfs.go +++ b/cephfs/cephfs.go @@ -155,39 +155,6 @@ func (mount *MountInfo) SyncFs() error { return getError(ret) } -// CurrentDir gets the current working directory. -func (mount *MountInfo) CurrentDir() string { - cDir := C.ceph_getcwd(mount.mount) - return C.GoString(cDir) -} - -// ChangeDir changes the current working directory. -func (mount *MountInfo) ChangeDir(path string) error { - cPath := C.CString(path) - defer C.free(unsafe.Pointer(cPath)) - - ret := C.ceph_chdir(mount.mount, cPath) - return getError(ret) -} - -// MakeDir creates a directory. -func (mount *MountInfo) MakeDir(path string, mode uint32) error { - cPath := C.CString(path) - defer C.free(unsafe.Pointer(cPath)) - - ret := C.ceph_mkdir(mount.mount, cPath, C.mode_t(mode)) - return getError(ret) -} - -// RemoveDir removes a directory. -func (mount *MountInfo) RemoveDir(path string) error { - cPath := C.CString(path) - defer C.free(unsafe.Pointer(cPath)) - - ret := C.ceph_rmdir(mount.mount, cPath) - return getError(ret) -} - // Chmod changes the mode bits (permissions) of a file/directory. func (mount *MountInfo) Chmod(path string, mode uint32) error { cPath := C.CString(path) diff --git a/cephfs/cephfs_test.go b/cephfs/cephfs_test.go index da18aaf..3711fea 100644 --- a/cephfs/cephfs_test.go +++ b/cephfs/cephfs_test.go @@ -110,58 +110,6 @@ func TestSyncFs(t *testing.T) { assert.NoError(t, err) } -func TestChangeDir(t *testing.T) { - mount := fsConnect(t) - defer fsDisconnect(t, mount) - - dir1 := mount.CurrentDir() - assert.NotNil(t, dir1) - - err := mount.MakeDir("/asdf", 0755) - assert.NoError(t, err) - - err = mount.ChangeDir("/asdf") - assert.NoError(t, err) - - dir2 := mount.CurrentDir() - assert.NotNil(t, dir2) - - assert.NotEqual(t, dir1, dir2) - assert.Equal(t, dir1, "/") - assert.Equal(t, dir2, "/asdf") - - err = mount.ChangeDir("/") - assert.NoError(t, err) - err = mount.RemoveDir("/asdf") - assert.NoError(t, err) -} - -func TestRemoveDir(t *testing.T) { - useMount(t) - - dirname := "one" - localPath := path.Join(CephMountDir, dirname) - mount := fsConnect(t) - defer fsDisconnect(t, mount) - - err := mount.MakeDir(dirname, 0755) - assert.NoError(t, err) - - err = mount.SyncFs() - assert.NoError(t, err) - - // os.Stat the actual mounted location to verify Makedir/RemoveDir - _, err = os.Stat(localPath) - assert.NoError(t, err) - - err = mount.RemoveDir(dirname) - assert.NoError(t, err) - - _, err = os.Stat(localPath) - assert.EqualError(t, err, - fmt.Sprintf("stat %s: no such file or directory", localPath)) -} - func TestUnmountMount(t *testing.T) { t.Run("neverMounted", func(t *testing.T) { mount, err := CreateMount() diff --git a/cephfs/path.go b/cephfs/path.go new file mode 100644 index 0000000..5bde60e --- /dev/null +++ b/cephfs/path.go @@ -0,0 +1,46 @@ +package cephfs + +/* +#cgo LDFLAGS: -lcephfs +#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64 +#include +#include +*/ +import "C" + +import ( + "unsafe" +) + +// CurrentDir gets the current working directory. +func (mount *MountInfo) CurrentDir() string { + cDir := C.ceph_getcwd(mount.mount) + return C.GoString(cDir) +} + +// ChangeDir changes the current working directory. +func (mount *MountInfo) ChangeDir(path string) error { + cPath := C.CString(path) + defer C.free(unsafe.Pointer(cPath)) + + ret := C.ceph_chdir(mount.mount, cPath) + return getError(ret) +} + +// MakeDir creates a directory. +func (mount *MountInfo) MakeDir(path string, mode uint32) error { + cPath := C.CString(path) + defer C.free(unsafe.Pointer(cPath)) + + ret := C.ceph_mkdir(mount.mount, cPath, C.mode_t(mode)) + return getError(ret) +} + +// RemoveDir removes a directory. +func (mount *MountInfo) RemoveDir(path string) error { + cPath := C.CString(path) + defer C.free(unsafe.Pointer(cPath)) + + ret := C.ceph_rmdir(mount.mount, cPath) + return getError(ret) +} diff --git a/cephfs/path_test.go b/cephfs/path_test.go new file mode 100644 index 0000000..2d593d4 --- /dev/null +++ b/cephfs/path_test.go @@ -0,0 +1,62 @@ +package cephfs + +import ( + "fmt" + "os" + "path" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestChangeDir(t *testing.T) { + mount := fsConnect(t) + defer fsDisconnect(t, mount) + + dir1 := mount.CurrentDir() + assert.NotNil(t, dir1) + + err := mount.MakeDir("/asdf", 0755) + assert.NoError(t, err) + + err = mount.ChangeDir("/asdf") + assert.NoError(t, err) + + dir2 := mount.CurrentDir() + assert.NotNil(t, dir2) + + assert.NotEqual(t, dir1, dir2) + assert.Equal(t, dir1, "/") + assert.Equal(t, dir2, "/asdf") + + err = mount.ChangeDir("/") + assert.NoError(t, err) + err = mount.RemoveDir("/asdf") + assert.NoError(t, err) +} + +func TestRemoveDir(t *testing.T) { + useMount(t) + + dirname := "one" + localPath := path.Join(CephMountDir, dirname) + mount := fsConnect(t) + defer fsDisconnect(t, mount) + + err := mount.MakeDir(dirname, 0755) + assert.NoError(t, err) + + err = mount.SyncFs() + assert.NoError(t, err) + + // os.Stat the actual mounted location to verify Makedir/RemoveDir + _, err = os.Stat(localPath) + assert.NoError(t, err) + + err = mount.RemoveDir(dirname) + assert.NoError(t, err) + + _, err = os.Stat(localPath) + assert.EqualError(t, err, + fmt.Sprintf("stat %s: no such file or directory", localPath)) +}