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"
|
|
|
|
"unsafe"
|
2019-12-12 18:27:28 +00:00
|
|
|
|
|
|
|
"github.com/ceph/go-ceph/errutil"
|
2019-12-18 22:27:12 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
2018-10-09 04:26:51 +00:00
|
|
|
)
|
|
|
|
|
2019-12-12 18:54:55 +00:00
|
|
|
type CephFSError int
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2019-12-12 18:54:55 +00:00
|
|
|
func (e CephFSError) Error() string {
|
2019-12-12 18:27:28 +00:00
|
|
|
errno, s := errutil.FormatErrno(int(e))
|
|
|
|
if s == "" {
|
|
|
|
return fmt.Sprintf("cephfs: ret=%d", errno)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
2019-12-12 18:27:28 +00:00
|
|
|
return fmt.Sprintf("cephfs: ret=%d, %s", errno, s)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-12 18:54:55 +00:00
|
|
|
func getError(e C.int) error {
|
|
|
|
if e == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return CephFSError(e)
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// MountInfo exports ceph's ceph_mount_info from libcephfs.cc
|
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
|
|
|
}
|
|
|
|
|
2019-12-19 20:11:50 +00:00
|
|
|
func createMount(id *C.char) (*MountInfo, error) {
|
2015-07-08 08:34:54 +00:00
|
|
|
mount := &MountInfo{}
|
2019-12-19 20:11:50 +00:00
|
|
|
ret := C.ceph_create(&mount.mount, id)
|
2018-10-11 18:36:26 +00:00
|
|
|
if ret != 0 {
|
2019-12-12 18:54:55 +00:00
|
|
|
return nil, getError(ret)
|
2015-07-08 08:34:54 +00:00
|
|
|
}
|
2018-10-11 18:36:26 +00:00
|
|
|
return mount, nil
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 20:11:50 +00:00
|
|
|
// CreateMount creates a mount handle for interacting with Ceph.
|
|
|
|
func CreateMount() (*MountInfo, error) {
|
|
|
|
return createMount(nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateMountWithId creates a mount handle for interacting with Ceph.
|
|
|
|
// The caller can specify a unique id that will identify this client.
|
|
|
|
func CreateMountWithId(id string) (*MountInfo, error) {
|
|
|
|
cid := C.CString(id)
|
|
|
|
defer C.free(unsafe.Pointer(cid))
|
|
|
|
return createMount(cid)
|
|
|
|
}
|
|
|
|
|
2019-12-18 22:27:12 +00:00
|
|
|
// CreateFromRados creates a mount handle using an existing rados cluster
|
|
|
|
// connection.
|
|
|
|
//
|
|
|
|
// Implements:
|
|
|
|
// int ceph_create_from_rados(struct ceph_mount_info **cmount, rados_t cluster);
|
|
|
|
func CreateFromRados(conn *rados.Conn) (*MountInfo, error) {
|
|
|
|
mount := &MountInfo{}
|
|
|
|
ret := C.ceph_create_from_rados(&mount.mount, C.rados_t(conn.Cluster()))
|
|
|
|
if ret != 0 {
|
|
|
|
return nil, getError(ret)
|
|
|
|
}
|
|
|
|
return mount, nil
|
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// ReadDefaultConfigFile loads the ceph configuration from the specified config file.
|
2018-10-10 22:06:47 +00:00
|
|
|
func (mount *MountInfo) ReadDefaultConfigFile() error {
|
|
|
|
ret := C.ceph_conf_read_file(mount.mount, nil)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-10 22:06:47 +00:00
|
|
|
}
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Mount mounts the mount handle.
|
2018-10-10 22:06:47 +00:00
|
|
|
func (mount *MountInfo) Mount() error {
|
|
|
|
ret := C.ceph_mount(mount.mount, nil)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Unmount unmounts the mount handle.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Unmount() error {
|
|
|
|
ret := C.ceph_unmount(mount.mount)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Release destroys the mount handle.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Release() error {
|
|
|
|
ret := C.ceph_release(mount.mount)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// SyncFs synchronizes all filesystem data to persistent media.
|
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)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// CurrentDir gets the current working directory.
|
2015-05-01 19:35:40 +00:00
|
|
|
func (mount *MountInfo) CurrentDir() string {
|
2018-10-11 18:36:26 +00:00
|
|
|
cDir := C.ceph_getcwd(mount.mount)
|
|
|
|
return C.GoString(cDir)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// ChangeDir changes the current working directory.
|
2015-05-01 19:35:40 +00:00
|
|
|
func (mount *MountInfo) ChangeDir(path string) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2015-05-01 19:35:40 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_chdir(mount.mount, cPath)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2015-05-01 19:35:40 +00:00
|
|
|
}
|
2015-05-01 19:54:38 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// MakeDir creates a directory.
|
2015-05-01 19:54:38 +00:00
|
|
|
func (mount *MountInfo) MakeDir(path string, mode uint32) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2015-05-01 19:54:38 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_mkdir(mount.mount, cPath, C.mode_t(mode))
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-10 22:06:47 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// RemoveDir removes a directory.
|
2018-10-10 22:06:47 +00:00
|
|
|
func (mount *MountInfo) RemoveDir(path string) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2018-10-10 22:06:47 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_rmdir(mount.mount, cPath)
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2015-05-01 19:54:38 +00:00
|
|
|
}
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Chmod changes the mode bits (permissions) of a file/directory.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Chmod(path string, mode uint32) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_chmod(mount.mount, cPath, C.mode_t(mode))
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// Chown changes the ownership of a file/directory.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) Chown(path string, user uint32, group uint32) error {
|
2018-10-11 18:36:26 +00:00
|
|
|
cPath := C.CString(path)
|
|
|
|
defer C.free(unsafe.Pointer(cPath))
|
2018-10-09 04:26:51 +00:00
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
ret := C.ceph_chown(mount.mount, cPath, C.int(user), C.int(group))
|
2019-12-12 18:54:55 +00:00
|
|
|
return getError(ret)
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-11 18:36:26 +00:00
|
|
|
// IsMounted checks mount status.
|
2018-10-09 04:26:51 +00:00
|
|
|
func (mount *MountInfo) IsMounted() bool {
|
|
|
|
ret := C.ceph_is_mounted(mount.mount)
|
2018-10-10 22:06:47 +00:00
|
|
|
return ret == 1
|
2018-10-09 04:26:51 +00:00
|
|
|
}
|