2016-07-17 22:03:23 +00:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2017-05-02 21:35:07 +00:00
|
|
|
"github.com/ncw/rclone/cmd/mountlib"
|
2017-05-09 10:39:33 +00:00
|
|
|
"github.com/ncw/rclone/fs"
|
2016-07-17 22:03:23 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// File represents a file
|
|
|
|
type File struct {
|
2017-05-02 21:35:07 +00:00
|
|
|
*mountlib.File
|
|
|
|
// size int64 // size of file - read and written with atomic int64 - must be 64 bit aligned
|
|
|
|
// d *Dir // parent directory - read only
|
|
|
|
// mu sync.RWMutex // protects the following
|
|
|
|
// o fs.Object // NB o may be nil if file is being written
|
|
|
|
// writers int // number of writers for this file
|
|
|
|
// pendingModTime time.Time // will be applied once o becomes available, i.e. after file was written
|
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) {
|
|
|
|
defer fs.Trace(f, "")("a=%+v, err=%v", a, &err)
|
2017-06-19 12:44:49 +00:00
|
|
|
modTime, Size, Blocks, err := f.File.Attr(mountlib.NoModTime)
|
2017-05-02 21:35:07 +00:00
|
|
|
if err != nil {
|
|
|
|
return translateError(err)
|
|
|
|
}
|
2017-06-19 12:44:49 +00:00
|
|
|
a.Gid = mountlib.GID
|
|
|
|
a.Uid = mountlib.UID
|
|
|
|
a.Mode = mountlib.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)
|
|
|
|
|
|
|
|
// Setattr handles attribute changes from FUSE. Currently supports ModTime only.
|
2017-05-09 10:39:33 +00: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-06-19 12:44:49 +00:00
|
|
|
if mountlib.NoModTime {
|
2017-03-23 19:41:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if req.Valid.MtimeNow() {
|
2017-05-02 21:35:07 +00:00
|
|
|
err = f.File.SetModTime(time.Now())
|
2017-03-23 19:41:21 +00:00
|
|
|
} else if req.Valid.Mtime() {
|
2017-05-02 21:35:07 +00:00
|
|
|
err = f.File.SetModTime(req.Mtime)
|
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) {
|
2017-05-09 10:39:33 +00:00
|
|
|
defer fs.Trace(f, "flags=%v", req.Flags)("fh=%v, err=%v", &fh, &err)
|
2016-07-17 22:03:23 +00:00
|
|
|
switch {
|
|
|
|
case req.Flags.IsReadOnly():
|
2017-06-19 12:44:49 +00:00
|
|
|
if mountlib.NoSeek {
|
2016-10-06 12:37:45 +00:00
|
|
|
resp.Flags |= fuse.OpenNonSeekable
|
|
|
|
}
|
2017-05-02 21:35:07 +00:00
|
|
|
var rfh *mountlib.ReadFileHandle
|
|
|
|
rfh, err = f.File.OpenRead()
|
|
|
|
fh = &ReadFileHandle{rfh}
|
2016-11-28 17:56:54 +00:00
|
|
|
case req.Flags.IsWriteOnly() || (req.Flags.IsReadWrite() && (req.Flags&fuse.OpenTruncate) != 0):
|
2016-09-10 21:25:26 +00:00
|
|
|
resp.Flags |= fuse.OpenNonSeekable
|
2017-05-02 21:35:07 +00:00
|
|
|
var wfh *mountlib.WriteFileHandle
|
|
|
|
wfh, err = f.File.OpenWrite()
|
|
|
|
fh = &WriteFileHandle{wfh}
|
2016-07-17 22:03:23 +00:00
|
|
|
case req.Flags.IsReadWrite():
|
2017-03-02 22:07:01 +00:00
|
|
|
err = errors.New("can't open for read and write simultaneously")
|
|
|
|
default:
|
|
|
|
err = errors.Errorf("can't figure out how to open with flags %v", req.Flags)
|
2016-07-17 22:03:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
// File was opened in append-only mode, all writes will go to end
|
|
|
|
// of file. OS X does not provide this information.
|
|
|
|
OpenAppend OpenFlags = syscall.O_APPEND
|
|
|
|
OpenCreate OpenFlags = syscall.O_CREAT
|
|
|
|
OpenDirectory OpenFlags = syscall.O_DIRECTORY
|
|
|
|
OpenExclusive OpenFlags = syscall.O_EXCL
|
|
|
|
OpenNonblock OpenFlags = syscall.O_NONBLOCK
|
|
|
|
OpenSync OpenFlags = syscall.O_SYNC
|
|
|
|
OpenTruncate OpenFlags = syscall.O_TRUNC
|
|
|
|
*/
|
2017-03-02 22:07:01 +00:00
|
|
|
|
|
|
|
if err != nil {
|
2017-05-02 21:35:07 +00:00
|
|
|
return nil, translateError(err)
|
2017-03-02 22:07:01 +00:00
|
|
|
}
|
|
|
|
return fh, 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) {
|
|
|
|
defer fs.Trace(f, "")("err=%v", &err)
|
2017-01-29 11:29:42 +00:00
|
|
|
return nil
|
|
|
|
}
|