2016-07-18 00:03:23 +02:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2017-11-18 12:50:58 +01:00
|
|
|
"os"
|
2016-07-18 00:03:23 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2018-01-12 17:30:54 +01:00
|
|
|
"github.com/ncw/rclone/fs/log"
|
2017-10-28 21:01:34 +02:00
|
|
|
"github.com/ncw/rclone/vfs"
|
2016-07-18 00:03:23 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File represents a file
|
|
|
|
type File struct {
|
2017-10-28 21:01:34 +02:00
|
|
|
*vfs.File
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.Node = (*File)(nil)
|
|
|
|
|
|
|
|
// Attr fills out the attributes for the file
|
2017-05-09 12:39:33 +02:00
|
|
|
func (f *File) Attr(ctx context.Context, a *fuse.Attr) (err error) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(f, "")("a=%+v, err=%v", a, &err)
|
2017-10-25 11:00:26 +02:00
|
|
|
modTime := f.File.ModTime()
|
|
|
|
Size := uint64(f.File.Size())
|
|
|
|
Blocks := (Size + 511) / 512
|
2017-10-29 12:00:56 +01:00
|
|
|
a.Gid = f.VFS().Opt.GID
|
|
|
|
a.Uid = f.VFS().Opt.UID
|
|
|
|
a.Mode = f.VFS().Opt.FilePerms
|
2017-05-02 23:35:07 +02:00
|
|
|
a.Size = Size
|
|
|
|
a.Atime = modTime
|
|
|
|
a.Mtime = modTime
|
|
|
|
a.Ctime = modTime
|
|
|
|
a.Crtime = modTime
|
|
|
|
a.Blocks = Blocks
|
2016-07-18 00:03:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-23 20:41:21 +01:00
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeSetattrer = (*File)(nil)
|
|
|
|
|
2017-11-20 20:42:35 +01:00
|
|
|
// Setattr handles attribute changes from FUSE. Currently supports ModTime and Size only
|
2017-05-09 12:39:33 +02:00
|
|
|
func (f *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) (err error) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(f, "a=%+v", req)("err=%v", &err)
|
2017-11-20 20:42:35 +01:00
|
|
|
if !f.VFS().Opt.NoModTime {
|
|
|
|
if req.Valid.Mtime() {
|
|
|
|
err = f.File.SetModTime(req.Mtime)
|
2018-01-29 21:49:13 +01:00
|
|
|
} else if req.Valid.MtimeNow() {
|
|
|
|
err = f.File.SetModTime(time.Now())
|
2017-11-20 20:42:35 +01:00
|
|
|
}
|
2017-03-23 20:41:21 +01:00
|
|
|
}
|
2017-11-20 20:42:35 +01:00
|
|
|
if req.Valid.Size() {
|
|
|
|
err = f.File.Truncate(int64(req.Size))
|
2017-03-23 20:41:21 +01:00
|
|
|
}
|
2017-05-02 23:35:07 +02:00
|
|
|
return translateError(err)
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.NodeOpener = (*File)(nil)
|
|
|
|
|
|
|
|
// Open the file for read or write
|
2017-03-02 23:07:01 +01:00
|
|
|
func (f *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fh fusefs.Handle, err error) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(f, "flags=%v", req.Flags)("fh=%v, err=%v", &fh, &err)
|
2017-10-30 11:14:39 +01:00
|
|
|
|
|
|
|
// fuse flags are based off syscall flags as are os flags, so
|
|
|
|
// should be compatible
|
2017-11-18 12:50:58 +01:00
|
|
|
//
|
|
|
|
// we seem to be missing O_CREATE here so add it in to allow
|
|
|
|
// file creation
|
|
|
|
handle, err := f.File.Open(int(req.Flags) | os.O_CREATE)
|
2017-10-30 11:14:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, translateError(err)
|
|
|
|
}
|
|
|
|
|
2017-11-02 19:22:26 +01:00
|
|
|
// See if seeking is supported and set FUSE hint accordingly
|
|
|
|
if _, err = handle.Seek(0, 1); err != nil {
|
2016-09-10 23:25:26 +02:00
|
|
|
resp.Flags |= fuse.OpenNonSeekable
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
2017-11-02 19:22:26 +01:00
|
|
|
return &FileHandle{handle}, nil
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-01-29 12:29:42 +01: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 12:39:33 +02:00
|
|
|
func (f *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) (err error) {
|
2018-01-12 17:30:54 +01:00
|
|
|
defer log.Trace(f, "")("err=%v", &err)
|
2017-01-29 12:29:42 +01:00
|
|
|
return nil
|
|
|
|
}
|