2016-07-18 00:03:23 +02:00
|
|
|
// +build linux darwin freebsd
|
|
|
|
|
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"bazil.org/fuse"
|
|
|
|
fusefs "bazil.org/fuse/fs"
|
|
|
|
"github.com/ncw/rclone/fs"
|
2017-05-01 14:38:41 +02:00
|
|
|
"github.com/pkg/errors"
|
2016-07-18 00:03:23 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ReadFileHandle is an open for read file handle on a File
|
|
|
|
type ReadFileHandle struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
closed bool // set if handle has been closed
|
2016-12-14 22:15:12 +01:00
|
|
|
r *fs.Account
|
2016-07-18 00:03:23 +02:00
|
|
|
o fs.Object
|
|
|
|
readCalled bool // set if read has been called
|
2016-09-10 23:25:26 +02:00
|
|
|
offset int64
|
2017-05-01 14:38:41 +02:00
|
|
|
hash *fs.MultiHasher
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func newReadFileHandle(o fs.Object) (*ReadFileHandle, error) {
|
|
|
|
r, err := o.Open()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-01 14:38:41 +02:00
|
|
|
|
|
|
|
var hash *fs.MultiHasher
|
|
|
|
if !noChecksum {
|
|
|
|
hash, err = fs.NewMultiHasherTypes(o.Fs().Hashes())
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(o.Fs(), "newReadFileHandle hash error: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-30 21:18:14 +01:00
|
|
|
fh := &ReadFileHandle{
|
2017-05-01 14:38:41 +02:00
|
|
|
o: o,
|
|
|
|
r: fs.NewAccount(r, o).WithBuffer(), // account the transfer
|
|
|
|
hash: hash,
|
2016-11-30 21:18:14 +01:00
|
|
|
}
|
2016-12-01 09:49:47 +01:00
|
|
|
fs.Stats.Transferring(fh.o.Remote())
|
2016-11-30 21:18:14 +01:00
|
|
|
return fh, nil
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.Handle = (*ReadFileHandle)(nil)
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.HandleReader = (*ReadFileHandle)(nil)
|
|
|
|
|
2016-09-10 23:25:26 +02:00
|
|
|
// seek to a new offset
|
2016-10-30 18:46:00 +01:00
|
|
|
//
|
2017-01-17 17:32:04 +01:00
|
|
|
// if reopen is true, then we won't attempt to use an io.Seeker interface
|
|
|
|
//
|
2016-10-30 18:46:00 +01:00
|
|
|
// Must be called with fh.mu held
|
2017-02-17 00:57:58 +01:00
|
|
|
func (fh *ReadFileHandle) seek(offset int64, reopen bool) (err error) {
|
|
|
|
fh.r.StopBuffering() // stop the background reading first
|
2017-05-01 14:38:41 +02:00
|
|
|
fh.hash = nil
|
2016-12-14 22:15:12 +01:00
|
|
|
oldReader := fh.r.GetReader()
|
2017-02-17 00:57:58 +01:00
|
|
|
r := oldReader
|
|
|
|
// Can we seek it directly?
|
2017-01-17 17:32:04 +01:00
|
|
|
if do, ok := oldReader.(io.Seeker); !reopen && ok {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.seek from %d to %d (io.Seeker)", fh.offset, offset)
|
2017-02-17 00:57:58 +01:00
|
|
|
_, err = do.Seek(offset, 0)
|
2016-10-20 18:47:33 +02:00
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Read io.Seeker failed: %v", err)
|
2016-10-20 18:47:33 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.seek from %d to %d", fh.offset, offset)
|
2017-02-17 00:57:58 +01:00
|
|
|
// close old one
|
2016-12-14 22:15:12 +01:00
|
|
|
err = oldReader.Close()
|
2016-10-20 18:47:33 +02:00
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Read seek close old failed: %v", err)
|
2016-10-20 18:47:33 +02:00
|
|
|
}
|
2017-02-17 00:57:58 +01:00
|
|
|
// re-open with a seek
|
|
|
|
r, err = fh.o.Open(&fs.SeekOption{Offset: offset})
|
|
|
|
if err != nil {
|
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Read seek failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
2016-09-10 23:25:26 +02:00
|
|
|
}
|
2017-02-17 00:57:58 +01:00
|
|
|
fh.r.UpdateReader(r)
|
2016-10-17 21:20:07 +02:00
|
|
|
fh.offset = offset
|
2016-09-10 23:25:26 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:03:23 +02:00
|
|
|
// Read from the file handle
|
2017-01-17 17:32:04 +01:00
|
|
|
func (fh *ReadFileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) (err error) {
|
2016-10-27 10:57:52 +02:00
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Read size %d offset %d", req.Size, req.Offset)
|
2016-07-18 00:03:23 +02:00
|
|
|
if fh.closed {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Errorf(fh.o, "ReadFileHandle.Read error: %v", errClosedFileHandle)
|
2016-07-18 00:03:23 +02:00
|
|
|
return errClosedFileHandle
|
|
|
|
}
|
2016-12-14 22:17:48 +01:00
|
|
|
doSeek := req.Offset != fh.offset
|
|
|
|
var n int
|
|
|
|
var newOffset int64
|
2017-01-17 17:32:04 +01:00
|
|
|
retries := 0
|
2016-12-14 22:17:48 +01:00
|
|
|
buf := make([]byte, req.Size)
|
2017-01-17 17:32:04 +01:00
|
|
|
doReopen := false
|
2016-12-14 22:17:48 +01:00
|
|
|
for {
|
|
|
|
if doSeek {
|
|
|
|
// Are we attempting to seek beyond the end of the
|
|
|
|
// file - if so just return EOF leaving the underlying
|
|
|
|
// file in an unchanged state.
|
|
|
|
if req.Offset >= fh.o.Size() {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Read attempt to read beyond end of file: %d > %d", req.Offset, fh.o.Size())
|
2016-12-14 22:17:48 +01:00
|
|
|
resp.Data = nil
|
|
|
|
return nil
|
|
|
|
}
|
2017-01-17 17:32:04 +01:00
|
|
|
// Otherwise do the seek
|
|
|
|
err = fh.seek(req.Offset, doReopen)
|
|
|
|
} else {
|
|
|
|
err = nil
|
2016-11-05 10:59:36 +01:00
|
|
|
}
|
2016-12-14 22:17:48 +01:00
|
|
|
if err == nil {
|
2017-01-17 17:32:04 +01:00
|
|
|
if req.Size > 0 {
|
|
|
|
fh.readCalled = true
|
|
|
|
}
|
|
|
|
// One exception to the above is if we fail to fully populate a
|
|
|
|
// page cache page; a read into page cache is always page aligned.
|
|
|
|
// Make sure we never serve a partial read, to avoid that.
|
|
|
|
n, err = io.ReadFull(fh.r, buf)
|
|
|
|
newOffset = fh.offset + int64(n)
|
|
|
|
// if err == nil && rand.Intn(10) == 0 {
|
|
|
|
// err = errors.New("random error")
|
|
|
|
// }
|
|
|
|
if err == nil {
|
|
|
|
break
|
|
|
|
} else if (err == io.ErrUnexpectedEOF || err == io.EOF) && newOffset == fh.o.Size() {
|
|
|
|
// Have read to end of file - reset error
|
|
|
|
err = nil
|
|
|
|
break
|
|
|
|
}
|
2016-12-14 22:17:48 +01:00
|
|
|
}
|
2017-01-17 17:32:04 +01:00
|
|
|
if retries >= fs.Config.LowLevelRetries {
|
|
|
|
break
|
2016-09-10 23:25:26 +02:00
|
|
|
}
|
2017-01-17 17:32:04 +01:00
|
|
|
retries++
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Errorf(fh.o, "ReadFileHandle.Read error: low level retry %d/%d: %v", retries, fs.Config.LowLevelRetries, err)
|
2016-12-14 22:17:48 +01:00
|
|
|
doSeek = true
|
2017-01-17 17:32:04 +01:00
|
|
|
doReopen = true
|
2016-09-10 23:25:26 +02:00
|
|
|
}
|
2017-01-17 17:32:04 +01:00
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Errorf(fh.o, "ReadFileHandle.Read error: %v", err)
|
2017-01-17 17:32:04 +01:00
|
|
|
} else {
|
|
|
|
resp.Data = buf[:n]
|
|
|
|
fh.offset = newOffset
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Read OK")
|
2017-05-01 14:38:41 +02:00
|
|
|
|
|
|
|
if fh.hash != nil {
|
|
|
|
_, err = fh.hash.Write(resp.Data)
|
|
|
|
if err != nil {
|
|
|
|
fs.Errorf(fh.o, "ReadFileHandle.Read HashError: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-01-17 17:32:04 +01:00
|
|
|
}
|
|
|
|
return err
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// close the file handle returning errClosedFileHandle if it has been
|
|
|
|
// closed already.
|
|
|
|
//
|
|
|
|
// Must be called with fh.mu held
|
|
|
|
func (fh *ReadFileHandle) close() error {
|
|
|
|
if fh.closed {
|
|
|
|
return errClosedFileHandle
|
|
|
|
}
|
|
|
|
fh.closed = true
|
2016-12-01 09:49:47 +01:00
|
|
|
fs.Stats.DoneTransferring(fh.o.Remote(), true)
|
2017-05-01 14:38:41 +02:00
|
|
|
|
|
|
|
if err := fh.checkHash(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:03:23 +02:00
|
|
|
return fh.r.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check interface satisfied
|
|
|
|
var _ fusefs.HandleFlusher = (*ReadFileHandle)(nil)
|
|
|
|
|
2017-05-01 14:38:41 +02:00
|
|
|
func (fh *ReadFileHandle) checkHash() error {
|
|
|
|
if fh.hash == nil || !fh.readCalled || fh.offset < fh.o.Size() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for hashType, dstSum := range fh.hash.Sums() {
|
|
|
|
srcSum, err := fh.o.Hash(hashType)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !fs.HashEquals(dstSum, srcSum) {
|
|
|
|
return errors.Errorf("corrupted on transfer: %v hash differ %q vs %q", hashType, dstSum, srcSum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-18 00:03:23 +02:00
|
|
|
// Flush is called each time the file or directory is closed.
|
|
|
|
// Because there can be multiple file descriptors referring to a
|
|
|
|
// single opened file, Flush can be called multiple times.
|
|
|
|
func (fh *ReadFileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Flush")
|
2016-07-18 00:03:23 +02:00
|
|
|
|
2017-05-01 14:38:41 +02:00
|
|
|
if err := fh.checkHash(); err != nil {
|
|
|
|
fs.Errorf(fh.o, "ReadFileHandle.Flush error: %v", err)
|
|
|
|
return err
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
2017-05-01 14:38:41 +02:00
|
|
|
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Flush OK")
|
2016-07-18 00:03:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ fusefs.HandleReleaser = (*ReadFileHandle)(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
|
|
|
|
func (fh *ReadFileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
|
|
|
|
fh.mu.Lock()
|
|
|
|
defer fh.mu.Unlock()
|
|
|
|
if fh.closed {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Release nothing to do")
|
2016-07-18 00:03:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Release closing")
|
2016-07-18 00:03:23 +02:00
|
|
|
err := fh.close()
|
|
|
|
if err != nil {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Errorf(fh.o, "ReadFileHandle.Release error: %v", err)
|
2016-07-18 00:03:23 +02:00
|
|
|
} else {
|
2017-02-09 12:01:20 +01:00
|
|
|
fs.Debugf(fh.o, "ReadFileHandle.Release OK")
|
2016-07-18 00:03:23 +02:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|