2016-11-22 04:52:32 +08:00
|
|
|
// FUSE main Fs
|
|
|
|
|
2021-09-09 20:25:25 +08:00
|
|
|
//go:build linux || (darwin && amd64)
|
2016-11-22 04:52:32 +08:00
|
|
|
|
|
|
|
package mount2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
2024-07-02 17:26:15 +08:00
|
|
|
"time"
|
2016-11-22 04:52:32 +08:00
|
|
|
|
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
|
|
|
"github.com/rclone/rclone/cmd/mountlib"
|
|
|
|
"github.com/rclone/rclone/fs"
|
2021-11-04 18:12:57 +08:00
|
|
|
"github.com/rclone/rclone/fs/fserrors"
|
2016-11-22 04:52:32 +08:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FS represents the top level filing system
|
|
|
|
type FS struct {
|
|
|
|
VFS *vfs.VFS
|
|
|
|
f fs.Fs
|
2020-07-24 00:17:01 +08:00
|
|
|
opt *mountlib.Options
|
2016-11-22 04:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewFS creates a pathfs.FileSystem from the fs.Fs passed in
|
2020-07-24 00:17:01 +08:00
|
|
|
func NewFS(VFS *vfs.VFS, opt *mountlib.Options) *FS {
|
2016-11-22 04:52:32 +08:00
|
|
|
fsys := &FS{
|
2020-07-23 00:58:49 +08:00
|
|
|
VFS: VFS,
|
|
|
|
f: VFS.Fs(),
|
2020-07-24 00:17:01 +08:00
|
|
|
opt: opt,
|
2016-11-22 04:52:32 +08:00
|
|
|
}
|
|
|
|
return fsys
|
|
|
|
}
|
|
|
|
|
|
|
|
// Root returns the root node
|
|
|
|
func (f *FS) Root() (node *Node, err error) {
|
|
|
|
defer log.Trace("", "")("node=%+v, err=%v", &node, &err)
|
|
|
|
root, err := f.VFS.Root()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return newNode(f, root), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDebug if called, provide debug output through the log package.
|
|
|
|
func (f *FS) SetDebug(debug bool) {
|
|
|
|
fs.Debugf(f.f, "SetDebug %v", debug)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the Mode from a vfs Node
|
|
|
|
func getMode(node os.FileInfo) uint32 {
|
|
|
|
Mode := node.Mode().Perm()
|
|
|
|
if node.IsDir() {
|
|
|
|
Mode |= fuse.S_IFDIR
|
|
|
|
} else {
|
|
|
|
Mode |= fuse.S_IFREG
|
|
|
|
}
|
|
|
|
return uint32(Mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill in attr from node
|
|
|
|
func setAttr(node vfs.Node, attr *fuse.Attr) {
|
|
|
|
Size := uint64(node.Size())
|
|
|
|
const BlockSize = 512
|
|
|
|
Blocks := (Size + BlockSize - 1) / BlockSize
|
|
|
|
modTime := node.ModTime()
|
|
|
|
// set attributes
|
|
|
|
vfs := node.VFS()
|
2020-08-20 11:25:03 +08:00
|
|
|
attr.Owner.Gid = vfs.Opt.GID
|
|
|
|
attr.Owner.Uid = vfs.Opt.UID
|
2016-11-22 04:52:32 +08:00
|
|
|
attr.Mode = getMode(node)
|
|
|
|
attr.Size = Size
|
|
|
|
attr.Nlink = 1
|
|
|
|
attr.Blocks = Blocks
|
|
|
|
// attr.Blksize = BlockSize // not supported in freebsd/darwin, defaults to 4k if not set
|
|
|
|
s := uint64(modTime.Unix())
|
|
|
|
ns := uint32(modTime.Nanosecond())
|
|
|
|
attr.Atime = s
|
|
|
|
attr.Atimensec = ns
|
|
|
|
attr.Mtime = s
|
|
|
|
attr.Mtimensec = ns
|
|
|
|
attr.Ctime = s
|
|
|
|
attr.Ctimensec = ns
|
|
|
|
//attr.Rdev
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill in AttrOut from node
|
2020-07-24 00:17:01 +08:00
|
|
|
func (f *FS) setAttrOut(node vfs.Node, out *fuse.AttrOut) {
|
2016-11-22 04:52:32 +08:00
|
|
|
setAttr(node, &out.Attr)
|
2024-07-02 17:26:15 +08:00
|
|
|
out.SetTimeout(time.Duration(f.opt.AttrTimeout))
|
2016-11-22 04:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// fill in EntryOut from node
|
2020-07-24 00:17:01 +08:00
|
|
|
func (f *FS) setEntryOut(node vfs.Node, out *fuse.EntryOut) {
|
2016-11-22 04:52:32 +08:00
|
|
|
setAttr(node, &out.Attr)
|
2024-07-02 17:26:15 +08:00
|
|
|
out.SetEntryTimeout(time.Duration(f.opt.AttrTimeout))
|
|
|
|
out.SetAttrTimeout(time.Duration(f.opt.AttrTimeout))
|
2016-11-22 04:52:32 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate errors from mountlib into Syscall error numbers
|
|
|
|
func translateError(err error) syscall.Errno {
|
|
|
|
if err == nil {
|
|
|
|
return 0
|
|
|
|
}
|
2021-11-04 18:12:57 +08:00
|
|
|
_, uErr := fserrors.Cause(err)
|
|
|
|
switch uErr {
|
2016-11-22 04:52:32 +08:00
|
|
|
case vfs.OK:
|
|
|
|
return 0
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.ENOENT, fs.ErrorDirNotFound, fs.ErrorObjectNotFound:
|
2016-11-22 04:52:32 +08:00
|
|
|
return syscall.ENOENT
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.EEXIST, fs.ErrorDirExists:
|
2016-11-22 04:52:32 +08:00
|
|
|
return syscall.EEXIST
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.EPERM, fs.ErrorPermissionDenied:
|
2016-11-22 04:52:32 +08:00
|
|
|
return syscall.EPERM
|
|
|
|
case vfs.ECLOSED:
|
|
|
|
return syscall.EBADF
|
|
|
|
case vfs.ENOTEMPTY:
|
|
|
|
return syscall.ENOTEMPTY
|
|
|
|
case vfs.ESPIPE:
|
|
|
|
return syscall.ESPIPE
|
|
|
|
case vfs.EBADF:
|
|
|
|
return syscall.EBADF
|
|
|
|
case vfs.EROFS:
|
|
|
|
return syscall.EROFS
|
2020-04-21 23:31:43 +08:00
|
|
|
case vfs.ENOSYS, fs.ErrorNotImplemented:
|
2016-11-22 04:52:32 +08:00
|
|
|
return syscall.ENOSYS
|
|
|
|
case vfs.EINVAL:
|
|
|
|
return syscall.EINVAL
|
|
|
|
}
|
|
|
|
fs.Errorf(nil, "IO error: %v", err)
|
|
|
|
return syscall.EIO
|
|
|
|
}
|