2016-07-18 06:03:23 +08:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2017-05-09 18:39:33 +08:00
|
|
|
"github.com/ncw/rclone/fs"
|
2017-10-29 03:01:34 +08:00
|
|
|
"github.com/ncw/rclone/vfs"
|
2016-07-18 06:03:23 +08:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File represents a file
|
|
|
|
type File struct {
|
2017-10-29 03:01:34 +08:00
|
|
|
*vfs.File
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.Node = (*File)(nil)
|
|
|
|
|
|
|
|
// Attr fills out the attributes for the file
|
2017-05-09 18:39:33 +08:00
|
|
|
func (f *File) Attr(ctx context.Context, a *fuse.Attr) (err error) {
|
|
|
|
defer fs.Trace(f, "")("a=%+v, err=%v", a, &err)
|
2017-10-25 17:00:26 +08:00
|
|
|
modTime := f.File.ModTime()
|
|
|
|
Size := uint64(f.File.Size())
|
|
|
|
Blocks := (Size + 511) / 512
|
2017-10-29 19:00:56 +08:00
|
|
|
a.Gid = f.VFS().Opt.GID
|
|
|
|
a.Uid = f.VFS().Opt.UID
|
|
|
|
a.Mode = f.VFS().Opt.FilePerms
|
2017-05-03 05:35:07 +08:00
|
|
|
a.Size = Size
|
|
|
|
a.Atime = modTime
|
|
|
|
a.Mtime = modTime
|
|
|
|
a.Ctime = modTime
|
|
|
|
a.Crtime = modTime
|
|
|
|
a.Blocks = Blocks
|
2016-07-18 06:03:23 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-24 03:41:21 +08:00
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeSetattrer = (*File)(nil)
|
|
|
|
|
|
|
|
// Setattr handles attribute changes from FUSE. Currently supports ModTime only.
|
2017-05-09 18:39:33 +08:00
|
|
|
func (f *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) (err error) {
|
|
|
|
defer fs.Trace(f, "a=%+v", req)("err=%v", &err)
|
2017-10-29 19:00:56 +08:00
|
|
|
if f.VFS().Opt.NoModTime {
|
2017-03-24 03:41:21 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if req.Valid.MtimeNow() {
|
2017-05-03 05:35:07 +08:00
|
|
|
err = f.File.SetModTime(time.Now())
|
2017-03-24 03:41:21 +08:00
|
|
|
} else if req.Valid.Mtime() {
|
2017-05-03 05:35:07 +08:00
|
|
|
err = f.File.SetModTime(req.Mtime)
|
2017-03-24 03:41:21 +08:00
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
return translateError(err)
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeOpener = (*File)(nil)
|
|
|
|
|
|
|
|
// Open the file for read or write
|
2017-03-03 06:07:01 +08:00
|
|
|
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fh fusefs.Handle, err error) {
|
2017-05-09 18:39:33 +08:00
|
|
|
defer fs.Trace(f, "flags=%v", req.Flags)("fh=%v, err=%v", &fh, &err)
|
2017-10-30 18:14:39 +08:00
|
|
|
|
|
|
|
// fuse flags are based off syscall flags as are os flags, so
|
|
|
|
// should be compatible
|
2017-10-30 05:14:05 +08:00
|
|
|
handle, err := f.File.Open(int(req.Flags))
|
2017-10-30 18:14:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, translateError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch h := handle.(type) {
|
|
|
|
case *vfs.ReadFileHandle:
|
2017-10-29 19:00:56 +08:00
|
|
|
if f.VFS().Opt.NoSeek {
|
2016-10-06 20:37:45 +08:00
|
|
|
resp.Flags |= fuse.OpenNonSeekable
|
|
|
|
}
|
2017-10-30 18:14:39 +08:00
|
|
|
fh = &ReadFileHandle{h}
|
|
|
|
case *vfs.WriteFileHandle:
|
2016-09-11 05:25:26 +08:00
|
|
|
resp.Flags |= fuse.OpenNonSeekable
|
2017-10-30 18:14:39 +08:00
|
|
|
fh = &WriteFileHandle{h}
|
2017-03-03 06:07:01 +08:00
|
|
|
default:
|
2017-10-30 18:14:39 +08:00
|
|
|
panic("unknown file handle type")
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|
|
|
|
|
2017-03-03 06:07:01 +08:00
|
|
|
return fh, nil
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|
2017-01-29 19:29:42 +08: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 18:39:33 +08:00
|
|
|
func (f *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) (err error) {
|
|
|
|
defer fs.Trace(f, "")("err=%v", &err)
|
2017-01-29 19:29:42 +08:00
|
|
|
return nil
|
|
|
|
}
|