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 <jmulligan@redhat.com>
This commit is contained in:
John Mulligan 2020-03-25 09:45:53 -04:00 committed by John Mulligan
parent ce64ef536a
commit bbf5fecf90
4 changed files with 108 additions and 85 deletions

View File

@ -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)

View File

@ -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()

46
cephfs/path.go Normal file
View File

@ -0,0 +1,46 @@
package cephfs
/*
#cgo LDFLAGS: -lcephfs
#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64
#include <stdlib.h>
#include <cephfs/libcephfs.h>
*/
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)
}

62
cephfs/path_test.go Normal file
View File

@ -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))
}