2015-05-01 19:35:40 +00:00
|
|
|
package cephfs
|
|
|
|
|
|
|
|
/*
|
|
|
|
#cgo LDFLAGS: -lcephfs
|
|
|
|
#cgo CPPFLAGS: -D_FILE_OFFSET_BITS=64
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <cephfs/libcephfs.h>
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
2018-10-09 04:26:51 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"math"
|
|
|
|
"syscall"
|
|
|
|
"unsafe"
|
|
|
|
)
|
|
|
|
|
2015-05-01 19:35:40 +00:00
|
|
|
type CephError int
|
|
|
|
|
|
|
|
func (e CephError) Error() string {
|
2018-10-09 04:26:51 +00:00
|
|
|
if e == 0 {
|
|
|
|
return fmt.Sprintf("")
|
|
|
|
} else {
|
|
|
|
err := syscall.Errno(uint(math.Abs(float64(e))))
|
|
|
|
return fmt.Sprintf("cephfs: ret=(%d) %v", e, err)
|
|
|
|
}
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MountInfo struct {
|
2015-07-08 08:34:54 +00:00
|
|
|
mount *C.struct_ceph_mount_info
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func CreateMount() (*MountInfo, error) {
|
2015-07-08 08:34:54 +00:00
|
|
|
mount := &MountInfo{}
|
|
|
|
ret := C.ceph_create(&mount.mount, nil)
|
|
|
|
if ret == 0 {
|
|
|
|
return mount, nil
|
|
|
|
} else {
|
2018-10-09 04:26:51 +00:00
|
|
|
log.Errorf("CreateMount: Failed to create mount")
|
2015-07-08 08:34:54 +00:00
|
|
|
return nil, CephError(ret)
|
|
|
|
}
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-09 04:26:51 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-01 19:35:40 +00:00
|
|
|
func (mount *MountInfo) ReadDefaultConfigFile() error {
|
2015-07-08 08:34:54 +00:00
|
|
|
ret := C.ceph_conf_read_file(mount.mount, nil)
|
|
|
|
if ret == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
2018-10-09 04:26:51 +00:00
|
|
|
log.Errorf("ReadDefaultConfigFile: Failed to read ceph config")
|
2015-07-08 08:34:54 +00:00
|
|
|
return CephError(ret)
|
|
|
|
}
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mount *MountInfo) Mount() error {
|
2015-07-08 08:34:54 +00:00
|
|
|
ret := C.ceph_mount(mount.mount, nil)
|
|
|
|
if ret == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
2018-10-09 04:26:51 +00:00
|
|
|
log.Errorf("Mount: Failed to mount")
|
2015-07-08 08:34:54 +00:00
|
|
|
return CephError(ret)
|
|
|
|
}
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mount *MountInfo) SyncFs() error {
|
2015-07-08 08:34:54 +00:00
|
|
|
ret := C.ceph_sync_fs(mount.mount)
|
|
|
|
if ret == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
2018-10-09 04:26:51 +00:00
|
|
|
log.Errorf("Mount: Failed to sync filesystem")
|
2015-07-08 08:34:54 +00:00
|
|
|
return CephError(ret)
|
|
|
|
}
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mount *MountInfo) CurrentDir() string {
|
2015-07-08 08:34:54 +00:00
|
|
|
c_dir := C.ceph_getcwd(mount.mount)
|
|
|
|
return C.GoString(c_dir)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (mount *MountInfo) ChangeDir(path string) error {
|
2015-07-08 08:34:54 +00:00
|
|
|
c_path := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(c_path))
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2015-07-08 08:34:54 +00:00
|
|
|
ret := C.ceph_chdir(mount.mount, c_path)
|
|
|
|
if ret == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
2018-10-09 04:26:51 +00:00
|
|
|
log.Errorf("ChangeDir: Failed to change directory")
|
2015-07-08 08:34:54 +00:00
|
|
|
return CephError(ret)
|
|
|
|
}
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
2015-05-01 19:54:38 +00:00
|
|
|
|
|
|
|
func (mount *MountInfo) MakeDir(path string, mode uint32) error {
|
2015-07-08 08:34:54 +00:00
|
|
|
c_path := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(c_path))
|
2015-05-01 19:54:38 +00:00
|
|
|
|
2015-07-08 08:34:54 +00:00
|
|
|
ret := C.ceph_mkdir(mount.mount, c_path, C.mode_t(mode))
|
|
|
|
if ret == 0 {
|
|
|
|
return nil
|
|
|
|
} else {
|
2018-10-09 04:26:51 +00:00
|
|
|
log.Errorf("MakeDir: Failed to make directory %s", path)
|
2015-07-08 08:34:54 +00:00
|
|
|
return CephError(ret)
|
|
|
|
}
|
2015-05-01 19:54:38 +00:00
|
|
|
}
|
2018-10-09 04:26:51 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|