2016-07-18 06:03:23 +08:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2017-05-03 05:35:07 +08:00
|
|
|
"errors"
|
2016-07-18 06:03:23 +08:00
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
2017-05-03 05:35:07 +08:00
|
|
|
"github.com/ncw/rclone/cmd/mountlib"
|
2017-05-09 18:39:33 +08:00
|
|
|
"github.com/ncw/rclone/fs"
|
2016-07-18 06:03:23 +08:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
var errClosedFileHandle = errors.New("Attempt to use closed file handle")
|
|
|
|
|
|
|
|
// WriteFileHandle is an open for write handle on a File
|
|
|
|
type WriteFileHandle struct {
|
2017-05-03 05:35:07 +08:00
|
|
|
*mountlib.WriteFileHandle
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.Handle = (*WriteFileHandle)(nil)
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.HandleWriter = (*WriteFileHandle)(nil)
|
|
|
|
|
|
|
|
// Write data to the file handle
|
2017-05-09 18:39:33 +08:00
|
|
|
func (fh *WriteFileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) (err error) {
|
|
|
|
defer fs.Trace(fh, "len=%d, offset=%d", len(req.Data), req.Offset)("written=%d, err=%v", &resp.Size, &err)
|
2017-10-28 04:41:34 +08:00
|
|
|
n, err := fh.WriteFileHandle.WriteAt(req.Data, req.Offset)
|
2016-07-18 06:03:23 +08:00
|
|
|
if err != nil {
|
2017-05-03 05:35:07 +08:00
|
|
|
return translateError(err)
|
2017-05-01 20:38:41 +08:00
|
|
|
}
|
2017-05-03 05:35:07 +08:00
|
|
|
resp.Size = int(n)
|
2016-07-18 06:03:23 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush is called on each close() of a file descriptor. So if a
|
|
|
|
// filesystem wants to return write errors in close() and the file has
|
|
|
|
// cached dirty data, this is a good place to write back data and
|
|
|
|
// return any errors. Since many applications ignore close() errors
|
|
|
|
// this is not always useful.
|
|
|
|
//
|
|
|
|
// NOTE: The flush() method may be called more than once for each
|
|
|
|
// open(). This happens if more than one file descriptor refers to an
|
|
|
|
// opened file due to dup(), dup2() or fork() calls. It is not
|
|
|
|
// possible to determine if a flush is final, so each flush should be
|
|
|
|
// treated equally. Multiple write-flush sequences are relatively
|
|
|
|
// rare, so this shouldn't be a problem.
|
|
|
|
//
|
|
|
|
// Filesystems shouldn't assume that flush will always be called after
|
|
|
|
// some writes, or that if will be called at all.
|
2017-05-09 18:39:33 +08:00
|
|
|
func (fh *WriteFileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) (err error) {
|
|
|
|
defer fs.Trace(fh, "")("err=%v", &err)
|
2017-05-03 05:35:07 +08:00
|
|
|
return translateError(fh.WriteFileHandle.Flush())
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.HandleReleaser = (*WriteFileHandle)(nil)
|
|
|
|
|
|
|
|
// Release is called when we are finished with the file handle
|
|
|
|
//
|
|
|
|
// It isn't called directly from userspace so the error is ignored by
|
|
|
|
// the kernel
|
2017-05-09 18:39:33 +08:00
|
|
|
func (fh *WriteFileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) (err error) {
|
|
|
|
defer fs.Trace(fh, "")("err=%v", &err)
|
2017-05-03 05:35:07 +08:00
|
|
|
return translateError(fh.WriteFileHandle.Release())
|
2016-07-18 06:03:23 +08:00
|
|
|
}
|