2016-07-17 22:03:23 +00:00
|
|
|
// FUSE main Fs
|
|
|
|
|
2020-01-08 08:02:01 +00:00
|
|
|
// +build linux,go1.11 darwin,go1.11 freebsd,go1.11
|
2016-07-17 22:03:23 +00:00
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2019-05-07 12:53:16 +00:00
|
|
|
"context"
|
2017-05-11 12:15:51 +00:00
|
|
|
"syscall"
|
|
|
|
|
2016-07-17 22:03:23 +00:00
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2017-05-02 21:35:07 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
|
|
|
"github.com/rclone/rclone/fs/log"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
"github.com/rclone/rclone/vfs/vfsflags"
|
2016-07-17 22:03:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// FS represents the top level filing system
|
|
|
|
type FS struct {
|
2017-10-28 19:01:34 +00:00
|
|
|
*vfs.VFS
|
2017-05-02 21:35:07 +00:00
|
|
|
f fs.Fs
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
2019-04-30 12:06:24 +00:00
|
|
|
// Check interface satisfied
|
2016-07-17 22:03:23 +00:00
|
|
|
var _ fusefs.FS = (*FS)(nil)
|
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
// NewFS makes a new FS
|
|
|
|
func NewFS(f fs.Fs) *FS {
|
|
|
|
fsys := &FS{
|
2017-10-29 11:00:56 +00:00
|
|
|
VFS: vfs.New(f, &vfsflags.Opt),
|
2017-10-28 19:01:34 +00:00
|
|
|
f: f,
|
2016-09-09 07:39:19 +00:00
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
return fsys
|
2016-09-09 07:39:19 +00:00
|
|
|
}
|
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
// Root returns the root node
|
2017-05-09 10:39:33 +00:00
|
|
|
func (f *FS) Root() (node fusefs.Node, err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace("", "")("node=%+v, err=%v", &node, &err)
|
2017-10-28 19:01:34 +00:00
|
|
|
root, err := f.VFS.Root()
|
2017-05-04 20:49:06 +00:00
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return nil, translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
return &Dir{root}, nil
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2016-11-20 22:54:03 +00:00
|
|
|
|
2019-04-30 12:06:24 +00:00
|
|
|
// Check interface satisfied
|
2016-11-20 22:54:03 +00:00
|
|
|
var _ fusefs.FSStatfser = (*FS)(nil)
|
|
|
|
|
|
|
|
// Statfs is called to obtain file system metadata.
|
|
|
|
// It should write that data to resp.
|
2017-05-09 10:39:33 +00:00
|
|
|
func (f *FS) Statfs(ctx context.Context, req *fuse.StatfsRequest, resp *fuse.StatfsResponse) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace("", "")("stat=%+v, err=%v", resp, &err)
|
2016-11-20 22:54:03 +00:00
|
|
|
const blockSize = 4096
|
|
|
|
const fsBlocks = (1 << 50) / blockSize
|
|
|
|
resp.Blocks = fsBlocks // Total data blocks in file system.
|
|
|
|
resp.Bfree = fsBlocks // Free blocks in file system.
|
|
|
|
resp.Bavail = fsBlocks // Free blocks in file system if you're not root.
|
2019-09-05 12:59:06 +00:00
|
|
|
resp.Files = 1e9 // Total files in file system.
|
|
|
|
resp.Ffree = 1e9 // Free files in file system.
|
2016-11-20 22:54:03 +00:00
|
|
|
resp.Bsize = blockSize // Block size
|
|
|
|
resp.Namelen = 255 // Maximum file name length?
|
|
|
|
resp.Frsize = blockSize // Fragment size, smallest addressable data size in the file system.
|
2018-04-17 22:19:34 +00:00
|
|
|
total, used, free := f.VFS.Statfs()
|
|
|
|
if total >= 0 {
|
|
|
|
resp.Blocks = uint64(total) / blockSize
|
|
|
|
}
|
|
|
|
if used >= 0 {
|
|
|
|
resp.Bfree = resp.Blocks - uint64(used)/blockSize
|
|
|
|
}
|
|
|
|
if free >= 0 {
|
|
|
|
resp.Bavail = uint64(free) / blockSize
|
|
|
|
}
|
2018-06-26 08:26:34 +00:00
|
|
|
mountlib.ClipBlocks(&resp.Blocks)
|
|
|
|
mountlib.ClipBlocks(&resp.Bfree)
|
|
|
|
mountlib.ClipBlocks(&resp.Bavail)
|
2016-11-20 22:54:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2017-05-04 20:49:06 +00:00
|
|
|
|
2017-05-02 21:35:07 +00:00
|
|
|
// Translate errors from mountlib
|
|
|
|
func translateError(err error) error {
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-10-28 19:16:03 +00:00
|
|
|
switch errors.Cause(err) {
|
|
|
|
case vfs.OK:
|
|
|
|
return nil
|
|
|
|
case vfs.ENOENT:
|
|
|
|
return fuse.ENOENT
|
|
|
|
case vfs.EEXIST:
|
|
|
|
return fuse.EEXIST
|
2017-11-03 11:35:36 +00:00
|
|
|
case vfs.EPERM:
|
|
|
|
return fuse.EPERM
|
|
|
|
case vfs.ECLOSED:
|
|
|
|
return fuse.Errno(syscall.EBADF)
|
|
|
|
case vfs.ENOTEMPTY:
|
|
|
|
return fuse.Errno(syscall.ENOTEMPTY)
|
2017-10-28 19:16:03 +00:00
|
|
|
case vfs.ESPIPE:
|
|
|
|
return fuse.Errno(syscall.ESPIPE)
|
|
|
|
case vfs.EBADF:
|
|
|
|
return fuse.Errno(syscall.EBADF)
|
|
|
|
case vfs.EROFS:
|
|
|
|
return fuse.Errno(syscall.EROFS)
|
2017-10-30 10:14:39 +00:00
|
|
|
case vfs.ENOSYS:
|
2017-10-31 15:33:08 +00:00
|
|
|
return fuse.ENOSYS
|
2018-02-23 22:39:28 +00:00
|
|
|
case vfs.EINVAL:
|
|
|
|
return fuse.Errno(syscall.EINVAL)
|
2017-05-02 21:35:07 +00:00
|
|
|
}
|
|
|
|
return err
|
2017-05-04 20:49:06 +00:00
|
|
|
}
|