2020-10-29 12:13:41 +00:00
|
|
|
// +build linux,go1.13 freebsd,go1.13
|
2016-07-17 22:03:23 +00:00
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2019-05-07 12:53:16 +00:00
|
|
|
"context"
|
2016-07-17 22:03:23 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2019-07-28 17:47:38 +00:00
|
|
|
"github.com/rclone/rclone/fs/log"
|
|
|
|
"github.com/rclone/rclone/vfs"
|
2016-07-17 22:03:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// File represents a file
|
|
|
|
type File struct {
|
2017-10-28 19:01:34 +00:00
|
|
|
*vfs.File
|
2020-07-23 16:17:01 +00:00
|
|
|
fsys *FS
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.Node = (*File)(nil)
|
|
|
|
|
|
|
|
// Attr fills out the attributes for the file
|
2017-05-09 10:39:33 +00:00
|
|
|
func (f *File) Attr(ctx context.Context, a *fuse.Attr) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(f, "")("a=%+v, err=%v", a, &err)
|
2020-07-23 16:17:01 +00:00
|
|
|
a.Valid = f.fsys.opt.AttrTimeout
|
2017-10-25 09:00:26 +00:00
|
|
|
modTime := f.File.ModTime()
|
|
|
|
Size := uint64(f.File.Size())
|
|
|
|
Blocks := (Size + 511) / 512
|
2017-10-29 11:00:56 +00:00
|
|
|
a.Gid = f.VFS().Opt.GID
|
|
|
|
a.Uid = f.VFS().Opt.UID
|
|
|
|
a.Mode = f.VFS().Opt.FilePerms
|
2017-05-02 21:35:07 +00:00
|
|
|
a.Size = Size
|
|
|
|
a.Atime = modTime
|
|
|
|
a.Mtime = modTime
|
|
|
|
a.Ctime = modTime
|
|
|
|
a.Crtime = modTime
|
|
|
|
a.Blocks = Blocks
|
2016-07-17 22:03:23 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-23 19:41:21 +00:00
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeSetattrer = (*File)(nil)
|
|
|
|
|
2017-11-20 19:42:35 +00:00
|
|
|
// Setattr handles attribute changes from FUSE. Currently supports ModTime and Size only
|
2017-05-09 10:39:33 +00:00
|
|
|
func (f *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(f, "a=%+v", req)("err=%v", &err)
|
2017-11-20 19:42:35 +00:00
|
|
|
if !f.VFS().Opt.NoModTime {
|
|
|
|
if req.Valid.Mtime() {
|
|
|
|
err = f.File.SetModTime(req.Mtime)
|
2018-01-29 20:49:13 +00:00
|
|
|
} else if req.Valid.MtimeNow() {
|
|
|
|
err = f.File.SetModTime(time.Now())
|
2017-11-20 19:42:35 +00:00
|
|
|
}
|
2017-03-23 19:41:21 +00:00
|
|
|
}
|
2017-11-20 19:42:35 +00:00
|
|
|
if req.Valid.Size() {
|
|
|
|
err = f.File.Truncate(int64(req.Size))
|
2017-03-23 19:41:21 +00:00
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
return translateError(err)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeOpener = (*File)(nil)
|
|
|
|
|
|
|
|
// Open the file for read or write
|
2017-03-02 22:07:01 +00:00
|
|
|
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fh fusefs.Handle, err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(f, "flags=%v", req.Flags)("fh=%v, err=%v", &fh, &err)
|
2017-10-30 10:14:39 +00:00
|
|
|
|
|
|
|
// fuse flags are based off syscall flags as are os flags, so
|
|
|
|
// should be compatible
|
2018-03-15 10:27:04 +00:00
|
|
|
handle, err := f.File.Open(int(req.Flags))
|
2017-10-30 10:14:39 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, translateError(err)
|
|
|
|
}
|
|
|
|
|
2019-09-14 12:11:21 +00:00
|
|
|
// If size unknown then use direct io to read
|
2019-10-06 11:06:26 +00:00
|
|
|
if entry := handle.Node().DirEntry(); entry != nil && entry.Size() < 0 {
|
2019-09-14 12:11:21 +00:00
|
|
|
resp.Flags |= fuse.OpenDirectIO
|
|
|
|
}
|
|
|
|
|
2017-11-02 18:22:26 +00:00
|
|
|
return &FileHandle{handle}, nil
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
2017-01-29 11:29:42 +00:00
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeFsyncer = (*File)(nil)
|
|
|
|
|
|
|
|
// Fsync the file
|
|
|
|
//
|
|
|
|
// Note that we don't do anything except return OK
|
2017-05-09 10:39:33 +00:00
|
|
|
func (f *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) (err error) {
|
2018-01-12 16:30:54 +00:00
|
|
|
defer log.Trace(f, "")("err=%v", &err)
|
2017-01-29 11:29:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
2018-05-03 08:15:32 +00:00
|
|
|
|
|
|
|
// Getxattr gets an extended attribute by the given name from the
|
|
|
|
// node.
|
|
|
|
//
|
|
|
|
// If there is no xattr by that name, returns fuse.ErrNoXattr.
|
|
|
|
func (f *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
|
|
|
|
return fuse.ENOSYS // we never implement this
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeGetxattrer = (*File)(nil)
|
|
|
|
|
|
|
|
// Listxattr lists the extended attributes recorded for the node.
|
|
|
|
func (f *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
|
|
|
|
return fuse.ENOSYS // we never implement this
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeListxattrer = (*File)(nil)
|
|
|
|
|
|
|
|
// Setxattr sets an extended attribute with the given name and
|
|
|
|
// value for the node.
|
|
|
|
func (f *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
|
|
|
|
return fuse.ENOSYS // we never implement this
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeSetxattrer = (*File)(nil)
|
|
|
|
|
|
|
|
// Removexattr removes an extended attribute for the name.
|
|
|
|
//
|
|
|
|
// If there is no xattr by that name, returns fuse.ErrNoXattr.
|
|
|
|
func (f *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
|
|
|
|
return fuse.ENOSYS // we never implement this
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.NodeRemovexattrer = (*File)(nil)
|